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()
236 $blogid = getBlogIDFromItemID($this->commentsObj->itemid);
237 $blog =& $manager->getBlog($blogid);
239 $item =& $manager->getItem($this->commentsObj->itemid, 1, 1);
240 $blog =& $manager->getBlog($item['blogid']);
241 >>>>>>> skinnable-master
242 echo $blog->getURL();
247 * CommentActions::parse_body()
248 * Parse templatevar body
253 public function parse_body() {
254 echo $this->highlight($this->currentComment['body']);
259 * CommentActions::parse_commentcount()
260 * Parse templatevar commentcount
265 public function parse_commentcount()
267 echo $this->commentsObj->commentcount;
272 * CommentActions::parse_commentid()
273 * Parse templatevar commentid
278 public function parse_commentid()
280 echo $this->currentComment['commentid'];
285 * CommentActions::parse_commentword()
286 * Parse templatevar commentword
291 public function parse_commentword()
293 if ( $this->commentsObj->commentcount == 1 )
295 echo $this->template['COMMENTS_ONE'];
299 echo $this->template['COMMENTS_MANY'];
305 * CommentActions::parse_date()
306 * Parse templatevar date
308 * @format String $format Date format according to PHP
311 public function parse_date($format = '')
313 if ( $format !== '' )
318 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
324 $format = $this->template['FORMAT_DATE'];
327 $offset = $this->commentsObj->itemActions->blog->getTimeOffset() * 3600;
329 echo i18n::formatted_datetime($format, $this->currentComment['timestamp'], $offset);
334 * CommentActions::parse_excerpt()
335 * Parse templatevar email
340 public function parse_email()
342 $email = $this->currentComment['email'];
343 $email = str_replace('@', ' (at) ', $email);
344 $email = str_replace('.', ' (dot) ', $email);
350 * CommentActions::parse_excerpt()
351 * Parse templatevar excerpt
356 public function parse_excerpt()
358 echo Entity::hen(Entity::shorten($this->currentComment['body'], 60, '...'));
363 * CommentActions::parse_host()
364 * Parse templatevar host
369 public function parse_host()
371 echo $this->currentComment['host'];
376 * CommentActions::parse_ip()
377 * Parse templatevar ip
382 public function parse_ip()
384 echo $this->currentComment['ip'];
389 * CommentActions::parse_itemid()
390 * Parse templatevar itemid
395 public function parse_itemid()
397 echo $this->commentsObj->itemid;
402 * CommentActions::parse_itemlink()
403 * Parse templatevar itemlink
408 public function parse_itemlink()
411 'itemid' => $this->commentsObj->itemid,
412 'timestamp' => $this->commentsObj->itemActions->currentItem['timestamp'],
413 'title' => $this->commentsObj->itemActions->currentItem['title'],
414 'extra' => $this->commentsObj->itemActions->linkparams
417 echo Link::create_link('item', $data);
422 * CommentActions::parse_itemtitle()
423 * Parse templatevar itemtitle
425 * @param integer $maxLength maximum length for item title
428 public function parse_itemtitle($maxLength = 0)
430 if ( $maxLength == 0 )
432 $this->commentsObj->itemActions->parse_title();
436 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
442 * CommentActions::parse_memberid()
443 * Parse templatevar memberid
448 public function parse_memberid()
450 echo $this->currentComment['memberid'];
455 * CommentActions::parse_short()
456 * Parse templatevar short
461 public function parse_short()
463 $tmp = strtok($this->currentComment['body'], "\n");
464 $tmp = str_replace('<br />', '', $tmp);
466 if ( $tmp != $this->currentComment['body'] )
468 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
474 * CommentActions::parse_time()
475 * Parse templatevar time
477 * @param string $format datetime format referring to strftime() in PHP's built-in function
480 public function parse_time($format = '')
482 if ( $format !== '' )
487 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
493 $format = $this->template['FORMAT_TIME'];
496 echo i18n::formatted_datetime($format, $this->currentComment['timestamp']);
501 * CommentActions::parse_timestamp()
502 * Parse templatevar timestamp
508 public function parse_timestamp()
510 echo $this->currentComment['timestamp'];
515 * CommentActions::parse_plugin()
516 * Executes a plugin templatevar
518 * @param string $pluginName name of plugin (without the NP_)
519 * @param extra parameters can be added
522 public function parse_plugin($pluginName)
526 // only continue when the plugin is really installed
527 if ( !$manager->pluginInstalled("NP_{$pluginName}") )
532 $plugin =& $manager->getPlugin("NP_{$pluginName}");
539 $params = func_get_args();
541 // remove plugin name
542 array_shift($params);
544 // pass info on current item and current comment as well
546 $params = array_merge(array(&$this->currentComment), $params);
547 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem), $params);
549 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
551 $target = array(&$this->currentComment);
552 $params = array_merge($target, $params);
553 $target = array(&$this->commentsObj->itemActions->currentItem);
554 $params = array_merge($target, $params);
556 call_user_func_array(array($plugin,'doTemplateCommentsVar'), $params);
557 >>>>>>> skinnable-master
562 * CommentActions::parse_user()
563 * Parse templatevar user
565 * @param string $mode realname or else
568 public function parse_user($mode = '')
572 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
574 $member =& $manager->getMember($this->currentComment['memberid']);
575 echo $member->getRealName();
579 echo Entity::hsc($this->currentComment['user']);
585 * CommentActions::parse_useremail()
586 * Output mail address
591 public function parse_useremail() {
593 if ( $this->currentComment['memberid'] > 0 )
595 $member =& $manager->getMember($this->currentComment['memberid']);
597 if ( $member->email != '' )
604 if ( NOTIFICATION::address_validation($this->currentComment['email']) )
606 echo $this->currentComment['email'];
608 elseif ( NOTIFICATION::address_validation($this->currentComment['userid']) )
610 echo $this->currentComment['userid'];
617 * CommentActions::parse_userid()
618 * Parse templatevar userid
623 public function parse_userid()
625 echo $this->currentComment['userid'];
630 * CommentActions::parse_userlink()
631 * Parse templatevar userlink
636 public function parse_userlink()
638 if ( $this->currentComment['userlinkraw'] )
640 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
644 echo $this->currentComment['user'];
650 * CommentActions::parse_userlinkraw()
651 * Parse templatevar userlinkraw
656 public function parse_userlinkraw()
659 echo $this->currentComment['userlinkraw'];
661 echo (array_key_exists('userlinkraw', $this->currentComment) && !empty($this->currentComment['userlinkraw'])) ? $this->currentComment['userlinkraw'] : '';
662 >>>>>>> skinnable-master
667 * CommentActions::parse_userwebsite()
668 * Parse templatevar userwebsite
673 public function parse_userwebsite()
675 if ( !(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false) )
677 echo $this->currentComment['userlinkraw'];
683 * CommentActions::parse_userwebsitelink()
684 * Parse templatevar userwebsitelink
689 public function parse_userwebsitelink()
691 if ( !(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false) )
693 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
697 echo $this->currentComment['user'];
703 * CommentActions::checkCondition()
704 * Checks conditions for if statements
706 * @param string $field type of <%if%>
707 * @param string $name property of field
708 * @param string $value value of property
711 protected function checkCondition($field, $name='', $value = '') {
712 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
717 $condition = ($blog && $this->ifCategory($name,$value));
720 $condition = ($this->ifItemCategory($name,$value));
723 $condition = ($blog && ($blog->getSetting($name) == $value));
725 case 'itemblogsetting':
727 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
728 $condition = ($b && ($b->getSetting($name) == $value));
730 $item =& $manager->getItem($this->currentComment['itemid'], 1, 1);
731 $blog =& $manager->getBlog($item['blogid']);
732 $condition = ($blog && ($blog->getSetting($name) == $value));
733 >>>>>>> skinnable-master
736 $condition = $member->isLoggedIn();
739 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
742 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
745 $condition = ($this->ifAuthor($name,$value));
748 $condition = $this->ifHasPlugin($name, $value);
751 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
758 * CommentActions::ifCategory()
759 * Different checks for a category
761 * @param string $key key of category
762 * @param string $value value for key of category
765 private function ifCategory($key = '', $value = '')
767 global $blog, $catid;
769 // when no parameter is defined, just check if a category is selected
770 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
772 return $blog->isValidCategory($catid);
775 // check category name
776 if ( $key == 'catname' )
778 $value = $blog->getCategoryIdFromName($value);
779 if ($value == $catid)
780 return $blog->isValidCategory($catid);
784 if ( ($key == 'catid') && ($value == $catid) )
786 return $blog->isValidCategory($catid);
792 * CommentActions::ifAuthor()
793 * Different checks for an author
795 * @param string $key key of data for author
796 * @param string $value value of data for author
797 * @return boolean correct or not
799 private function ifAuthor($key = '', $value = '')
801 global $member, $manager;
803 if ( $this->currentComment['memberid'] == 0 )
809 $mem =& $manager->getMember($this->currentComment['memberid']);
810 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
811 $citem =& $manager->getItem($this->currentComment['itemid'], 1, 1);
813 // when no parameter is defined, just check if item author is current visitor
814 if (($key != 'isadmin' && $key != 'name' && $key != 'isauthor' && $key != 'isonteam')) {
815 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
817 $member =& $manager->getMember($this->currentComment['memberid']);
818 $item =& $manager->getItem($this->currentComment['itemid'], 1, 1);
820 // when no parameter is defined, just check if item author is current visitor
821 if ( ($key != 'isadmin' && $key != 'name' && $key != 'isauthor' && $key != 'isonteam') )
823 return (intval($memberber->getID()) > 0 && intval($memberber->getID()) == (integer) $item['authorid']);
824 >>>>>>> skinnable-master
827 // check comment author name
828 if ( $key == 'name' )
830 $value = trim(strtolower($value));
836 if ( $value == strtolower($mem->getDisplayName()) )
838 if ( $value == strtolower($member->getDisplayName()) )
839 >>>>>>> skinnable-master
845 // check if comment author is admin
846 if ( $key == 'isadmin' )
849 $blogid = intval($b->getID());
850 if ( $mem->isAdmin() )
854 return $mem->isBlogAdmin($blogid);
856 if ( $member->isAdmin() )
860 return $member->isBlogAdmin($item['blogid']);
861 >>>>>>> skinnable-master
864 // check if comment author is item author
865 if ( $key == 'isauthor' )
868 return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
870 return ((integer) $item['authorid'] == (integer) $this->currentComment['memberid']);
871 >>>>>>> skinnable-master
874 // check if comment author is on team
875 if ( $key == 'isonteam' )
878 return $mem->teamRights(intval($b->getID()));
880 return $member->teamRights((integer) $item['blogid']);
881 >>>>>>> skinnable-master
887 * CommentActions::ifItemCategory()
888 * Different checks for a category
890 * @param string $key key of data for category to which item belongs
891 * @param string $value value of data for category to which item belongs
892 * @return boolean correct or not
894 private function ifItemCategory($key = '', $value = '')
896 global $catid, $manager;
899 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
900 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
901 $icatid = $citem['catid'];
903 // when no parameter is defined, just check if a category is selected
904 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
906 return $b->isValidCategory($icatid);
909 // check category name
910 if ( $key == 'catname' )
912 $value = $b->getCategoryIdFromName($value);
913 if ( $value == $icatid )
915 return $b->isValidCategory($icatid);
920 if ( ($key == 'catid') && ($value == $icatid) )
922 return $b->isValidCategory($icatid);
925 $item =& $manager->getItem($this->currentComment['itemid'],1,1);
926 $blog =& $manager->getBlog($item['blogid']);
928 // when no parameter is defined, just check if a category is selected
929 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
931 return $blog->isValidCategory($item['catid']);
934 // check category name
935 if ( $key == 'catname' )
937 $value = $blog->getCategoryIdFromName($value);
938 if ( $value == $item['catid'] )
940 return $blog->isValidCategory($item['catid']);
945 if ( ($key == 'catid') && ($value == $item['catid']) )
947 return $blog->isValidCategory($item['catid']);
948 >>>>>>> skinnable-master
954 * CommentActions::ifOnTeam()
955 * Checks if a member is on the team of a blog and return his rights
957 * @param string $blogName name of weblog
958 * @return boolean correct or not
960 private function ifOnTeam($blogName = '')
963 global $blog, $member, $manager;
965 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
967 // when no blog found
968 if ( ($blogName == '') && (!is_object($b)) )
970 global $member, $manager;
972 $item =& $manager->getItem($this->currentComment['itemid'], 1, 1);
973 $blog =& $manager->getBlog($item['blogid']);
975 // when no blog found
976 if ( ($blogName == '') && !is_object($blog) )
977 >>>>>>> skinnable-master
982 // explicit blog selection
983 if ( $blogName != '' )
985 $blogid = getBlogIDFromName($blogName);
989 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
992 $blogid = $b->getID();
994 $blogid = $blog->getID();
995 >>>>>>> skinnable-master
998 return $member->teamRights($blogid);
1002 * CommentActions::ifAdmin()
1003 * Checks if a member is admin of a blog
1005 * @param string $blogName name of weblog
1006 * @return boolean correct or not
1008 private function ifAdmin($blogName = '')
1011 global $blog, $member, $manager;
1013 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
1015 // when no blog found
1016 if ( ($blogName == '') && (!is_object($b)) )
1018 global $member, $manager;
1020 $item =& $manager->getItem($this->currentComment['itemid'], 1, 1);
1021 $blog =& $manager->getBlog($item['blogid']);
1023 // when no blog found
1024 if ( ($blogName == '') && !is_object($blog) )
1025 >>>>>>> skinnable-master
1030 // explicit blog selection
1031 if ( $blogName != '' )
1033 $blogid = getBlogIDFromName($blogName);
1037 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
1040 $blogid = $b->getID();
1042 $blogid = $blog->getID();
1043 >>>>>>> skinnable-master
1046 return $member->isBlogAdmin($blogid);
1050 * CommentActions::ifHasPlugin()
1051 * hasplugin,PlugName
1052 * -> checks if plugin exists
1053 * hasplugin,PlugName,OptionName
1054 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
1055 * hasplugin,PlugName,OptionName=value
1056 * -> checks if the option OptionName from plugin PlugName is set to value
1058 * @param string $name name of plugin
1059 * @param string $value key (and value) of plugin option
1060 * @return boolean correct or not
1062 private function ifHasPlugin($name, $value)
1067 // (pluginInstalled method won't write a message in the actionlog on failure)
1068 if ( $manager->pluginInstalled('NP_'.$name) )
1070 $plugin =& $manager->getPlugin("NP_{$name}");
1071 if ( $plugin != NULL )
1079 list($name2, $value2) = preg_split('#=#', $value, 2);
1080 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
1084 else if ( $plugin->getOption($name2) == $value2 )
1095 * CommentActions::ifPlugin()
1096 * Checks if a plugin exists and call its doIf function
1098 * @param string $name name of plugin
1099 * @param string $key key of plugin option
1100 * @param string $value value of plugin option
1101 * @return boolean callback output from plugin
1103 private function ifPlugin($name, $key = '', $value = '')
1107 $plugin =& $manager->getPlugin("NP_{$name}");
1113 $params = func_get_args();
1114 array_shift($params);
1117 return call_user_func_array(array(&$plugin, 'doIf'), $params);
1119 return call_user_func_array(array($plugin, 'doIf'), $params);
1120 >>>>>>> skinnable-master