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 1886 2012-06-17 08:27:27Z 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::getAvailableActions()
133 * Returns an array with the actions that are defined
134 * in the ItemActions class
139 public function getAvailableActions()
141 return array_merge(self::$defined_actions, parent::getAvailableActions());
145 * ItemActions::setLastVisit()
147 * @param timestamp $lastVisit timestamp of latest visit
150 public function setLastVisit($lastVisit)
152 $this->lastVisit = $lastVisit;
157 * ItemActions::setCurrentItem()
159 * @param object $item instance of Item class
162 public function setCurrentItem(&$item)
164 global $currentitemid;
165 $this->currentItem =& $item;
166 $currentitemid = $this->currentItem['itemid'];
171 * ItemActions::setBlog()
173 * @param object &$blog instance of Blog class
176 public function setBlog(&$blog)
178 $this->blog =& $blog;
183 * ItemActions::setTemplate()
185 * @param array $template array including templates
188 public function setTemplate($template)
190 $this->template =& $template;
195 * ItemActions::setShowComments()
197 * @param boolean $val need to be displayed or not
200 public function setShowComments($val)
202 $this->showComments = (boolean) $val;
207 * ItemActions::parse_blogid()
208 * Parse templatevar blogid
213 public function parse_blogid()
215 echo $this->blog->getID();
219 * ItemActions::parse_body()
220 * Parse templatevar body
225 public function parse_body()
227 $this->highlightAndParse($this->currentItem['body']);
232 * ItemActions::parse_more()
233 * Parse templatevar more
238 public function parse_more()
240 $this->highlightAndParse($this->currentItem['more']);
245 * ItemActions::parse_itemid()
246 * Parse templatevar itemid
251 public function parse_itemid()
253 echo $this->currentItem['itemid'];
258 * ItemActions::parse_category()
259 * Parse templatevar category
264 public function parse_category()
266 echo $this->currentItem['category'];
271 * ItemActions::parse_categorylink()
272 * Parse templatevar categorylink
277 public function parse_categorylink()
279 echo Link::create_link('category', array('catid' => $this->currentItem['catid'], 'name' => $this->currentItem['category']));
284 * ItemActions::parse_catid()
285 * Parse templatevar catid
290 public function parse_catid()
292 echo $this->currentItem['catid'];
297 * ItemActions::parse_authorid()
298 * Parse templatevar authorid
303 public function parse_authorid()
305 echo $this->currentItem['authorid'];
310 * ItemActions::parse_authorlink()
311 * Parse templatevar authorlink
316 public function parse_authorlink()
319 'memberid' => $this->currentItem['authorid'],
320 'name' => $this->currentItem['author'],
321 'extra' => $this->linkparams
324 echo Link::create_link('member', $data);
329 * ItemActions::parse_query()
330 * Parse templatevar query
335 public function parse_query()
337 echo $this->strHighlight;
342 * ItemActions::parse_itemlink()
343 * Parse templatevar itemlink
348 public function parse_itemlink()
351 'itemid' => $this->currentItem['itemid'],
352 'title' => $this->currentItem['title'],
353 'timestamp' => $this->currentItem['timestamp'],
354 'extra' => $this->linkparams
357 echo Link::create_link('item', $data);
362 * ItemActions::parse_blogurl()
363 * Parse templatevar blogurl
368 public function parse_blogurl()
370 echo $this->blog->getURL();
375 * ItemActions::parse_closed()
376 * Parse templatevar closed
381 public function parse_closed()
383 echo $this->currentItem['closed'];
388 * ItemActions::parse_relevance()
389 * Parse templatevar relevance
394 public function parse_relevance()
396 echo round($this->currentItem['score'], 2);
401 * ItemActions::parse_title()
402 * Parse templatevar title
404 * @param string $format defines in which format the title is shown
407 public function parse_title($format = '')
409 if ( is_array($this->currentItem) )
411 $itemtitle = $this->currentItem['title'];
416 echo Entity::hen($itemtitle);
419 echo Entity::hsc($itemtitle);
425 $this->highlightAndParse($itemtitle);
432 * ItemActions::parse_karma()
433 * Parse templatevar karma
435 * @param string $type type of data for karma
438 public function parse_karma($type = 'totalscore')
443 $karma =& $manager->getKarma($this->currentItem['itemid']);
448 echo $karma->getNbPosVotes();
451 echo $karma->getNbNegVotes();
454 echo $karma->getNbOfVotes();
457 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
458 echo number_format($percentage,2), '%';
461 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
462 echo number_format($percentage,2), '%';
466 echo $karma->getTotalScore();
473 * ItemActions::parse_author()
474 * Parse templatevar author
476 * @param string $which key of data for author
479 public function parse_author($which = '')
484 echo $this->currentItem['authorname'];
487 echo $this->currentItem['authorid'];
490 echo $this->currentItem['authormail'];
493 echo $this->currentItem['authorurl'];
497 echo $this->currentItem['author'];
503 * ItemActions::parse_smartbody()
504 * Parse templatevar smartbody
509 public function parse_smartbody()
511 if ( !$this->currentItem['more'] )
513 $this->highlightAndParse($this->currentItem['body']);
517 $this->highlightAndParse($this->currentItem['more']);
523 * ItemActions::parse_morelink()
524 * Parse templatevar morelink
526 public function parse_morelink()
528 if ( $this->currentItem['more'] )
530 $this->parser->parse($this->template['MORELINK']);
536 * ItemActions::parse_date()
537 * Parse templatevar date
539 * @param string $format format optional strftime format
542 public function parse_date($format = '')
544 if ( $format !== '' )
549 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
551 /* depends on the PHP's current locale */
556 $format = $this->template['FORMAT_DATE'];
562 $offset = $this->blog->getTimeOffset() * 3600;
565 echo i18n::formatted_datetime($format, $this->currentItem['timestamp'], $offset);
570 * ItemActions::parse_time()
571 * Parse templatevar time
573 * @param string $format format optional strftime format
576 public function parse_time($format = '')
578 if ( $format !== '' )
583 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
585 /* depends on the PHP's current locale */
590 $format = $this->template['FORMAT_TIME'];
592 echo i18n::formatted_datetime($format, $this->currentItem['timestamp']);
597 * ItemActions::parse_syndicate_title()
598 * Parse templatevar syndicate_title
600 * @param string $maxLength maxLength optional maximum length
601 * @return string syndicated title
603 public function parse_syndicate_title($maxLength = 100) {
604 $syndicated = strip_tags($this->currentItem['title']);
605 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
609 * ItemActions::parse_syndicate_description()
610 * Parse templatevar syndicate_description
612 * @param stromg $maxLength maxlength optional maximum length
613 * @param string $addHighlight highlighted string
616 public function parse_syndicate_description($maxLength = 250, $addHighlight = 0)
618 $syndicated = strip_tags($this->currentItem['body']);
621 $tmp_highlight = Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
622 echo $this->highlightAndParse($tmp_highlight);
626 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
632 * ItemActions::parse_karmaposlink()
633 * Parse templatevar karmaposlink
635 * @param string $text text element for anchor element
638 public function parse_karmaposlink($text = '')
641 $link = $CONF['ActionURL'] . '?action=votepositive&itemid=' . $this->currentItem['itemid'];
644 echo '<a href="'.$link.'">' . $text . '</a>';
655 * ItemActions::parse_karmaneglink()
656 * Parse templatevar karmaneglink
658 * @param string $text text element for anchor element
661 public function parse_karmaneglink($text = '')
664 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem['itemid'];
668 echo '<a href="'.$link.'">' . $text . '</a>';
679 * ItemActions::parse_new()
680 * Parse templatevar new
685 public function parse_new()
687 if ( ($this->lastVisit != 0) && ($this->currentItem['timestamp'] > $this->lastVisit) )
689 echo $this->template['NEW'];
695 * ItemActions::parse_daylink()
696 * Parse templatevar daylink
701 public function parse_daylink()
703 echo Link::create_archive_link($this->blog->getID(), i18n::formatted_datetime('%Y-%m-%d', $this->currentItem['timestamp']), $this->linkparams);
708 * ItemActions::parse_comments(
709 * Parse templatevar comments
711 * @param integer $maxToShow maximum number of comments in a display
714 public function parse_comments($maxToShow = 0)
716 if ( $maxToShow == 0 )
718 $maxToShow = $this->blog->getMaxComments();
722 if ( $this->showComments && $this->blog->commentsEnabled() )
724 $comments = new Comments($this->currentItem['itemid']);
725 $comments->setItemActions($this);
726 $comments->showComments($this->template, $maxToShow, $this->currentItem['closed'] ? 0 : 1, $this->strHighlight);
732 * ItemActions::parse_plugin()
733 * Executes a plugin templatevar
735 * @param string $pluginName name of plugin (without the NP_)
736 * @param extra parameters can be added
739 public function parse_plugin($pluginName)
743 $plugin =& $manager->getPlugin("NP_{$pluginName}");
750 $params = func_get_args();
752 // remove plugin name
753 array_shift($params);
755 // add item reference (array_unshift didn't work)
756 $params = array_merge(array(&$this->currentItem),$params);
758 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
763 * ItemActions::parse_edit()
764 * Parse templatevar edit
769 public function parse_edit()
771 global $member, $CONF;
772 if ( $this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem['authorid'])) )
774 $this->parser->parse($this->template['EDITLINK']);
780 * ItemActions::parse_editlink()
781 * Parse templatevar editlink
783 public function parse_editlink()
786 echo $CONF['AdminURL'] . 'bookmarklet.php?action=edit&itemid=' . $this->currentItem['itemid'];
791 * ItemActions::parse_editpopupcode()
792 * Parse templatevar editpopupcode
797 public function parse_editpopupcode()
799 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;";
804 * ItemActions::highlightAndParse()
805 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
806 * from hacking your weblog.
807 * 'plugin variables in items' implementation by Andy
812 public function highlightAndParse(&$data)
814 $handler = new BodyActions($this->blog);
815 $handler->setTemplate($this->template);
816 $handler->setHighlight($this->strHighlight);
817 $handler->setCurrentItem($this->currentItem);
819 $parser = new Parser($handler);
820 $highlight = $handler->highlight($data);
821 $parser->parse($highlight);
826 * ItemActions::checkCondition()
827 * Checks conditions for if statements
829 * @param string $field type of <%if%>
830 * @param string $name property of field
831 * @param string $value value of property
834 protected function checkCondition($field, $name='', $value = '')
836 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
842 $condition = ($blog && $this->ifCategory($name,$value));
845 $condition = ($this->ifItemCategory($name,$value));
848 $condition = ($blog && ($blog->getSetting($name) == $value));
850 case 'itemblogsetting':
851 $item =& $manager->getItem($this->currentItem['itemid'], 1, 1);
852 $t_blog =& $manager->getBlog($item['blogid']);
853 $condition = ($t_blog && ($t_blog->getSetting($name) == $value));
856 $condition = $member->isLoggedIn();
859 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
862 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
865 $condition = ($this->ifAuthor($name,$value));
868 $condition = $this->ifHasPlugin($name, $value);
871 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
878 * ItemActions::ifCategory()
879 * Different checks for a category
881 * @param string $key key of category
882 * @param string $value value for key of category
885 private function ifCategory($key = '', $value = '')
887 global $blog, $catid;
889 // when no parameter is defined, just check if a category is selected
890 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
892 return (boolean) $blog->isValidCategory($catid);
895 // check category name
896 if ( $key == 'catname' )
898 $value = $blog->getCategoryIdFromName($value);
899 if ( $value == $catid )
901 return (boolean) $blog->isValidCategory($catid);
906 if ( ($key == 'catid') && ($value == $catid) )
908 return (boolean) $blog->isValidCategory($catid);
914 * ItemActions::ifAuthor()
915 * Different checks for an author
917 * @param string $key key of data for author
918 * @param string $value value of data for author
919 * @return boolean correct or not
921 private function ifAuthor($key = '', $value = '')
923 global $member, $manager;
925 // when no parameter is defined, just check if author is current visitor
926 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
928 return (boolean) ((integer) $member->getID() > 0 && (integer) $member->getID() == (integer) $this->currentItem['authorid']);
932 if ( $key == 'name' )
934 $value = strtolower($value);
935 if ( $value == strtolower($this->currentItem['author']) )
941 // check if author is admin
942 if ( ($key == 'isadmin') )
944 $i_author =& $manager->getMember($this->currentItem['authorid']);
945 if ( $i_author->isAdmin() )
949 return (boolean) $i_author->isBlogAdmin($this->currentItem['blogid']);
956 * ItemActions::ifItemCategory()
957 * Different checks for a category
959 * @param string $key key of data for category to which item belongs
960 * @param string $value value of data for category to which item belongs
961 * @return boolean correct or not
963 private function ifItemCategory($key = '', $value='')
965 global $catid, $manager;
967 $blog =& $manager->getBlog($this->currentItem['blogid']);
969 // when no parameter is defined, just check if a category is selected
970 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
972 return (boolean) $blog->isValidCategory($catid);
975 $icatid = $this->currentItem['catid'];
977 // check category name
978 if ( $key == 'catname' )
980 $value = $blog->getCategoryIdFromName($value);
981 if ( $value == $icatid )
983 return (boolean) $blog->isValidCategory($icatid);
988 if ( ($key == 'catid') && ($value == $icatid) )
990 return (boolean) $blog->isValidCategory($icatid);
997 * ItemActions::ifOnTeam()
998 * Checks if a member is on the team of a blog and return his rights
1000 * @param string $blogName name of weblog
1001 * @return boolean correct or not
1003 private function ifOnTeam($blogName = '')
1005 global $blog, $member, $manager;
1007 // when no blog found
1008 if ( ($blogName == '') && (!is_object($blog)) )
1013 // explicit blog selection
1014 if ( $blogName != '' )
1016 $blogid = getBlogIDFromName($blogName);
1020 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
1022 $blogid = $blog->getID();
1024 return (boolean) $member->teamRights($blogid);
1028 * ItemActions::ifAdmin()
1029 * Checks if a member is admin of a blog
1031 * @param string $blogName name of weblog
1032 * @return boolean correct or not
1034 private function ifAdmin($blogName = '')
1036 global $blog, $member, $manager;
1038 // when no blog found
1039 if ( ($blogName == '') && (!is_object($blog)) )
1044 // explicit blog selection
1045 if ( $blogName != '' )
1047 $blogid = getBlogIDFromName($blogName);
1051 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
1053 $blogid = $blog->getID();
1055 return (boolean) $member->isBlogAdmin($blogid);
1060 * ItemActions::ifHasPlugin()
1061 * hasplugin,PlugName
1062 * -> checks if plugin exists
1063 * hasplugin,PlugName,OptionName
1064 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
1065 * hasplugin,PlugName,OptionName=value
1066 * -> checks if the option OptionName from plugin PlugName is set to value
1068 * @param string $name name of plugin
1069 * @param string $value key (and value) of plugin option
1070 * @return boolean correct or not
1072 private function ifHasPlugin($name, $value)
1076 // (pluginInstalled method won't write a message in the actionlog on failure)
1077 if ( $manager->pluginInstalled("NP_{$name}"))
1079 $plugin =& $manager->getPlugin('NP_' . $name);
1080 if ( $plugin != NULL )
1088 list($name2, $value2) = preg_split('#=#', $value, 2);
1089 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
1093 else if ( $plugin->getOption($name2) == $value2 )
1100 return (boolean) $condition;
1104 * ItemActions::ifPlugin()
1105 * Checks if a plugin exists and call its doIf function
1107 * @param string $name name of plugin
1108 * @param string $key key of plugin option
1109 * @param string $value value of plugin option
1110 * @return boolean callback output from plugin
1112 private function ifPlugin($name, $key = '', $value = '')
1116 $plugin =& $manager->getPlugin("NP_{$name}");
1121 $params = func_get_args();
1122 array_shift($params);
1124 return (boolean) call_user_func_array(array(&$plugin, 'doIf'), $params);