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'];
426 elseif ( is_object($this->currentItem) )
428 $itemtitle = $this->currentItem->title;
433 echo Entity::hen($itemtitle);
436 echo Entity::hsc($itemtitle);
442 $this->highlightAndParse($itemtitle);
449 * ItemActions::parse_karma()
450 * Parse templatevar karma
452 * @param string $type type of data for karma
455 public function parse_karma($type = 'totalscore')
460 $karma =& $manager->getKarma($this->currentItem->itemid);
465 echo $karma->getNbPosVotes();
468 echo $karma->getNbNegVotes();
471 echo $karma->getNbOfVotes();
474 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
475 echo number_format($percentage,2), '%';
478 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
479 echo number_format($percentage,2), '%';
483 echo $karma->getTotalScore();
490 * ItemActions::parse_author()
491 * Parse templatevar author
493 * @param string $which key of data for author
496 public function parse_author($which = '')
501 echo $this->currentItem->authorname;
504 echo $this->currentItem->authorid;
507 echo $this->currentItem->authormail;
510 echo $this->currentItem->authorurl;
514 echo $this->currentItem->author;
520 * ItemActions::parse_smartbody()
521 * Parse templatevar smartbody
526 public function parse_smartbody()
528 if ( !$this->currentItem->more )
530 $this->highlightAndParse($this->currentItem->body);
534 $this->highlightAndParse($this->currentItem->more);
540 * ItemActions::parse_morelink()
541 * Parse templatevar morelink
543 public function parse_morelink()
545 if ( $this->currentItem->more )
547 $this->parser->parse($this->template['MORELINK']);
553 * ItemActions::parse_date()
554 * Parse templatevar date
556 * @param string $format format optional strftime format
559 public function parse_date($format = '')
561 if ( $format !== '' )
566 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
568 /* depends on the PHP's current locale */
573 $format = $this->template['FORMAT_DATE'];
579 $offset = $this->blog->getTimeOffset() * 3600;
582 echo i18n::formatted_datetime($format, $this->currentItem->timestamp, $offset);
587 * ItemActions::parse_time()
588 * Parse templatevar time
590 * @param string $format format optional strftime format
593 public function parse_time($format = '')
595 if ( $format !== '' )
600 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
602 /* depends on the PHP's current locale */
607 $format = $this->template['FORMAT_TIME'];
609 echo i18n::formatted_datetime($format, $this->currentItem->timestamp);
614 * ItemActions::parse_syndicate_title()
615 * Parse templatevar syndicate_title
617 * @param string $maxLength maxLength optional maximum length
618 * @return string syndicated title
620 public function parse_syndicate_title($maxLength = 100) {
621 $syndicated = strip_tags($this->currentItem->title);
622 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
626 * ItemActions::parse_syndicate_description()
627 * Parse templatevar syndicate_description
629 * @param stromg $maxLength maxlength optional maximum length
630 * @param string $addHighlight highlighted string
633 public function parse_syndicate_description($maxLength = 250, $addHighlight = 0)
635 $syndicated = strip_tags($this->currentItem->body);
638 $tmp_highlight = Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
639 echo $this->highlightAndParse($tmp_highlight);
643 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
649 * ItemActions::parse_karmaposlink()
650 * Parse templatevar karmaposlink
652 * @param string $text text element for anchor element
655 public function parse_karmaposlink($text = '')
658 $link = $CONF['ActionURL'] . '?action=votepositive&itemid=' . $this->currentItem->itemid;
661 echo '<a href="'.$link.'">' . $text . '</a>';
672 * ItemActions::parse_karmaneglink()
673 * Parse templatevar karmaneglink
675 * @param string $text text element for anchor element
678 public function parse_karmaneglink($text = '')
681 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem->itemid;
685 echo '<a href="'.$link.'">' . $text . '</a>';
696 * ItemActions::parse_new()
697 * Parse templatevar new
702 public function parse_new()
704 if ( ($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit) )
706 echo $this->template['NEW'];
712 * ItemActions::parse_daylink()
713 * Parse templatevar daylink
718 public function parse_daylink()
720 echo Link::create_archive_link($this->blog->getID(), i18n::formatted_datetime('%Y-%m-%d', $this->currentItem->timestamp), $this->linkparams);
725 * ItemActions::parse_comments(
726 * Parse templatevar comments
728 * @param integer $maxToShow maximum number of comments in a display
731 public function parse_comments($maxToShow = 0)
733 if ( $maxToShow == 0 )
735 $maxToShow = $this->blog->getMaxComments();
739 if ( $this->showComments && $this->blog->commentsEnabled() )
741 $comments = new Comments($this->currentItem->itemid);
742 $comments->setItemActions($this);
743 $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
749 * ItemActions::parse_plugin()
750 * Executes a plugin templatevar
752 * @param string $pluginName name of plugin (without the NP_)
753 * @param extra parameters can be added
756 public function parse_plugin($pluginName)
760 $plugin =& $manager->getPlugin("NP_{$pluginName}");
767 $params = func_get_args();
769 // remove plugin name
770 array_shift($params);
772 // add item reference (array_unshift didn't work)
773 $params = array_merge(array(&$this->currentItem),$params);
775 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
780 * ItemActions::parse_edit()
781 * Parse templatevar edit
786 public function parse_edit()
788 global $member, $CONF;
789 if ( $this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) )
791 $this->parser->parse($this->template['EDITLINK']);
797 * ItemActions::parse_editlink()
798 * Parse templatevar editlink
800 public function parse_editlink()
803 echo $CONF['AdminURL'] . 'bookmarklet.php?action=edit&itemid=' . $this->currentItem->itemid;
808 * ItemActions::parse_editpopupcode()
809 * Parse templatevar editpopupcode
814 public function parse_editpopupcode()
816 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;";
821 * ItemActions::highlightAndParse()
822 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
823 * from hacking your weblog.
824 * 'plugin variables in items' implementation by Andy
829 public function highlightAndParse(&$data)
831 $actions = new BodyActions($this->blog);
832 $parser = new Parser($actions->getDefinedActions(), $actions);
833 $actions->setTemplate($this->template);
834 $actions->setHighlight($this->strHighlight);
835 $actions->setCurrentItem($this->currentItem);
836 $parser->parse($actions->highlight($data));
841 * ItemActions::checkCondition()
842 * Checks conditions for if statements
844 * @param string $field type of <%if%>
845 * @param string $name property of field
846 * @param string $value value of property
849 protected function checkCondition($field, $name='', $value = '')
851 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
857 $condition = ($blog && $this->ifCategory($name,$value));
860 $condition = ($this->ifItemCategory($name,$value));
863 $condition = ($blog && ($blog->getSetting($name) == $value));
865 case 'itemblogsetting':
866 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
867 $condition = ($b && ($b->getSetting($name) == $value));
870 $condition = $member->isLoggedIn();
873 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
876 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
879 $condition = ($this->ifAuthor($name,$value));
882 $condition = $this->ifHasPlugin($name, $value);
885 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
892 * ItemActions::ifCategory()
893 * Different checks for a category
895 * @param string $key key of category
896 * @param string $value value for key of category
899 private function ifCategory($key = '', $value = '')
901 global $blog, $catid;
903 // when no parameter is defined, just check if a category is selected
904 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
906 return (boolean) $blog->isValidCategory($catid);
909 // check category name
910 if ( $key == 'catname' )
912 $value = $blog->getCategoryIdFromName($value);
913 if ( $value == $catid )
915 return (boolean) $blog->isValidCategory($catid);
920 if ( ($key == 'catid') && ($value == $catid) )
922 return (boolean) $blog->isValidCategory($catid);
928 * ItemActions::ifAuthor()
929 * Different checks for an author
931 * @param string $key key of data for author
932 * @param string $value value of data for author
933 * @return boolean correct or not
935 private function ifAuthor($key = '', $value = '')
937 global $member, $manager;
939 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
941 // when no parameter is defined, just check if author is current visitor
942 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
944 return (boolean) ((integer) $member->getID() > 0 && (integer) $member->getID() == (integer) $this->currentItem->authorid);
948 if ( $key == 'name' )
950 $value = strtolower($value);
951 if ( $value == strtolower($this->currentItem->author) )
957 // check if author is admin
958 if ( ($key == 'isadmin') )
960 $aid = intval($this->currentItem->authorid);
961 $blogid = intval($b->getID());
962 $amember =& $manager->getMember($aid);
963 if ( $amember->isAdmin() )
967 return (boolean) $amember->isBlogAdmin($blogid);
974 * ItemActions::ifItemCategory()
975 * Different checks for a category
977 * @param string $key key of data for category to which item belongs
978 * @param string $value value of data for category to which item belongs
979 * @return boolean correct or not
981 private function ifItemCategory($key = '', $value='')
983 global $catid, $manager;
985 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
987 // when no parameter is defined, just check if a category is selected
988 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
990 return (boolean) $b->isValidCategory($catid);
993 $icatid = $this->currentItem->catid;
995 // check category name
996 if ( $key == 'catname' )
998 $value = $b->getCategoryIdFromName($value);
999 if ( $value == $icatid )
1001 return (boolean) $b->isValidCategory($icatid);
1005 // check category id
1006 if ( ($key == 'catid') && ($value == $icatid) )
1008 return (boolean) $b->isValidCategory($icatid);
1015 * ItemActions::ifOnTeam()
1016 * Checks if a member is on the team of a blog and return his rights
1018 * @param string $blogName name of weblog
1019 * @return boolean correct or not
1021 private function ifOnTeam($blogName = '')
1023 global $blog, $member, $manager;
1025 // when no blog found
1026 if ( ($blogName == '') && (!is_object($blog)) )
1031 // explicit blog selection
1032 if ( $blogName != '' )
1034 $blogid = getBlogIDFromName($blogName);
1038 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
1040 $blogid = $blog->getID();
1042 return (boolean) $member->teamRights($blogid);
1046 * ItemActions::ifAdmin()
1047 * Checks if a member is admin of a blog
1049 * @param string $blogName name of weblog
1050 * @return boolean correct or not
1052 private function ifAdmin($blogName = '')
1054 global $blog, $member, $manager;
1056 // when no blog found
1057 if ( ($blogName == '') && (!is_object($blog)) )
1062 // explicit blog selection
1063 if ( $blogName != '' )
1065 $blogid = getBlogIDFromName($blogName);
1069 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
1071 $blogid = $blog->getID();
1073 return (boolean) $member->isBlogAdmin($blogid);
1078 * ItemActions::ifHasPlugin()
1079 * hasplugin,PlugName
1080 * -> checks if plugin exists
1081 * hasplugin,PlugName,OptionName
1082 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
1083 * hasplugin,PlugName,OptionName=value
1084 * -> checks if the option OptionName from plugin PlugName is set to value
1086 * @param string $name name of plugin
1087 * @param string $value key (and value) of plugin option
1088 * @return boolean correct or not
1090 private function ifHasPlugin($name, $value)
1094 // (pluginInstalled method won't write a message in the actionlog on failure)
1095 if ( $manager->pluginInstalled("NP_{$name}"))
1097 $plugin =& $manager->getPlugin('NP_' . $name);
1098 if ( $plugin != NULL )
1106 list($name2, $value2) = preg_split('#=#', $value, 2);
1107 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
1111 else if ( $plugin->getOption($name2) == $value2 )
1118 return (boolean) $condition;
1122 * ItemActions::ifPlugin()
1123 * Checks if a plugin exists and call its doIf function
1125 * @param string $name name of plugin
1126 * @param string $key key of plugin option
1127 * @param string $value value of plugin option
1128 * @return boolean callback output from plugin
1130 private function ifPlugin($name, $key = '', $value = '')
1134 $plugin =& $manager->getPlugin("NP_{$name}");
1139 $params = func_get_args();
1140 array_shift($params);
1142 return (boolean) call_user_func_array(array(&$plugin, 'doIf'), $params);