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(
86 * CommentActions::__construct()
88 * @param object $comments instance of Comments class
91 public function __construct(&$comments)
93 // call constructor of superclass first
96 // reference to the comments object
97 $this->setCommentsObj($comments);
102 * CommentActions::getDefinedActions()
105 * @return array actions array
107 public function getDefinedActions()
109 return self::$defined_actions;
113 * CommentActions::setParser()
115 * @param object $parser instance of Parser class
118 public function setParser(&$parser)
120 $this->parser =& $parser;
126 * CommentActions::setCommentsObj()
128 * @param object $commentsObj instance of Comments class
131 public function setCommentsObj(&$commentsObj)
133 $this->commentsObj =& $commentsObj;
138 * CommentActions::setTemplate()
140 * @param array $template array includes templates
143 public function setTemplate($template)
145 $this->template =& $template;
150 * CommentActions::setCurrentComment()
151 * Set $currentcommentid and $currentcommentarray
153 * @param array $comment associated array includes comment information
156 public function setCurrentComment(&$comment)
158 global $currentcommentid, $currentcommentarray, $manager;
160 if ( $comment['memberid'] != 0 )
162 if ( !array_key_exists('COMMENTS_AUTH', $this->template) )
164 $comment['authtext'] = '';
168 $comment['authtext'] = $this->template['COMMENTS_AUTH'];
171 $mem =& $manager->getMember($comment['memberid']);
172 $comment['user'] = $mem->getDisplayName();
174 if ( $mem->getURL() )
176 $comment['userid'] = $mem->getURL();
180 $comment['userid'] = $mem->getEmail();
184 'memberid' => $comment['memberid'],
185 'name' => $mem->getDisplayName(),
186 'extra' => $this->commentsObj->itemActions->linkparams
189 $comment['userlinkraw'] = Link::create_link('member', $data);
193 // create smart links
194 if ( !array_key_exists('userid', $comment) || !empty($comment['userid']) )
196 if ( (i18n::strpos($comment['userid'], 'http://') === 0) || (i18n::strpos($comment['userid'], 'https://') === 0) )
198 $comment['userlinkraw'] = $comment['userid'];
202 $comment['userlinkraw'] = 'http://' . $comment['userid'];
205 else if ( NOTIFICATION::address_validation($comment['email']) )
207 $comment['userlinkraw'] = 'mailto:' . $comment['email'];
209 else if ( NOTIFICATION::address_validation($comment['userid']) )
211 $comment['userlinkraw'] = 'mailto:' . $comment['userid'];
215 $this->currentComment =& $comment;
216 $currentcommentid = $comment['commentid'];
217 $currentcommentarray = $comment;
222 * CommentActions::parse_authtext()
223 * Parse templatevar authtext
228 public function parse_authtext()
230 if ( $this->currentComment['memberid'] != 0 )
232 $this->parser->parse($this->template['COMMENTS_AUTH']);
238 * CommentActions::parse_blogid()
239 * Parse templatevar blogid
244 public function parse_blogid() {
245 echo $this->currentComment['blogid'];
249 * CommentActions::parse_blogurl()
250 * Parse templatevar blogurl
255 public function parse_blogurl()
258 $blogid = getBlogIDFromItemID($this->commentsObj->itemid);
259 $blog =& $manager->getBlog($blogid);
260 echo $blog->getURL();
265 * CommentActions::parse_body()
266 * Parse templatevar body
271 public function parse_body() {
272 echo $this->highlight($this->currentComment['body']);
277 * CommentActions::parse_commentcount()
278 * Parse templatevar commentcount
283 public function parse_commentcount()
285 echo $this->commentsObj->commentcount;
290 * CommentActions::parse_commentid()
291 * Parse templatevar commentid
296 public function parse_commentid()
298 echo $this->currentComment['commentid'];
303 * CommentActions::parse_commentword()
304 * Parse templatevar commentword
309 public function parse_commentword()
311 if ( $this->commentsObj->commentcount == 1 )
313 echo $this->template['COMMENTS_ONE'];
317 echo $this->template['COMMENTS_MANY'];
323 * CommentActions::parse_date()
324 * Parse templatevar date
326 * @format String $format Date format according to PHP
329 public function parse_date($format = '')
331 if ( $format !== '' )
336 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
342 $format = $this->template['FORMAT_DATE'];
345 $offset = $this->commentsObj->itemActions->blog->getTimeOffset() * 3600;
347 echo i18n::formatted_datetime($format, $this->currentComment['timestamp'], $offset);
352 * CommentActions::parse_excerpt()
353 * Parse templatevar email
358 public function parse_email()
360 $email = $this->currentComment['email'];
361 $email = str_replace('@', ' (at) ', $email);
362 $email = str_replace('.', ' (dot) ', $email);
368 * CommentActions::parse_excerpt()
369 * Parse templatevar excerpt
374 public function parse_excerpt()
376 echo Entity::hen(Entity::shorten($this->currentComment['body'], 60, '...'));
381 * CommentActions::parse_host()
382 * Parse templatevar host
387 public function parse_host()
389 echo $this->currentComment['host'];
394 * CommentActions::parse_ip()
395 * Parse templatevar ip
400 public function parse_ip()
402 echo $this->currentComment['ip'];
407 * CommentActions::parse_itemid()
408 * Parse templatevar itemid
413 public function parse_itemid()
415 echo $this->commentsObj->itemid;
420 * CommentActions::parse_itemlink()
421 * Parse templatevar itemlink
426 public function parse_itemlink()
429 'itemid' => $this->commentsObj->itemid,
430 'timestamp' => $this->commentsObj->itemActions->currentItem->timestamp,
431 'title' => $this->commentsObj->itemActions->currentItem->title,
432 'extra' => $this->commentsObj->itemActions->linkparams
435 echo Link::create_link('item', $data);
440 * CommentActions::parse_itemtitle()
441 * Parse templatevar itemtitle
443 * @param integer $maxLength maximum length for item title
446 public function parse_itemtitle($maxLength = 0)
448 if ( $maxLength == 0 )
450 $this->commentsObj->itemActions->parse_title();
454 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
460 * CommentActions::parse_memberid()
461 * Parse templatevar memberid
466 public function parse_memberid()
468 echo $this->currentComment['memberid'];
473 * CommentActions::parse_short()
474 * Parse templatevar short
479 public function parse_short()
481 $tmp = strtok($this->currentComment['body'], "\n");
482 $tmp = str_replace('<br />', '', $tmp);
484 if ( $tmp != $this->currentComment['body'] )
486 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
492 * CommentActions::parse_time()
493 * Parse templatevar time
495 * @param string $format datetime format referring to strftime() in PHP's built-in function
498 public function parse_time($format = '')
500 if ( $format === '' )
505 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
511 $format = $this->template['FORMAT_TIME'];
514 echo i18n::formatted_datetime($format, $this->currentComment['timestamp']);
519 * CommentActions::parse_timestamp()
520 * Parse templatevar timestamp
526 public function parse_timestamp()
528 echo $this->currentComment['timestamp'];
533 * CommentActions::parse_plugin()
534 * Executes a plugin templatevar
536 * @param string $pluginName name of plugin (without the NP_)
537 * @param extra parameters can be added
540 public function parse_plugin($pluginName)
544 // only continue when the plugin is really installed
545 if ( !$manager->pluginInstalled("NP_{$pluginName}") )
550 $plugin =& $manager->getPlugin("NP_{$pluginName}");
557 $params = func_get_args();
559 // remove plugin name
560 array_shift($params);
562 // pass info on current item and current comment as well
563 $params = array_merge(array(&$this->currentComment), $params);
564 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem), $params);
566 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
571 * CommentActions::parse_user()
572 * Parse templatevar user
574 * @param string $mode realname or else
577 public function parse_user($mode = '')
581 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
583 $member =& $manager->getMember($this->currentComment['memberid']);
584 echo $member->getRealName();
588 echo Entity::hsc($this->currentComment['user']);
594 * CommentActions::parse_useremail()
595 * Output mail address
600 public function parse_useremail() {
602 if ( $this->currentComment['memberid'] > 0 )
604 $member =& $manager->getMember($this->currentComment['memberid']);
606 if ( $member->email != '' )
613 if ( NOTIFICATION::address_validation($this->currentComment['email']) )
615 echo $this->currentComment['email'];
617 elseif ( NOTIFICATION::address_validation($this->currentComment['userid']) )
619 echo $this->currentComment['userid'];
626 * CommentActions::parse_userid()
627 * Parse templatevar userid
632 public function parse_userid()
634 echo $this->currentComment['userid'];
639 * CommentActions::parse_userlink()
640 * Parse templatevar userlink
645 public function parse_userlink()
647 if ( $this->currentComment['userlinkraw'] )
649 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
653 echo $this->currentComment['user'];
659 * CommentActions::parse_userlinkraw()
660 * Parse templatevar userlinkraw
665 public function parse_userlinkraw()
667 echo $this->currentComment['userlinkraw'];
672 * CommentActions::parse_userwebsite()
673 * Parse templatevar userwebsite
678 public function parse_userwebsite()
680 if ( !(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false) )
682 echo $this->currentComment['userlinkraw'];
688 * CommentActions::parse_userwebsitelink()
689 * Parse templatevar userwebsitelink
694 public function parse_userwebsitelink()
696 if ( !(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false) )
698 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
702 echo $this->currentComment['user'];
708 * ItemActions::checkCondition()
709 * Checks conditions for if statements
711 * @param string $field type of <%if%>
712 * @param string $name property of field
713 * @param string $value value of property
716 private function checkCondition($field, $name='', $value = '') {
717 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
722 $condition = ($blog && $this->ifCategory($name,$value));
725 $condition = ($this->ifItemCategory($name,$value));
728 $condition = ($blog && ($blog->getSetting($name) == $value));
730 case 'itemblogsetting':
731 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
732 $condition = ($b && ($b->getSetting($name) == $value));
735 $condition = $member->isLoggedIn();
738 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
741 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
744 $condition = ($this->ifAuthor($name,$value));
747 $condition = $this->ifHasPlugin($name, $value);
750 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
757 * CommentActions::ifCategory()
758 * Different checks for a category
760 * @param string $key key of category
761 * @param string $value value for key of category
764 private function ifCategory($key = '', $value = '')
766 global $blog, $catid;
768 // when no parameter is defined, just check if a category is selected
769 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
771 return $blog->isValidCategory($catid);
774 // check category name
775 if ( $key == 'catname' )
777 $value = $blog->getCategoryIdFromName($value);
778 if ($value == $catid)
779 return $blog->isValidCategory($catid);
783 if ( ($key == 'catid') && ($value == $catid) )
785 return $blog->isValidCategory($catid);
791 * CommentActions::ifAuthor()
792 * Different checks for an author
794 * @param string $key key of data for author
795 * @param string $value value of data for author
796 * @return boolean correct or not
798 private function ifAuthor($key = '', $value = '')
800 global $member, $manager;
802 if ( $this->currentComment['memberid'] == 0 )
807 $mem =& $manager->getMember($this->currentComment['memberid']);
808 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
809 $citem =& $manager->getItem($this->currentComment['itemid'], 1, 1);
811 // when no parameter is defined, just check if item author is current visitor
812 if (($key != 'isadmin' && $key != 'name' && $key != 'isauthor' && $key != 'isonteam')) {
813 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
816 // check comment author name
817 if ( $key == 'name' )
819 $value = trim(strtolower($value));
824 if ( $value == strtolower($mem->getDisplayName()) )
830 // check if comment author is admin
831 if ( $key == 'isadmin' )
833 $blogid = intval($b->getID());
834 if ( $mem->isAdmin() )
838 return $mem->isBlogAdmin($blogid);
841 // check if comment author is item author
842 if ( $key == 'isauthor' )
844 return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
847 // check if comment author is on team
848 if ( $key == 'isonteam' )
850 return $mem->teamRights(intval($b->getID()));
856 * CommentActions::ifItemCategory()
857 * Different checks for a category
859 * @param string $key key of data for category to which item belongs
860 * @param string $value value of data for category to which item belongs
861 * @return boolean correct or not
863 private function ifItemCategory($key = '', $value = '')
865 global $catid, $manager;
867 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
868 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
869 $icatid = $citem['catid'];
871 // when no parameter is defined, just check if a category is selected
872 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
874 return $b->isValidCategory($icatid);
877 // check category name
878 if ( $key == 'catname' )
880 $value = $b->getCategoryIdFromName($value);
881 if ( $value == $icatid )
883 return $b->isValidCategory($icatid);
888 if ( ($key == 'catid') && ($value == $icatid) )
890 return $b->isValidCategory($icatid);
896 * CommentActions::ifOnTeam()
897 * Checks if a member is on the team of a blog and return his rights
899 * @param string $blogName name of weblog
900 * @return boolean correct or not
902 private function ifOnTeam($blogName = '')
904 global $blog, $member, $manager;
906 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
908 // when no blog found
909 if ( ($blogName == '') && (!is_object($b)) )
914 // explicit blog selection
915 if ( $blogName != '' )
917 $blogid = getBlogIDFromName($blogName);
921 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
923 $blogid = $b->getID();
926 return $member->teamRights($blogid);
930 * CommentActions::ifAdmin()
931 * Checks if a member is admin of a blog
933 * @param string $blogName name of weblog
934 * @return boolean correct or not
936 private function ifAdmin($blogName = '')
938 global $blog, $member, $manager;
940 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
942 // when no blog found
943 if ( ($blogName == '') && (!is_object($b)) )
948 // explicit blog selection
949 if ( $blogName != '' )
951 $blogid = getBlogIDFromName($blogName);
955 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
957 $blogid = $b->getID();
960 return $member->isBlogAdmin($blogid);
964 * CommentActions::ifHasPlugin()
966 * -> checks if plugin exists
967 * hasplugin,PlugName,OptionName
968 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
969 * hasplugin,PlugName,OptionName=value
970 * -> checks if the option OptionName from plugin PlugName is set to value
972 * @param string $name name of plugin
973 * @param string $value key (and value) of plugin option
974 * @return boolean correct or not
976 private function ifHasPlugin($name, $value)
981 // (pluginInstalled method won't write a message in the actionlog on failure)
982 if ( $manager->pluginInstalled('NP_'.$name) )
984 $plugin =& $manager->getPlugin("NP_{$name}");
985 if ( $plugin != NULL )
993 list($name2, $value2) = preg_split('#=#', $value, 2);
994 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
998 else if ( $plugin->getOption($name2) == $value2 )
1009 * CommentActions::ifPlugin()
1010 * Checks if a plugin exists and call its doIf function
1012 * @param string $name name of plugin
1013 * @param string $key key of plugin option
1014 * @param string $value value of plugin option
1015 * @return boolean callback output from plugin
1017 private function ifPlugin($name, $key = '', $value = '')
1021 $plugin =& $manager->getPlugin("NP_{$name}");
1027 $params = func_get_args();
1028 array_shift($params);
1030 return call_user_func_array(array(&$plugin, 'doIf'), $params);