3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2011 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-2011 The Nucleus Group
18 * @version $NucleusJP: ITEMACTIONS.php,v 1.5.2.3 2007/10/30 19:05:20 kmorimatsu Exp $
20 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 ITEMACTIONS(&$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) {
124 $this->currentItem =& $item;
125 global $currentitemid;
126 if (is_array($this->currentItem)) {
127 $currentitemid = $this->currentItem['itemid'];
129 $currentitemid = $this->currentItem->itemid;
133 function setBlog(&$blog) {
\r
134 $this->blog =& $blog;
\r
137 function setTemplate($template) {
\r
138 $this->template =& $template;
\r
141 function setShowComments($val) {
\r
142 $this->showComments = $val;
\r
145 // methods used by parser to insert content
\r
149 * Parse templatevar blogid
\r
151 function parse_blogid() {
\r
152 echo $this->blog->getID();
\r
156 * Parse templatevar body
\r
158 function parse_body() {
\r
159 $this->highlightAndParse($this->currentItem->body);
\r
163 * Parse templatevar more
\r
165 function parse_more() {
\r
166 $this->highlightAndParse($this->currentItem->more);
\r
170 * Parse templatevar itemid
\r
172 function parse_itemid() {
\r
173 echo $this->currentItem->itemid;
\r
177 * Parse templatevar category
\r
179 function parse_category() {
\r
180 echo $this->currentItem->category;
\r
184 * Parse templatevar categorylink
\r
186 function parse_categorylink() {
\r
187 echo createLink('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
\r
191 * Parse templatevar catid
\r
193 function parse_catid() {
\r
194 echo $this->currentItem->catid;
\r
198 * Parse templatevar authorid
\r
200 function parse_authorid() {
\r
201 echo $this->currentItem->authorid;
\r
205 * Parse templatevar authorlink
\r
207 function parse_authorlink() {
\r
211 'memberid' => $this->currentItem->authorid,
\r
212 'name' => $this->currentItem->author,
\r
213 'extra' => $this->linkparams
\r
219 * Parse templatevar query
\r
221 function parse_query() {
\r
222 echo $this->strHighlight;
\r
226 * Parse templatevar itemlink
\r
228 function parse_itemlink() {
\r
232 'itemid' => $this->currentItem->itemid,
\r
233 'title' => $this->currentItem->title,
\r
234 'timestamp' => $this->currentItem->timestamp,
\r
235 'extra' => $this->linkparams
\r
241 * Parse templatevar blogurl
\r
243 function parse_blogurl() {
\r
244 echo $this->blog->getURL();
\r
248 * Parse templatevar closed
\r
250 function parse_closed() {
\r
251 echo $this->currentItem->closed;
\r
255 * Parse templatevar relevance
\r
257 function parse_relevance() {
\r
258 echo round($this->currentItem->score,2);
\r
262 * Parse templatevar title
\r
264 * @param string $format defines in which format the title is shown
\r
266 function parse_title($format = '') {
\r
267 if (is_array($this->currentItem)) {
\r
268 $itemtitle = $this->currentItem['title'];
\r
269 } elseif (is_object($this->currentItem)) {
\r
270 $itemtitle = $this->currentItem->title;
\r
274 // echo stringToXML ($this->currentItem->title);
\r
275 echo stringToXML ($itemtitle);
\r
278 // echo stringToAttribute ($this->currentItem->title);
\r
279 echo stringToAttribute ($itemtitle);
\r
282 // echo $this->currentItem->title;
\r
286 // $this->highlightAndParse($this->currentItem->title);
\r
287 $this->highlightAndParse($itemtitle);
\r
293 * Parse templatevar karma
\r
295 function parse_karma($type = 'totalscore') {
\r
298 // get karma object
\r
299 $karma =& $manager->getKarma($this->currentItem->itemid);
\r
303 echo $karma->getNbPosVotes();
\r
306 echo $karma->getNbNegVotes();
\r
309 echo $karma->getNbOfVotes();
\r
312 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
\r
313 echo number_format($percentage,2), '%';
\r
316 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
\r
317 echo number_format($percentage,2), '%';
\r
321 echo $karma->getTotalScore();
\r
328 * Parse templatevar author
\r
330 function parse_author($which = '') {
\r
334 echo $this->currentItem->authorname;
\r
337 echo $this->currentItem->authorid;
\r
340 echo $this->currentItem->authormail;
\r
343 echo $this->currentItem->authorurl;
\r
347 echo $this->currentItem->author;
\r
352 * Parse templatevar smartbody
\r
354 function parse_smartbody() {
\r
355 if (!$this->currentItem->more) {
\r
356 $this->highlightAndParse($this->currentItem->body);
\r
358 $this->highlightAndParse($this->currentItem->more);
\r
363 * Parse templatevar morelink
\r
365 function parse_morelink() {
\r
366 if ($this->currentItem->more)
\r
367 $this->parser->parse($this->template['MORELINK']);
\r
371 * Parse templatevar date
\r
373 * @param format optional strftime format
\r
375 function parse_date($format = '') {
\r
376 if (!isset($this->template['FORMAT_DATE'])) $this->template['FORMAT_DATE'] = '';
377 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $this->blog);
\r
381 * Parse templatevar time
\r
383 * @param format optional strftime format
\r
385 function parse_time($format = '') {
\r
386 if (!isset($this->template['FORMAT_TIME'])) $this->template['FORMAT_TIME'] = '';
387 echo strftimejp($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);
\r
391 * Parse templatevar syndicate_title
\r
393 * @param maxLength optional maximum length
\r
395 function parse_syndicate_title($maxLength = 100) {
\r
396 $syndicated = strip_tags($this->currentItem->title);
\r
397 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
\r
401 * Parse templatevar syndicate_description
\r
403 * @param maxLength optional maximum length
\r
405 function parse_syndicate_description($maxLength = 250, $addHighlight = 0) {
\r
406 $syndicated = strip_tags($this->currentItem->body);
\r
407 if ($addHighlight) {
\r
408 $tmp_highlight = htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
\r
409 echo $this->highlightAndParse($tmp_highlight);
\r
411 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
\r
416 * Parse templatevar karmaposlink
\r
418 * @param string text
\r
420 function parse_karmaposlink($text = '') {
\r
422 $link = $CONF['ActionURL'] . '?action=votepositive&itemid='.$this->currentItem->itemid;
\r
423 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
\r
427 * Parse templatevar karmaneglink
\r
429 * @param string text
\r
431 function parse_karmaneglink($text = '') {
\r
433 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem->itemid;
\r
434 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
\r
438 * Parse templatevar new
\r
440 function parse_new() {
\r
441 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
\r
442 echo $this->template['NEW'];
\r
446 * Parse templatevar daylink
\r
448 function parse_daylink() {
\r
449 echo createArchiveLink($this->blog->getID(), strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
\r
453 * Parse templatevar comments
\r
455 function parse_comments($maxToShow = 0) {
\r
456 if ($maxToShow == 0)
\r
457 $maxToShow = $this->blog->getMaxComments();
\r
460 if ($this->showComments && $this->blog->commentsEnabled()) {
\r
461 $comments = new COMMENTS($this->currentItem->itemid);
\r
462 $comments->setItemActions($this);
\r
463 $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
\r
468 * Executes a plugin templatevar
\r
470 * @param pluginName name of plugin (without the NP_)
\r
472 * extra parameters can be added
\r
474 function parse_plugin($pluginName) {
\r
477 // should be already tested from the parser (PARSER.php)
\r
478 // only continue when the plugin is really installed
\r
479 /*if (!$manager->pluginInstalled('NP_' . $pluginName))
\r
482 $plugin =& $manager->getPlugin('NP_' . $pluginName);
\r
483 if (!$plugin) return;
\r
486 $params = func_get_args();
\r
488 // remove plugin name
\r
489 array_shift($params);
\r
491 // add item reference (array_unshift didn't work)
\r
492 $params = array_merge(array(&$this->currentItem),$params);
\r
494 call_user_func_array(array($plugin,'doTemplateVar'), $params);
\r
498 * Parse templatevar edit
\r
500 function parse_edit() {
\r
501 global $member, $CONF;
\r
502 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {
\r
503 $this->parser->parse($this->template['EDITLINK']);
\r
508 * Parse templatevar editlink
\r
510 function parse_editlink() {
\r
512 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&itemid=',$this->currentItem->itemid;
\r
516 * Parse templatevar editpopupcode
\r
518 function parse_editpopupcode() {
\r
519 echo "if (event && event.preventDefault) event.preventDefault();winbm=window.open(this.href,'nucleusbm','scrollbars=no,width=710,height=550,left=10,top=10,status=no,resizable=yes');winbm.focus();return false;";
\r
522 // helper functions
\r
525 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
\r
526 * from hacking your weblog.
\r
527 * 'plugin variables in items' implementation by Andy
\r
529 function highlightAndParse(&$data) {
\r
530 $actions = new BODYACTIONS($this->blog);
\r
531 $parser = new PARSER($actions->getDefinedActions(), $actions);
\r
532 $actions->setTemplate($this->template);
\r
533 $actions->setHighlight($this->strHighlight);
\r
534 $actions->setCurrentItem($this->currentItem);
\r
535 //$actions->setParser($parser);
\r
536 $parser->parse($actions->highlight($data));
\r
540 // this is the function previous to the 'plugin variables in items' implementation by Andy
\r
541 function highlightAndParse(&$data) {
\r
542 // allow only a limited subset of actions (do not allow includes etc, they might be evil)
\r
543 $this->parser->actions = array('image','media','popup');
\r
544 $tmp_highlight = $this->highlight($data);
\r
545 $this->parser->parse($tmp_highlight);
\r
546 $this->parser->actions = $this->getDefinedActions();
\r
550 // function to enable if-else-elseif-elseifnot-ifnot-endif to item template fields
553 * Checks conditions for if statements
555 * @param string $field type of <%if%>
556 * @param string $name property of field
557 * @param string $value value of property
559 function checkCondition($field, $name='', $value = '') {
560 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
565 $condition = ($blog && $this->_ifCategory($name,$value));
568 $condition = ($this->_ifItemCategory($name,$value));
571 $condition = ($blog && ($blog->getSetting($name) == $value));
573 case 'itemblogsetting':
574 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
575 $condition = ($b && ($b->getSetting($name) == $value));
578 $condition = $member->isLoggedIn();
581 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
584 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
587 $condition = ($this->_ifAuthor($name,$value));
590 $condition = ($itemidnext != '');
593 $condition = ($itemidprev != '');
595 case 'archiveprevexists':
596 $condition = ($archiveprevexists == true);
598 case 'archivenextexists':
599 $condition = ($archivenextexists == true);
602 $condition = ($name == $this->skintype);
605 $condition = $this->_ifHasPlugin($name, $value);
608 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
615 * Different checks for a category
617 function _ifCategory($name = '', $value='') {
618 global $blog, $catid;
620 // when no parameter is defined, just check if a category is selected
621 if (($name != 'catname' && $name != 'catid') || ($value == ''))
622 return $blog->isValidCategory($catid);
624 // check category name
625 if ($name == 'catname') {
626 $value = $blog->getCategoryIdFromName($value);
627 if ($value == $catid)
628 return $blog->isValidCategory($catid);
632 if (($name == 'catid') && ($value == $catid))
633 return $blog->isValidCategory($catid);
640 * Different checks for an author
642 function _ifAuthor($name = '', $value='') {
643 global $member, $manager;
645 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
647 // when no parameter is defined, just check if author is current visitor
648 if (($name != 'isadmin' && $name != 'name') || ($name == 'name' && $value == '')) {
649 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));
653 if ($name == 'name') {
654 $value = strtolower($value);
655 if ($value == strtolower($this->currentItem->author))
659 // check if author is admin
660 if (($name == 'isadmin')) {
661 $aid = intval($this->currentItem->authorid);
662 $blogid = intval($b->getID());
663 $amember =& $manager->getMember($aid);
664 if ($amember->isAdmin())
667 return $amember->isBlogAdmin($blogid);
674 * Different checks for a category
676 function _ifItemCategory($name = '', $value='') {
677 global $catid, $manager;
679 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
681 // when no parameter is defined, just check if a category is selected
682 if (($name != 'catname' && $name != 'catid') || ($value == ''))
683 return $b->isValidCategory($catid);
685 $icatid = $this->currentItem->catid;
686 //$icategory = $this->currentItem->category;
688 // check category name
689 if ($name == 'catname') {
690 $value = $b->getCategoryIdFromName($value);
691 if ($value == $icatid)
692 return $b->isValidCategory($icatid);
696 if (($name == 'catid') && ($value == $icatid))
697 return $b->isValidCategory($icatid);
704 * Checks if a member is on the team of a blog and return his rights
706 function _ifOnTeam($blogName = '') {
707 global $blog, $member, $manager;
709 // when no blog found
710 if (($blogName == '') && (!is_object($blog)))
713 // explicit blog selection
715 $blogid = getBlogIDFromName($blogName);
717 if (($blogName == '') || !$manager->existsBlogID($blogid))
719 $blogid = $blog->getID();
721 return $member->teamRights($blogid);
725 * Checks if a member is admin of a blog
727 function _ifAdmin($blogName = '') {
728 global $blog, $member, $manager;
730 // when no blog found
731 if (($blogName == '') && (!is_object($blog)))
734 // explicit blog selection
736 $blogid = getBlogIDFromName($blogName);
738 if (($blogName == '') || !$manager->existsBlogID($blogid))
740 $blogid = $blog->getID();
742 return $member->isBlogAdmin($blogid);
748 * -> checks if plugin exists
749 * hasplugin,PlugName,OptionName
750 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
751 * hasplugin,PlugName,OptionName=value
752 * -> checks if the option OptionName from plugin PlugName is set to value
754 function _ifHasPlugin($name, $value) {
757 // (pluginInstalled method won't write a message in the actionlog on failure)
758 if ($manager->pluginInstalled('NP_'.$name)) {
759 $plugin =& $manager->getPlugin('NP_' . $name);
760 if ($plugin != NULL) {
764 list($name2, $value2) = explode('=', $value, 2);
765 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
767 } else if ($plugin->getOption($name2) == $value2) {
777 * Checks if a plugin exists and call its doIf function
779 function _ifPlugin($name, $key = '', $value = '') {
782 $plugin =& $manager->getPlugin('NP_' . $name);
783 if (!$plugin) return;
785 $params = func_get_args();
786 array_shift($params);
788 return call_user_func_array(array($plugin, 'doIf'), $params);