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::getAvailableActions()
94 * @return array actions array
96 public function getAvailableActions()
98 return array_merge(self::$defined_actions, parent::getAvailableActions());
103 * CommentActions::setCommentsObj()
105 * @param object $commentsObj instance of Comments class
108 public function setCommentsObj(&$commentsObj)
110 $this->commentsObj =& $commentsObj;
115 * CommentActions::setTemplate()
117 * @param array $template array includes templates
120 public function setTemplate($template)
122 $this->template =& $template;
127 * CommentActions::setCurrentComment()
128 * Set $currentcommentid and $currentcommentarray
130 * @param array $comment associated array includes comment information
133 public function setCurrentComment(&$comment)
135 global $currentcommentid, $currentcommentarray, $manager;
137 if ( $comment['memberid'] != 0 )
139 if ( !array_key_exists('COMMENTS_AUTH', $this->template) )
141 $comment['authtext'] = '';
145 $comment['authtext'] = $this->template['COMMENTS_AUTH'];
148 $mem =& $manager->getMember($comment['memberid']);
149 $comment['user'] = $mem->getDisplayName();
151 if ( $mem->getURL() )
153 $comment['userid'] = $mem->getURL();
157 $comment['userid'] = $mem->getEmail();
161 'memberid' => $comment['memberid'],
162 'name' => $mem->getDisplayName(),
163 'extra' => $this->commentsObj->itemActions->linkparams
166 $comment['userlinkraw'] = Link::create_link('member', $data);
170 // create smart links
171 if ( !array_key_exists('userid', $comment) || !empty($comment['userid']) )
173 if ( (i18n::strpos($comment['userid'], 'http://') === 0) || (i18n::strpos($comment['userid'], 'https://') === 0) )
175 $comment['userlinkraw'] = $comment['userid'];
179 $comment['userlinkraw'] = 'http://' . $comment['userid'];
182 else if ( NOTIFICATION::address_validation($comment['email']) )
184 $comment['userlinkraw'] = 'mailto:' . $comment['email'];
186 else if ( NOTIFICATION::address_validation($comment['userid']) )
188 $comment['userlinkraw'] = 'mailto:' . $comment['userid'];
192 $this->currentComment =& $comment;
193 $currentcommentid = $comment['commentid'];
194 $currentcommentarray = $comment;
199 * CommentActions::parse_authtext()
200 * Parse templatevar authtext
205 public function parse_authtext()
207 if ( $this->currentComment['memberid'] != 0 )
209 $this->parser->parse($this->template['COMMENTS_AUTH']);
215 * CommentActions::parse_blogid()
216 * Parse templatevar blogid
221 public function parse_blogid() {
222 echo $this->currentComment['blogid'];
226 * CommentActions::parse_blogurl()
227 * Parse templatevar blogurl
232 public function parse_blogurl()
235 $blogid = getBlogIDFromItemID($this->commentsObj->itemid);
236 $blog =& $manager->getBlog($blogid);
237 echo $blog->getURL();
242 * CommentActions::parse_body()
243 * Parse templatevar body
248 public function parse_body() {
249 echo $this->highlight($this->currentComment['body']);
254 * CommentActions::parse_commentcount()
255 * Parse templatevar commentcount
260 public function parse_commentcount()
262 echo $this->commentsObj->commentcount;
267 * CommentActions::parse_commentid()
268 * Parse templatevar commentid
273 public function parse_commentid()
275 echo $this->currentComment['commentid'];
280 * CommentActions::parse_commentword()
281 * Parse templatevar commentword
286 public function parse_commentword()
288 if ( $this->commentsObj->commentcount == 1 )
290 echo $this->template['COMMENTS_ONE'];
294 echo $this->template['COMMENTS_MANY'];
300 * CommentActions::parse_date()
301 * Parse templatevar date
303 * @format String $format Date format according to PHP
306 public function parse_date($format = '')
308 if ( $format !== '' )
313 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
319 $format = $this->template['FORMAT_DATE'];
322 $offset = $this->commentsObj->itemActions->blog->getTimeOffset() * 3600;
324 echo i18n::formatted_datetime($format, $this->currentComment['timestamp'], $offset);
329 * CommentActions::parse_excerpt()
330 * Parse templatevar email
335 public function parse_email()
337 $email = $this->currentComment['email'];
338 $email = str_replace('@', ' (at) ', $email);
339 $email = str_replace('.', ' (dot) ', $email);
345 * CommentActions::parse_excerpt()
346 * Parse templatevar excerpt
351 public function parse_excerpt()
353 echo Entity::hen(Entity::shorten($this->currentComment['body'], 60, '...'));
358 * CommentActions::parse_host()
359 * Parse templatevar host
364 public function parse_host()
366 echo $this->currentComment['host'];
371 * CommentActions::parse_ip()
372 * Parse templatevar ip
377 public function parse_ip()
379 echo $this->currentComment['ip'];
384 * CommentActions::parse_itemid()
385 * Parse templatevar itemid
390 public function parse_itemid()
392 echo $this->commentsObj->itemid;
397 * CommentActions::parse_itemlink()
398 * Parse templatevar itemlink
403 public function parse_itemlink()
406 'itemid' => $this->commentsObj->itemid,
407 'timestamp' => $this->commentsObj->itemActions->currentItem['timestamp'],
408 'title' => $this->commentsObj->itemActions->currentItem['title'],
409 'extra' => $this->commentsObj->itemActions->linkparams
412 echo Link::create_link('item', $data);
417 * CommentActions::parse_itemtitle()
418 * Parse templatevar itemtitle
420 * @param integer $maxLength maximum length for item title
423 public function parse_itemtitle($maxLength = 0)
425 if ( $maxLength == 0 )
427 $this->commentsObj->itemActions->parse_title();
431 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
437 * CommentActions::parse_memberid()
438 * Parse templatevar memberid
443 public function parse_memberid()
445 echo $this->currentComment['memberid'];
450 * CommentActions::parse_short()
451 * Parse templatevar short
456 public function parse_short()
458 $tmp = strtok($this->currentComment['body'], "\n");
459 $tmp = str_replace('<br />', '', $tmp);
461 if ( $tmp != $this->currentComment['body'] )
463 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
469 * CommentActions::parse_time()
470 * Parse templatevar time
472 * @param string $format datetime format referring to strftime() in PHP's built-in function
475 public function parse_time($format = '')
477 if ( $format !== '' )
482 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
488 $format = $this->template['FORMAT_TIME'];
491 echo i18n::formatted_datetime($format, $this->currentComment['timestamp']);
496 * CommentActions::parse_timestamp()
497 * Parse templatevar timestamp
503 public function parse_timestamp()
505 echo $this->currentComment['timestamp'];
510 * CommentActions::parse_plugin()
511 * Executes a plugin templatevar
513 * @param string $pluginName name of plugin (without the NP_)
514 * @param extra parameters can be added
517 public function parse_plugin($pluginName)
521 // only continue when the plugin is really installed
522 if ( !$manager->pluginInstalled("NP_{$pluginName}") )
527 $plugin =& $manager->getPlugin("NP_{$pluginName}");
534 $params = func_get_args();
536 // remove plugin name
537 array_shift($params);
539 // pass info on current item and current comment as well
540 $params = array_merge(array(&$this->currentComment), $params);
541 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem), $params);
543 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
548 * CommentActions::parse_user()
549 * Parse templatevar user
551 * @param string $mode realname or else
554 public function parse_user($mode = '')
558 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
560 $member =& $manager->getMember($this->currentComment['memberid']);
561 echo $member->getRealName();
565 echo Entity::hsc($this->currentComment['user']);
571 * CommentActions::parse_useremail()
572 * Output mail address
577 public function parse_useremail() {
579 if ( $this->currentComment['memberid'] > 0 )
581 $member =& $manager->getMember($this->currentComment['memberid']);
583 if ( $member->email != '' )
590 if ( NOTIFICATION::address_validation($this->currentComment['email']) )
592 echo $this->currentComment['email'];
594 elseif ( NOTIFICATION::address_validation($this->currentComment['userid']) )
596 echo $this->currentComment['userid'];
603 * CommentActions::parse_userid()
604 * Parse templatevar userid
609 public function parse_userid()
611 echo $this->currentComment['userid'];
616 * CommentActions::parse_userlink()
617 * Parse templatevar userlink
622 public function parse_userlink()
624 if ( $this->currentComment['userlinkraw'] )
626 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
630 echo $this->currentComment['user'];
636 * CommentActions::parse_userlinkraw()
637 * Parse templatevar userlinkraw
642 public function parse_userlinkraw()
644 echo $this->currentComment['userlinkraw'];
649 * CommentActions::parse_userwebsite()
650 * Parse templatevar userwebsite
655 public function parse_userwebsite()
657 if ( !(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false) )
659 echo $this->currentComment['userlinkraw'];
665 * CommentActions::parse_userwebsitelink()
666 * Parse templatevar userwebsitelink
671 public function parse_userwebsitelink()
673 if ( !(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false) )
675 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
679 echo $this->currentComment['user'];
685 * CommentActions::checkCondition()
686 * Checks conditions for if statements
688 * @param string $field type of <%if%>
689 * @param string $name property of field
690 * @param string $value value of property
693 protected function checkCondition($field, $name='', $value = '') {
694 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
699 $condition = ($blog && $this->ifCategory($name,$value));
702 $condition = ($this->ifItemCategory($name,$value));
705 $condition = ($blog && ($blog->getSetting($name) == $value));
707 case 'itemblogsetting':
708 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
709 $condition = ($b && ($b->getSetting($name) == $value));
712 $condition = $member->isLoggedIn();
715 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
718 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
721 $condition = ($this->ifAuthor($name,$value));
724 $condition = $this->ifHasPlugin($name, $value);
727 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
734 * CommentActions::ifCategory()
735 * Different checks for a category
737 * @param string $key key of category
738 * @param string $value value for key of category
741 private function ifCategory($key = '', $value = '')
743 global $blog, $catid;
745 // when no parameter is defined, just check if a category is selected
746 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
748 return $blog->isValidCategory($catid);
751 // check category name
752 if ( $key == 'catname' )
754 $value = $blog->getCategoryIdFromName($value);
755 if ($value == $catid)
756 return $blog->isValidCategory($catid);
760 if ( ($key == 'catid') && ($value == $catid) )
762 return $blog->isValidCategory($catid);
768 * CommentActions::ifAuthor()
769 * Different checks for an author
771 * @param string $key key of data for author
772 * @param string $value value of data for author
773 * @return boolean correct or not
775 private function ifAuthor($key = '', $value = '')
777 global $member, $manager;
779 if ( $this->currentComment['memberid'] == 0 )
784 $mem =& $manager->getMember($this->currentComment['memberid']);
785 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
786 $citem =& $manager->getItem($this->currentComment['itemid'], 1, 1);
788 // when no parameter is defined, just check if item author is current visitor
789 if (($key != 'isadmin' && $key != 'name' && $key != 'isauthor' && $key != 'isonteam')) {
790 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
793 // check comment author name
794 if ( $key == 'name' )
796 $value = trim(strtolower($value));
801 if ( $value == strtolower($mem->getDisplayName()) )
807 // check if comment author is admin
808 if ( $key == 'isadmin' )
810 $blogid = intval($b->getID());
811 if ( $mem->isAdmin() )
815 return $mem->isBlogAdmin($blogid);
818 // check if comment author is item author
819 if ( $key == 'isauthor' )
821 return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
824 // check if comment author is on team
825 if ( $key == 'isonteam' )
827 return $mem->teamRights(intval($b->getID()));
833 * CommentActions::ifItemCategory()
834 * Different checks for a category
836 * @param string $key key of data for category to which item belongs
837 * @param string $value value of data for category to which item belongs
838 * @return boolean correct or not
840 private function ifItemCategory($key = '', $value = '')
842 global $catid, $manager;
844 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
845 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
846 $icatid = $citem['catid'];
848 // when no parameter is defined, just check if a category is selected
849 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
851 return $b->isValidCategory($icatid);
854 // check category name
855 if ( $key == 'catname' )
857 $value = $b->getCategoryIdFromName($value);
858 if ( $value == $icatid )
860 return $b->isValidCategory($icatid);
865 if ( ($key == 'catid') && ($value == $icatid) )
867 return $b->isValidCategory($icatid);
873 * CommentActions::ifOnTeam()
874 * Checks if a member is on the team of a blog and return his rights
876 * @param string $blogName name of weblog
877 * @return boolean correct or not
879 private function ifOnTeam($blogName = '')
881 global $blog, $member, $manager;
883 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
885 // when no blog found
886 if ( ($blogName == '') && (!is_object($b)) )
891 // explicit blog selection
892 if ( $blogName != '' )
894 $blogid = getBlogIDFromName($blogName);
898 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
900 $blogid = $b->getID();
903 return $member->teamRights($blogid);
907 * CommentActions::ifAdmin()
908 * Checks if a member is admin of a blog
910 * @param string $blogName name of weblog
911 * @return boolean correct or not
913 private function ifAdmin($blogName = '')
915 global $blog, $member, $manager;
917 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
919 // when no blog found
920 if ( ($blogName == '') && (!is_object($b)) )
925 // explicit blog selection
926 if ( $blogName != '' )
928 $blogid = getBlogIDFromName($blogName);
932 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
934 $blogid = $b->getID();
937 return $member->isBlogAdmin($blogid);
941 * CommentActions::ifHasPlugin()
943 * -> checks if plugin exists
944 * hasplugin,PlugName,OptionName
945 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
946 * hasplugin,PlugName,OptionName=value
947 * -> checks if the option OptionName from plugin PlugName is set to value
949 * @param string $name name of plugin
950 * @param string $value key (and value) of plugin option
951 * @return boolean correct or not
953 private function ifHasPlugin($name, $value)
958 // (pluginInstalled method won't write a message in the actionlog on failure)
959 if ( $manager->pluginInstalled('NP_'.$name) )
961 $plugin =& $manager->getPlugin("NP_{$name}");
962 if ( $plugin != NULL )
970 list($name2, $value2) = preg_split('#=#', $value, 2);
971 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
975 else if ( $plugin->getOption($name2) == $value2 )
986 * CommentActions::ifPlugin()
987 * Checks if a plugin exists and call its doIf function
989 * @param string $name name of plugin
990 * @param string $key key of plugin option
991 * @param string $value value of plugin option
992 * @return boolean callback output from plugin
994 private function ifPlugin($name, $key = '', $value = '')
998 $plugin =& $manager->getPlugin("NP_{$name}");
1004 $params = func_get_args();
1005 array_shift($params);
1007 return call_user_func_array(array(&$plugin, 'doIf'), $params);