3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2009 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-2009 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 // contains an assoc array with parameters that need to be included when
\r
23 // generating links to items/archives/... (e.g. catid)
\r
26 // true when the current user is a blog admin (and thus allowed to edit all items)
\r
29 // timestamp of last visit
\r
32 // item currently being handled (mysql result object, see Blog::showUsingQuery)
\r
35 // reference to the blog currently being displayed
\r
38 // associative array with template info (part name => contents)
\r
41 // true when comments need to be displayed
\r
44 function __construct(&$blog) {
\r
45 // call constructor of superclass first
\r
46 $this->BaseActions();
\r
48 // extra parameters for created links
\r
51 $this->linkparams = array('catid' => $catid);
\r
53 // check if member is blog admin (and thus allowed to edit all items)
\r
55 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
\r
56 $this->setBlog($blog);
\r
60 * Returns an array with the actions that are defined
\r
61 * in the ItemActions class
\r
63 function getDefinedActions() {
\r
86 'syndicate_description',
\r
115 function setLastVisit($lastVisit) {
\r
116 $this->lastVisit = $lastVisit;
\r
119 function setParser(&$parser) {
\r
120 $this->parser =& $parser;
\r
123 function setCurrentItem(&$item) {
\r
124 $this->currentItem =& $item;
\r
125 global $currentitemid;
\r
126 $currentitemid = $this->currentItem->itemid;
\r
129 function setBlog(&$blog) {
\r
130 $this->blog =& $blog;
\r
133 function setTemplate($template) {
\r
134 $this->template =& $template;
\r
137 function setShowComments($val) {
\r
138 $this->showComments = $val;
\r
141 // methods used by parser to insert content
\r
145 * Parse templatevar blogid
\r
147 function parse_blogid() {
\r
148 echo $this->blog->getID();
\r
152 * Parse templatevar body
\r
154 function parse_body() {
\r
155 $this->highlightAndParse($this->currentItem->body);
\r
159 * Parse templatevar more
\r
161 function parse_more() {
\r
162 $this->highlightAndParse($this->currentItem->more);
\r
166 * Parse templatevar itemid
\r
168 function parse_itemid() {
\r
169 echo $this->currentItem->itemid;
\r
173 * Parse templatevar category
\r
175 function parse_category() {
\r
176 echo $this->currentItem->category;
\r
180 * ItemActions::parse_categorylink()
\r
181 * Parse templatevar categorylink
\r
186 function parse_categorylink()
\r
188 echo Link::create_link('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
\r
193 * Parse templatevar catid
\r
195 function parse_catid() {
\r
196 echo $this->currentItem->catid;
\r
200 * Parse templatevar authorid
\r
202 function parse_authorid() {
\r
203 echo $this->currentItem->authorid;
\r
207 * ItemActions::parse_authorlink()
\r
208 * Parse templatevar authorlink
\r
213 function parse_authorlink()
\r
215 echo Link::create_link(
\r
218 'memberid' => $this->currentItem->authorid,
\r
219 'name' => $this->currentItem->author,
\r
220 'extra' => $this->linkparams
\r
227 * Parse templatevar query
\r
229 function parse_query() {
\r
230 echo $this->strHighlight;
\r
234 * ItemActions::parse_itemlink()
\r
235 * Parse templatevar itemlink
\r
240 function parse_itemlink()
\r
242 echo Link::create_link(
\r
245 'itemid' => $this->currentItem->itemid,
\r
246 'title' => $this->currentItem->title,
\r
247 'timestamp' => $this->currentItem->timestamp,
\r
248 'extra' => $this->linkparams
\r
255 * Parse templatevar blogurl
\r
257 function parse_blogurl() {
\r
258 echo $this->blog->getURL();
\r
262 * Parse templatevar closed
\r
264 function parse_closed() {
\r
265 echo $this->currentItem->closed;
\r
269 * Parse templatevar relevance
\r
271 function parse_relevance() {
\r
272 echo round($this->currentItem->score,2);
\r
276 * Parse templatevar title
\r
278 * @param string $format defines in which format the title is shown
\r
280 function parse_title($format = '')
\r
282 if ( is_array($this->currentItem) )
\r
284 $itemtitle = $this->currentItem['title'];
\r
286 elseif ( is_object($this->currentItem) )
\r
288 $itemtitle = $this->currentItem->title;
\r
293 echo Entity::hen($itemtitle);
\r
296 echo Entity::hsc($itemtitle);
\r
302 $this->highlightAndParse($itemtitle);
\r
309 * Parse templatevar karma
\r
311 function parse_karma($type = 'totalscore') {
\r
314 // get karma object
\r
315 $karma =& $manager->getKarma($this->currentItem->itemid);
\r
319 echo $karma->getNbPosVotes();
\r
322 echo $karma->getNbNegVotes();
\r
325 echo $karma->getNbOfVotes();
\r
328 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
\r
329 echo number_format($percentage,2), '%';
\r
332 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
\r
333 echo number_format($percentage,2), '%';
\r
337 echo $karma->getTotalScore();
\r
344 * Parse templatevar author
\r
346 function parse_author($which = '') {
\r
350 echo $this->currentItem->authorname;
\r
353 echo $this->currentItem->authorid;
\r
356 echo $this->currentItem->authormail;
\r
359 echo $this->currentItem->authorurl;
\r
363 echo $this->currentItem->author;
\r
368 * Parse templatevar smartbody
\r
370 function parse_smartbody() {
\r
371 if (!$this->currentItem->more) {
\r
372 $this->highlightAndParse($this->currentItem->body);
\r
374 $this->highlightAndParse($this->currentItem->more);
\r
379 * Parse templatevar morelink
\r
381 function parse_morelink() {
\r
382 if ($this->currentItem->more)
\r
383 $this->parser->parse($this->template['MORELINK']);
\r
387 * ItemActions::parse_date()
\r
388 * Parse templatevar date
\r
390 * @param string $format format optional strftime format
\r
393 function parse_date($format = '')
\r
395 if ( $format !== '' )
\r
400 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
\r
402 /* depends on the PHP's current locale */
\r
407 $format = $this->template['FORMAT_DATE'];
\r
413 $offset = $this->blog->getTimeOffset() * 3600;
\r
416 echo i18n::formatted_datetime($format, $this->currentItem->timestamp, $offset);
\r
421 * ItemActions::parse_time()
\r
422 * Parse templatevar time
\r
424 * @param string $format format optional strftime format
\r
428 function parse_time($format = '')
\r
430 if ( $format !== '' )
\r
435 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
\r
437 /* depends on the PHP's current locale */
\r
442 $format = $this->template['FORMAT_TIME'];
\r
444 echo i18n::formatted_datetime($format, $this->currentItem->timestamp);
\r
449 * ItemActions::parse_syndicate_title()
\r
450 * Parse templatevar syndicate_title
\r
452 * @param String $maxLength maxLength optional maximum length
\r
453 * @return String syndicated title
\r
455 function parse_syndicate_title($maxLength = 100) {
\r
456 $syndicated = strip_tags($this->currentItem->title);
\r
457 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
\r
461 * ItemActions::parse_syndicate_description()
\r
462 * Parse templatevar syndicate_description
\r
464 * @param Stromg $maxLength maxlength optional maximum length
\r
465 * @param String $addHighlight highlighted string
\r
468 function parse_syndicate_description($maxLength = 250, $addHighlight = 0)
\r
470 $syndicated = strip_tags($this->currentItem->body);
\r
471 if ( $addHighlight )
\r
473 $tmp_highlight = Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
\r
474 echo $this->highlightAndParse($tmp_highlight);
\r
478 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
\r
484 * Parse templatevar karmaposlink
\r
486 * @param string text
\r
488 function parse_karmaposlink($text = '') {
\r
490 $link = $CONF['ActionURL'] . '?action=votepositive&itemid='.$this->currentItem->itemid;
\r
491 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
\r
495 * Parse templatevar karmaneglink
\r
497 * @param string text
\r
499 function parse_karmaneglink($text = '') {
\r
501 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem->itemid;
\r
502 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
\r
506 * Parse templatevar new
\r
508 function parse_new() {
\r
509 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
\r
510 echo $this->template['NEW'];
\r
514 * ItemActions::parse_daylink()
\r
515 * Parse templatevar daylink
\r
520 function parse_daylink()
\r
522 echo Link::create_archive_link($this->blog->getID(), i18n::formatted_datetime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
\r
527 * Parse templatevar comments
\r
529 function parse_comments($maxToShow = 0) {
\r
530 if ($maxToShow == 0)
\r
531 $maxToShow = $this->blog->getMaxComments();
\r
534 if ($this->showComments && $this->blog->commentsEnabled()) {
\r
535 $comments = new Comments($this->currentItem->itemid);
\r
536 $comments->setItemActions($this);
\r
537 $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
\r
542 * Executes a plugin templatevar
\r
544 * @param pluginName name of plugin (without the NP_)
\r
546 * extra parameters can be added
\r
548 function parse_plugin($pluginName) {
\r
551 // should be already tested from the parser (PARSER.php)
\r
552 // only continue when the plugin is really installed
\r
553 /*if (!$manager->pluginInstalled('NP_' . $pluginName))
\r
556 $plugin =& $manager->getPlugin('NP_' . $pluginName);
\r
557 if (!$plugin) return;
\r
560 $params = func_get_args();
\r
562 // remove plugin name
\r
563 array_shift($params);
\r
565 // add item reference (array_unshift didn't work)
\r
566 $params = array_merge(array(&$this->currentItem),$params);
\r
568 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
\r
572 * Parse templatevar edit
\r
574 function parse_edit() {
\r
575 global $member, $CONF;
\r
576 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {
\r
577 $this->parser->parse($this->template['EDITLINK']);
\r
582 * Parse templatevar editlink
\r
584 function parse_editlink() {
\r
586 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&itemid=',$this->currentItem->itemid;
\r
590 * Parse templatevar editpopupcode
\r
592 function parse_editpopupcode() {
\r
593 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
596 // helper functions
\r
599 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
\r
600 * from hacking your weblog.
\r
601 * 'plugin variables in items' implementation by Andy
\r
603 function highlightAndParse(&$data) {
\r
604 $actions = new BodyActions($this->blog);
\r
605 $parser = new Parser($actions->getDefinedActions(), $actions);
\r
606 $actions->setTemplate($this->template);
\r
607 $actions->setHighlight($this->strHighlight);
\r
608 $actions->setCurrentItem($this->currentItem);
\r
609 //$actions->setParser($parser);
\r
610 $parser->parse($actions->highlight($data));
\r
614 // this is the function previous to the 'plugin variables in items' implementation by Andy
\r
615 function highlightAndParse(&$data) {
\r
616 // allow only a limited subset of actions (do not allow includes etc, they might be evil)
\r
617 $this->parser->actions = array('image','media','popup');
\r
618 $tmp_highlight = $this->highlight($data);
\r
619 $this->parser->parse($tmp_highlight);
\r
620 $this->parser->actions = $this->getDefinedActions();
\r
624 // function to enable if-else-elseif-elseifnot-ifnot-endif to item template fields
\r
627 * Checks conditions for if statements
\r
629 * @param string $field type of <%if%>
\r
630 * @param string $name property of field
\r
631 * @param string $value value of property
\r
633 function checkCondition($field, $name='', $value = '') {
\r
634 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
\r
639 $condition = ($blog && $this->_ifCategory($name,$value));
\r
641 case 'itemcategory':
\r
642 $condition = ($this->_ifItemCategory($name,$value));
\r
644 case 'blogsetting':
\r
645 $condition = ($blog && ($blog->getSetting($name) == $value));
\r
647 case 'itemblogsetting':
\r
648 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
\r
649 $condition = ($b && ($b->getSetting($name) == $value));
\r
652 $condition = $member->isLoggedIn();
\r
655 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
\r
658 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
\r
661 $condition = ($this->_ifAuthor($name,$value));
\r
663 /* case 'nextitem':
\r
664 $condition = ($itemidnext != '');
\r
667 $condition = ($itemidprev != '');
\r
669 case 'archiveprevexists':
\r
670 $condition = ($archiveprevexists == true);
\r
672 case 'archivenextexists':
\r
673 $condition = ($archivenextexists == true);
\r
676 $condition = ($name == $this->skintype);
\r
679 $condition = $this->_ifHasPlugin($name, $value);
\r
682 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
\r
689 * Different checks for a category
\r
691 function _ifCategory($name = '', $value='') {
\r
692 global $blog, $catid;
\r
694 // when no parameter is defined, just check if a category is selected
\r
695 if (($name != 'catname' && $name != 'catid') || ($value == ''))
\r
696 return $blog->isValidCategory($catid);
\r
698 // check category name
\r
699 if ($name == 'catname') {
\r
700 $value = $blog->getCategoryIdFromName($value);
\r
701 if ($value == $catid)
\r
702 return $blog->isValidCategory($catid);
\r
705 // check category id
\r
706 if (($name == 'catid') && ($value == $catid))
\r
707 return $blog->isValidCategory($catid);
\r
714 * Different checks for an author
\r
716 function _ifAuthor($name = '', $value='') {
\r
717 global $member, $manager;
\r
719 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
\r
721 // when no parameter is defined, just check if author is current visitor
\r
722 if (($name != 'isadmin' && $name != 'name') || ($name == 'name' && $value == '')) {
\r
723 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));
\r
726 // check author name
\r
727 if ($name == 'name') {
\r
728 $value = strtolower($value);
\r
729 if ($value == strtolower($this->currentItem->author))
\r
733 // check if author is admin
\r
734 if (($name == 'isadmin')) {
\r
735 $aid = intval($this->currentItem->authorid);
\r
736 $blogid = intval($b->getID());
\r
737 $amember =& $manager->getMember($aid);
\r
738 if ($amember->isAdmin())
\r
741 return $amember->isBlogAdmin($blogid);
\r
748 * Different checks for a category
\r
750 function _ifItemCategory($name = '', $value='') {
\r
751 global $catid, $manager;
\r
753 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
\r
755 // when no parameter is defined, just check if a category is selected
\r
756 if (($name != 'catname' && $name != 'catid') || ($value == ''))
\r
757 return $b->isValidCategory($catid);
\r
759 $icatid = $this->currentItem->catid;
\r
760 //$icategory = $this->currentItem->category;
\r
762 // check category name
\r
763 if ($name == 'catname') {
\r
764 $value = $b->getCategoryIdFromName($value);
\r
765 if ($value == $icatid)
\r
766 return $b->isValidCategory($icatid);
\r
769 // check category id
\r
770 if (($name == 'catid') && ($value == $icatid))
\r
771 return $b->isValidCategory($icatid);
\r
778 * Checks if a member is on the team of a blog and return his rights
\r
780 function _ifOnTeam($blogName = '') {
\r
781 global $blog, $member, $manager;
\r
783 // when no blog found
\r
784 if (($blogName == '') && (!is_object($blog)))
\r
787 // explicit blog selection
\r
788 if ($blogName != '')
\r
789 $blogid = getBlogIDFromName($blogName);
\r
791 if (($blogName == '') || !$manager->existsBlogID($blogid))
\r
792 // use current blog
\r
793 $blogid = $blog->getID();
\r
795 return $member->teamRights($blogid);
\r
799 * Checks if a member is admin of a blog
\r
801 function _ifAdmin($blogName = '') {
\r
802 global $blog, $member, $manager;
\r
804 // when no blog found
\r
805 if (($blogName == '') && (!is_object($blog)))
\r
808 // explicit blog selection
\r
809 if ($blogName != '')
\r
810 $blogid = getBlogIDFromName($blogName);
\r
812 if (($blogName == '') || !$manager->existsBlogID($blogid))
\r
813 // use current blog
\r
814 $blogid = $blog->getID();
\r
816 return $member->isBlogAdmin($blogid);
\r
821 * hasplugin,PlugName
\r
822 * -> checks if plugin exists
\r
823 * hasplugin,PlugName,OptionName
\r
824 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
\r
825 * hasplugin,PlugName,OptionName=value
\r
826 * -> checks if the option OptionName from plugin PlugName is set to value
\r
828 function _ifHasPlugin($name, $value) {
\r
830 $condition = false;
\r
831 // (pluginInstalled method won't write a message in the actionlog on failure)
\r
832 if ($manager->pluginInstalled('NP_'.$name)) {
\r
833 $plugin =& $manager->getPlugin('NP_' . $name);
\r
834 if ($plugin != NULL) {
\r
835 if ($value == "") {
\r
838 list($name2, $value2) = i18n::explode('=', $value, 2);
\r
839 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
\r
841 } else if ($plugin->getOption($name2) == $value2) {
\r
851 * Checks if a plugin exists and call its doIf function
\r
853 function _ifPlugin($name, $key = '', $value = '') {
\r
856 $plugin =& $manager->getPlugin('NP_' . $name);
\r
857 if (!$plugin) return;
\r
859 $params = func_get_args();
\r
860 array_shift($params);
\r
862 return call_user_func_array(array(&$plugin, 'doIf'), $params);
\r