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;
95 function setCurrentComment(&$comment) {
99 // begin if: member comment
100 if ($comment['memberid'] != 0)
102 $comment['authtext'] = $template['COMMENTS_AUTH'];
104 $mem =& $manager->getMember($comment['memberid']);
105 $comment['user'] = $mem->getDisplayName();
107 // begin if: member URL exists, set it as the userid
110 $comment['userid'] = $mem->getURL();
112 // else: set the email as the userid
115 $comment['userid'] = $mem->getEmail();
118 $comment['userlinkraw'] = createLink(
121 'memberid' => $comment['memberid'],
122 'name' => $mem->getDisplayName(),
123 'extra' => $this->commentsObj->itemActions->linkparams
128 // else: non-member comment
132 // create smart links
134 // begin if: comment userid is not empty
135 if (!empty($comment['userid']) )
138 // begin if: comment userid has either "http://" or "https://" at the beginning
139 if ( (i18n::strpos($comment['userid'], 'http://') === 0) || (i18n::strpos($comment['userid'], 'https://') === 0) )
141 $comment['userlinkraw'] = $comment['userid'];
143 // else: prepend the "http://" (backwards compatibility before rev 1471)
146 $comment['userlinkraw'] = 'http://' . $comment['userid'];
150 // else if: comment email is valid
151 else if (isValidMailAddress($comment['email']) )
153 $comment['userlinkraw'] = 'mailto:' . $comment['email'];
155 // else if: comment userid is a valid email
156 else if (isValidMailAddress($comment['userid']) )
158 $comment['userlinkraw'] = 'mailto:' . $comment['userid'];
163 $this->currentComment =& $comment;
164 global $currentcommentid, $currentcommentarray;
165 $currentcommentid = $comment['commentid'];
166 $currentcommentarray = $comment;
170 * Parse templatevar authtext
172 function parse_authtext() {
173 if ($this->currentComment['memberid'] != 0)
174 $this->parser->parse($this->template['COMMENTS_AUTH']);
178 * Parse templatevar blogid
180 function parse_blogid() {
181 echo $this->currentComment['blogid'];
185 * Parse templatevar blogurl
187 function parse_blogurl() {
189 $blogid = getBlogIDFromItemID($this->commentsObj->itemid);
190 $blog =& $manager->getBlog($blogid);
191 echo $blog->getURL();
195 * Parse templatevar body
197 function parse_body() {
198 echo $this->highlight($this->currentComment['body']);
202 * Parse templatevar commentcount
204 function parse_commentcount() {
205 echo $this->commentsObj->commentcount;
209 * Parse templatevar commentid
211 function parse_commentid() {
212 echo $this->currentComment['commentid'];
216 * Parse templatevar commentword
218 function parse_commentword() {
219 if ($this->commentsObj->commentcount == 1)
220 echo $this->template['COMMENTS_ONE'];
222 echo $this->template['COMMENTS_MANY'];
226 * Parse templatevar date
228 function parse_date($format = '') {
229 echo formatDate($format, $this->currentComment['timestamp'], $this->template['FORMAT_DATE'], $this->commentsObj->itemActions->blog);
233 * Parse templatevar email
235 function parse_email() {
236 $email = $this->currentComment['email'];
237 $email = str_replace('@', ' (at) ', $email);
238 $email = str_replace('.', ' (dot) ', $email);
243 * Parse templatevar excerpt
245 function parse_excerpt() {
246 echo stringToXML(shorten($this->currentComment['body'], 60, '...'));
250 * Parse templatevar host
252 function parse_host() {
253 echo $this->currentComment['host'];
257 * Parse templatevar ip
259 function parse_ip() {
260 echo $this->currentComment['ip'];
264 * Parse templatevar itemid
266 function parse_itemid() {
267 echo $this->commentsObj->itemid;
271 * Parse templatevar itemlink
273 function parse_itemlink() {
277 'itemid' => $this->commentsObj->itemid,
278 'timestamp' => $this->commentsObj->itemActions->currentItem->timestamp,
279 'title' => $this->commentsObj->itemActions->currentItem->title,
280 'extra' => $this->commentsObj->itemActions->linkparams
286 * Parse templatevar itemtitle
288 function parse_itemtitle($maxLength = 0) {
290 $this->commentsObj->itemActions->parse_title();
292 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
296 * Parse templatevar memberid
298 function parse_memberid() {
299 echo $this->currentComment['memberid'];
303 * Parse templatevar short
305 function parse_short() {
306 $tmp = strtok($this->currentComment['body'],"\n");
307 $tmp = str_replace('<br />','',$tmp);
309 if ($tmp != $this->currentComment['body'])
310 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
314 * Parse templatevar time
316 function parse_time($format = '') {
318 ($format == '') ? $this->template['FORMAT_TIME'] : $format,
319 $this->currentComment['timestamp']
324 * Parse templatevar timestamp
326 function parse_timestamp() {
327 echo $this->currentComment['timestamp'];
331 * Executes a plugin templatevar
333 * @param pluginName name of plugin (without the NP_)
335 * extra parameters can be added
337 function parse_plugin($pluginName) {
340 // only continue when the plugin is really installed
341 if (!$manager->pluginInstalled('NP_' . $pluginName))
344 $plugin =& $manager->getPlugin('NP_' . $pluginName);
345 if (!$plugin) return;
348 $params = func_get_args();
350 // remove plugin name
351 array_shift($params);
353 // pass info on current item and current comment as well
354 $params = array_merge(array(&$this->currentComment),$params);
355 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem),$params);
357 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
361 * Parse templatevar user
362 * @param string $mode
364 function parse_user($mode = '')
368 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
370 $member =& $manager->getMember($this->currentComment['memberid']);
371 echo $member->getRealName();
375 echo i18n::hsc($this->currentComment['user']);
380 * Parse templatevar useremail
382 function parse_useremail() {
384 if ($this->currentComment['memberid'] > 0)
386 $member =& $manager->getMember($this->currentComment['memberid']);
388 if ($member->email != '')
393 if (isValidMailAddress($this->currentComment['email']))
394 echo $this->currentComment['email'];
395 elseif (isValidMailAddress($this->currentComment['userid']))
396 echo $this->currentComment['userid'];
397 // if (!(i18n::strpos($this->currentComment['userlinkraw'], 'mailto:') === false))
398 // echo str_replace('mailto:', '', $this->currentComment['userlinkraw']);
403 * Parse templatevar userid
405 function parse_userid() {
406 echo $this->currentComment['userid'];
411 * Parse templatevar userlink
413 function parse_userlink() {
414 if ($this->currentComment['userlinkraw']) {
415 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
417 echo $this->currentComment['user'];
422 * Parse templatevar userlinkraw
424 function parse_userlinkraw() {
425 echo $this->currentComment['userlinkraw'];
429 * Parse templatevar userwebsite
431 function parse_userwebsite() {
432 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false))
433 echo $this->currentComment['userlinkraw'];
437 * Parse templatevar userwebsitelink
439 function parse_userwebsitelink() {
440 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false)) {
441 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
443 echo $this->currentComment['user'];
447 // function to enable if-else-elseif-elseifnot-ifnot-endif to comment template fields
450 * Checks conditions for if statements
452 * @param string $field type of <%if%>
453 * @param string $name property of field
454 * @param string $value value of property
456 function checkCondition($field, $name='', $value = '') {
457 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
462 $condition = ($blog && $this->_ifCategory($name,$value));
465 $condition = ($this->_ifItemCategory($name,$value));
468 $condition = ($blog && ($blog->getSetting($name) == $value));
470 case 'itemblogsetting':
471 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
472 $condition = ($b && ($b->getSetting($name) == $value));
475 $condition = $member->isLoggedIn();
478 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
481 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
484 $condition = ($this->_ifAuthor($name,$value));
487 $condition = ($itemidnext != '');
490 $condition = ($itemidprev != '');
492 case 'archiveprevexists':
493 $condition = ($archiveprevexists == true);
495 case 'archivenextexists':
496 $condition = ($archivenextexists == true);
499 $condition = ($name == $this->skintype);
502 $condition = $this->_ifHasPlugin($name, $value);
505 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
512 * Different checks for a category
514 function _ifCategory($name = '', $value='') {
515 global $blog, $catid;
517 // when no parameter is defined, just check if a category is selected
518 if (($name != 'catname' && $name != 'catid') || ($value == ''))
519 return $blog->isValidCategory($catid);
521 // check category name
522 if ($name == 'catname') {
523 $value = $blog->getCategoryIdFromName($value);
524 if ($value == $catid)
525 return $blog->isValidCategory($catid);
529 if (($name == 'catid') && ($value == $catid))
530 return $blog->isValidCategory($catid);
537 * Different checks for an author
539 function _ifAuthor($name = '', $value='') {
540 global $member, $manager;
542 if ($this->currentComment['memberid'] == 0) return false;
544 $mem =& $manager->getMember($this->currentComment['memberid']);
545 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
546 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
548 // when no parameter is defined, just check if item author is current visitor
549 if (($name != 'isadmin' && $name != 'name' && $name != 'isauthor' && $name != 'isonteam')) {
550 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
553 // check comment author name
554 if ($name == 'name') {
555 $value = trim(strtolower($value));
558 if ($value == strtolower($mem->getDisplayName()))
562 // check if comment author is admin
563 if ($name == 'isadmin') {
564 $blogid = intval($b->getID());
568 return $mem->isBlogAdmin($blogid);
571 // check if comment author is item author
572 if ($name == 'isauthor') {
573 return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
576 // check if comment author is on team
577 if ($name == 'isonteam') {
578 return $mem->teamRights(intval($b->getID()));
585 * Different checks for a category
587 function _ifItemCategory($name = '', $value='') {
588 global $catid, $manager;
590 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
591 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
592 $icatid = $citem['catid'];
594 // when no parameter is defined, just check if a category is selected
595 if (($name != 'catname' && $name != 'catid') || ($value == ''))
596 return $b->isValidCategory($icatid);
598 // check category name
599 if ($name == 'catname') {
600 $value = $b->getCategoryIdFromName($value);
601 if ($value == $icatid)
602 return $b->isValidCategory($icatid);
606 if (($name == 'catid') && ($value == $icatid))
607 return $b->isValidCategory($icatid);
614 * Checks if a member is on the team of a blog and return his rights
616 function _ifOnTeam($blogName = '') {
617 global $blog, $member, $manager;
619 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
621 // when no blog found
622 if (($blogName == '') && (!is_object($b)))
625 // explicit blog selection
627 $blogid = getBlogIDFromName($blogName);
629 if (($blogName == '') || !$manager->existsBlogID($blogid))
631 $blogid = $b->getID();
633 return $member->teamRights($blogid);
637 * Checks if a member is admin of a blog
639 function _ifAdmin($blogName = '') {
640 global $blog, $member, $manager;
642 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
644 // when no blog found
645 if (($blogName == '') && (!is_object($b)))
648 // explicit blog selection
650 $blogid = getBlogIDFromName($blogName);
652 if (($blogName == '') || !$manager->existsBlogID($blogid))
654 $blogid = $b->getID();
656 return $member->isBlogAdmin($blogid);
662 * -> checks if plugin exists
663 * hasplugin,PlugName,OptionName
664 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
665 * hasplugin,PlugName,OptionName=value
666 * -> checks if the option OptionName from plugin PlugName is set to value
668 function _ifHasPlugin($name, $value) {
671 // (pluginInstalled method won't write a message in the actionlog on failure)
672 if ($manager->pluginInstalled('NP_'.$name)) {
673 $plugin =& $manager->getPlugin('NP_' . $name);
674 if ($plugin != NULL) {
678 list($name2, $value2) = i18n::explode('=', $value, 2);
679 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
681 } else if ($plugin->getOption($name2) == $value2) {
691 * Checks if a plugin exists and call its doIf function
693 function _ifPlugin($name, $key = '', $value = '') {
696 $plugin =& $manager->getPlugin('NP_' . $name);
697 if (!$plugin) return;
699 $params = func_get_args();
700 array_shift($params);
702 return call_user_func_array(array(&$plugin, 'doIf'), $params);