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());
103 * CommentActions::setParser()
105 * @param object $parser instance of Parser class
108 public function setParser(&$parser)
110 $this->parser =& $parser;
116 * CommentActions::setCommentsObj()
118 * @param object $commentsObj instance of Comments class
121 public function setCommentsObj(&$commentsObj)
123 $this->commentsObj =& $commentsObj;
128 * CommentActions::setTemplate()
130 * @param array $template array includes templates
133 public function setTemplate($template)
135 $this->template =& $template;
140 * CommentActions::setCurrentComment()
141 * Set $currentcommentid and $currentcommentarray
143 * @param array $comment associated array includes comment information
146 public function setCurrentComment(&$comment)
148 global $currentcommentid, $currentcommentarray, $manager;
150 if ( $comment['memberid'] != 0 )
152 if ( !array_key_exists('COMMENTS_AUTH', $this->template) )
154 $comment['authtext'] = '';
158 $comment['authtext'] = $this->template['COMMENTS_AUTH'];
161 $mem =& $manager->getMember($comment['memberid']);
162 $comment['user'] = $mem->getDisplayName();
164 if ( $mem->getURL() )
166 $comment['userid'] = $mem->getURL();
170 $comment['userid'] = $mem->getEmail();
174 'memberid' => $comment['memberid'],
175 'name' => $mem->getDisplayName(),
176 'extra' => $this->commentsObj->itemActions->linkparams
179 $comment['userlinkraw'] = Link::create_link('member', $data);
183 // create smart links
184 if ( !array_key_exists('userid', $comment) || !empty($comment['userid']) )
186 if ( (i18n::strpos($comment['userid'], 'http://') === 0) || (i18n::strpos($comment['userid'], 'https://') === 0) )
188 $comment['userlinkraw'] = $comment['userid'];
192 $comment['userlinkraw'] = 'http://' . $comment['userid'];
195 else if ( NOTIFICATION::address_validation($comment['email']) )
197 $comment['userlinkraw'] = 'mailto:' . $comment['email'];
199 else if ( NOTIFICATION::address_validation($comment['userid']) )
201 $comment['userlinkraw'] = 'mailto:' . $comment['userid'];
205 $this->currentComment =& $comment;
206 $currentcommentid = $comment['commentid'];
207 $currentcommentarray = $comment;
212 * CommentActions::parse_authtext()
213 * Parse templatevar authtext
218 public function parse_authtext()
220 if ( $this->currentComment['memberid'] != 0 )
222 $this->parser->parse($this->template['COMMENTS_AUTH']);
228 * CommentActions::parse_blogid()
229 * Parse templatevar blogid
234 public function parse_blogid() {
235 echo $this->currentComment['blogid'];
239 * CommentActions::parse_blogurl()
240 * Parse templatevar blogurl
245 public function parse_blogurl()
248 $blogid = getBlogIDFromItemID($this->commentsObj->itemid);
249 $blog =& $manager->getBlog($blogid);
250 echo $blog->getURL();
255 * CommentActions::parse_body()
256 * Parse templatevar body
261 public function parse_body() {
262 echo $this->highlight($this->currentComment['body']);
267 * CommentActions::parse_commentcount()
268 * Parse templatevar commentcount
273 public function parse_commentcount()
275 echo $this->commentsObj->commentcount;
280 * CommentActions::parse_commentid()
281 * Parse templatevar commentid
286 public function parse_commentid()
288 echo $this->currentComment['commentid'];
293 * CommentActions::parse_commentword()
294 * Parse templatevar commentword
299 public function parse_commentword()
301 if ( $this->commentsObj->commentcount == 1 )
303 echo $this->template['COMMENTS_ONE'];
307 echo $this->template['COMMENTS_MANY'];
313 * CommentActions::parse_date()
314 * Parse templatevar date
316 * @format String $format Date format according to PHP
319 public function parse_date($format = '')
321 if ( $format !== '' )
326 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
332 $format = $this->template['FORMAT_DATE'];
335 $offset = $this->commentsObj->itemActions->blog->getTimeOffset() * 3600;
337 echo i18n::formatted_datetime($format, $this->currentComment['timestamp'], $offset);
342 * CommentActions::parse_excerpt()
343 * Parse templatevar email
348 public function parse_email()
350 $email = $this->currentComment['email'];
351 $email = str_replace('@', ' (at) ', $email);
352 $email = str_replace('.', ' (dot) ', $email);
358 * CommentActions::parse_excerpt()
359 * Parse templatevar excerpt
364 public function parse_excerpt()
366 echo Entity::hen(Entity::shorten($this->currentComment['body'], 60, '...'));
371 * CommentActions::parse_host()
372 * Parse templatevar host
377 public function parse_host()
379 echo $this->currentComment['host'];
384 * CommentActions::parse_ip()
385 * Parse templatevar ip
390 public function parse_ip()
392 echo $this->currentComment['ip'];
397 * CommentActions::parse_itemid()
398 * Parse templatevar itemid
403 public function parse_itemid()
405 echo $this->commentsObj->itemid;
410 * CommentActions::parse_itemlink()
411 * Parse templatevar itemlink
416 public function parse_itemlink()
419 'itemid' => $this->commentsObj->itemid,
420 'timestamp' => $this->commentsObj->itemActions->currentItem->timestamp,
421 'title' => $this->commentsObj->itemActions->currentItem->title,
422 'extra' => $this->commentsObj->itemActions->linkparams
425 echo Link::create_link('item', $data);
430 * CommentActions::parse_itemtitle()
431 * Parse templatevar itemtitle
433 * @param integer $maxLength maximum length for item title
436 public function parse_itemtitle($maxLength = 0)
438 if ( $maxLength == 0 )
440 $this->commentsObj->itemActions->parse_title();
444 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
450 * CommentActions::parse_memberid()
451 * Parse templatevar memberid
456 public function parse_memberid()
458 echo $this->currentComment['memberid'];
463 * CommentActions::parse_short()
464 * Parse templatevar short
469 public function parse_short()
471 $tmp = strtok($this->currentComment['body'], "\n");
472 $tmp = str_replace('<br />', '', $tmp);
474 if ( $tmp != $this->currentComment['body'] )
476 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
482 * CommentActions::parse_time()
483 * Parse templatevar time
485 * @param string $format datetime format referring to strftime() in PHP's built-in function
488 public function parse_time($format = '')
490 if ( $format === '' )
495 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
501 $format = $this->template['FORMAT_TIME'];
504 echo i18n::formatted_datetime($format, $this->currentComment['timestamp']);
509 * CommentActions::parse_timestamp()
510 * Parse templatevar timestamp
516 public function parse_timestamp()
518 echo $this->currentComment['timestamp'];
523 * CommentActions::parse_plugin()
524 * Executes a plugin templatevar
526 * @param string $pluginName name of plugin (without the NP_)
527 * @param extra parameters can be added
530 public function parse_plugin($pluginName)
534 // only continue when the plugin is really installed
535 if ( !$manager->pluginInstalled("NP_{$pluginName}") )
540 $plugin =& $manager->getPlugin("NP_{$pluginName}");
547 $params = func_get_args();
549 // remove plugin name
550 array_shift($params);
552 // pass info on current item and current comment as well
553 $params = array_merge(array(&$this->currentComment), $params);
554 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem), $params);
556 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
561 * CommentActions::parse_user()
562 * Parse templatevar user
564 * @param string $mode realname or else
567 public function parse_user($mode = '')
571 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
573 $member =& $manager->getMember($this->currentComment['memberid']);
574 echo $member->getRealName();
578 echo Entity::hsc($this->currentComment['user']);
584 * CommentActions::parse_useremail()
585 * Output mail address
590 public function parse_useremail() {
592 if ( $this->currentComment['memberid'] > 0 )
594 $member =& $manager->getMember($this->currentComment['memberid']);
596 if ( $member->email != '' )
603 if ( NOTIFICATION::address_validation($this->currentComment['email']) )
605 echo $this->currentComment['email'];
607 elseif ( NOTIFICATION::address_validation($this->currentComment['userid']) )
609 echo $this->currentComment['userid'];
616 * CommentActions::parse_userid()
617 * Parse templatevar userid
622 public function parse_userid()
624 echo $this->currentComment['userid'];
629 * CommentActions::parse_userlink()
630 * Parse templatevar userlink
635 public function parse_userlink()
637 if ( $this->currentComment['userlinkraw'] )
639 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
643 echo $this->currentComment['user'];
649 * CommentActions::parse_userlinkraw()
650 * Parse templatevar userlinkraw
655 public function parse_userlinkraw()
657 echo $this->currentComment['userlinkraw'];
662 * CommentActions::parse_userwebsite()
663 * Parse templatevar userwebsite
668 public function parse_userwebsite()
670 if ( !(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false) )
672 echo $this->currentComment['userlinkraw'];
678 * CommentActions::parse_userwebsitelink()
679 * Parse templatevar userwebsitelink
684 public function parse_userwebsitelink()
686 if ( !(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false) )
688 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
692 echo $this->currentComment['user'];
698 * ItemActions::checkCondition()
699 * Checks conditions for if statements
701 * @param string $field type of <%if%>
702 * @param string $name property of field
703 * @param string $value value of property
706 protected function checkCondition($field, $name='', $value = '') {
707 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
712 $condition = ($blog && $this->ifCategory($name,$value));
715 $condition = ($this->ifItemCategory($name,$value));
718 $condition = ($blog && ($blog->getSetting($name) == $value));
720 case 'itemblogsetting':
721 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
722 $condition = ($b && ($b->getSetting($name) == $value));
725 $condition = $member->isLoggedIn();
728 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
731 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
734 $condition = ($this->ifAuthor($name,$value));
737 $condition = $this->ifHasPlugin($name, $value);
740 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
747 * CommentActions::ifCategory()
748 * Different checks for a category
750 * @param string $key key of category
751 * @param string $value value for key of category
754 private function ifCategory($key = '', $value = '')
756 global $blog, $catid;
758 // when no parameter is defined, just check if a category is selected
759 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
761 return $blog->isValidCategory($catid);
764 // check category name
765 if ( $key == 'catname' )
767 $value = $blog->getCategoryIdFromName($value);
768 if ($value == $catid)
769 return $blog->isValidCategory($catid);
773 if ( ($key == 'catid') && ($value == $catid) )
775 return $blog->isValidCategory($catid);
781 * CommentActions::ifAuthor()
782 * Different checks for an author
784 * @param string $key key of data for author
785 * @param string $value value of data for author
786 * @return boolean correct or not
788 private function ifAuthor($key = '', $value = '')
790 global $member, $manager;
792 if ( $this->currentComment['memberid'] == 0 )
797 $mem =& $manager->getMember($this->currentComment['memberid']);
798 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
799 $citem =& $manager->getItem($this->currentComment['itemid'], 1, 1);
801 // when no parameter is defined, just check if item author is current visitor
802 if (($key != 'isadmin' && $key != 'name' && $key != 'isauthor' && $key != 'isonteam')) {
803 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
806 // check comment author name
807 if ( $key == 'name' )
809 $value = trim(strtolower($value));
814 if ( $value == strtolower($mem->getDisplayName()) )
820 // check if comment author is admin
821 if ( $key == 'isadmin' )
823 $blogid = intval($b->getID());
824 if ( $mem->isAdmin() )
828 return $mem->isBlogAdmin($blogid);
831 // check if comment author is item author
832 if ( $key == 'isauthor' )
834 return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
837 // check if comment author is on team
838 if ( $key == 'isonteam' )
840 return $mem->teamRights(intval($b->getID()));
846 * CommentActions::ifItemCategory()
847 * Different checks for a category
849 * @param string $key key of data for category to which item belongs
850 * @param string $value value of data for category to which item belongs
851 * @return boolean correct or not
853 private function ifItemCategory($key = '', $value = '')
855 global $catid, $manager;
857 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
858 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
859 $icatid = $citem['catid'];
861 // when no parameter is defined, just check if a category is selected
862 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
864 return $b->isValidCategory($icatid);
867 // check category name
868 if ( $key == 'catname' )
870 $value = $b->getCategoryIdFromName($value);
871 if ( $value == $icatid )
873 return $b->isValidCategory($icatid);
878 if ( ($key == 'catid') && ($value == $icatid) )
880 return $b->isValidCategory($icatid);
886 * CommentActions::ifOnTeam()
887 * Checks if a member is on the team of a blog and return his rights
889 * @param string $blogName name of weblog
890 * @return boolean correct or not
892 private function ifOnTeam($blogName = '')
894 global $blog, $member, $manager;
896 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
898 // when no blog found
899 if ( ($blogName == '') && (!is_object($b)) )
904 // explicit blog selection
905 if ( $blogName != '' )
907 $blogid = getBlogIDFromName($blogName);
911 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
913 $blogid = $b->getID();
916 return $member->teamRights($blogid);
920 * CommentActions::ifAdmin()
921 * Checks if a member is admin of a blog
923 * @param string $blogName name of weblog
924 * @return boolean correct or not
926 private function ifAdmin($blogName = '')
928 global $blog, $member, $manager;
930 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
932 // when no blog found
933 if ( ($blogName == '') && (!is_object($b)) )
938 // explicit blog selection
939 if ( $blogName != '' )
941 $blogid = getBlogIDFromName($blogName);
945 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
947 $blogid = $b->getID();
950 return $member->isBlogAdmin($blogid);
954 * CommentActions::ifHasPlugin()
956 * -> checks if plugin exists
957 * hasplugin,PlugName,OptionName
958 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
959 * hasplugin,PlugName,OptionName=value
960 * -> checks if the option OptionName from plugin PlugName is set to value
962 * @param string $name name of plugin
963 * @param string $value key (and value) of plugin option
964 * @return boolean correct or not
966 private function ifHasPlugin($name, $value)
971 // (pluginInstalled method won't write a message in the actionlog on failure)
972 if ( $manager->pluginInstalled('NP_'.$name) )
974 $plugin =& $manager->getPlugin("NP_{$name}");
975 if ( $plugin != NULL )
983 list($name2, $value2) = preg_split('#=#', $value, 2);
984 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
988 else if ( $plugin->getOption($name2) == $value2 )
999 * CommentActions::ifPlugin()
1000 * Checks if a plugin exists and call its doIf function
1002 * @param string $name name of plugin
1003 * @param string $key key of plugin option
1004 * @param string $value value of plugin option
1005 * @return boolean callback output from plugin
1007 private function ifPlugin($name, $key = '', $value = '')
1011 $plugin =& $manager->getPlugin("NP_{$name}");
1017 $params = func_get_args();
1018 array_shift($params);
1020 return call_user_func_array(array(&$plugin, 'doIf'), $params);