3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2012 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)
12 * This class is used to parse item templates
14 class ITEMACTIONS extends BaseActions {
\r
16 // contains an assoc array with parameters that need to be included when
\r
17 // generating links to items/archives/... (e.g. catid)
\r
20 // true when the current user is a blog admin (and thus allowed to edit all items)
\r
23 // timestamp of last visit
\r
26 // item currently being handled (mysql result object, see BLOG::showUsingQuery)
\r
29 // reference to the blog currently being displayed
\r
32 // associative array with template info (part name => contents)
\r
35 // true when comments need to be displayed
\r
38 function ITEMACTIONS(&$blog) {
\r
39 // call constructor of superclass first
\r
40 $this->BaseActions();
\r
42 // extra parameters for created links
\r
45 $this->linkparams = array('catid' => $catid);
\r
47 // check if member is blog admin (and thus allowed to edit all items)
\r
49 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
\r
50 $this->setBlog($blog);
\r
54 * Returns an array with the actions that are defined
\r
55 * in the ITEMACTIONS class
\r
57 function getDefinedActions() {
\r
80 'syndicate_description',
\r
109 function setLastVisit($lastVisit) {
\r
110 $this->lastVisit = $lastVisit;
\r
113 function setParser(&$parser) {
\r
114 $this->parser =& $parser;
\r
117 function setCurrentItem(&$item) {
118 $this->currentItem =& $item;
119 global $currentitemid;
120 if (is_array($this->currentItem)) {
121 $currentitemid = $this->currentItem['itemid'];
123 $currentitemid = $this->currentItem->itemid;
127 function setBlog(&$blog) {
\r
128 $this->blog =& $blog;
\r
131 function setTemplate($template) {
\r
132 $this->template =& $template;
\r
135 function setShowComments($val) {
\r
136 $this->showComments = $val;
\r
139 // methods used by parser to insert content
\r
143 * Parse templatevar blogid
\r
145 function parse_blogid() {
\r
146 echo $this->blog->getID();
\r
150 * Parse templatevar body
\r
152 function parse_body() {
\r
153 $this->highlightAndParse($this->currentItem->body);
\r
157 * Parse templatevar more
\r
159 function parse_more() {
\r
160 $this->highlightAndParse($this->currentItem->more);
\r
164 * Parse templatevar itemid
\r
166 function parse_itemid() {
\r
167 echo $this->currentItem->itemid;
\r
171 * Parse templatevar category
\r
173 function parse_category() {
\r
174 echo $this->currentItem->category;
\r
178 * Parse templatevar categorylink
\r
180 function parse_categorylink() {
\r
181 echo createLink('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
\r
185 * Parse templatevar catid
\r
187 function parse_catid() {
\r
188 echo $this->currentItem->catid;
\r
192 * Parse templatevar authorid
\r
194 function parse_authorid() {
\r
195 echo $this->currentItem->authorid;
\r
199 * Parse templatevar authorlink
\r
201 function parse_authorlink() {
\r
205 'memberid' => $this->currentItem->authorid,
\r
206 'name' => $this->currentItem->author,
\r
207 'extra' => $this->linkparams
\r
213 * Parse templatevar query
\r
215 function parse_query() {
\r
216 echo $this->strHighlight;
\r
220 * Parse templatevar itemlink
\r
222 function parse_itemlink() {
\r
226 'itemid' => $this->currentItem->itemid,
\r
227 'title' => $this->currentItem->title,
\r
228 'timestamp' => $this->currentItem->timestamp,
\r
229 'extra' => $this->linkparams
\r
235 * Parse templatevar blogurl
\r
237 function parse_blogurl() {
\r
238 echo $this->blog->getURL();
\r
242 * Parse templatevar closed
\r
244 function parse_closed() {
\r
245 echo $this->currentItem->closed;
\r
249 * Parse templatevar relevance
\r
251 function parse_relevance() {
\r
252 echo round($this->currentItem->score,2);
\r
256 * Parse templatevar title
\r
258 * @param string $format defines in which format the title is shown
\r
260 function parse_title($format = '') {
\r
261 if (is_array($this->currentItem)) {
\r
262 $itemtitle = $this->currentItem['title'];
\r
263 } elseif (is_object($this->currentItem)) {
\r
264 $itemtitle = $this->currentItem->title;
\r
268 // echo stringToXML ($this->currentItem->title);
\r
269 echo stringToXML ($itemtitle);
\r
272 // echo stringToAttribute ($this->currentItem->title);
\r
273 echo stringToAttribute ($itemtitle);
\r
276 // echo $this->currentItem->title;
\r
280 // $this->highlightAndParse($this->currentItem->title);
\r
281 $this->highlightAndParse($itemtitle);
\r
287 * Parse templatevar karma
\r
289 function parse_karma($type = 'totalscore') {
\r
292 // get karma object
\r
293 $karma =& $manager->getKarma($this->currentItem->itemid);
\r
297 echo $karma->getNbPosVotes();
\r
300 echo $karma->getNbNegVotes();
\r
303 echo $karma->getNbOfVotes();
\r
306 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
\r
307 echo number_format($percentage,2), '%';
\r
310 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
\r
311 echo number_format($percentage,2), '%';
\r
315 echo $karma->getTotalScore();
\r
322 * Parse templatevar author
\r
324 function parse_author($which = '') {
\r
328 echo $this->currentItem->authorname;
\r
331 echo $this->currentItem->authorid;
\r
334 echo $this->currentItem->authormail;
\r
337 echo $this->currentItem->authorurl;
\r
341 echo $this->currentItem->author;
\r
346 * Parse templatevar smartbody
\r
348 function parse_smartbody() {
\r
349 if (!$this->currentItem->more) {
\r
350 $this->highlightAndParse($this->currentItem->body);
\r
352 $this->highlightAndParse($this->currentItem->more);
\r
357 * Parse templatevar morelink
\r
359 function parse_morelink() {
\r
360 if ($this->currentItem->more)
\r
361 $this->parser->parse($this->template['MORELINK']);
\r
365 * Parse templatevar date
\r
367 * @param format optional strftime format
\r
369 function parse_date($format = '') {
\r
370 if (!isset($this->template['FORMAT_DATE'])) $this->template['FORMAT_DATE'] = '';
371 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $this->blog);
\r
375 * Parse templatevar time
\r
377 * @param format optional strftime format
\r
379 function parse_time($format = '') {
\r
380 if (!isset($this->template['FORMAT_TIME'])) $this->template['FORMAT_TIME'] = '';
381 echo strftimejp($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);
\r
385 * Parse templatevar syndicate_title
\r
387 * @param maxLength optional maximum length
\r
389 function parse_syndicate_title($maxLength = 100) {
\r
390 $syndicated = strip_tags($this->currentItem->title);
\r
391 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
\r
395 * Parse templatevar syndicate_description
\r
397 * @param maxLength optional maximum length
\r
399 function parse_syndicate_description($maxLength = 250, $addHighlight = 0) {
\r
400 $syndicated = strip_tags($this->currentItem->body);
\r
401 if ($addHighlight) {
\r
402 $tmp_highlight = htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
\r
403 echo $this->highlightAndParse($tmp_highlight);
\r
405 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
\r
410 * Parse templatevar karmaposlink
\r
412 * @param string text
\r
414 function parse_karmaposlink($text = '') {
\r
416 $link = $CONF['ActionURL'] . '?action=votepositive&itemid='.$this->currentItem->itemid;
\r
417 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
\r
421 * Parse templatevar karmaneglink
\r
423 * @param string text
\r
425 function parse_karmaneglink($text = '') {
\r
427 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem->itemid;
\r
428 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
\r
432 * Parse templatevar new
\r
434 function parse_new() {
\r
435 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
\r
436 echo $this->template['NEW'];
\r
440 * Parse templatevar daylink
\r
442 function parse_daylink() {
\r
443 echo createArchiveLink($this->blog->getID(), strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
\r
447 * Parse templatevar comments
\r
449 function parse_comments($maxToShow = 0) {
\r
450 if ($maxToShow == 0)
\r
451 $maxToShow = $this->blog->getMaxComments();
\r
454 if ($this->showComments && $this->blog->commentsEnabled()) {
\r
455 $comments = new COMMENTS($this->currentItem->itemid);
\r
456 $comments->setItemActions($this);
\r
457 $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
\r
462 * Executes a plugin templatevar
\r
464 * @param pluginName name of plugin (without the NP_)
\r
466 * extra parameters can be added
\r
468 function parse_plugin($pluginName) {
\r
471 // should be already tested from the parser (PARSER.php)
\r
472 // only continue when the plugin is really installed
\r
473 /*if (!$manager->pluginInstalled('NP_' . $pluginName))
\r
476 $plugin =& $manager->getPlugin('NP_' . $pluginName);
\r
477 if (!$plugin) return;
\r
480 $params = func_get_args();
\r
482 // remove plugin name
\r
483 array_shift($params);
\r
485 // add item reference (array_unshift didn't work)
\r
486 $params = array_merge(array(&$this->currentItem),$params);
\r
488 call_user_func_array(array($plugin,'doTemplateVar'), $params);
\r
492 * Parse templatevar edit
\r
494 function parse_edit() {
\r
495 global $member, $CONF;
\r
496 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {
\r
497 $this->parser->parse($this->template['EDITLINK']);
\r
502 * Parse templatevar editlink
\r
504 function parse_editlink() {
\r
506 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&itemid=',$this->currentItem->itemid;
\r
510 * Parse templatevar editpopupcode
\r
512 function parse_editpopupcode() {
\r
513 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
516 // helper functions
\r
519 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
\r
520 * from hacking your weblog.
\r
521 * 'plugin variables in items' implementation by Andy
\r
523 function highlightAndParse(&$data) {
\r
524 $actions = new BODYACTIONS($this->blog);
\r
525 $parser = new PARSER($actions->getDefinedActions(), $actions);
\r
526 $actions->setTemplate($this->template);
\r
527 $actions->setHighlight($this->strHighlight);
\r
528 $actions->setCurrentItem($this->currentItem);
\r
529 //$actions->setParser($parser);
\r
530 $parser->parse($actions->highlight($data));
\r
534 // this is the function previous to the 'plugin variables in items' implementation by Andy
\r
535 function highlightAndParse(&$data) {
\r
536 // allow only a limited subset of actions (do not allow includes etc, they might be evil)
\r
537 $this->parser->actions = array('image','media','popup');
\r
538 $tmp_highlight = $this->highlight($data);
\r
539 $this->parser->parse($tmp_highlight);
\r
540 $this->parser->actions = $this->getDefinedActions();
\r
544 // function to enable if-else-elseif-elseifnot-ifnot-endif to item template fields
547 * Checks conditions for if statements
549 * @param string $field type of <%if%>
550 * @param string $name property of field
551 * @param string $value value of property
553 function checkCondition($field, $name='', $value = '') {
554 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
559 $condition = ($blog && $this->_ifCategory($name,$value));
562 $condition = ($this->_ifItemCategory($name,$value));
565 $condition = ($blog && ($blog->getSetting($name) == $value));
567 case 'itemblogsetting':
568 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
569 $condition = ($b && ($b->getSetting($name) == $value));
572 $condition = $member->isLoggedIn();
575 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
578 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
581 $condition = ($this->_ifAuthor($name,$value));
584 $condition = ($itemidnext != '');
587 $condition = ($itemidprev != '');
589 case 'archiveprevexists':
590 $condition = ($archiveprevexists == true);
592 case 'archivenextexists':
593 $condition = ($archivenextexists == true);
596 $condition = ($name == $this->skintype);
599 $condition = $this->_ifHasPlugin($name, $value);
602 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
609 * Different checks for a category
611 function _ifCategory($name = '', $value='') {
612 global $blog, $catid;
614 // when no parameter is defined, just check if a category is selected
615 if (($name != 'catname' && $name != 'catid') || ($value == ''))
616 return $blog->isValidCategory($catid);
618 // check category name
619 if ($name == 'catname') {
620 $value = $blog->getCategoryIdFromName($value);
621 if ($value == $catid)
622 return $blog->isValidCategory($catid);
626 if (($name == 'catid') && ($value == $catid))
627 return $blog->isValidCategory($catid);
634 * Different checks for an author
636 function _ifAuthor($name = '', $value='') {
637 global $member, $manager;
639 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
641 // when no parameter is defined, just check if author is current visitor
642 if (($name != 'isadmin' && $name != 'name') || ($name == 'name' && $value == '')) {
643 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));
647 if ($name == 'name') {
648 $value = strtolower($value);
649 if ($value == strtolower($this->currentItem->author))
653 // check if author is admin
654 if (($name == 'isadmin')) {
655 $aid = intval($this->currentItem->authorid);
656 $blogid = intval($b->getID());
657 $amember =& $manager->getMember($aid);
658 if ($amember->isAdmin())
661 return $amember->isBlogAdmin($blogid);
668 * Different checks for a category
670 function _ifItemCategory($name = '', $value='') {
671 global $catid, $manager;
673 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
675 // when no parameter is defined, just check if a category is selected
676 if (($name != 'catname' && $name != 'catid') || ($value == ''))
677 return $b->isValidCategory($catid);
679 $icatid = $this->currentItem->catid;
680 //$icategory = $this->currentItem->category;
682 // check category name
683 if ($name == 'catname') {
684 $value = $b->getCategoryIdFromName($value);
685 if ($value == $icatid)
686 return $b->isValidCategory($icatid);
690 if (($name == 'catid') && ($value == $icatid))
691 return $b->isValidCategory($icatid);
698 * Checks if a member is on the team of a blog and return his rights
700 function _ifOnTeam($blogName = '') {
701 global $blog, $member, $manager;
703 // when no blog found
704 if (($blogName == '') && (!is_object($blog)))
707 // explicit blog selection
709 $blogid = getBlogIDFromName($blogName);
711 if (($blogName == '') || !$manager->existsBlogID($blogid))
713 $blogid = $blog->getID();
715 return $member->teamRights($blogid);
719 * Checks if a member is admin of a blog
721 function _ifAdmin($blogName = '') {
722 global $blog, $member, $manager;
724 // when no blog found
725 if (($blogName == '') && (!is_object($blog)))
728 // explicit blog selection
730 $blogid = getBlogIDFromName($blogName);
732 if (($blogName == '') || !$manager->existsBlogID($blogid))
734 $blogid = $blog->getID();
736 return $member->isBlogAdmin($blogid);
742 * -> checks if plugin exists
743 * hasplugin,PlugName,OptionName
744 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
745 * hasplugin,PlugName,OptionName=value
746 * -> checks if the option OptionName from plugin PlugName is set to value
748 function _ifHasPlugin($name, $value) {
751 // (pluginInstalled method won't write a message in the actionlog on failure)
752 if ($manager->pluginInstalled('NP_'.$name)) {
753 $plugin =& $manager->getPlugin('NP_' . $name);
754 if ($plugin != NULL) {
758 list($name2, $value2) = explode('=', $value, 2);
759 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
761 } else if ($plugin->getOption($name2) == $value2) {
771 * Checks if a plugin exists and call its doIf function
773 function _ifPlugin($name, $key = '', $value = '') {
776 $plugin =& $manager->getPlugin('NP_' . $name);
777 if (!$plugin) return;
779 $params = func_get_args();
780 array_shift($params);
782 return call_user_func_array(array($plugin, 'doIf'), $params);