3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2009 The Nucleus Group
\r
6 * This program is free software; you can redistribute it and/or
\r
7 * modify it under the terms of the GNU General Public License
\r
8 * as published by the Free Software Foundation; either version 2
\r
9 * of the License, or (at your option) any later version.
\r
10 * (see nucleus/documentation/index.html#license for more info)
\r
13 * This class is used when parsing comment templates
\r
15 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
16 * @copyright Copyright (C) 2002-2009 The Nucleus Group
\r
17 * @version $Id: CommentActions.php 1626 2012-01-09 15:46:54Z sakamocchi $
\r
20 class CommentActions extends BaseActions
\r
23 * CommentsActions::$commentsObj
\r
24 * ref to COMMENTS object which is using this object to handle its templatevars
\r
26 private $commentsObj;
\r
29 * CommentsActions::$template
\r
30 * template to use to parse the comments
\r
35 * CommentsActions::$currentComment
\r
36 * comment currenlty being handled (mysql result assoc array; see Comments::showComments())
\r
38 private $currentComment;
\r
41 * CommentsActions::$defined_actions
\r
42 * defined actions in this class
\r
44 static private $defined_actions = array(
\r
75 * CommentActions::__construct()
\r
77 * @param object $comments instance of Comments class
\r
80 public function __construct(&$comments)
\r
82 // call constructor of superclass first
\r
83 parent::__construct();
85 // reference to the comments object
\r
86 $this->setCommentsObj($comments);
\r
91 * CommentActions::getAvailableActions()
\r
94 * @return array actions array
\r
96 public function getAvailableActions()
\r
98 return array_merge(self::$defined_actions, parent::getAvailableActions());
\r
103 * CommentActions::setCommentsObj()
\r
105 * @param object $commentsObj instance of Comments class
\r
108 public function setCommentsObj(&$commentsObj)
\r
110 $this->commentsObj =& $commentsObj;
\r
115 * CommentActions::setTemplate()
\r
117 * @param array $template array includes templates
\r
120 public function setTemplate($template)
\r
122 $this->template =& $template;
\r
127 * CommentActions::setCurrentComment()
\r
128 * Set $currentcommentid and $currentcommentarray
\r
130 * @param array $comment associated array includes comment information
\r
133 public function setCurrentComment(&$comment)
\r
135 global $currentcommentid, $currentcommentarray, $manager;
\r
137 if ( $comment['memberid'] != 0 )
\r
139 if ( !array_key_exists('COMMENTS_AUTH', $this->template) )
\r
141 $comment['authtext'] = '';
\r
145 $comment['authtext'] = $this->template['COMMENTS_AUTH'];
\r
148 $mem =& $manager->getMember($comment['memberid']);
\r
149 $comment['user'] = $mem->getDisplayName();
\r
151 if ( $mem->getURL() )
\r
153 $comment['userid'] = $mem->getURL();
\r
157 $comment['userid'] = $mem->getEmail();
\r
161 'memberid' => $comment['memberid'],
\r
162 'name' => $mem->getDisplayName(),
\r
163 'extra' => $this->commentsObj->itemActions->linkparams
\r
166 $comment['userlinkraw'] = Link::create_link('member', $data);
\r
170 // create smart links
\r
171 if ( !array_key_exists('userid', $comment) || !empty($comment['userid']) )
\r
173 if ( (i18n::strpos($comment['userid'], 'http://') === 0) || (i18n::strpos($comment['userid'], 'https://') === 0) )
\r
175 $comment['userlinkraw'] = $comment['userid'];
\r
179 $comment['userlinkraw'] = 'http://' . $comment['userid'];
\r
182 else if ( NOTIFICATION::address_validation($comment['email']) )
\r
184 $comment['userlinkraw'] = 'mailto:' . $comment['email'];
\r
186 else if ( NOTIFICATION::address_validation($comment['userid']) )
\r
188 $comment['userlinkraw'] = 'mailto:' . $comment['userid'];
\r
192 $this->currentComment =& $comment;
\r
193 $currentcommentid = $comment['commentid'];
\r
194 $currentcommentarray = $comment;
\r
199 * CommentActions::parse_authtext()
\r
200 * Parse templatevar authtext
\r
205 public function parse_authtext()
\r
207 if ( $this->currentComment['memberid'] != 0 )
\r
209 $this->parser->parse($this->template['COMMENTS_AUTH']);
\r
215 * CommentActions::parse_blogid()
\r
216 * Parse templatevar blogid
\r
221 public function parse_blogid() {
\r
222 echo $this->currentComment['blogid'];
\r
226 * CommentActions::parse_blogurl()
\r
227 * Parse templatevar blogurl
\r
232 public function parse_blogurl()
\r
235 $item =& $manager->getItem($this->commentsObj->itemid, 1, 1);
\r
236 $blog =& $manager->getBlog($item['blogid']);
\r
237 echo $blog->getURL();
\r
242 * CommentActions::parse_body()
\r
243 * Parse templatevar body
\r
248 public function parse_body() {
\r
249 echo $this->highlight($this->currentComment['body']);
\r
254 * CommentActions::parse_commentcount()
\r
255 * Parse templatevar commentcount
\r
260 public function parse_commentcount()
\r
262 echo $this->commentsObj->commentcount;
\r
267 * CommentActions::parse_commentid()
\r
268 * Parse templatevar commentid
\r
273 public function parse_commentid()
\r
275 echo $this->currentComment['commentid'];
\r
280 * CommentActions::parse_commentword()
\r
281 * Parse templatevar commentword
\r
286 public function parse_commentword()
\r
288 if ( $this->commentsObj->commentcount == 1 )
\r
290 echo $this->template['COMMENTS_ONE'];
\r
294 echo $this->template['COMMENTS_MANY'];
\r
300 * CommentActions::parse_date()
\r
301 * Parse templatevar date
\r
303 * @format String $format Date format according to PHP
\r
306 public function parse_date($format = '')
\r
308 if ( $format !== '' )
\r
313 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
\r
319 $format = $this->template['FORMAT_DATE'];
\r
322 $offset = $this->commentsObj->itemActions->blog->getTimeOffset() * 3600;
\r
324 echo i18n::formatted_datetime($format, $this->currentComment['timestamp'], $offset);
\r
329 * CommentActions::parse_excerpt()
\r
330 * Parse templatevar email
\r
335 public function parse_email()
\r
337 $email = $this->currentComment['email'];
\r
338 $email = str_replace('@', ' (at) ', $email);
\r
339 $email = str_replace('.', ' (dot) ', $email);
\r
345 * CommentActions::parse_excerpt()
\r
346 * Parse templatevar excerpt
\r
351 public function parse_excerpt()
\r
353 echo Entity::hen(Entity::shorten($this->currentComment['body'], 60, '...'));
\r
358 * CommentActions::parse_host()
\r
359 * Parse templatevar host
\r
364 public function parse_host()
\r
366 echo $this->currentComment['host'];
\r
371 * CommentActions::parse_ip()
\r
372 * Parse templatevar ip
\r
377 public function parse_ip()
\r
379 echo $this->currentComment['ip'];
\r
384 * CommentActions::parse_itemid()
\r
385 * Parse templatevar itemid
\r
390 public function parse_itemid()
\r
392 echo $this->commentsObj->itemid;
\r
397 * CommentActions::parse_itemlink()
\r
398 * Parse templatevar itemlink
\r
403 public function parse_itemlink()
\r
406 'itemid' => $this->commentsObj->itemid,
\r
407 'timestamp' => $this->commentsObj->itemActions->currentItem['timestamp'],
\r
408 'title' => $this->commentsObj->itemActions->currentItem['title'],
\r
409 'extra' => $this->commentsObj->itemActions->linkparams
\r
412 echo Link::create_link('item', $data);
\r
417 * CommentActions::parse_itemtitle()
\r
418 * Parse templatevar itemtitle
\r
420 * @param integer $maxLength maximum length for item title
\r
423 public function parse_itemtitle($maxLength = 0)
\r
425 if ( $maxLength == 0 )
\r
427 $this->commentsObj->itemActions->parse_title();
\r
431 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
\r
437 * CommentActions::parse_memberid()
\r
438 * Parse templatevar memberid
\r
443 public function parse_memberid()
\r
445 echo $this->currentComment['memberid'];
\r
450 * CommentActions::parse_short()
\r
451 * Parse templatevar short
\r
456 public function parse_short()
\r
458 $tmp = strtok($this->currentComment['body'], "\n");
\r
459 $tmp = str_replace('<br />', '', $tmp);
\r
461 if ( $tmp != $this->currentComment['body'] )
\r
463 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
\r
469 * CommentActions::parse_time()
\r
470 * Parse templatevar time
\r
472 * @param string $format datetime format referring to strftime() in PHP's built-in function
\r
475 public function parse_time($format = '')
\r
477 if ( $format !== '' )
482 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
\r
488 $format = $this->template['FORMAT_TIME'];
\r
491 echo i18n::formatted_datetime($format, $this->currentComment['timestamp']);
\r
496 * CommentActions::parse_timestamp()
\r
497 * Parse templatevar timestamp
\r
503 public function parse_timestamp()
\r
505 echo $this->currentComment['timestamp'];
\r
510 * CommentActions::parse_plugin()
\r
511 * Executes a plugin templatevar
\r
513 * @param string $pluginName name of plugin (without the NP_)
\r
514 * @param extra parameters can be added
\r
517 public function parse_plugin($pluginName)
\r
521 // only continue when the plugin is really installed
\r
522 if ( !$manager->pluginInstalled("NP_{$pluginName}") )
\r
527 $plugin =& $manager->getPlugin("NP_{$pluginName}");
\r
534 $params = func_get_args();
\r
536 // remove plugin name
\r
537 array_shift($params);
\r
539 // pass info on current item and current comment as well
\r
540 $params = array_merge(array(&$this->currentComment), $params);
\r
541 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem), $params);
\r
543 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
\r
548 * CommentActions::parse_user()
\r
549 * Parse templatevar user
\r
551 * @param string $mode realname or else
\r
554 public function parse_user($mode = '')
\r
558 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
\r
560 $member =& $manager->getMember($this->currentComment['memberid']);
\r
561 echo $member->getRealName();
\r
565 echo Entity::hsc($this->currentComment['user']);
\r
571 * CommentActions::parse_useremail()
\r
572 * Output mail address
\r
577 public function parse_useremail() {
\r
579 if ( $this->currentComment['memberid'] > 0 )
\r
581 $member =& $manager->getMember($this->currentComment['memberid']);
\r
583 if ( $member->email != '' )
\r
585 echo $member->email;
\r
590 if ( NOTIFICATION::address_validation($this->currentComment['email']) )
\r
592 echo $this->currentComment['email'];
\r
594 elseif ( NOTIFICATION::address_validation($this->currentComment['userid']) )
\r
596 echo $this->currentComment['userid'];
\r
603 * CommentActions::parse_userid()
\r
604 * Parse templatevar userid
\r
609 public function parse_userid()
\r
611 echo $this->currentComment['userid'];
\r
616 * CommentActions::parse_userlink()
\r
617 * Parse templatevar userlink
\r
622 public function parse_userlink()
\r
624 if ( $this->currentComment['userlinkraw'] )
\r
626 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
\r
630 echo $this->currentComment['user'];
\r
636 * CommentActions::parse_userlinkraw()
\r
637 * Parse templatevar userlinkraw
\r
642 public function parse_userlinkraw()
\r
644 echo (array_key_exists('userlinkraw', $this->currentComment) && !empty($this->currentComment['userlinkraw'])) ? $this->currentComment['userlinkraw'] : '';
\r
649 * CommentActions::parse_userwebsite()
\r
650 * Parse templatevar userwebsite
\r
655 public function parse_userwebsite()
\r
657 if ( !(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false) )
\r
659 echo $this->currentComment['userlinkraw'];
\r
665 * CommentActions::parse_userwebsitelink()
\r
666 * Parse templatevar userwebsitelink
\r
671 public function parse_userwebsitelink()
\r
673 if ( !(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false) )
\r
675 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
\r
679 echo $this->currentComment['user'];
\r
685 * CommentActions::checkCondition()
\r
686 * Checks conditions for if statements
\r
688 * @param string $field type of <%if%>
\r
689 * @param string $name property of field
\r
690 * @param string $value value of property
\r
693 protected function checkCondition($field, $name='', $value = '') {
\r
694 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
\r
699 $condition = ($blog && $this->ifCategory($name,$value));
\r
701 case 'itemcategory':
\r
702 $condition = ($this->ifItemCategory($name,$value));
\r
704 case 'blogsetting':
\r
705 $condition = ($blog && ($blog->getSetting($name) == $value));
\r
707 case 'itemblogsetting':
\r
708 $item =& $manager->getItem($this->currentComment['itemid'], 1, 1);
\r
709 $blog =& $manager->getBlog($item['blogid']);
\r
710 $condition = ($blog && ($blog->getSetting($name) == $value));
\r
713 $condition = $member->isLoggedIn();
\r
716 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
\r
719 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
\r
722 $condition = ($this->ifAuthor($name,$value));
\r
725 $condition = $this->ifHasPlugin($name, $value);
\r
728 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
\r
735 * CommentActions::ifCategory()
\r
736 * Different checks for a category
\r
738 * @param string $key key of category
\r
739 * @param string $value value for key of category
\r
742 private function ifCategory($key = '', $value = '')
\r
744 global $blog, $catid;
\r
746 // when no parameter is defined, just check if a category is selected
\r
747 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
\r
749 return $blog->isValidCategory($catid);
\r
752 // check category name
\r
753 if ( $key == 'catname' )
\r
755 $value = $blog->getCategoryIdFromName($value);
\r
756 if ($value == $catid)
\r
757 return $blog->isValidCategory($catid);
\r
760 // check category id
\r
761 if ( ($key == 'catid') && ($value == $catid) )
\r
763 return $blog->isValidCategory($catid);
\r
769 * CommentActions::ifAuthor()
\r
770 * Different checks for an author
\r
772 * @param string $key key of data for author
\r
773 * @param string $value value of data for author
\r
774 * @return boolean correct or not
\r
776 private function ifAuthor($key = '', $value = '')
\r
778 global $member, $manager;
\r
780 if ( $this->currentComment['memberid'] == 0 )
\r
785 $member =& $manager->getMember($this->currentComment['memberid']);
\r
786 $item =& $manager->getItem($this->currentComment['itemid'], 1, 1);
\r
788 // when no parameter is defined, just check if item author is current visitor
\r
789 if ( ($key != 'isadmin' && $key != 'name' && $key != 'isauthor' && $key != 'isonteam') )
\r
791 return (intval($memberber->getID()) > 0 && intval($memberber->getID()) == (integer) $item['authorid']);
\r
794 // check comment author name
\r
795 if ( $key == 'name' )
\r
797 $value = trim(strtolower($value));
\r
798 if ( $value == '' )
\r
802 if ( $value == strtolower($member->getDisplayName()) )
\r
808 // check if comment author is admin
\r
809 if ( $key == 'isadmin' )
\r
811 if ( $member->isAdmin() )
\r
815 return $member->isBlogAdmin($item['blogid']);
\r
818 // check if comment author is item author
\r
819 if ( $key == 'isauthor' )
\r
821 return ((integer) $item['authorid'] == (integer) $this->currentComment['memberid']);
\r
824 // check if comment author is on team
\r
825 if ( $key == 'isonteam' )
\r
827 return $member->teamRights((integer) $item['blogid']);
\r
833 * CommentActions::ifItemCategory()
\r
834 * Different checks for a category
\r
836 * @param string $key key of data for category to which item belongs
\r
837 * @param string $value value of data for category to which item belongs
\r
838 * @return boolean correct or not
\r
840 private function ifItemCategory($key = '', $value = '')
\r
842 global $catid, $manager;
\r
844 $item =& $manager->getItem($this->currentComment['itemid'],1,1);
\r
845 $blog =& $manager->getBlog($item['blogid']);
\r
847 // when no parameter is defined, just check if a category is selected
\r
848 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
\r
850 return $blog->isValidCategory($item['catid']);
\r
853 // check category name
\r
854 if ( $key == 'catname' )
\r
856 $value = $blog->getCategoryIdFromName($value);
\r
857 if ( $value == $item['catid'] )
\r
859 return $blog->isValidCategory($item['catid']);
\r
863 // check category id
\r
864 if ( ($key == 'catid') && ($value == $item['catid']) )
\r
866 return $blog->isValidCategory($item['catid']);
\r
872 * CommentActions::ifOnTeam()
\r
873 * Checks if a member is on the team of a blog and return his rights
\r
875 * @param string $blogName name of weblog
\r
876 * @return boolean correct or not
\r
878 private function ifOnTeam($blogName = '')
\r
880 global $member, $manager;
\r
882 $item =& $manager->getItem($this->currentComment['itemid'], 1, 1);
\r
883 $blog =& $manager->getBlog($item['blogid']);
\r
885 // when no blog found
\r
886 if ( ($blogName == '') && !is_object($blog) )
\r
891 // explicit blog selection
\r
892 if ( $blogName != '' )
\r
894 $blogid = getBlogIDFromName($blogName);
\r
897 // use current blog
\r
898 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
\r
900 $blogid = $blog->getID();
\r
903 return $member->teamRights($blogid);
\r
907 * CommentActions::ifAdmin()
\r
908 * Checks if a member is admin of a blog
\r
910 * @param string $blogName name of weblog
\r
911 * @return boolean correct or not
\r
913 private function ifAdmin($blogName = '')
\r
915 global $member, $manager;
\r
917 $item =& $manager->getItem($this->currentComment['itemid'], 1, 1);
\r
918 $blog =& $manager->getBlog($item['blogid']);
\r
920 // when no blog found
\r
921 if ( ($blogName == '') && !is_object($blog) )
\r
926 // explicit blog selection
\r
927 if ( $blogName != '' )
\r
929 $blogid = getBlogIDFromName($blogName);
\r
932 // use current blog
\r
933 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
\r
935 $blogid = $blog->getID();
\r
938 return $member->isBlogAdmin($blogid);
\r
942 * CommentActions::ifHasPlugin()
\r
943 * hasplugin,PlugName
\r
944 * -> checks if plugin exists
\r
945 * hasplugin,PlugName,OptionName
\r
946 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
\r
947 * hasplugin,PlugName,OptionName=value
\r
948 * -> checks if the option OptionName from plugin PlugName is set to value
\r
950 * @param string $name name of plugin
\r
951 * @param string $value key (and value) of plugin option
\r
952 * @return boolean correct or not
\r
954 private function ifHasPlugin($name, $value)
\r
957 $condition = FALSE;
\r
959 // (pluginInstalled method won't write a message in the actionlog on failure)
\r
960 if ( $manager->pluginInstalled('NP_'.$name) )
\r
962 $plugin =& $manager->getPlugin("NP_{$name}");
\r
963 if ( $plugin != NULL )
\r
965 if ( $value == "" )
\r
971 list($name2, $value2) = preg_split('#=#', $value, 2);
\r
972 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
\r
976 else if ( $plugin->getOption($name2) == $value2 )
\r
987 * CommentActions::ifPlugin()
\r
988 * Checks if a plugin exists and call its doIf function
\r
990 * @param string $name name of plugin
\r
991 * @param string $key key of plugin option
\r
992 * @param string $value value of plugin option
\r
993 * @return boolean callback output from plugin
\r
995 private function ifPlugin($name, $key = '', $value = '')
\r
999 $plugin =& $manager->getPlugin("NP_{$name}");
\r
1005 $params = func_get_args();
\r
1006 array_shift($params);
\r
1008 return call_user_func_array(array(&$plugin, 'doIf'), $params);
\r