3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2009 The Nucleus Group
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * (see nucleus/documentation/index.html#license for more info)
13 * This class is used to parse item templates
15 * @license http://nucleuscms.org/license.txt GNU General Public License
16 * @copyright Copyright (C) 2002-2009 The Nucleus Group
17 * @version $Id: ITEMACTIONS.php 1626 2012-01-09 15:46:54Z sakamocchi $
19 class ITEMACTIONS extends BaseActions {
21 // contains an assoc array with parameters that need to be included when
22 // generating links to items/archives/... (e.g. catid)
25 // true when the current user is a blog admin (and thus allowed to edit all items)
28 // timestamp of last visit
31 // item currently being handled (mysql result object, see BLOG::showUsingQuery)
34 // reference to the blog currently being displayed
37 // associative array with template info (part name => contents)
40 // true when comments need to be displayed
43 function ITEMACTIONS(&$blog) {
44 // call constructor of superclass first
47 // extra parameters for created links
50 $this->linkparams = array('catid' => $catid);
52 // check if member is blog admin (and thus allowed to edit all items)
54 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
55 $this->setBlog($blog);
59 * Returns an array with the actions that are defined
60 * in the ITEMACTIONS class
62 function getDefinedActions() {
85 'syndicate_description',
114 function setLastVisit($lastVisit) {
115 $this->lastVisit = $lastVisit;
118 function setParser(&$parser) {
119 $this->parser =& $parser;
122 function setCurrentItem(&$item) {
123 $this->currentItem =& $item;
124 global $currentitemid;
125 $currentitemid = $this->currentItem->itemid;
128 function setBlog(&$blog) {
129 $this->blog =& $blog;
132 function setTemplate($template) {
133 $this->template =& $template;
136 function setShowComments($val) {
137 $this->showComments = $val;
140 // methods used by parser to insert content
144 * Parse templatevar blogid
146 function parse_blogid() {
147 echo $this->blog->getID();
151 * Parse templatevar body
153 function parse_body() {
154 $this->highlightAndParse($this->currentItem->body);
158 * Parse templatevar more
160 function parse_more() {
161 $this->highlightAndParse($this->currentItem->more);
165 * Parse templatevar itemid
167 function parse_itemid() {
168 echo $this->currentItem->itemid;
172 * Parse templatevar category
174 function parse_category() {
175 echo $this->currentItem->category;
179 * Parse templatevar categorylink
181 function parse_categorylink() {
182 echo Link::create_link('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
186 * Parse templatevar catid
188 function parse_catid() {
189 echo $this->currentItem->catid;
193 * Parse templatevar authorid
195 function parse_authorid() {
196 echo $this->currentItem->authorid;
200 * Parse templatevar authorlink
202 function parse_authorlink() {
203 echo Link::create_link(
206 'memberid' => $this->currentItem->authorid,
207 'name' => $this->currentItem->author,
208 'extra' => $this->linkparams
214 * Parse templatevar query
216 function parse_query() {
217 echo $this->strHighlight;
221 * Parse templatevar itemlink
223 function parse_itemlink() {
224 echo Link::create_link(
227 'itemid' => $this->currentItem->itemid,
228 'title' => $this->currentItem->title,
229 'timestamp' => $this->currentItem->timestamp,
230 'extra' => $this->linkparams
236 * Parse templatevar blogurl
238 function parse_blogurl() {
239 echo $this->blog->getURL();
243 * Parse templatevar closed
245 function parse_closed() {
246 echo $this->currentItem->closed;
250 * Parse templatevar relevance
252 function parse_relevance() {
253 echo round($this->currentItem->score,2);
257 * Parse templatevar title
259 * @param string $format defines in which format the title is shown
261 function parse_title($format = '')
263 if ( is_array($this->currentItem) )
265 $itemtitle = $this->currentItem['title'];
267 elseif ( is_object($this->currentItem) )
269 $itemtitle = $this->currentItem->title;
274 echo ENTITY::hen($itemtitle);
277 echo ENTITY::hsc($itemtitle);
283 $this->highlightAndParse($itemtitle);
290 * Parse templatevar karma
292 function parse_karma($type = 'totalscore') {
296 $karma =& $manager->getKarma($this->currentItem->itemid);
300 echo $karma->getNbPosVotes();
303 echo $karma->getNbNegVotes();
306 echo $karma->getNbOfVotes();
309 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
310 echo number_format($percentage,2), '%';
313 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
314 echo number_format($percentage,2), '%';
318 echo $karma->getTotalScore();
325 * Parse templatevar author
327 function parse_author($which = '') {
331 echo $this->currentItem->authorname;
334 echo $this->currentItem->authorid;
337 echo $this->currentItem->authormail;
340 echo $this->currentItem->authorurl;
344 echo $this->currentItem->author;
349 * Parse templatevar smartbody
351 function parse_smartbody() {
352 if (!$this->currentItem->more) {
353 $this->highlightAndParse($this->currentItem->body);
355 $this->highlightAndParse($this->currentItem->more);
360 * Parse templatevar morelink
362 function parse_morelink() {
363 if ($this->currentItem->more)
364 $this->parser->parse($this->template['MORELINK']);
368 * ITEMACTIONS::parse_date()
369 * Parse templatevar date
371 * @param String $format format optional strftime format
372 * @return String formatted datetime
374 function parse_date($format = '')
376 if ( !isset($this->template['FORMAT_DATE']) )
378 $this->template['FORMAT_DATE'] = '';
381 $offset = date('Z', $timestamp);
384 $offset += $this->blog->getTimeOffset() * 3600;
387 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $offset);
391 * Parse templatevar time
393 * @param format optional strftime format
395 function parse_time($format = '') {
396 if (!isset($this->template['FORMAT_TIME'])) $this->template['FORMAT_TIME'] = '';
397 echo i18n::strftime($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);
401 * ITEMACTIONS::parse_syndicate_title()
402 * Parse templatevar syndicate_title
404 * @param String $maxLength maxLength optional maximum length
405 * @return String syndicated title
407 function parse_syndicate_title($maxLength = 100) {
408 $syndicated = strip_tags($this->currentItem->title);
409 echo ENTITY::hsc(ENTITY::shorten($syndicated,$maxLength,'...'));
413 * ITEMACTIONS::parse_syndicate_description()
414 * Parse templatevar syndicate_description
416 * @param Stromg $maxLength maxlength optional maximum length
417 * @param String $addHighlight highlighted string
420 function parse_syndicate_description($maxLength = 250, $addHighlight = 0)
422 $syndicated = strip_tags($this->currentItem->body);
425 $tmp_highlight = ENTITY::hsc(ENTITY::shorten($syndicated,$maxLength,'...'));
426 echo $this->highlightAndParse($tmp_highlight);
430 echo ENTITY::hsc(ENTITY::shorten($syndicated,$maxLength,'...'));
436 * Parse templatevar karmaposlink
440 function parse_karmaposlink($text = '') {
442 $link = $CONF['ActionURL'] . '?action=votepositive&itemid='.$this->currentItem->itemid;
443 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
447 * Parse templatevar karmaneglink
451 function parse_karmaneglink($text = '') {
453 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem->itemid;
454 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
458 * Parse templatevar new
460 function parse_new() {
461 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
462 echo $this->template['NEW'];
466 * Parse templatevar daylink
468 function parse_daylink() {
469 echo Link::create_archive_link($this->blog->getID(), i18n::strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
473 * Parse templatevar comments
475 function parse_comments($maxToShow = 0) {
477 $maxToShow = $this->blog->getMaxComments();
480 if ($this->showComments && $this->blog->commentsEnabled()) {
481 $comments = new COMMENTS($this->currentItem->itemid);
482 $comments->setItemActions($this);
483 $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
488 * Executes a plugin templatevar
490 * @param pluginName name of plugin (without the NP_)
492 * extra parameters can be added
494 function parse_plugin($pluginName) {
497 // should be already tested from the parser (PARSER.php)
498 // only continue when the plugin is really installed
499 /*if (!$manager->pluginInstalled('NP_' . $pluginName))
502 $plugin =& $manager->getPlugin('NP_' . $pluginName);
503 if (!$plugin) return;
506 $params = func_get_args();
508 // remove plugin name
509 array_shift($params);
511 // add item reference (array_unshift didn't work)
512 $params = array_merge(array(&$this->currentItem),$params);
514 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
518 * Parse templatevar edit
520 function parse_edit() {
521 global $member, $CONF;
522 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {
523 $this->parser->parse($this->template['EDITLINK']);
528 * Parse templatevar editlink
530 function parse_editlink() {
532 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&itemid=',$this->currentItem->itemid;
536 * Parse templatevar editpopupcode
538 function parse_editpopupcode() {
539 echo "if (event && event.preventDefault) event.preventDefault();winbm=window.open(this.href,'nucleusbm','scrollbars=yes,width=600,height=550,left=10,top=10,status=yes,resizable=yes');winbm.focus();return false;";
545 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
546 * from hacking your weblog.
547 * 'plugin variables in items' implementation by Andy
549 function highlightAndParse(&$data) {
550 $actions = new BODYACTIONS($this->blog);
551 $parser = new PARSER($actions->getDefinedActions(), $actions);
552 $actions->setTemplate($this->template);
553 $actions->setHighlight($this->strHighlight);
554 $actions->setCurrentItem($this->currentItem);
555 //$actions->setParser($parser);
556 $parser->parse($actions->highlight($data));
560 // this is the function previous to the 'plugin variables in items' implementation by Andy
561 function highlightAndParse(&$data) {
562 // allow only a limited subset of actions (do not allow includes etc, they might be evil)
563 $this->parser->actions = array('image','media','popup');
564 $tmp_highlight = $this->highlight($data);
565 $this->parser->parse($tmp_highlight);
566 $this->parser->actions = $this->getDefinedActions();
570 // function to enable if-else-elseif-elseifnot-ifnot-endif to item template fields
573 * Checks conditions for if statements
575 * @param string $field type of <%if%>
576 * @param string $name property of field
577 * @param string $value value of property
579 function checkCondition($field, $name='', $value = '') {
580 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
585 $condition = ($blog && $this->_ifCategory($name,$value));
588 $condition = ($this->_ifItemCategory($name,$value));
591 $condition = ($blog && ($blog->getSetting($name) == $value));
593 case 'itemblogsetting':
594 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
595 $condition = ($b && ($b->getSetting($name) == $value));
598 $condition = $member->isLoggedIn();
601 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
604 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
607 $condition = ($this->_ifAuthor($name,$value));
610 $condition = ($itemidnext != '');
613 $condition = ($itemidprev != '');
615 case 'archiveprevexists':
616 $condition = ($archiveprevexists == true);
618 case 'archivenextexists':
619 $condition = ($archivenextexists == true);
622 $condition = ($name == $this->skintype);
625 $condition = $this->_ifHasPlugin($name, $value);
628 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
635 * Different checks for a category
637 function _ifCategory($name = '', $value='') {
638 global $blog, $catid;
640 // when no parameter is defined, just check if a category is selected
641 if (($name != 'catname' && $name != 'catid') || ($value == ''))
642 return $blog->isValidCategory($catid);
644 // check category name
645 if ($name == 'catname') {
646 $value = $blog->getCategoryIdFromName($value);
647 if ($value == $catid)
648 return $blog->isValidCategory($catid);
652 if (($name == 'catid') && ($value == $catid))
653 return $blog->isValidCategory($catid);
660 * Different checks for an author
662 function _ifAuthor($name = '', $value='') {
663 global $member, $manager;
665 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
667 // when no parameter is defined, just check if author is current visitor
668 if (($name != 'isadmin' && $name != 'name') || ($name == 'name' && $value == '')) {
669 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));
673 if ($name == 'name') {
674 $value = strtolower($value);
675 if ($value == strtolower($this->currentItem->author))
679 // check if author is admin
680 if (($name == 'isadmin')) {
681 $aid = intval($this->currentItem->authorid);
682 $blogid = intval($b->getID());
683 $amember =& $manager->getMember($aid);
684 if ($amember->isAdmin())
687 return $amember->isBlogAdmin($blogid);
694 * Different checks for a category
696 function _ifItemCategory($name = '', $value='') {
697 global $catid, $manager;
699 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
701 // when no parameter is defined, just check if a category is selected
702 if (($name != 'catname' && $name != 'catid') || ($value == ''))
703 return $b->isValidCategory($catid);
705 $icatid = $this->currentItem->catid;
706 //$icategory = $this->currentItem->category;
708 // check category name
709 if ($name == 'catname') {
710 $value = $b->getCategoryIdFromName($value);
711 if ($value == $icatid)
712 return $b->isValidCategory($icatid);
716 if (($name == 'catid') && ($value == $icatid))
717 return $b->isValidCategory($icatid);
724 * Checks if a member is on the team of a blog and return his rights
726 function _ifOnTeam($blogName = '') {
727 global $blog, $member, $manager;
729 // when no blog found
730 if (($blogName == '') && (!is_object($blog)))
733 // explicit blog selection
735 $blogid = getBlogIDFromName($blogName);
737 if (($blogName == '') || !$manager->existsBlogID($blogid))
739 $blogid = $blog->getID();
741 return $member->teamRights($blogid);
745 * Checks if a member is admin of a blog
747 function _ifAdmin($blogName = '') {
748 global $blog, $member, $manager;
750 // when no blog found
751 if (($blogName == '') && (!is_object($blog)))
754 // explicit blog selection
756 $blogid = getBlogIDFromName($blogName);
758 if (($blogName == '') || !$manager->existsBlogID($blogid))
760 $blogid = $blog->getID();
762 return $member->isBlogAdmin($blogid);
768 * -> checks if plugin exists
769 * hasplugin,PlugName,OptionName
770 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
771 * hasplugin,PlugName,OptionName=value
772 * -> checks if the option OptionName from plugin PlugName is set to value
774 function _ifHasPlugin($name, $value) {
777 // (pluginInstalled method won't write a message in the actionlog on failure)
778 if ($manager->pluginInstalled('NP_'.$name)) {
779 $plugin =& $manager->getPlugin('NP_' . $name);
780 if ($plugin != NULL) {
784 list($name2, $value2) = i18n::explode('=', $value, 2);
785 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
787 } else if ($plugin->getOption($name2) == $value2) {
797 * Checks if a plugin exists and call its doIf function
799 function _ifPlugin($name, $key = '', $value = '') {
802 $plugin =& $manager->getPlugin('NP_' . $name);
803 if (!$plugin) return;
805 $params = func_get_args();
806 array_shift($params);
808 return call_user_func_array(array(&$plugin, 'doIf'), $params);