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 $currentitemid = $this->currentItem->itemid;
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 * Parse templatevar categorylink
\r
182 function parse_categorylink() {
\r
183 echo createLink('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
\r
187 * Parse templatevar catid
\r
189 function parse_catid() {
\r
190 echo $this->currentItem->catid;
\r
194 * Parse templatevar authorid
\r
196 function parse_authorid() {
\r
197 echo $this->currentItem->authorid;
\r
201 * Parse templatevar authorlink
\r
203 function parse_authorlink() {
\r
207 'memberid' => $this->currentItem->authorid,
\r
208 'name' => $this->currentItem->author,
\r
209 'extra' => $this->linkparams
\r
215 * Parse templatevar query
\r
217 function parse_query() {
\r
218 echo $this->strHighlight;
\r
222 * Parse templatevar itemlink
\r
224 function parse_itemlink() {
\r
228 'itemid' => $this->currentItem->itemid,
\r
229 'title' => $this->currentItem->title,
\r
230 'timestamp' => $this->currentItem->timestamp,
\r
231 'extra' => $this->linkparams
\r
237 * Parse templatevar blogurl
\r
239 function parse_blogurl() {
\r
240 echo $this->blog->getURL();
\r
244 * Parse templatevar closed
\r
246 function parse_closed() {
\r
247 echo $this->currentItem->closed;
\r
251 * Parse templatevar relevance
\r
253 function parse_relevance() {
\r
254 echo round($this->currentItem->score,2);
\r
258 * Parse templatevar title
\r
260 * @param string $format defines in which format the title is shown
\r
262 function parse_title($format = '') {
\r
263 if (is_array($this->currentItem)) {
\r
264 $itemtitle = $this->currentItem['title'];
\r
265 } elseif (is_object($this->currentItem)) {
\r
266 $itemtitle = $this->currentItem->title;
\r
270 // echo stringToXML ($this->currentItem->title);
\r
271 echo stringToXML ($itemtitle);
\r
274 // echo stringToAttribute ($this->currentItem->title);
\r
275 echo stringToAttribute ($itemtitle);
\r
278 // echo $this->currentItem->title;
\r
282 // $this->highlightAndParse($this->currentItem->title);
\r
283 $this->highlightAndParse($itemtitle);
\r
289 * Parse templatevar karma
\r
291 function parse_karma($type = 'totalscore') {
\r
294 // get karma object
\r
295 $karma =& $manager->getKarma($this->currentItem->itemid);
\r
299 echo $karma->getNbPosVotes();
\r
302 echo $karma->getNbNegVotes();
\r
305 echo $karma->getNbOfVotes();
\r
308 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
\r
309 echo number_format($percentage,2), '%';
\r
312 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
\r
313 echo number_format($percentage,2), '%';
\r
317 echo $karma->getTotalScore();
\r
324 * Parse templatevar author
\r
326 function parse_author($which = '') {
\r
330 echo $this->currentItem->authorname;
\r
333 echo $this->currentItem->authorid;
\r
336 echo $this->currentItem->authormail;
\r
339 echo $this->currentItem->authorurl;
\r
343 echo $this->currentItem->author;
\r
348 * Parse templatevar smartbody
\r
350 function parse_smartbody() {
\r
351 if (!$this->currentItem->more) {
\r
352 $this->highlightAndParse($this->currentItem->body);
\r
354 $this->highlightAndParse($this->currentItem->more);
\r
359 * Parse templatevar morelink
\r
361 function parse_morelink() {
\r
362 if ($this->currentItem->more)
\r
363 $this->parser->parse($this->template['MORELINK']);
\r
367 * Parse templatevar date
\r
369 * @param format optional strftime format
\r
371 function parse_date($format = '') {
\r
372 if (!isset($this->template['FORMAT_DATE'])) $this->template['FORMAT_DATE'] = '';
373 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $this->blog);
\r
377 * Parse templatevar time
\r
379 * @param format optional strftime format
\r
381 function parse_time($format = '') {
\r
382 if (!isset($this->template['FORMAT_TIME'])) $this->template['FORMAT_TIME'] = '';
383 echo strftimejp($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);
\r
387 * Parse templatevar syndicate_title
\r
389 * @param maxLength optional maximum length
\r
391 function parse_syndicate_title($maxLength = 100) {
\r
392 $syndicated = strip_tags($this->currentItem->title);
\r
393 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
\r
397 * Parse templatevar syndicate_description
\r
399 * @param maxLength optional maximum length
\r
401 function parse_syndicate_description($maxLength = 250, $addHighlight = 0) {
\r
402 $syndicated = strip_tags($this->currentItem->body);
\r
403 if ($addHighlight) {
\r
404 $tmp_highlight = htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
\r
405 echo $this->highlightAndParse($tmp_highlight);
\r
407 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
\r
412 * Parse templatevar karmaposlink
\r
414 * @param string text
\r
416 function parse_karmaposlink($text = '') {
\r
418 $link = $CONF['ActionURL'] . '?action=votepositive&itemid='.$this->currentItem->itemid;
\r
419 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
\r
423 * Parse templatevar karmaneglink
\r
425 * @param string text
\r
427 function parse_karmaneglink($text = '') {
\r
429 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem->itemid;
\r
430 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
\r
434 * Parse templatevar new
\r
436 function parse_new() {
\r
437 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
\r
438 echo $this->template['NEW'];
\r
442 * Parse templatevar daylink
\r
444 function parse_daylink() {
\r
445 echo createArchiveLink($this->blog->getID(), strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
\r
449 * Parse templatevar comments
\r
451 function parse_comments($maxToShow = 0) {
\r
452 if ($maxToShow == 0)
\r
453 $maxToShow = $this->blog->getMaxComments();
\r
456 if ($this->showComments && $this->blog->commentsEnabled()) {
\r
457 $comments =& new COMMENTS($this->currentItem->itemid);
\r
458 $comments->setItemActions($this);
\r
459 $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
\r
464 * Executes a plugin templatevar
\r
466 * @param pluginName name of plugin (without the NP_)
\r
468 * extra parameters can be added
\r
470 function parse_plugin($pluginName) {
\r
473 // should be already tested from the parser (PARSER.php)
\r
474 // only continue when the plugin is really installed
\r
475 /*if (!$manager->pluginInstalled('NP_' . $pluginName))
\r
478 $plugin =& $manager->getPlugin('NP_' . $pluginName);
\r
479 if (!$plugin) return;
\r
482 $params = func_get_args();
\r
484 // remove plugin name
\r
485 array_shift($params);
\r
487 // add item reference (array_unshift didn't work)
\r
488 $params = array_merge(array(&$this->currentItem),$params);
\r
490 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
\r
494 * Parse templatevar edit
\r
496 function parse_edit() {
\r
497 global $member, $CONF;
\r
498 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {
\r
499 $this->parser->parse($this->template['EDITLINK']);
\r
504 * Parse templatevar editlink
\r
506 function parse_editlink() {
\r
508 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&itemid=',$this->currentItem->itemid;
\r
512 * Parse templatevar editpopupcode
\r
514 function parse_editpopupcode() {
\r
515 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
518 // helper functions
\r
521 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
\r
522 * from hacking your weblog.
\r
523 * 'plugin variables in items' implementation by Andy
\r
525 function highlightAndParse(&$data) {
\r
526 $actions =& new BODYACTIONS($this->blog);
\r
527 $parser =& new PARSER($actions->getDefinedActions(), $actions);
\r
528 $actions->setTemplate($this->template);
\r
529 $actions->setHighlight($this->strHighlight);
\r
530 $actions->setCurrentItem($this->currentItem);
\r
531 //$actions->setParser($parser);
\r
532 $parser->parse($actions->highlight($data));
\r
536 // this is the function previous to the 'plugin variables in items' implementation by Andy
\r
537 function highlightAndParse(&$data) {
\r
538 // allow only a limited subset of actions (do not allow includes etc, they might be evil)
\r
539 $this->parser->actions = array('image','media','popup');
\r
540 $tmp_highlight = $this->highlight($data);
\r
541 $this->parser->parse($tmp_highlight);
\r
542 $this->parser->actions = $this->getDefinedActions();
\r
546 // function to enable if-else-elseif-elseifnot-ifnot-endif to item template fields
549 * Checks conditions for if statements
551 * @param string $field type of <%if%>
552 * @param string $name property of field
553 * @param string $value value of property
555 function checkCondition($field, $name='', $value = '') {
556 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
561 $condition = ($blog && $this->_ifCategory($name,$value));
564 $condition = ($this->_ifItemCategory($name,$value));
567 $condition = ($blog && ($blog->getSetting($name) == $value));
569 case 'itemblogsetting':
570 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
571 $condition = ($b && ($b->getSetting($name) == $value));
574 $condition = $member->isLoggedIn();
577 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
580 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
583 $condition = ($this->_ifAuthor($name,$value));
586 $condition = ($itemidnext != '');
589 $condition = ($itemidprev != '');
591 case 'archiveprevexists':
592 $condition = ($archiveprevexists == true);
594 case 'archivenextexists':
595 $condition = ($archivenextexists == true);
598 $condition = ($name == $this->skintype);
601 $condition = $this->_ifHasPlugin($name, $value);
604 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
611 * Different checks for a category
613 function _ifCategory($name = '', $value='') {
614 global $blog, $catid;
616 // when no parameter is defined, just check if a category is selected
617 if (($name != 'catname' && $name != 'catid') || ($value == ''))
618 return $blog->isValidCategory($catid);
620 // check category name
621 if ($name == 'catname') {
622 $value = $blog->getCategoryIdFromName($value);
623 if ($value == $catid)
624 return $blog->isValidCategory($catid);
628 if (($name == 'catid') && ($value == $catid))
629 return $blog->isValidCategory($catid);
636 * Different checks for an author
638 function _ifAuthor($name = '', $value='') {
639 global $member, $manager;
641 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
643 // when no parameter is defined, just check if author is current visitor
644 if (($name != 'isadmin' && $name != 'name') || ($name == 'name' && $value == '')) {
645 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));
649 if ($name == 'name') {
650 $value = strtolower($value);
651 if ($value == strtolower($this->currentItem->author))
655 // check if author is admin
656 if (($name == 'isadmin')) {
657 $aid = intval($this->currentItem->authorid);
658 $blogid = intval($b->getID());
659 $amember =& $manager->getMember($aid);
660 if ($amember->isAdmin())
663 return $amember->isBlogAdmin($blogid);
670 * Different checks for a category
672 function _ifItemCategory($name = '', $value='') {
673 global $catid, $manager;
675 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
677 // when no parameter is defined, just check if a category is selected
678 if (($name != 'catname' && $name != 'catid') || ($value == ''))
679 return $b->isValidCategory($catid);
681 $icatid = $this->currentItem->catid;
682 //$icategory = $this->currentItem->category;
684 // check category name
685 if ($name == 'catname') {
686 $value = $b->getCategoryIdFromName($value);
687 if ($value == $icatid)
688 return $b->isValidCategory($icatid);
692 if (($name == 'catid') && ($value == $icatid))
693 return $b->isValidCategory($icatid);
700 * Checks if a member is on the team of a blog and return his rights
702 function _ifOnTeam($blogName = '') {
703 global $blog, $member, $manager;
705 // when no blog found
706 if (($blogName == '') && (!is_object($blog)))
709 // explicit blog selection
711 $blogid = getBlogIDFromName($blogName);
713 if (($blogName == '') || !$manager->existsBlogID($blogid))
715 $blogid = $blog->getID();
717 return $member->teamRights($blogid);
721 * Checks if a member is admin of a blog
723 function _ifAdmin($blogName = '') {
724 global $blog, $member, $manager;
726 // when no blog found
727 if (($blogName == '') && (!is_object($blog)))
730 // explicit blog selection
732 $blogid = getBlogIDFromName($blogName);
734 if (($blogName == '') || !$manager->existsBlogID($blogid))
736 $blogid = $blog->getID();
738 return $member->isBlogAdmin($blogid);
744 * -> checks if plugin exists
745 * hasplugin,PlugName,OptionName
746 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
747 * hasplugin,PlugName,OptionName=value
748 * -> checks if the option OptionName from plugin PlugName is set to value
750 function _ifHasPlugin($name, $value) {
753 // (pluginInstalled method won't write a message in the actionlog on failure)
754 if ($manager->pluginInstalled('NP_'.$name)) {
755 $plugin =& $manager->getPlugin('NP_' . $name);
756 if ($plugin != NULL) {
760 list($name2, $value2) = explode('=', $value, 2);
761 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
763 } else if ($plugin->getOption($name2) == $value2) {
773 * Checks if a plugin exists and call its doIf function
775 function _ifPlugin($name, $key = '', $value = '') {
778 $plugin =& $manager->getPlugin('NP_' . $name);
779 if (!$plugin) return;
781 $params = func_get_args();
782 array_shift($params);
784 return call_user_func_array(array(&$plugin, 'doIf'), $params);