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 1757 2012-04-15 09:02:32Z sakamocchi $
19 class ItemActions extends BaseActions
22 * ItemActions::$currentItem
23 * item currently being handled (mysql result object, see Blog::showUsingQuery)
28 * ItemActions::$linkparams
29 * contains an assoc array with parameters that need to be included when
30 * generating links to items/archives/... (e.g. catid)
35 * ItemActions::$allowEditAll
36 * true when the current user is a blog admin (and thus allowed to edit all items)
38 private $allowEditAll;
41 * ItemActions::$lastVisit
42 * timestamp of last visit
48 * reference to the blog currently being displayed
53 * ItemActions::$template
54 * associative array with template info (part name => contents)
59 * ItemActions::$showComments
60 * true when comments need to be displayed
62 private $showComments;
65 * ItemActions::$defined_actions
66 * defined actions in this class
68 static private $defined_actions = array(
97 'syndicate_description',
101 /* actions defined in BodyAction class */
108 * ItemActions::__construct
109 * Enter description here ...
110 * @param unknown_type $blog
112 public function __construct(&$blog)
114 global $catid, $member;
116 // call constructor of superclass first
117 parent::__construct();
119 // extra parameters for created links
122 $this->linkparams = array('catid' => $catid);
125 // check if member is blog admin (and thus allowed to edit all items)
126 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
127 $this->setBlog($blog);
132 * ItemActions::getDefinedActions()
133 * Returns an array with the actions that are defined
134 * in the ItemActions class
140 static public function getDefinedActions()
142 return array_merge(self::$defined_actions, parent::getDefinedActions());
146 * ItemActions::setLastVisit()
148 * @param timestamp $lastVisit timestamp of latest visit
151 public function setLastVisit($lastVisit)
153 $this->lastVisit = $lastVisit;
158 * ItemActions::setParser()
160 * @param object &$parser instance of Parser class
163 public function setParser(&$parser)
165 $this->parser =& $parser;
170 * ItemActions::setCurrentItem()
172 * @param object $item instance of Item class
175 public function setCurrentItem(&$item)
177 global $currentitemid;
178 $this->currentItem =& $item;
179 $currentitemid = $this->currentItem['itemid'];
184 * ItemActions::setBlog()
186 * @param object &$blog instance of Blog class
189 public function setBlog(&$blog)
191 $this->blog =& $blog;
196 * ItemActions::setTemplate()
198 * @param array $template array including templates
201 public function setTemplate($template)
203 $this->template =& $template;
208 * ItemActions::setShowComments()
210 * @param boolean $val need to be displayed or not
213 public function setShowComments($val)
215 $this->showComments = (boolean) $val;
220 * ItemActions::parse_blogid()
221 * Parse templatevar blogid
226 public function parse_blogid()
228 echo $this->blog->getID();
232 * ItemActions::parse_body()
233 * Parse templatevar body
238 public function parse_body()
240 $this->highlightAndParse($this->currentItem['body']);
245 * ItemActions::parse_more()
246 * Parse templatevar more
251 public function parse_more()
253 $this->highlightAndParse($this->currentItem['more']);
258 * ItemActions::parse_itemid()
259 * Parse templatevar itemid
264 public function parse_itemid()
266 echo $this->currentItem['itemid'];
271 * ItemActions::parse_category()
272 * Parse templatevar category
277 public function parse_category()
279 echo $this->currentItem['category'];
284 * ItemActions::parse_categorylink()
285 * Parse templatevar categorylink
290 public function parse_categorylink()
292 echo Link::create_link('category', array('catid' => $this->currentItem['catid'], 'name' => $this->currentItem['category']));
297 * ItemActions::parse_catid()
298 * Parse templatevar catid
303 public function parse_catid()
305 echo $this->currentItem['catid'];
310 * ItemActions::parse_authorid()
311 * Parse templatevar authorid
316 public function parse_authorid()
318 echo $this->currentItem['authorid'];
323 * ItemActions::parse_authorlink()
324 * Parse templatevar authorlink
329 public function parse_authorlink()
332 'memberid' => $this->currentItem['authorid'],
333 'name' => $this->currentItem['author'],
334 'extra' => $this->linkparams
337 echo Link::create_link('member', $data);
342 * ItemActions::parse_query()
343 * Parse templatevar query
348 public function parse_query()
350 echo $this->strHighlight;
355 * ItemActions::parse_itemlink()
356 * Parse templatevar itemlink
361 public function parse_itemlink()
364 'itemid' => $this->currentItem['itemid'],
365 'title' => $this->currentItem['title'],
366 'timestamp' => $this->currentItem['timestamp'],
367 'extra' => $this->linkparams
370 echo Link::create_link('item', $data);
375 * ItemActions::parse_blogurl()
376 * Parse templatevar blogurl
381 public function parse_blogurl()
383 echo $this->blog->getURL();
388 * ItemActions::parse_closed()
389 * Parse templatevar closed
394 public function parse_closed()
396 echo $this->currentItem['closed'];
401 * ItemActions::parse_relevance()
402 * Parse templatevar relevance
407 public function parse_relevance()
409 echo round($this->currentItem['score'], 2);
414 * ItemActions::parse_title()
415 * Parse templatevar title
417 * @param string $format defines in which format the title is shown
420 public function parse_title($format = '')
422 if ( is_array($this->currentItem) )
424 $itemtitle = $this->currentItem['title'];
429 echo Entity::hen($itemtitle);
432 echo Entity::hsc($itemtitle);
438 $this->highlightAndParse($itemtitle);
445 * ItemActions::parse_karma()
446 * Parse templatevar karma
448 * @param string $type type of data for karma
451 public function parse_karma($type = 'totalscore')
456 $karma =& $manager->getKarma($this->currentItem['itemid']);
461 echo $karma->getNbPosVotes();
464 echo $karma->getNbNegVotes();
467 echo $karma->getNbOfVotes();
470 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
471 echo number_format($percentage,2), '%';
474 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
475 echo number_format($percentage,2), '%';
479 echo $karma->getTotalScore();
486 * ItemActions::parse_author()
487 * Parse templatevar author
489 * @param string $which key of data for author
492 public function parse_author($which = '')
497 echo $this->currentItem['authorname'];
500 echo $this->currentItem['authorid'];
503 echo $this->currentItem['authormail'];
506 echo $this->currentItem['authorurl'];
510 echo $this->currentItem['author'];
516 * ItemActions::parse_smartbody()
517 * Parse templatevar smartbody
522 public function parse_smartbody()
524 if ( !$this->currentItem['more'] )
526 $this->highlightAndParse($this->currentItem['body']);
530 $this->highlightAndParse($this->currentItem['more']);
536 * ItemActions::parse_morelink()
537 * Parse templatevar morelink
539 public function parse_morelink()
541 if ( $this->currentItem['more'] )
543 $this->parser->parse($this->template['MORELINK']);
549 * ItemActions::parse_date()
550 * Parse templatevar date
552 * @param string $format format optional strftime format
555 public function parse_date($format = '')
557 if ( $format !== '' )
562 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
564 /* depends on the PHP's current locale */
569 $format = $this->template['FORMAT_DATE'];
575 $offset = $this->blog->getTimeOffset() * 3600;
578 echo i18n::formatted_datetime($format, $this->currentItem['timestamp'], $offset);
583 * ItemActions::parse_time()
584 * Parse templatevar time
586 * @param string $format format optional strftime format
589 public function parse_time($format = '')
591 if ( $format !== '' )
596 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
598 /* depends on the PHP's current locale */
603 $format = $this->template['FORMAT_TIME'];
605 echo i18n::formatted_datetime($format, $this->currentItem['timestamp']);
610 * ItemActions::parse_syndicate_title()
611 * Parse templatevar syndicate_title
613 * @param string $maxLength maxLength optional maximum length
614 * @return string syndicated title
616 public function parse_syndicate_title($maxLength = 100) {
617 $syndicated = strip_tags($this->currentItem['title']);
618 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
622 * ItemActions::parse_syndicate_description()
623 * Parse templatevar syndicate_description
625 * @param stromg $maxLength maxlength optional maximum length
626 * @param string $addHighlight highlighted string
629 public function parse_syndicate_description($maxLength = 250, $addHighlight = 0)
631 $syndicated = strip_tags($this->currentItem['body']);
634 $tmp_highlight = Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
635 echo $this->highlightAndParse($tmp_highlight);
639 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
645 * ItemActions::parse_karmaposlink()
646 * Parse templatevar karmaposlink
648 * @param string $text text element for anchor element
651 public function parse_karmaposlink($text = '')
654 $link = $CONF['ActionURL'] . '?action=votepositive&itemid=' . $this->currentItem['itemid'];
657 echo '<a href="'.$link.'">' . $text . '</a>';
668 * ItemActions::parse_karmaneglink()
669 * Parse templatevar karmaneglink
671 * @param string $text text element for anchor element
674 public function parse_karmaneglink($text = '')
677 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem['itemid'];
681 echo '<a href="'.$link.'">' . $text . '</a>';
692 * ItemActions::parse_new()
693 * Parse templatevar new
698 public function parse_new()
700 if ( ($this->lastVisit != 0) && ($this->currentItem['timestamp'] > $this->lastVisit) )
702 echo $this->template['NEW'];
708 * ItemActions::parse_daylink()
709 * Parse templatevar daylink
714 public function parse_daylink()
716 echo Link::create_archive_link($this->blog->getID(), i18n::formatted_datetime('%Y-%m-%d', $this->currentItem['timestamp']), $this->linkparams);
721 * ItemActions::parse_comments(
722 * Parse templatevar comments
724 * @param integer $maxToShow maximum number of comments in a display
727 public function parse_comments($maxToShow = 0)
729 if ( $maxToShow == 0 )
731 $maxToShow = $this->blog->getMaxComments();
735 if ( $this->showComments && $this->blog->commentsEnabled() )
737 $comments = new Comments($this->currentItem['itemid']);
738 $comments->setItemActions($this);
739 $comments->showComments($this->template, $maxToShow, $this->currentItem['closed'] ? 0 : 1, $this->strHighlight);
745 * ItemActions::parse_plugin()
746 * Executes a plugin templatevar
748 * @param string $pluginName name of plugin (without the NP_)
749 * @param extra parameters can be added
752 public function parse_plugin($pluginName)
756 $plugin =& $manager->getPlugin("NP_{$pluginName}");
763 $params = func_get_args();
765 // remove plugin name
766 array_shift($params);
768 // add item reference (array_unshift didn't work)
769 $params = array_merge(array(&$this->currentItem),$params);
771 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
776 * ItemActions::parse_edit()
777 * Parse templatevar edit
782 public function parse_edit()
784 global $member, $CONF;
785 if ( $this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem['authorid'])) )
787 $this->parser->parse($this->template['EDITLINK']);
793 * ItemActions::parse_editlink()
794 * Parse templatevar editlink
796 public function parse_editlink()
799 echo $CONF['AdminURL'] . 'bookmarklet.php?action=edit&itemid=' . $this->currentItem['itemid'];
804 * ItemActions::parse_editpopupcode()
805 * Parse templatevar editpopupcode
810 public function parse_editpopupcode()
812 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;";
817 * ItemActions::highlightAndParse()
818 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
819 * from hacking your weblog.
820 * 'plugin variables in items' implementation by Andy
825 public function highlightAndParse(&$data)
827 $handler = new BodyActions($this->blog);
828 $parser = new Parser($handler->getDefinedActions(), $handler);
829 $handler->setTemplate($this->template);
830 $handler->setHighlight($this->strHighlight);
831 $handler->setCurrentItem($this->currentItem);
832 $parser->parse($handler->highlight($data));
837 * ItemActions::checkCondition()
838 * Checks conditions for if statements
840 * @param string $field type of <%if%>
841 * @param string $name property of field
842 * @param string $value value of property
845 protected function checkCondition($field, $name='', $value = '')
847 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
853 $condition = ($blog && $this->ifCategory($name,$value));
856 $condition = ($this->ifItemCategory($name,$value));
859 $condition = ($blog && ($blog->getSetting($name) == $value));
861 case 'itemblogsetting':
862 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem['itemid']));
863 $condition = ($b && ($b->getSetting($name) == $value));
866 $condition = $member->isLoggedIn();
869 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
872 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
875 $condition = ($this->ifAuthor($name,$value));
878 $condition = $this->ifHasPlugin($name, $value);
881 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
888 * ItemActions::ifCategory()
889 * Different checks for a category
891 * @param string $key key of category
892 * @param string $value value for key of category
895 private function ifCategory($key = '', $value = '')
897 global $blog, $catid;
899 // when no parameter is defined, just check if a category is selected
900 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
902 return (boolean) $blog->isValidCategory($catid);
905 // check category name
906 if ( $key == 'catname' )
908 $value = $blog->getCategoryIdFromName($value);
909 if ( $value == $catid )
911 return (boolean) $blog->isValidCategory($catid);
916 if ( ($key == 'catid') && ($value == $catid) )
918 return (boolean) $blog->isValidCategory($catid);
924 * ItemActions::ifAuthor()
925 * Different checks for an author
927 * @param string $key key of data for author
928 * @param string $value value of data for author
929 * @return boolean correct or not
931 private function ifAuthor($key = '', $value = '')
933 global $member, $manager;
935 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem['itemid']));
937 // when no parameter is defined, just check if author is current visitor
938 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
940 return (boolean) ((integer) $member->getID() > 0 && (integer) $member->getID() == (integer) $this->currentItem['authorid']);
944 if ( $key == 'name' )
946 $value = strtolower($value);
947 if ( $value == strtolower($this->currentItem['author']) )
953 // check if author is admin
954 if ( ($key == 'isadmin') )
956 $aid = intval($this->currentItem['authorid']);
957 $blogid = intval($b->getID());
958 $amember =& $manager->getMember($aid);
959 if ( $amember->isAdmin() )
963 return (boolean) $amember->isBlogAdmin($blogid);
970 * ItemActions::ifItemCategory()
971 * Different checks for a category
973 * @param string $key key of data for category to which item belongs
974 * @param string $value value of data for category to which item belongs
975 * @return boolean correct or not
977 private function ifItemCategory($key = '', $value='')
979 global $catid, $manager;
981 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem['itemid']));
983 // when no parameter is defined, just check if a category is selected
984 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
986 return (boolean) $b->isValidCategory($catid);
989 $icatid = $this->currentItem['catid'];
991 // check category name
992 if ( $key == 'catname' )
994 $value = $b->getCategoryIdFromName($value);
995 if ( $value == $icatid )
997 return (boolean) $b->isValidCategory($icatid);
1001 // check category id
1002 if ( ($key == 'catid') && ($value == $icatid) )
1004 return (boolean) $b->isValidCategory($icatid);
1011 * ItemActions::ifOnTeam()
1012 * Checks if a member is on the team of a blog and return his rights
1014 * @param string $blogName name of weblog
1015 * @return boolean correct or not
1017 private function ifOnTeam($blogName = '')
1019 global $blog, $member, $manager;
1021 // when no blog found
1022 if ( ($blogName == '') && (!is_object($blog)) )
1027 // explicit blog selection
1028 if ( $blogName != '' )
1030 $blogid = getBlogIDFromName($blogName);
1034 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
1036 $blogid = $blog->getID();
1038 return (boolean) $member->teamRights($blogid);
1042 * ItemActions::ifAdmin()
1043 * Checks if a member is admin of a blog
1045 * @param string $blogName name of weblog
1046 * @return boolean correct or not
1048 private function ifAdmin($blogName = '')
1050 global $blog, $member, $manager;
1052 // when no blog found
1053 if ( ($blogName == '') && (!is_object($blog)) )
1058 // explicit blog selection
1059 if ( $blogName != '' )
1061 $blogid = getBlogIDFromName($blogName);
1065 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
1067 $blogid = $blog->getID();
1069 return (boolean) $member->isBlogAdmin($blogid);
1074 * ItemActions::ifHasPlugin()
1075 * hasplugin,PlugName
1076 * -> checks if plugin exists
1077 * hasplugin,PlugName,OptionName
1078 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
1079 * hasplugin,PlugName,OptionName=value
1080 * -> checks if the option OptionName from plugin PlugName is set to value
1082 * @param string $name name of plugin
1083 * @param string $value key (and value) of plugin option
1084 * @return boolean correct or not
1086 private function ifHasPlugin($name, $value)
1090 // (pluginInstalled method won't write a message in the actionlog on failure)
1091 if ( $manager->pluginInstalled("NP_{$name}"))
1093 $plugin =& $manager->getPlugin('NP_' . $name);
1094 if ( $plugin != NULL )
1102 list($name2, $value2) = preg_split('#=#', $value, 2);
1103 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
1107 else if ( $plugin->getOption($name2) == $value2 )
1114 return (boolean) $condition;
1118 * ItemActions::ifPlugin()
1119 * Checks if a plugin exists and call its doIf function
1121 * @param string $name name of plugin
1122 * @param string $key key of plugin option
1123 * @param string $value value of plugin option
1124 * @return boolean callback output from plugin
1126 private function ifPlugin($name, $key = '', $value = '')
1130 $plugin =& $manager->getPlugin("NP_{$name}");
1135 $params = func_get_args();
1136 array_shift($params);
1138 return (boolean) call_user_func_array(array(&$plugin, 'doIf'), $params);