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(
90 'syndicate_description',
119 * ItemActions::__construct
120 * Enter description here ...
121 * @param unknown_type $blog
123 public function __construct(&$blog)
125 global $catid, $member;
126 // call constructor of superclass first
129 // extra parameters for created links
132 $this->linkparams = array('catid' => $catid);
135 // check if member is blog admin (and thus allowed to edit all items)
136 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
137 $this->setBlog($blog);
142 * ItemActions::getDefinedActions()
143 * Returns an array with the actions that are defined
144 * in the ItemActions class
149 public function getDefinedActions()
151 return self::$defined_actions;
155 * ItemActions::setLastVisit()
157 * @param timestamp $lastVisit timestamp of latest visit
160 public function setLastVisit($lastVisit)
162 $this->lastVisit = $lastVisit;
167 * ItemActions::setParser()
169 * @param object &$parser instance of Parser class
172 public function setParser(&$parser)
174 $this->parser =& $parser;
179 * ItemActions::setCurrentItem()
181 * @param object $item instance of Item class
184 public function setCurrentItem(&$item)
186 global $currentitemid;
187 $this->currentItem =& $item;
188 $currentitemid = $this->currentItem->itemid;
193 * ItemActions::setBlog()
195 * @param object &$blog instance of Blog class
198 public function setBlog(&$blog)
200 $this->blog =& $blog;
205 * ItemActions::setTemplate()
207 * @param array $template array including templates
210 public function setTemplate($template)
212 $this->template =& $template;
217 * ItemActions::setShowComments()
219 * @param boolean $val need to be displayed or not
222 public function setShowComments($val)
224 $this->showComments = (boolean) $val;
229 * ItemActions::parse_blogid()
230 * Parse templatevar blogid
235 public function parse_blogid()
237 echo $this->blog->getID();
241 * ItemActions::parse_body()
242 * Parse templatevar body
247 public function parse_body()
249 $this->highlightAndParse($this->currentItem->body);
254 * ItemActions::parse_more()
255 * Parse templatevar more
260 public function parse_more()
262 $this->highlightAndParse($this->currentItem->more);
267 * ItemActions::parse_itemid()
268 * Parse templatevar itemid
273 public function parse_itemid()
275 echo $this->currentItem->itemid;
280 * ItemActions::parse_category()
281 * Parse templatevar category
286 public function parse_category()
288 echo $this->currentItem->category;
293 * ItemActions::parse_categorylink()
294 * Parse templatevar categorylink
299 public function parse_categorylink()
301 echo Link::create_link('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
306 * ItemActions::parse_catid()
307 * Parse templatevar catid
312 public function parse_catid()
314 echo $this->currentItem->catid;
319 * ItemActions::parse_authorid()
320 * Parse templatevar authorid
325 public function parse_authorid()
327 echo $this->currentItem->authorid;
332 * ItemActions::parse_authorlink()
333 * Parse templatevar authorlink
338 public function parse_authorlink()
341 'memberid' => $this->currentItem->authorid,
342 'name' => $this->currentItem->author,
343 'extra' => $this->linkparams
346 echo Link::create_link('member', $data);
351 * ItemActions::parse_query()
352 * Parse templatevar query
357 public function parse_query()
359 echo $this->strHighlight;
364 * ItemActions::parse_itemlink()
365 * Parse templatevar itemlink
370 public function parse_itemlink()
373 'itemid' => $this->currentItem->itemid,
374 'title' => $this->currentItem->title,
375 'timestamp' => $this->currentItem->timestamp,
376 'extra' => $this->linkparams
379 echo Link::create_link('item', $data);
384 * ItemActions::parse_blogurl()
385 * Parse templatevar blogurl
390 public function parse_blogurl()
392 echo $this->blog->getURL();
397 * ItemActions::parse_closed()
398 * Parse templatevar closed
403 public function parse_closed()
405 echo $this->currentItem->closed;
410 * ItemActions::parse_relevance()
411 * Parse templatevar relevance
416 public function parse_relevance()
418 echo round($this->currentItem->score,2);
423 * ItemActions::parse_title()
424 * Parse templatevar title
426 * @param string $format defines in which format the title is shown
429 public function parse_title($format = '')
431 if ( is_array($this->currentItem) )
433 $itemtitle = $this->currentItem['title'];
435 elseif ( is_object($this->currentItem) )
437 $itemtitle = $this->currentItem->title;
442 echo Entity::hen($itemtitle);
445 echo Entity::hsc($itemtitle);
451 $this->highlightAndParse($itemtitle);
458 * ItemActions::parse_karma()
459 * Parse templatevar karma
461 * @param string $type type of data for karma
464 public function parse_karma($type = 'totalscore')
469 $karma =& $manager->getKarma($this->currentItem->itemid);
474 echo $karma->getNbPosVotes();
477 echo $karma->getNbNegVotes();
480 echo $karma->getNbOfVotes();
483 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
484 echo number_format($percentage,2), '%';
487 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
488 echo number_format($percentage,2), '%';
492 echo $karma->getTotalScore();
499 * ItemActions::parse_author()
500 * Parse templatevar author
502 * @param string $which key of data for author
505 public function parse_author($which = '')
510 echo $this->currentItem->authorname;
513 echo $this->currentItem->authorid;
516 echo $this->currentItem->authormail;
519 echo $this->currentItem->authorurl;
523 echo $this->currentItem->author;
529 * ItemActions::parse_smartbody()
530 * Parse templatevar smartbody
535 public function parse_smartbody()
537 if ( !$this->currentItem->more )
539 $this->highlightAndParse($this->currentItem->body);
543 $this->highlightAndParse($this->currentItem->more);
549 * ItemActions::parse_morelink()
550 * Parse templatevar morelink
552 public function parse_morelink()
554 if ( $this->currentItem->more )
556 $this->parser->parse($this->template['MORELINK']);
562 * ItemActions::parse_date()
563 * Parse templatevar date
565 * @param string $format format optional strftime format
568 public function parse_date($format = '')
570 if ( $format !== '' )
575 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
577 /* depends on the PHP's current locale */
582 $format = $this->template['FORMAT_DATE'];
588 $offset = $this->blog->getTimeOffset() * 3600;
591 echo i18n::formatted_datetime($format, $this->currentItem->timestamp, $offset);
596 * ItemActions::parse_time()
597 * Parse templatevar time
599 * @param string $format format optional strftime format
602 public function parse_time($format = '')
604 if ( $format !== '' )
609 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
611 /* depends on the PHP's current locale */
616 $format = $this->template['FORMAT_TIME'];
618 echo i18n::formatted_datetime($format, $this->currentItem->timestamp);
623 * ItemActions::parse_syndicate_title()
624 * Parse templatevar syndicate_title
626 * @param string $maxLength maxLength optional maximum length
627 * @return string syndicated title
629 public function parse_syndicate_title($maxLength = 100) {
630 $syndicated = strip_tags($this->currentItem->title);
631 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
635 * ItemActions::parse_syndicate_description()
636 * Parse templatevar syndicate_description
638 * @param stromg $maxLength maxlength optional maximum length
639 * @param string $addHighlight highlighted string
642 public function parse_syndicate_description($maxLength = 250, $addHighlight = 0)
644 $syndicated = strip_tags($this->currentItem->body);
647 $tmp_highlight = Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
648 echo $this->highlightAndParse($tmp_highlight);
652 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
658 * ItemActions::parse_karmaposlink()
659 * Parse templatevar karmaposlink
661 * @param string $text text element for anchor element
664 public function parse_karmaposlink($text = '')
667 $link = $CONF['ActionURL'] . '?action=votepositive&itemid=' . $this->currentItem->itemid;
670 echo '<a href="'.$link.'">' . $text . '</a>';
681 * ItemActions::parse_karmaneglink()
682 * Parse templatevar karmaneglink
684 * @param string $text text element for anchor element
687 public function parse_karmaneglink($text = '')
690 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem->itemid;
694 echo '<a href="'.$link.'">' . $text . '</a>';
705 * ItemActions::parse_new()
706 * Parse templatevar new
711 public function parse_new()
713 if ( ($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit) )
715 echo $this->template['NEW'];
721 * ItemActions::parse_daylink()
722 * Parse templatevar daylink
727 public function parse_daylink()
729 echo Link::create_archive_link($this->blog->getID(), i18n::formatted_datetime('%Y-%m-%d', $this->currentItem->timestamp), $this->linkparams);
734 * ItemActions::parse_comments(
735 * Parse templatevar comments
737 * @param integer $maxToShow maximum number of comments in a display
740 public function parse_comments($maxToShow = 0)
742 if ( $maxToShow == 0 )
744 $maxToShow = $this->blog->getMaxComments();
748 if ( $this->showComments && $this->blog->commentsEnabled() )
750 $comments = new Comments($this->currentItem->itemid);
751 $comments->setItemActions($this);
752 $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
758 * ItemActions::parse_plugin()
759 * Executes a plugin templatevar
761 * @param string $pluginName name of plugin (without the NP_)
762 * @param extra parameters can be added
765 public function parse_plugin($pluginName)
769 $plugin =& $manager->getPlugin("NP_{$pluginName}");
776 $params = func_get_args();
778 // remove plugin name
779 array_shift($params);
781 // add item reference (array_unshift didn't work)
782 $params = array_merge(array(&$this->currentItem),$params);
784 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
789 * ItemActions::parse_edit()
790 * Parse templatevar edit
795 public function parse_edit()
797 global $member, $CONF;
798 if ( $this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) )
800 $this->parser->parse($this->template['EDITLINK']);
806 * ItemActions::parse_editlink()
807 * Parse templatevar editlink
809 public function parse_editlink()
812 echo $CONF['AdminURL'] . 'bookmarklet.php?action=edit&itemid=' . $this->currentItem->itemid;
817 * ItemActions::parse_editpopupcode()
818 * Parse templatevar editpopupcode
823 public function parse_editpopupcode()
825 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;";
830 * ItemActions::highlightAndParse()
831 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
832 * from hacking your weblog.
833 * 'plugin variables in items' implementation by Andy
838 public function highlightAndParse(&$data)
840 $actions = new BodyActions($this->blog);
841 $parser = new Parser($actions->getDefinedActions(), $actions);
842 $actions->setTemplate($this->template);
843 $actions->setHighlight($this->strHighlight);
844 $actions->setCurrentItem($this->currentItem);
845 $parser->parse($actions->highlight($data));
850 * ItemActions::checkCondition()
851 * Checks conditions for if statements
853 * @param string $field type of <%if%>
854 * @param string $name property of field
855 * @param string $value value of property
858 private function checkCondition($field, $name='', $value = '')
860 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
866 $condition = ($blog && $this->ifCategory($name,$value));
869 $condition = ($this->ifItemCategory($name,$value));
872 $condition = ($blog && ($blog->getSetting($name) == $value));
874 case 'itemblogsetting':
875 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
876 $condition = ($b && ($b->getSetting($name) == $value));
879 $condition = $member->isLoggedIn();
882 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
885 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
888 $condition = ($this->ifAuthor($name,$value));
891 $condition = $this->ifHasPlugin($name, $value);
894 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
901 * ItemActions::ifCategory()
902 * Different checks for a category
904 * @param string $key key of category
905 * @param string $value value for key of category
908 private function ifCategory($key = '', $value = '')
910 global $blog, $catid;
912 // when no parameter is defined, just check if a category is selected
913 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
915 return (boolean) $blog->isValidCategory($catid);
918 // check category name
919 if ( $key == 'catname' )
921 $value = $blog->getCategoryIdFromName($value);
922 if ( $value == $catid )
924 return (boolean) $blog->isValidCategory($catid);
929 if ( ($key == 'catid') && ($value == $catid) )
931 return (boolean) $blog->isValidCategory($catid);
937 * ItemActions::ifAuthor()
938 * Different checks for an author
940 * @param string $key key of data for author
941 * @param string $value value of data for author
942 * @return boolean correct or not
944 private function ifAuthor($key = '', $value = '')
946 global $member, $manager;
948 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
950 // when no parameter is defined, just check if author is current visitor
951 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
953 return (boolean) ((integer) $member->getID() > 0 && (integer) $member->getID() == (integer) $this->currentItem->authorid);
957 if ( $key == 'name' )
959 $value = strtolower($value);
960 if ( $value == strtolower($this->currentItem->author) )
966 // check if author is admin
967 if ( ($key == 'isadmin') )
969 $aid = intval($this->currentItem->authorid);
970 $blogid = intval($b->getID());
971 $amember =& $manager->getMember($aid);
972 if ( $amember->isAdmin() )
976 return (boolean) $amember->isBlogAdmin($blogid);
983 * ItemActions::ifItemCategory()
984 * Different checks for a category
986 * @param string $key key of data for category to which item belongs
987 * @param string $value value of data for category to which item belongs
988 * @return boolean correct or not
990 private function ifItemCategory($key = '', $value='')
992 global $catid, $manager;
994 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
996 // when no parameter is defined, just check if a category is selected
997 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
999 return (boolean) $b->isValidCategory($catid);
1002 $icatid = $this->currentItem->catid;
1004 // check category name
1005 if ( $key == 'catname' )
1007 $value = $b->getCategoryIdFromName($value);
1008 if ( $value == $icatid )
1010 return (boolean) $b->isValidCategory($icatid);
1014 // check category id
1015 if ( ($key == 'catid') && ($value == $icatid) )
1017 return (boolean) $b->isValidCategory($icatid);
1024 * ItemActions::ifOnTeam()
1025 * Checks if a member is on the team of a blog and return his rights
1027 * @param string $blogName name of weblog
1028 * @return boolean correct or not
1030 private function ifOnTeam($blogName = '')
1032 global $blog, $member, $manager;
1034 // when no blog found
1035 if ( ($blogName == '') && (!is_object($blog)) )
1040 // explicit blog selection
1041 if ( $blogName != '' )
1043 $blogid = getBlogIDFromName($blogName);
1047 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
1049 $blogid = $blog->getID();
1051 return (boolean) $member->teamRights($blogid);
1055 * ItemActions::ifAdmin()
1056 * Checks if a member is admin of a blog
1058 * @param string $blogName name of weblog
1059 * @return boolean correct or not
1061 private function ifAdmin($blogName = '')
1063 global $blog, $member, $manager;
1065 // when no blog found
1066 if ( ($blogName == '') && (!is_object($blog)) )
1071 // explicit blog selection
1072 if ( $blogName != '' )
1074 $blogid = getBlogIDFromName($blogName);
1078 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
1080 $blogid = $blog->getID();
1082 return (boolean) $member->isBlogAdmin($blogid);
1087 * ItemActions::ifHasPlugin()
1088 * hasplugin,PlugName
1089 * -> checks if plugin exists
1090 * hasplugin,PlugName,OptionName
1091 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
1092 * hasplugin,PlugName,OptionName=value
1093 * -> checks if the option OptionName from plugin PlugName is set to value
1095 * @param string $name name of plugin
1096 * @param string $value key (and value) of plugin option
1097 * @return boolean correct or not
1099 private function ifHasPlugin($name, $value)
1103 // (pluginInstalled method won't write a message in the actionlog on failure)
1104 if ( $manager->pluginInstalled("NP_{$name}"))
1106 $plugin =& $manager->getPlugin('NP_' . $name);
1107 if ( $plugin != NULL )
1115 list($name2, $value2) = preg_split('#=#', $value, 2);
1116 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
1120 else if ( $plugin->getOption($name2) == $value2 )
1127 return (boolean) $condition;
1131 * ItemActions::ifPlugin()
1132 * Checks if a plugin exists and call its doIf function
1134 * @param string $name name of plugin
1135 * @param string $key key of plugin option
1136 * @param string $value value of plugin option
1137 * @return boolean callback output from plugin
1139 private function ifPlugin($name, $key = '', $value = '')
1143 $plugin =& $manager->getPlugin("NP_{$name}");
1148 $params = func_get_args();
1149 array_shift($params);
1151 return (boolean) call_user_func_array(array(&$plugin, 'doIf'), $params);