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 when parsing comment 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: CommentActions.php 1626 2012-01-09 15:46:54Z sakamocchi $
\r
20 class CommentActions extends BaseActions
\r
23 // ref to COMMENTS object which is using this object to handle
\r
27 // template to use to parse the comments
\r
30 // comment currenlty being handled (mysql result assoc array; see Comments::showComments())
\r
31 var $currentComment;
\r
33 function __construct(&$comments) {
\r
34 // call constructor of superclass first
\r
35 $this->BaseActions();
\r
37 // reference to the comments object
\r
38 $this->setCommentsObj($comments);
\r
41 function getDefinedActions() {
\r
84 function setParser(&$parser) {
\r
85 $this->parser =& $parser;
\r
88 function setCommentsObj(&$commentsObj) {
\r
89 $this->commentsObj =& $commentsObj;
\r
92 function setTemplate($template) {
\r
93 $this->template =& $template;
\r
97 * CommentActions::setCurrentComment()
\r
98 * Set $currentcommentid and $currentcommentarray
\r
100 * @param Array $comment array with comment elements
\r
104 function setCurrentComment(&$comment)
\r
106 global $currentcommentid, $currentcommentarray, $manager;
\r
108 if ( $comment['memberid'] != 0 )
\r
110 if ( !array_key_exists('COMMENTS_AUTH', $this->template) )
\r
112 $comment['authtext'] = '';
\r
116 $comment['authtext'] = $this->template['COMMENTS_AUTH'];
\r
119 $mem =& $manager->getMember($comment['memberid']);
\r
120 $comment['user'] = $mem->getDisplayName();
\r
122 if ( $mem->getURL() )
\r
124 $comment['userid'] = $mem->getURL();
\r
128 $comment['userid'] = $mem->getEmail();
\r
131 $comment['userlinkraw'] = Link::create_link(
\r
134 'memberid' => $comment['memberid'],
\r
135 'name' => $mem->getDisplayName(),
\r
136 'extra' => $this->commentsObj->itemActions->linkparams
\r
142 // create smart links
\r
143 if ( !array_key_exists('userid', $comment) || !empty($comment['userid']) )
\r
145 if ( (i18n::strpos($comment['userid'], 'http://') === 0) || (i18n::strpos($comment['userid'], 'https://') === 0) )
\r
147 $comment['userlinkraw'] = $comment['userid'];
\r
151 $comment['userlinkraw'] = 'http://' . $comment['userid'];
\r
154 else if ( NOTIFICATION::address_validation($comment['email']) )
\r
156 $comment['userlinkraw'] = 'mailto:' . $comment['email'];
\r
158 else if ( NOTIFICATION::address_validation($comment['userid']) )
\r
160 $comment['userlinkraw'] = 'mailto:' . $comment['userid'];
\r
164 $this->currentComment =& $comment;
\r
165 $currentcommentid = $comment['commentid'];
\r
166 $currentcommentarray = $comment;
\r
171 * Parse templatevar authtext
\r
173 function parse_authtext() {
\r
174 if ($this->currentComment['memberid'] != 0)
\r
175 $this->parser->parse($this->template['COMMENTS_AUTH']);
\r
179 * Parse templatevar blogid
\r
181 function parse_blogid() {
\r
182 echo $this->currentComment['blogid'];
\r
186 * Parse templatevar blogurl
\r
188 function parse_blogurl() {
\r
190 $blogid = getBlogIDFromItemID($this->commentsObj->itemid);
\r
191 $blog =& $manager->getBlog($blogid);
\r
192 echo $blog->getURL();
\r
196 * Parse templatevar body
\r
198 function parse_body() {
\r
199 echo $this->highlight($this->currentComment['body']);
\r
203 * Parse templatevar commentcount
\r
205 function parse_commentcount() {
\r
206 echo $this->commentsObj->commentcount;
\r
210 * Parse templatevar commentid
\r
212 function parse_commentid() {
\r
213 echo $this->currentComment['commentid'];
\r
217 * Parse templatevar commentword
\r
219 function parse_commentword() {
\r
220 if ($this->commentsObj->commentcount == 1)
\r
221 echo $this->template['COMMENTS_ONE'];
\r
223 echo $this->template['COMMENTS_MANY'];
\r
227 * CommentActions::parse_date()
\r
228 * Parse templatevar date
\r
230 * @format String $format Date format according to PHP
\r
234 function parse_date($format = '')
\r
236 if ( $format !== '' )
\r
241 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
\r
247 $format = $this->template['FORMAT_DATE'];
\r
250 $offset = $this->commentsObj->itemActions->blog->getTimeOffset() * 3600;
\r
252 echo i18n::formatted_datetime($format, $this->currentComment['timestamp'], $offset);
\r
257 * Parse templatevar email
\r
259 function parse_email() {
\r
260 $email = $this->currentComment['email'];
\r
261 $email = str_replace('@', ' (at) ', $email);
\r
262 $email = str_replace('.', ' (dot) ', $email);
\r
267 * CommentActions::parse_excerpt()
\r
268 * Parse templatevar excerpt
\r
271 * @return String templatevar excerpt
\r
273 function parse_excerpt()
\r
275 echo Entity::hen(Entity::shorten($this->currentComment['body'], 60, '...'));
\r
279 * Parse templatevar host
\r
281 function parse_host() {
\r
282 echo $this->currentComment['host'];
\r
286 * Parse templatevar ip
\r
288 function parse_ip() {
\r
289 echo $this->currentComment['ip'];
\r
293 * Parse templatevar itemid
\r
295 function parse_itemid() {
\r
296 echo $this->commentsObj->itemid;
\r
300 * CommentActions::parse_itemlink()
\r
301 * Parse templatevar itemlink
\r
306 function parse_itemlink()
\r
308 echo Link::create_link(
\r
311 'itemid' => $this->commentsObj->itemid,
\r
312 'timestamp' => $this->commentsObj->itemActions->currentItem->timestamp,
\r
313 'title' => $this->commentsObj->itemActions->currentItem->title,
\r
314 'extra' => $this->commentsObj->itemActions->linkparams
\r
321 * Parse templatevar itemtitle
\r
323 function parse_itemtitle($maxLength = 0) {
\r
324 if ($maxLength == 0)
\r
325 $this->commentsObj->itemActions->parse_title();
\r
327 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
\r
331 * Parse templatevar memberid
\r
333 function parse_memberid() {
\r
334 echo $this->currentComment['memberid'];
\r
338 * Parse templatevar short
\r
340 function parse_short() {
\r
341 $tmp = strtok($this->currentComment['body'],"\n");
\r
342 $tmp = str_replace('<br />','',$tmp);
\r
344 if ($tmp != $this->currentComment['body'])
\r
345 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
\r
349 * CommentActions::parse_time()
\r
350 * Parse templatevar time
\r
352 * @param string $format datetime format referring to strftime() in PHP's built-in function
\r
356 function parse_time($format = '')
\r
358 if ( $format === '' )
\r
363 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
\r
369 $format = $this->template['FORMAT_TIME'];
\r
372 echo i18n::formatted_datetime($format, $this->currentComment['timestamp']);
\r
377 * Parse templatevar timestamp
\r
379 function parse_timestamp() {
\r
380 echo $this->currentComment['timestamp'];
\r
384 * Executes a plugin templatevar
\r
386 * @param pluginName name of plugin (without the NP_)
\r
388 * extra parameters can be added
\r
390 function parse_plugin($pluginName) {
\r
393 // only continue when the plugin is really installed
\r
394 if (!$manager->pluginInstalled('NP_' . $pluginName))
\r
397 $plugin =& $manager->getPlugin('NP_' . $pluginName);
\r
398 if (!$plugin) return;
\r
401 $params = func_get_args();
\r
403 // remove plugin name
\r
404 array_shift($params);
\r
406 // pass info on current item and current comment as well
\r
407 $params = array_merge(array(&$this->currentComment),$params);
\r
408 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem),$params);
\r
410 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
\r
414 * Parse templatevar user
\r
415 * @param string $mode
\r
417 function parse_user($mode = '')
\r
421 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
\r
423 $member =& $manager->getMember($this->currentComment['memberid']);
\r
424 echo $member->getRealName();
\r
428 echo Entity::hsc($this->currentComment['user']);
\r
433 * CommentActions::parse_useremail()
\r
434 * Output mail address
\r
439 function parse_useremail() {
\r
441 if ( $this->currentComment['memberid'] > 0 )
\r
443 $member =& $manager->getMember($this->currentComment['memberid']);
\r
445 if ( $member->email != '' )
\r
447 echo $member->email;
\r
452 if ( NOTIFICATION::address_validation($this->currentComment['email']) )
\r
454 echo $this->currentComment['email'];
\r
456 elseif ( NOTIFICATION::address_validation($this->currentComment['userid']) )
\r
458 echo $this->currentComment['userid'];
\r
465 * Parse templatevar userid
\r
467 function parse_userid() {
\r
468 echo $this->currentComment['userid'];
\r
473 * Parse templatevar userlink
\r
475 function parse_userlink() {
\r
476 if ($this->currentComment['userlinkraw']) {
\r
477 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
\r
479 echo $this->currentComment['user'];
\r
484 * Parse templatevar userlinkraw
\r
486 function parse_userlinkraw() {
\r
487 echo $this->currentComment['userlinkraw'];
\r
491 * Parse templatevar userwebsite
\r
493 function parse_userwebsite() {
\r
494 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false))
\r
495 echo $this->currentComment['userlinkraw'];
\r
499 * Parse templatevar userwebsitelink
\r
501 function parse_userwebsitelink() {
\r
502 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false)) {
\r
503 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
\r
505 echo $this->currentComment['user'];
\r
509 // function to enable if-else-elseif-elseifnot-ifnot-endif to comment template fields
\r
512 * Checks conditions for if statements
\r
514 * @param string $field type of <%if%>
\r
515 * @param string $name property of field
\r
516 * @param string $value value of property
\r
518 function checkCondition($field, $name='', $value = '') {
\r
519 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
\r
524 $condition = ($blog && $this->_ifCategory($name,$value));
\r
526 case 'itemcategory':
\r
527 $condition = ($this->_ifItemCategory($name,$value));
\r
529 case 'blogsetting':
\r
530 $condition = ($blog && ($blog->getSetting($name) == $value));
\r
532 case 'itemblogsetting':
\r
533 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
\r
534 $condition = ($b && ($b->getSetting($name) == $value));
\r
537 $condition = $member->isLoggedIn();
\r
540 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
\r
543 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
\r
546 $condition = ($this->_ifAuthor($name,$value));
\r
548 /* case 'nextitem':
\r
549 $condition = ($itemidnext != '');
\r
552 $condition = ($itemidprev != '');
\r
554 case 'archiveprevexists':
\r
555 $condition = ($archiveprevexists == true);
\r
557 case 'archivenextexists':
\r
558 $condition = ($archivenextexists == true);
\r
561 $condition = ($name == $this->skintype);
\r
564 $condition = $this->_ifHasPlugin($name, $value);
\r
567 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
\r
574 * Different checks for a category
\r
576 function _ifCategory($name = '', $value='') {
\r
577 global $blog, $catid;
\r
579 // when no parameter is defined, just check if a category is selected
\r
580 if (($name != 'catname' && $name != 'catid') || ($value == ''))
\r
581 return $blog->isValidCategory($catid);
\r
583 // check category name
\r
584 if ($name == 'catname') {
\r
585 $value = $blog->getCategoryIdFromName($value);
\r
586 if ($value == $catid)
\r
587 return $blog->isValidCategory($catid);
\r
590 // check category id
\r
591 if (($name == 'catid') && ($value == $catid))
\r
592 return $blog->isValidCategory($catid);
\r
599 * Different checks for an author
\r
601 function _ifAuthor($name = '', $value='') {
\r
602 global $member, $manager;
\r
604 if ($this->currentComment['memberid'] == 0) return false;
\r
606 $mem =& $manager->getMember($this->currentComment['memberid']);
\r
607 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
\r
608 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
\r
610 // when no parameter is defined, just check if item author is current visitor
\r
611 if (($name != 'isadmin' && $name != 'name' && $name != 'isauthor' && $name != 'isonteam')) {
\r
612 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
\r
615 // check comment author name
\r
616 if ($name == 'name') {
\r
617 $value = trim(strtolower($value));
\r
620 if ($value == strtolower($mem->getDisplayName()))
\r
624 // check if comment author is admin
\r
625 if ($name == 'isadmin') {
\r
626 $blogid = intval($b->getID());
\r
627 if ($mem->isAdmin())
\r
630 return $mem->isBlogAdmin($blogid);
\r
633 // check if comment author is item author
\r
634 if ($name == 'isauthor') {
\r
635 return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
\r
638 // check if comment author is on team
\r
639 if ($name == 'isonteam') {
\r
640 return $mem->teamRights(intval($b->getID()));
\r
647 * Different checks for a category
\r
649 function _ifItemCategory($name = '', $value='') {
\r
650 global $catid, $manager;
\r
652 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
\r
653 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
\r
654 $icatid = $citem['catid'];
\r
656 // when no parameter is defined, just check if a category is selected
\r
657 if (($name != 'catname' && $name != 'catid') || ($value == ''))
\r
658 return $b->isValidCategory($icatid);
\r
660 // check category name
\r
661 if ($name == 'catname') {
\r
662 $value = $b->getCategoryIdFromName($value);
\r
663 if ($value == $icatid)
\r
664 return $b->isValidCategory($icatid);
\r
667 // check category id
\r
668 if (($name == 'catid') && ($value == $icatid))
\r
669 return $b->isValidCategory($icatid);
\r
676 * Checks if a member is on the team of a blog and return his rights
\r
678 function _ifOnTeam($blogName = '') {
\r
679 global $blog, $member, $manager;
\r
681 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
\r
683 // when no blog found
\r
684 if (($blogName == '') && (!is_object($b)))
\r
687 // explicit blog selection
\r
688 if ($blogName != '')
\r
689 $blogid = getBlogIDFromName($blogName);
\r
691 if (($blogName == '') || !$manager->existsBlogID($blogid))
\r
692 // use current blog
\r
693 $blogid = $b->getID();
\r
695 return $member->teamRights($blogid);
\r
699 * Checks if a member is admin of a blog
\r
701 function _ifAdmin($blogName = '') {
\r
702 global $blog, $member, $manager;
\r
704 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
\r
706 // when no blog found
\r
707 if (($blogName == '') && (!is_object($b)))
\r
710 // explicit blog selection
\r
711 if ($blogName != '')
\r
712 $blogid = getBlogIDFromName($blogName);
\r
714 if (($blogName == '') || !$manager->existsBlogID($blogid))
\r
715 // use current blog
\r
716 $blogid = $b->getID();
\r
718 return $member->isBlogAdmin($blogid);
\r
723 * hasplugin,PlugName
\r
724 * -> checks if plugin exists
\r
725 * hasplugin,PlugName,OptionName
\r
726 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
\r
727 * hasplugin,PlugName,OptionName=value
\r
728 * -> checks if the option OptionName from plugin PlugName is set to value
\r
730 function _ifHasPlugin($name, $value) {
\r
732 $condition = false;
\r
733 // (pluginInstalled method won't write a message in the actionlog on failure)
\r
734 if ($manager->pluginInstalled('NP_'.$name)) {
\r
735 $plugin =& $manager->getPlugin('NP_' . $name);
\r
736 if ($plugin != NULL) {
\r
737 if ($value == "") {
\r
740 list($name2, $value2) = i18n::explode('=', $value, 2);
\r
741 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
\r
743 } else if ($plugin->getOption($name2) == $value2) {
\r
753 * Checks if a plugin exists and call its doIf function
\r
755 function _ifPlugin($name, $key = '', $value = '') {
\r
758 $plugin =& $manager->getPlugin('NP_' . $name);
\r
759 if (!$plugin) return;
\r
761 $params = func_get_args();
\r
762 array_shift($params);
\r
764 return call_user_func_array(array(&$plugin, 'doIf'), $params);
\r