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 when parsing comment templates
15 * @license http://nucleuscms.org/license.txt GNU General Public License
16 * @copyright Copyright (C) 2002-2009 The Nucleus Group
17 * @version $Id: CommentActions.php 1626 2012-01-09 15:46:54Z sakamocchi $
20 class CommentActions extends BaseActions
23 * CommentsActions::$commentsObj
24 * ref to COMMENTS object which is using this object to handle its templatevars
29 * CommentsActions::$template
30 * template to use to parse the comments
35 * CommentsActions::$currentComment
36 * comment currenlty being handled (mysql result assoc array; see Comments::showComments())
38 private $currentComment;
41 * CommentsActions::$defined_actions
42 * defined actions in this class
44 static private $defined_actions = array(
75 * CommentActions::__construct()
77 * @param object $comments instance of Comments class
80 public function __construct(&$comments)
82 // call constructor of superclass first
83 parent::__construct();
85 // reference to the comments object
86 $this->setCommentsObj($comments);
91 * CommentActions::getDefinedActions()
95 * @return array actions array
97 static public function getDefinedActions()
99 return array_merge(self::$defined_actions, parent::getDefinedActions());
104 * CommentActions::setCommentsObj()
106 * @param object $commentsObj instance of Comments class
109 public function setCommentsObj(&$commentsObj)
111 $this->commentsObj =& $commentsObj;
116 * CommentActions::setTemplate()
118 * @param array $template array includes templates
121 public function setTemplate($template)
123 $this->template =& $template;
128 * CommentActions::setCurrentComment()
129 * Set $currentcommentid and $currentcommentarray
131 * @param array $comment associated array includes comment information
134 public function setCurrentComment(&$comment)
136 global $currentcommentid, $currentcommentarray, $manager;
138 if ( $comment['memberid'] != 0 )
140 if ( !array_key_exists('COMMENTS_AUTH', $this->template) )
142 $comment['authtext'] = '';
146 $comment['authtext'] = $this->template['COMMENTS_AUTH'];
149 $mem =& $manager->getMember($comment['memberid']);
150 $comment['user'] = $mem->getDisplayName();
152 if ( $mem->getURL() )
154 $comment['userid'] = $mem->getURL();
158 $comment['userid'] = $mem->getEmail();
162 'memberid' => $comment['memberid'],
163 'name' => $mem->getDisplayName(),
164 'extra' => $this->commentsObj->itemActions->linkparams
167 $comment['userlinkraw'] = Link::create_link('member', $data);
171 // create smart links
172 if ( !array_key_exists('userid', $comment) || !empty($comment['userid']) )
174 if ( (i18n::strpos($comment['userid'], 'http://') === 0) || (i18n::strpos($comment['userid'], 'https://') === 0) )
176 $comment['userlinkraw'] = $comment['userid'];
180 $comment['userlinkraw'] = 'http://' . $comment['userid'];
183 else if ( NOTIFICATION::address_validation($comment['email']) )
185 $comment['userlinkraw'] = 'mailto:' . $comment['email'];
187 else if ( NOTIFICATION::address_validation($comment['userid']) )
189 $comment['userlinkraw'] = 'mailto:' . $comment['userid'];
193 $this->currentComment =& $comment;
194 $currentcommentid = $comment['commentid'];
195 $currentcommentarray = $comment;
200 * CommentActions::parse_authtext()
201 * Parse templatevar authtext
206 public function parse_authtext()
208 if ( $this->currentComment['memberid'] != 0 )
210 $this->parser->parse($this->template['COMMENTS_AUTH']);
216 * CommentActions::parse_blogid()
217 * Parse templatevar blogid
222 public function parse_blogid() {
223 echo $this->currentComment['blogid'];
227 * CommentActions::parse_blogurl()
228 * Parse templatevar blogurl
233 public function parse_blogurl()
236 $blogid = getBlogIDFromItemID($this->commentsObj->itemid);
237 $blog =& $manager->getBlog($blogid);
238 echo $blog->getURL();
243 * CommentActions::parse_body()
244 * Parse templatevar body
249 public function parse_body() {
250 echo $this->highlight($this->currentComment['body']);
255 * CommentActions::parse_commentcount()
256 * Parse templatevar commentcount
261 public function parse_commentcount()
263 echo $this->commentsObj->commentcount;
268 * CommentActions::parse_commentid()
269 * Parse templatevar commentid
274 public function parse_commentid()
276 echo $this->currentComment['commentid'];
281 * CommentActions::parse_commentword()
282 * Parse templatevar commentword
287 public function parse_commentword()
289 if ( $this->commentsObj->commentcount == 1 )
291 echo $this->template['COMMENTS_ONE'];
295 echo $this->template['COMMENTS_MANY'];
301 * CommentActions::parse_date()
302 * Parse templatevar date
304 * @format String $format Date format according to PHP
307 public function parse_date($format = '')
309 if ( $format !== '' )
314 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
320 $format = $this->template['FORMAT_DATE'];
323 $offset = $this->commentsObj->itemActions->blog->getTimeOffset() * 3600;
325 echo i18n::formatted_datetime($format, $this->currentComment['timestamp'], $offset);
330 * CommentActions::parse_excerpt()
331 * Parse templatevar email
336 public function parse_email()
338 $email = $this->currentComment['email'];
339 $email = str_replace('@', ' (at) ', $email);
340 $email = str_replace('.', ' (dot) ', $email);
346 * CommentActions::parse_excerpt()
347 * Parse templatevar excerpt
352 public function parse_excerpt()
354 echo Entity::hen(Entity::shorten($this->currentComment['body'], 60, '...'));
359 * CommentActions::parse_host()
360 * Parse templatevar host
365 public function parse_host()
367 echo $this->currentComment['host'];
372 * CommentActions::parse_ip()
373 * Parse templatevar ip
378 public function parse_ip()
380 echo $this->currentComment['ip'];
385 * CommentActions::parse_itemid()
386 * Parse templatevar itemid
391 public function parse_itemid()
393 echo $this->commentsObj->itemid;
398 * CommentActions::parse_itemlink()
399 * Parse templatevar itemlink
404 public function parse_itemlink()
407 'itemid' => $this->commentsObj->itemid,
408 'timestamp' => $this->commentsObj->itemActions->currentItem['timestamp'],
409 'title' => $this->commentsObj->itemActions->currentItem['title'],
410 'extra' => $this->commentsObj->itemActions->linkparams
413 echo Link::create_link('item', $data);
418 * CommentActions::parse_itemtitle()
419 * Parse templatevar itemtitle
421 * @param integer $maxLength maximum length for item title
424 public function parse_itemtitle($maxLength = 0)
426 if ( $maxLength == 0 )
428 $this->commentsObj->itemActions->parse_title();
432 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
438 * CommentActions::parse_memberid()
439 * Parse templatevar memberid
444 public function parse_memberid()
446 echo $this->currentComment['memberid'];
451 * CommentActions::parse_short()
452 * Parse templatevar short
457 public function parse_short()
459 $tmp = strtok($this->currentComment['body'], "\n");
460 $tmp = str_replace('<br />', '', $tmp);
462 if ( $tmp != $this->currentComment['body'] )
464 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
470 * CommentActions::parse_time()
471 * Parse templatevar time
473 * @param string $format datetime format referring to strftime() in PHP's built-in function
476 public function parse_time($format = '')
478 if ( $format !== '' )
483 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
489 $format = $this->template['FORMAT_TIME'];
492 echo i18n::formatted_datetime($format, $this->currentComment['timestamp']);
497 * CommentActions::parse_timestamp()
498 * Parse templatevar timestamp
504 public function parse_timestamp()
506 echo $this->currentComment['timestamp'];
511 * CommentActions::parse_plugin()
512 * Executes a plugin templatevar
514 * @param string $pluginName name of plugin (without the NP_)
515 * @param extra parameters can be added
518 public function parse_plugin($pluginName)
522 // only continue when the plugin is really installed
523 if ( !$manager->pluginInstalled("NP_{$pluginName}") )
528 $plugin =& $manager->getPlugin("NP_{$pluginName}");
535 $params = func_get_args();
537 // remove plugin name
538 array_shift($params);
540 // pass info on current item and current comment as well
541 $params = array_merge(array(&$this->currentComment), $params);
542 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem), $params);
544 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
549 * CommentActions::parse_user()
550 * Parse templatevar user
552 * @param string $mode realname or else
555 public function parse_user($mode = '')
559 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
561 $member =& $manager->getMember($this->currentComment['memberid']);
562 echo $member->getRealName();
566 echo Entity::hsc($this->currentComment['user']);
572 * CommentActions::parse_useremail()
573 * Output mail address
578 public function parse_useremail() {
580 if ( $this->currentComment['memberid'] > 0 )
582 $member =& $manager->getMember($this->currentComment['memberid']);
584 if ( $member->email != '' )
591 if ( NOTIFICATION::address_validation($this->currentComment['email']) )
593 echo $this->currentComment['email'];
595 elseif ( NOTIFICATION::address_validation($this->currentComment['userid']) )
597 echo $this->currentComment['userid'];
604 * CommentActions::parse_userid()
605 * Parse templatevar userid
610 public function parse_userid()
612 echo $this->currentComment['userid'];
617 * CommentActions::parse_userlink()
618 * Parse templatevar userlink
623 public function parse_userlink()
625 if ( $this->currentComment['userlinkraw'] )
627 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
631 echo $this->currentComment['user'];
637 * CommentActions::parse_userlinkraw()
638 * Parse templatevar userlinkraw
643 public function parse_userlinkraw()
645 echo $this->currentComment['userlinkraw'];
650 * CommentActions::parse_userwebsite()
651 * Parse templatevar userwebsite
656 public function parse_userwebsite()
658 if ( !(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false) )
660 echo $this->currentComment['userlinkraw'];
666 * CommentActions::parse_userwebsitelink()
667 * Parse templatevar userwebsitelink
672 public function parse_userwebsitelink()
674 if ( !(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false) )
676 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
680 echo $this->currentComment['user'];
686 * CommentActions::checkCondition()
687 * Checks conditions for if statements
689 * @param string $field type of <%if%>
690 * @param string $name property of field
691 * @param string $value value of property
694 protected function checkCondition($field, $name='', $value = '') {
695 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
700 $condition = ($blog && $this->ifCategory($name,$value));
703 $condition = ($this->ifItemCategory($name,$value));
706 $condition = ($blog && ($blog->getSetting($name) == $value));
708 case 'itemblogsetting':
709 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
710 $condition = ($b && ($b->getSetting($name) == $value));
713 $condition = $member->isLoggedIn();
716 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
719 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
722 $condition = ($this->ifAuthor($name,$value));
725 $condition = $this->ifHasPlugin($name, $value);
728 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
735 * CommentActions::ifCategory()
736 * Different checks for a category
738 * @param string $key key of category
739 * @param string $value value for key of category
742 private function ifCategory($key = '', $value = '')
744 global $blog, $catid;
746 // when no parameter is defined, just check if a category is selected
747 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
749 return $blog->isValidCategory($catid);
752 // check category name
753 if ( $key == 'catname' )
755 $value = $blog->getCategoryIdFromName($value);
756 if ($value == $catid)
757 return $blog->isValidCategory($catid);
761 if ( ($key == 'catid') && ($value == $catid) )
763 return $blog->isValidCategory($catid);
769 * CommentActions::ifAuthor()
770 * Different checks for an author
772 * @param string $key key of data for author
773 * @param string $value value of data for author
774 * @return boolean correct or not
776 private function ifAuthor($key = '', $value = '')
778 global $member, $manager;
780 if ( $this->currentComment['memberid'] == 0 )
785 $mem =& $manager->getMember($this->currentComment['memberid']);
786 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
787 $citem =& $manager->getItem($this->currentComment['itemid'], 1, 1);
789 // when no parameter is defined, just check if item author is current visitor
790 if (($key != 'isadmin' && $key != 'name' && $key != 'isauthor' && $key != 'isonteam')) {
791 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
794 // check comment author name
795 if ( $key == 'name' )
797 $value = trim(strtolower($value));
802 if ( $value == strtolower($mem->getDisplayName()) )
808 // check if comment author is admin
809 if ( $key == 'isadmin' )
811 $blogid = intval($b->getID());
812 if ( $mem->isAdmin() )
816 return $mem->isBlogAdmin($blogid);
819 // check if comment author is item author
820 if ( $key == 'isauthor' )
822 return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
825 // check if comment author is on team
826 if ( $key == 'isonteam' )
828 return $mem->teamRights(intval($b->getID()));
834 * CommentActions::ifItemCategory()
835 * Different checks for a category
837 * @param string $key key of data for category to which item belongs
838 * @param string $value value of data for category to which item belongs
839 * @return boolean correct or not
841 private function ifItemCategory($key = '', $value = '')
843 global $catid, $manager;
845 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
846 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
847 $icatid = $citem['catid'];
849 // when no parameter is defined, just check if a category is selected
850 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
852 return $b->isValidCategory($icatid);
855 // check category name
856 if ( $key == 'catname' )
858 $value = $b->getCategoryIdFromName($value);
859 if ( $value == $icatid )
861 return $b->isValidCategory($icatid);
866 if ( ($key == 'catid') && ($value == $icatid) )
868 return $b->isValidCategory($icatid);
874 * CommentActions::ifOnTeam()
875 * Checks if a member is on the team of a blog and return his rights
877 * @param string $blogName name of weblog
878 * @return boolean correct or not
880 private function ifOnTeam($blogName = '')
882 global $blog, $member, $manager;
884 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
886 // when no blog found
887 if ( ($blogName == '') && (!is_object($b)) )
892 // explicit blog selection
893 if ( $blogName != '' )
895 $blogid = getBlogIDFromName($blogName);
899 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
901 $blogid = $b->getID();
904 return $member->teamRights($blogid);
908 * CommentActions::ifAdmin()
909 * Checks if a member is admin of a blog
911 * @param string $blogName name of weblog
912 * @return boolean correct or not
914 private function ifAdmin($blogName = '')
916 global $blog, $member, $manager;
918 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
920 // when no blog found
921 if ( ($blogName == '') && (!is_object($b)) )
926 // explicit blog selection
927 if ( $blogName != '' )
929 $blogid = getBlogIDFromName($blogName);
933 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
935 $blogid = $b->getID();
938 return $member->isBlogAdmin($blogid);
942 * CommentActions::ifHasPlugin()
944 * -> checks if plugin exists
945 * hasplugin,PlugName,OptionName
946 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
947 * hasplugin,PlugName,OptionName=value
948 * -> checks if the option OptionName from plugin PlugName is set to value
950 * @param string $name name of plugin
951 * @param string $value key (and value) of plugin option
952 * @return boolean correct or not
954 private function ifHasPlugin($name, $value)
959 // (pluginInstalled method won't write a message in the actionlog on failure)
960 if ( $manager->pluginInstalled('NP_'.$name) )
962 $plugin =& $manager->getPlugin("NP_{$name}");
963 if ( $plugin != NULL )
971 list($name2, $value2) = preg_split('#=#', $value, 2);
972 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
976 else if ( $plugin->getOption($name2) == $value2 )
987 * CommentActions::ifPlugin()
988 * Checks if a plugin exists and call its doIf function
990 * @param string $name name of plugin
991 * @param string $key key of plugin option
992 * @param string $value value of plugin option
993 * @return boolean callback output from plugin
995 private function ifPlugin($name, $key = '', $value = '')
999 $plugin =& $manager->getPlugin("NP_{$name}");
1005 $params = func_get_args();
1006 array_shift($params);
1008 return call_user_func_array(array(&$plugin, 'doIf'), $params);