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