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 1626 2012-01-09 15:46:54Z sakamocchi $
19 class ITEMACTIONS extends BaseActions {
21 // contains an assoc array with parameters that need to be included when
22 // generating links to items/archives/... (e.g. catid)
25 // true when the current user is a blog admin (and thus allowed to edit all items)
28 // timestamp of last visit
31 // item currently being handled (mysql result object, see BLOG::showUsingQuery)
34 // reference to the blog currently being displayed
37 // associative array with template info (part name => contents)
40 // true when comments need to be displayed
43 function ITEMACTIONS(&$blog) {
44 // call constructor of superclass first
47 // extra parameters for created links
50 $this->linkparams = array('catid' => $catid);
52 // check if member is blog admin (and thus allowed to edit all items)
54 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
55 $this->setBlog($blog);
59 * Returns an array with the actions that are defined
60 * in the ITEMACTIONS class
62 function getDefinedActions() {
85 'syndicate_description',
114 function setLastVisit($lastVisit) {
115 $this->lastVisit = $lastVisit;
118 function setParser(&$parser) {
119 $this->parser =& $parser;
122 function setCurrentItem(&$item) {
123 $this->currentItem =& $item;
124 global $currentitemid;
125 $currentitemid = $this->currentItem->itemid;
128 function setBlog(&$blog) {
129 $this->blog =& $blog;
132 function setTemplate($template) {
133 $this->template =& $template;
136 function setShowComments($val) {
137 $this->showComments = $val;
140 // methods used by parser to insert content
144 * Parse templatevar blogid
146 function parse_blogid() {
147 echo $this->blog->getID();
151 * Parse templatevar body
153 function parse_body() {
154 $this->highlightAndParse($this->currentItem->body);
158 * Parse templatevar more
160 function parse_more() {
161 $this->highlightAndParse($this->currentItem->more);
165 * Parse templatevar itemid
167 function parse_itemid() {
168 echo $this->currentItem->itemid;
172 * Parse templatevar category
174 function parse_category() {
175 echo $this->currentItem->category;
179 * Parse templatevar categorylink
181 function parse_categorylink() {
182 echo createLink('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
186 * Parse templatevar catid
188 function parse_catid() {
189 echo $this->currentItem->catid;
193 * Parse templatevar authorid
195 function parse_authorid() {
196 echo $this->currentItem->authorid;
200 * Parse templatevar authorlink
202 function parse_authorlink() {
206 'memberid' => $this->currentItem->authorid,
207 'name' => $this->currentItem->author,
208 'extra' => $this->linkparams
214 * Parse templatevar query
216 function parse_query() {
217 echo $this->strHighlight;
221 * Parse templatevar itemlink
223 function parse_itemlink() {
227 'itemid' => $this->currentItem->itemid,
228 'title' => $this->currentItem->title,
229 'timestamp' => $this->currentItem->timestamp,
230 'extra' => $this->linkparams
236 * Parse templatevar blogurl
238 function parse_blogurl() {
239 echo $this->blog->getURL();
243 * Parse templatevar closed
245 function parse_closed() {
246 echo $this->currentItem->closed;
250 * Parse templatevar relevance
252 function parse_relevance() {
253 echo round($this->currentItem->score,2);
257 * Parse templatevar title
259 * @param string $format defines in which format the title is shown
261 function parse_title($format = '') {
262 if (is_array($this->currentItem)) {
263 $itemtitle = $this->currentItem['title'];
264 } elseif (is_object($this->currentItem)) {
265 $itemtitle = $this->currentItem->title;
269 // echo stringToXML ($this->currentItem->title);
270 echo stringToXML ($itemtitle);
273 // echo stringToAttribute ($this->currentItem->title);
274 echo stringToAttribute ($itemtitle);
277 // echo $this->currentItem->title;
281 // $this->highlightAndParse($this->currentItem->title);
282 $this->highlightAndParse($itemtitle);
288 * Parse templatevar karma
290 function parse_karma($type = 'totalscore') {
294 $karma =& $manager->getKarma($this->currentItem->itemid);
298 echo $karma->getNbPosVotes();
301 echo $karma->getNbNegVotes();
304 echo $karma->getNbOfVotes();
307 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
308 echo number_format($percentage,2), '%';
311 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
312 echo number_format($percentage,2), '%';
316 echo $karma->getTotalScore();
323 * Parse templatevar author
325 function parse_author($which = '') {
329 echo $this->currentItem->authorname;
332 echo $this->currentItem->authorid;
335 echo $this->currentItem->authormail;
338 echo $this->currentItem->authorurl;
342 echo $this->currentItem->author;
347 * Parse templatevar smartbody
349 function parse_smartbody() {
350 if (!$this->currentItem->more) {
351 $this->highlightAndParse($this->currentItem->body);
353 $this->highlightAndParse($this->currentItem->more);
358 * Parse templatevar morelink
360 function parse_morelink() {
361 if ($this->currentItem->more)
362 $this->parser->parse($this->template['MORELINK']);
366 * Parse templatevar date
368 * @param format optional strftime format
370 function parse_date($format = '') {
371 if (!isset($this->template['FORMAT_DATE'])) $this->template['FORMAT_DATE'] = '';
372 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $this->blog);
376 * Parse templatevar time
378 * @param format optional strftime format
380 function parse_time($format = '') {
381 if (!isset($this->template['FORMAT_TIME'])) $this->template['FORMAT_TIME'] = '';
382 echo i18n::strftime($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);
386 * Parse templatevar syndicate_title
388 * @param maxLength optional maximum length
390 function parse_syndicate_title($maxLength = 100) {
391 $syndicated = strip_tags($this->currentItem->title);
392 echo i18n::hsc(shorten($syndicated,$maxLength,'...'));
396 * Parse templatevar syndicate_description
398 * @param maxLength optional maximum length
400 function parse_syndicate_description($maxLength = 250, $addHighlight = 0) {
401 $syndicated = strip_tags($this->currentItem->body);
403 $tmp_highlight = i18n::hsc(shorten($syndicated,$maxLength,'...'));
404 echo $this->highlightAndParse($tmp_highlight);
406 echo i18n::hsc(shorten($syndicated,$maxLength,'...'));
411 * Parse templatevar karmaposlink
415 function parse_karmaposlink($text = '') {
417 $link = $CONF['ActionURL'] . '?action=votepositive&itemid='.$this->currentItem->itemid;
418 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
422 * Parse templatevar karmaneglink
426 function parse_karmaneglink($text = '') {
428 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem->itemid;
429 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
433 * Parse templatevar new
435 function parse_new() {
436 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
437 echo $this->template['NEW'];
441 * Parse templatevar daylink
443 function parse_daylink() {
444 echo createArchiveLink($this->blog->getID(), i18n::strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
448 * Parse templatevar comments
450 function parse_comments($maxToShow = 0) {
452 $maxToShow = $this->blog->getMaxComments();
455 if ($this->showComments && $this->blog->commentsEnabled()) {
456 $comments = new COMMENTS($this->currentItem->itemid);
457 $comments->setItemActions($this);
458 $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
463 * Executes a plugin templatevar
465 * @param pluginName name of plugin (without the NP_)
467 * extra parameters can be added
469 function parse_plugin($pluginName) {
472 // should be already tested from the parser (PARSER.php)
473 // only continue when the plugin is really installed
474 /*if (!$manager->pluginInstalled('NP_' . $pluginName))
477 $plugin =& $manager->getPlugin('NP_' . $pluginName);
478 if (!$plugin) return;
481 $params = func_get_args();
483 // remove plugin name
484 array_shift($params);
486 // add item reference (array_unshift didn't work)
487 $params = array_merge(array(&$this->currentItem),$params);
489 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
493 * Parse templatevar edit
495 function parse_edit() {
496 global $member, $CONF;
497 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {
498 $this->parser->parse($this->template['EDITLINK']);
503 * Parse templatevar editlink
505 function parse_editlink() {
507 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&itemid=',$this->currentItem->itemid;
511 * Parse templatevar editpopupcode
513 function parse_editpopupcode() {
514 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;";
520 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
521 * from hacking your weblog.
522 * 'plugin variables in items' implementation by Andy
524 function highlightAndParse(&$data) {
525 $actions = new BODYACTIONS($this->blog);
526 $parser = new PARSER($actions->getDefinedActions(), $actions);
527 $actions->setTemplate($this->template);
528 $actions->setHighlight($this->strHighlight);
529 $actions->setCurrentItem($this->currentItem);
530 //$actions->setParser($parser);
531 $parser->parse($actions->highlight($data));
535 // this is the function previous to the 'plugin variables in items' implementation by Andy
536 function highlightAndParse(&$data) {
537 // allow only a limited subset of actions (do not allow includes etc, they might be evil)
538 $this->parser->actions = array('image','media','popup');
539 $tmp_highlight = $this->highlight($data);
540 $this->parser->parse($tmp_highlight);
541 $this->parser->actions = $this->getDefinedActions();
545 // function to enable if-else-elseif-elseifnot-ifnot-endif to item template fields
548 * Checks conditions for if statements
550 * @param string $field type of <%if%>
551 * @param string $name property of field
552 * @param string $value value of property
554 function checkCondition($field, $name='', $value = '') {
555 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
560 $condition = ($blog && $this->_ifCategory($name,$value));
563 $condition = ($this->_ifItemCategory($name,$value));
566 $condition = ($blog && ($blog->getSetting($name) == $value));
568 case 'itemblogsetting':
569 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
570 $condition = ($b && ($b->getSetting($name) == $value));
573 $condition = $member->isLoggedIn();
576 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
579 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
582 $condition = ($this->_ifAuthor($name,$value));
585 $condition = ($itemidnext != '');
588 $condition = ($itemidprev != '');
590 case 'archiveprevexists':
591 $condition = ($archiveprevexists == true);
593 case 'archivenextexists':
594 $condition = ($archivenextexists == true);
597 $condition = ($name == $this->skintype);
600 $condition = $this->_ifHasPlugin($name, $value);
603 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
610 * Different checks for a category
612 function _ifCategory($name = '', $value='') {
613 global $blog, $catid;
615 // when no parameter is defined, just check if a category is selected
616 if (($name != 'catname' && $name != 'catid') || ($value == ''))
617 return $blog->isValidCategory($catid);
619 // check category name
620 if ($name == 'catname') {
621 $value = $blog->getCategoryIdFromName($value);
622 if ($value == $catid)
623 return $blog->isValidCategory($catid);
627 if (($name == 'catid') && ($value == $catid))
628 return $blog->isValidCategory($catid);
635 * Different checks for an author
637 function _ifAuthor($name = '', $value='') {
638 global $member, $manager;
640 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
642 // when no parameter is defined, just check if author is current visitor
643 if (($name != 'isadmin' && $name != 'name') || ($name == 'name' && $value == '')) {
644 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));
648 if ($name == 'name') {
649 $value = strtolower($value);
650 if ($value == strtolower($this->currentItem->author))
654 // check if author is admin
655 if (($name == 'isadmin')) {
656 $aid = intval($this->currentItem->authorid);
657 $blogid = intval($b->getID());
658 $amember =& $manager->getMember($aid);
659 if ($amember->isAdmin())
662 return $amember->isBlogAdmin($blogid);
669 * Different checks for a category
671 function _ifItemCategory($name = '', $value='') {
672 global $catid, $manager;
674 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
676 // when no parameter is defined, just check if a category is selected
677 if (($name != 'catname' && $name != 'catid') || ($value == ''))
678 return $b->isValidCategory($catid);
680 $icatid = $this->currentItem->catid;
681 //$icategory = $this->currentItem->category;
683 // check category name
684 if ($name == 'catname') {
685 $value = $b->getCategoryIdFromName($value);
686 if ($value == $icatid)
687 return $b->isValidCategory($icatid);
691 if (($name == 'catid') && ($value == $icatid))
692 return $b->isValidCategory($icatid);
699 * Checks if a member is on the team of a blog and return his rights
701 function _ifOnTeam($blogName = '') {
702 global $blog, $member, $manager;
704 // when no blog found
705 if (($blogName == '') && (!is_object($blog)))
708 // explicit blog selection
710 $blogid = getBlogIDFromName($blogName);
712 if (($blogName == '') || !$manager->existsBlogID($blogid))
714 $blogid = $blog->getID();
716 return $member->teamRights($blogid);
720 * Checks if a member is admin of a blog
722 function _ifAdmin($blogName = '') {
723 global $blog, $member, $manager;
725 // when no blog found
726 if (($blogName == '') && (!is_object($blog)))
729 // explicit blog selection
731 $blogid = getBlogIDFromName($blogName);
733 if (($blogName == '') || !$manager->existsBlogID($blogid))
735 $blogid = $blog->getID();
737 return $member->isBlogAdmin($blogid);
743 * -> checks if plugin exists
744 * hasplugin,PlugName,OptionName
745 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
746 * hasplugin,PlugName,OptionName=value
747 * -> checks if the option OptionName from plugin PlugName is set to value
749 function _ifHasPlugin($name, $value) {
752 // (pluginInstalled method won't write a message in the actionlog on failure)
753 if ($manager->pluginInstalled('NP_'.$name)) {
754 $plugin =& $manager->getPlugin('NP_' . $name);
755 if ($plugin != NULL) {
759 list($name2, $value2) = i18n::explode('=', $value, 2);
760 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
762 } else if ($plugin->getOption($name2) == $value2) {
772 * Checks if a plugin exists and call its doIf function
774 function _ifPlugin($name, $key = '', $value = '') {
777 $plugin =& $manager->getPlugin('NP_' . $name);
778 if (!$plugin) return;
780 $params = func_get_args();
781 array_shift($params);
783 return call_user_func_array(array(&$plugin, 'doIf'), $params);