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 $item =& $manager->getItem($this->commentsObj->itemid, 1, 1);
236 $blog =& $manager->getBlog($item['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 (array_key_exists('userlinkraw', $this->currentComment) && !empty($this->currentComment['userlinkraw'])) ? $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 $item =& $manager->getItem($this->currentComment['itemid'], 1, 1);
709 $blog =& $manager->getBlog($item['blogid']);
710 $condition = ($blog && ($blog->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 $member =& $manager->getMember($this->currentComment['memberid']);
786 $item =& $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') )
791 return (intval($memberber->getID()) > 0 && intval($memberber->getID()) == (integer) $item['authorid']);
794 // check comment author name
795 if ( $key == 'name' )
797 $value = trim(strtolower($value));
802 if ( $value == strtolower($member->getDisplayName()) )
808 // check if comment author is admin
809 if ( $key == 'isadmin' )
811 if ( $member->isAdmin() )
815 return $member->isBlogAdmin($item['blogid']);
818 // check if comment author is item author
819 if ( $key == 'isauthor' )
821 return ((integer) $item['authorid'] == (integer) $this->currentComment['memberid']);
824 // check if comment author is on team
825 if ( $key == 'isonteam' )
827 return $member->teamRights((integer) $item['blogid']);
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 $item =& $manager->getItem($this->currentComment['itemid'],1,1);
845 $blog =& $manager->getBlog($item['blogid']);
847 // when no parameter is defined, just check if a category is selected
848 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
850 return $blog->isValidCategory($item['catid']);
853 // check category name
854 if ( $key == 'catname' )
856 $value = $blog->getCategoryIdFromName($value);
857 if ( $value == $item['catid'] )
859 return $blog->isValidCategory($item['catid']);
864 if ( ($key == 'catid') && ($value == $item['catid']) )
866 return $blog->isValidCategory($item['catid']);
872 * CommentActions::ifOnTeam()
873 * Checks if a member is on the team of a blog and return his rights
875 * @param string $blogName name of weblog
876 * @return boolean correct or not
878 private function ifOnTeam($blogName = '')
880 global $member, $manager;
882 $item =& $manager->getItem($this->currentComment['itemid'], 1, 1);
883 $blog =& $manager->getBlog($item['blogid']);
885 // when no blog found
886 if ( ($blogName == '') && !is_object($blog) )
891 // explicit blog selection
892 if ( $blogName != '' )
894 $blogid = getBlogIDFromName($blogName);
898 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
900 $blogid = $blog->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 $member, $manager;
917 $item =& $manager->getItem($this->currentComment['itemid'], 1, 1);
918 $blog =& $manager->getBlog($item['blogid']);
920 // when no blog found
921 if ( ($blogName == '') && !is_object($blog) )
926 // explicit blog selection
927 if ( $blogName != '' )
929 $blogid = getBlogIDFromName($blogName);
933 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
935 $blogid = $blog->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);