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