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 {
22 // ref to COMMENTS object which is using this object to handle
26 // template to use to parse the comments
29 // comment currenlty being handled (mysql result assoc array; see COMMENTS::showComments())
32 function COMMENTACTIONS(&$comments) {
33 // call constructor of superclass first
36 // reference to the comments object
37 $this->setCommentsObj($comments);
40 function getDefinedActions() {
83 function setParser(&$parser) {
84 $this->parser =& $parser;
87 function setCommentsObj(&$commentsObj) {
88 $this->commentsObj =& $commentsObj;
91 function setTemplate($template) {
92 $this->template =& $template;
96 * COMMENTACTIONS::setCurrentComment()
97 * Set $currentcommentid and $currentcommentarray
99 * @param Array $comment array with comment elements
103 function setCurrentComment(&$comment)
107 if ( $comment['memberid'] != 0 )
109 $comment['authtext'] = $template['COMMENTS_AUTH'];
110 $mem =& $manager->getMember($comment['memberid']);
111 $comment['user'] = $mem->getDisplayName();
113 if ( $mem->getURL() )
115 $comment['userid'] = $mem->getURL();
119 $comment['userid'] = $mem->getEmail();
122 $comment['userlinkraw'] = Link::create_link(
125 'memberid' => $comment['memberid'],
126 'name' => $mem->getDisplayName(),
127 'extra' => $this->commentsObj->itemActions->linkparams
133 // create smart links
134 if ( !empty($comment['userid']) )
136 if ( (i18n::strpos($comment['userid'], 'http://') === 0) || (i18n::strpos($comment['userid'], 'https://') === 0) )
138 $comment['userlinkraw'] = $comment['userid'];
142 $comment['userlinkraw'] = 'http://' . $comment['userid'];
145 else if ( NOTIFICATION::address_validation($comment['email']) )
147 $comment['userlinkraw'] = 'mailto:' . $comment['email'];
149 else if ( NOTIFICATION::address_validation($comment['userid']) )
151 $comment['userlinkraw'] = 'mailto:' . $comment['userid'];
155 $this->currentComment =& $comment;
156 global $currentcommentid, $currentcommentarray;
157 $currentcommentid = $comment['commentid'];
158 $currentcommentarray = $comment;
163 * Parse templatevar authtext
165 function parse_authtext() {
166 if ($this->currentComment['memberid'] != 0)
167 $this->parser->parse($this->template['COMMENTS_AUTH']);
171 * Parse templatevar blogid
173 function parse_blogid() {
174 echo $this->currentComment['blogid'];
178 * Parse templatevar blogurl
180 function parse_blogurl() {
182 $blogid = getBlogIDFromItemID($this->commentsObj->itemid);
183 $blog =& $manager->getBlog($blogid);
184 echo $blog->getURL();
188 * Parse templatevar body
190 function parse_body() {
191 echo $this->highlight($this->currentComment['body']);
195 * Parse templatevar commentcount
197 function parse_commentcount() {
198 echo $this->commentsObj->commentcount;
202 * Parse templatevar commentid
204 function parse_commentid() {
205 echo $this->currentComment['commentid'];
209 * Parse templatevar commentword
211 function parse_commentword() {
212 if ($this->commentsObj->commentcount == 1)
213 echo $this->template['COMMENTS_ONE'];
215 echo $this->template['COMMENTS_MANY'];
219 * Parse templatevar date
221 function parse_date($format = '') {
222 echo formatDate($format, $this->currentComment['timestamp'], $this->template['FORMAT_DATE'], $this->commentsObj->itemActions->blog);
226 * Parse templatevar email
228 function parse_email() {
229 $email = $this->currentComment['email'];
230 $email = str_replace('@', ' (at) ', $email);
231 $email = str_replace('.', ' (dot) ', $email);
236 * COMMENTACTIONS::parse_excerpt()
237 * Parse templatevar excerpt
240 * @return String templatevar excerpt
242 function parse_excerpt()
244 echo stringToXML(shorten($this->currentComment['body'], 60, '...'));
248 * Parse templatevar host
250 function parse_host() {
251 echo $this->currentComment['host'];
255 * Parse templatevar ip
257 function parse_ip() {
258 echo $this->currentComment['ip'];
262 * Parse templatevar itemid
264 function parse_itemid() {
265 echo $this->commentsObj->itemid;
269 * Parse templatevar itemlink
271 function parse_itemlink() {
272 echo Link::create_link(
275 'itemid' => $this->commentsObj->itemid,
276 'timestamp' => $this->commentsObj->itemActions->currentItem->timestamp,
277 'title' => $this->commentsObj->itemActions->currentItem->title,
278 'extra' => $this->commentsObj->itemActions->linkparams
284 * Parse templatevar itemtitle
286 function parse_itemtitle($maxLength = 0) {
288 $this->commentsObj->itemActions->parse_title();
290 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
294 * Parse templatevar memberid
296 function parse_memberid() {
297 echo $this->currentComment['memberid'];
301 * Parse templatevar short
303 function parse_short() {
304 $tmp = strtok($this->currentComment['body'],"\n");
305 $tmp = str_replace('<br />','',$tmp);
307 if ($tmp != $this->currentComment['body'])
308 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
312 * Parse templatevar time
314 function parse_time($format = '') {
316 ($format == '') ? $this->template['FORMAT_TIME'] : $format,
317 $this->currentComment['timestamp']
322 * Parse templatevar timestamp
324 function parse_timestamp() {
325 echo $this->currentComment['timestamp'];
329 * Executes a plugin templatevar
331 * @param pluginName name of plugin (without the NP_)
333 * extra parameters can be added
335 function parse_plugin($pluginName) {
338 // only continue when the plugin is really installed
339 if (!$manager->pluginInstalled('NP_' . $pluginName))
342 $plugin =& $manager->getPlugin('NP_' . $pluginName);
343 if (!$plugin) return;
346 $params = func_get_args();
348 // remove plugin name
349 array_shift($params);
351 // pass info on current item and current comment as well
352 $params = array_merge(array(&$this->currentComment),$params);
353 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem),$params);
355 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
359 * Parse templatevar user
360 * @param string $mode
362 function parse_user($mode = '')
366 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
368 $member =& $manager->getMember($this->currentComment['memberid']);
369 echo $member->getRealName();
373 echo ENTITY::hsc($this->currentComment['user']);
378 * COMMENTACTIONS::parse_useremail()
379 * Output mail address
384 function parse_useremail() {
386 if ( $this->currentComment['memberid'] > 0 )
388 $member =& $manager->getMember($this->currentComment['memberid']);
390 if ( $member->email != '' )
397 if ( NOTIFICATION::address_validation($this->currentComment['email']) )
399 echo $this->currentComment['email'];
401 elseif ( NOTIFICATION::address_validation($this->currentComment['userid']) )
403 echo $this->currentComment['userid'];
410 * Parse templatevar userid
412 function parse_userid() {
413 echo $this->currentComment['userid'];
418 * Parse templatevar userlink
420 function parse_userlink() {
421 if ($this->currentComment['userlinkraw']) {
422 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
424 echo $this->currentComment['user'];
429 * Parse templatevar userlinkraw
431 function parse_userlinkraw() {
432 echo $this->currentComment['userlinkraw'];
436 * Parse templatevar userwebsite
438 function parse_userwebsite() {
439 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false))
440 echo $this->currentComment['userlinkraw'];
444 * Parse templatevar userwebsitelink
446 function parse_userwebsitelink() {
447 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false)) {
448 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
450 echo $this->currentComment['user'];
454 // function to enable if-else-elseif-elseifnot-ifnot-endif to comment template fields
457 * Checks conditions for if statements
459 * @param string $field type of <%if%>
460 * @param string $name property of field
461 * @param string $value value of property
463 function checkCondition($field, $name='', $value = '') {
464 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
469 $condition = ($blog && $this->_ifCategory($name,$value));
472 $condition = ($this->_ifItemCategory($name,$value));
475 $condition = ($blog && ($blog->getSetting($name) == $value));
477 case 'itemblogsetting':
478 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
479 $condition = ($b && ($b->getSetting($name) == $value));
482 $condition = $member->isLoggedIn();
485 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
488 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
491 $condition = ($this->_ifAuthor($name,$value));
494 $condition = ($itemidnext != '');
497 $condition = ($itemidprev != '');
499 case 'archiveprevexists':
500 $condition = ($archiveprevexists == true);
502 case 'archivenextexists':
503 $condition = ($archivenextexists == true);
506 $condition = ($name == $this->skintype);
509 $condition = $this->_ifHasPlugin($name, $value);
512 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
519 * Different checks for a category
521 function _ifCategory($name = '', $value='') {
522 global $blog, $catid;
524 // when no parameter is defined, just check if a category is selected
525 if (($name != 'catname' && $name != 'catid') || ($value == ''))
526 return $blog->isValidCategory($catid);
528 // check category name
529 if ($name == 'catname') {
530 $value = $blog->getCategoryIdFromName($value);
531 if ($value == $catid)
532 return $blog->isValidCategory($catid);
536 if (($name == 'catid') && ($value == $catid))
537 return $blog->isValidCategory($catid);
544 * Different checks for an author
546 function _ifAuthor($name = '', $value='') {
547 global $member, $manager;
549 if ($this->currentComment['memberid'] == 0) return false;
551 $mem =& $manager->getMember($this->currentComment['memberid']);
552 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
553 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
555 // when no parameter is defined, just check if item author is current visitor
556 if (($name != 'isadmin' && $name != 'name' && $name != 'isauthor' && $name != 'isonteam')) {
557 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
560 // check comment author name
561 if ($name == 'name') {
562 $value = trim(strtolower($value));
565 if ($value == strtolower($mem->getDisplayName()))
569 // check if comment author is admin
570 if ($name == 'isadmin') {
571 $blogid = intval($b->getID());
575 return $mem->isBlogAdmin($blogid);
578 // check if comment author is item author
579 if ($name == 'isauthor') {
580 return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
583 // check if comment author is on team
584 if ($name == 'isonteam') {
585 return $mem->teamRights(intval($b->getID()));
592 * Different checks for a category
594 function _ifItemCategory($name = '', $value='') {
595 global $catid, $manager;
597 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
598 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
599 $icatid = $citem['catid'];
601 // when no parameter is defined, just check if a category is selected
602 if (($name != 'catname' && $name != 'catid') || ($value == ''))
603 return $b->isValidCategory($icatid);
605 // check category name
606 if ($name == 'catname') {
607 $value = $b->getCategoryIdFromName($value);
608 if ($value == $icatid)
609 return $b->isValidCategory($icatid);
613 if (($name == 'catid') && ($value == $icatid))
614 return $b->isValidCategory($icatid);
621 * Checks if a member is on the team of a blog and return his rights
623 function _ifOnTeam($blogName = '') {
624 global $blog, $member, $manager;
626 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
628 // when no blog found
629 if (($blogName == '') && (!is_object($b)))
632 // explicit blog selection
634 $blogid = getBlogIDFromName($blogName);
636 if (($blogName == '') || !$manager->existsBlogID($blogid))
638 $blogid = $b->getID();
640 return $member->teamRights($blogid);
644 * Checks if a member is admin of a blog
646 function _ifAdmin($blogName = '') {
647 global $blog, $member, $manager;
649 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
651 // when no blog found
652 if (($blogName == '') && (!is_object($b)))
655 // explicit blog selection
657 $blogid = getBlogIDFromName($blogName);
659 if (($blogName == '') || !$manager->existsBlogID($blogid))
661 $blogid = $b->getID();
663 return $member->isBlogAdmin($blogid);
669 * -> checks if plugin exists
670 * hasplugin,PlugName,OptionName
671 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
672 * hasplugin,PlugName,OptionName=value
673 * -> checks if the option OptionName from plugin PlugName is set to value
675 function _ifHasPlugin($name, $value) {
678 // (pluginInstalled method won't write a message in the actionlog on failure)
679 if ($manager->pluginInstalled('NP_'.$name)) {
680 $plugin =& $manager->getPlugin('NP_' . $name);
681 if ($plugin != NULL) {
685 list($name2, $value2) = i18n::explode('=', $value, 2);
686 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
688 } else if ($plugin->getOption($name2) == $value2) {
698 * Checks if a plugin exists and call its doIf function
700 function _ifPlugin($name, $key = '', $value = '') {
703 $plugin =& $manager->getPlugin('NP_' . $name);
704 if (!$plugin) return;
706 $params = func_get_args();
707 array_shift($params);
709 return call_user_func_array(array(&$plugin, 'doIf'), $params);