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 * COMMENTACTIONS::parse_date()
220 * Parse templatevar date
222 * @format String $format Date format according to PHP
223 * @return String formatted datetime
226 function parse_date($format = '')
228 $offset = $this->commentsObj->itemActions->blog->getTimeOffset() * 3600;
229 echo i18n::formatted_datetime($format, $this->currentComment['timestamp'], $this->template['FORMAT_DATE'], $offset);
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 * COMMENTACTIONS::parse_excerpt()
244 * Parse templatevar excerpt
247 * @return String templatevar excerpt
249 function parse_excerpt()
251 echo ENTITY::hen(ENTITY::shorten($this->currentComment['body'], 60, '...'));
255 * Parse templatevar host
257 function parse_host() {
258 echo $this->currentComment['host'];
262 * Parse templatevar ip
264 function parse_ip() {
265 echo $this->currentComment['ip'];
269 * Parse templatevar itemid
271 function parse_itemid() {
272 echo $this->commentsObj->itemid;
276 * COMMENTACTIONS::parse_itemlink()
277 * Parse templatevar itemlink
282 function parse_itemlink()
284 echo LINK::create_link(
287 'itemid' => $this->commentsObj->itemid,
288 'timestamp' => $this->commentsObj->itemActions->currentItem->timestamp,
289 'title' => $this->commentsObj->itemActions->currentItem->title,
290 'extra' => $this->commentsObj->itemActions->linkparams
297 * Parse templatevar itemtitle
299 function parse_itemtitle($maxLength = 0) {
301 $this->commentsObj->itemActions->parse_title();
303 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
307 * Parse templatevar memberid
309 function parse_memberid() {
310 echo $this->currentComment['memberid'];
314 * Parse templatevar short
316 function parse_short() {
317 $tmp = strtok($this->currentComment['body'],"\n");
318 $tmp = str_replace('<br />','',$tmp);
320 if ($tmp != $this->currentComment['body'])
321 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
325 * Parse templatevar time
327 function parse_time($format = '') {
329 ($format == '') ? $this->template['FORMAT_TIME'] : $format,
330 $this->currentComment['timestamp']
335 * Parse templatevar timestamp
337 function parse_timestamp() {
338 echo $this->currentComment['timestamp'];
342 * Executes a plugin templatevar
344 * @param pluginName name of plugin (without the NP_)
346 * extra parameters can be added
348 function parse_plugin($pluginName) {
351 // only continue when the plugin is really installed
352 if (!$manager->pluginInstalled('NP_' . $pluginName))
355 $plugin =& $manager->getPlugin('NP_' . $pluginName);
356 if (!$plugin) return;
359 $params = func_get_args();
361 // remove plugin name
362 array_shift($params);
364 // pass info on current item and current comment as well
365 $params = array_merge(array(&$this->currentComment),$params);
366 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem),$params);
368 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
372 * Parse templatevar user
373 * @param string $mode
375 function parse_user($mode = '')
379 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
381 $member =& $manager->getMember($this->currentComment['memberid']);
382 echo $member->getRealName();
386 echo ENTITY::hsc($this->currentComment['user']);
391 * COMMENTACTIONS::parse_useremail()
392 * Output mail address
397 function parse_useremail() {
399 if ( $this->currentComment['memberid'] > 0 )
401 $member =& $manager->getMember($this->currentComment['memberid']);
403 if ( $member->email != '' )
410 if ( NOTIFICATION::address_validation($this->currentComment['email']) )
412 echo $this->currentComment['email'];
414 elseif ( NOTIFICATION::address_validation($this->currentComment['userid']) )
416 echo $this->currentComment['userid'];
423 * Parse templatevar userid
425 function parse_userid() {
426 echo $this->currentComment['userid'];
431 * Parse templatevar userlink
433 function parse_userlink() {
434 if ($this->currentComment['userlinkraw']) {
435 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
437 echo $this->currentComment['user'];
442 * Parse templatevar userlinkraw
444 function parse_userlinkraw() {
445 echo $this->currentComment['userlinkraw'];
449 * Parse templatevar userwebsite
451 function parse_userwebsite() {
452 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false))
453 echo $this->currentComment['userlinkraw'];
457 * Parse templatevar userwebsitelink
459 function parse_userwebsitelink() {
460 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false)) {
461 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
463 echo $this->currentComment['user'];
467 // function to enable if-else-elseif-elseifnot-ifnot-endif to comment template fields
470 * Checks conditions for if statements
472 * @param string $field type of <%if%>
473 * @param string $name property of field
474 * @param string $value value of property
476 function checkCondition($field, $name='', $value = '') {
477 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
482 $condition = ($blog && $this->_ifCategory($name,$value));
485 $condition = ($this->_ifItemCategory($name,$value));
488 $condition = ($blog && ($blog->getSetting($name) == $value));
490 case 'itemblogsetting':
491 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
492 $condition = ($b && ($b->getSetting($name) == $value));
495 $condition = $member->isLoggedIn();
498 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
501 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
504 $condition = ($this->_ifAuthor($name,$value));
507 $condition = ($itemidnext != '');
510 $condition = ($itemidprev != '');
512 case 'archiveprevexists':
513 $condition = ($archiveprevexists == true);
515 case 'archivenextexists':
516 $condition = ($archivenextexists == true);
519 $condition = ($name == $this->skintype);
522 $condition = $this->_ifHasPlugin($name, $value);
525 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
532 * Different checks for a category
534 function _ifCategory($name = '', $value='') {
535 global $blog, $catid;
537 // when no parameter is defined, just check if a category is selected
538 if (($name != 'catname' && $name != 'catid') || ($value == ''))
539 return $blog->isValidCategory($catid);
541 // check category name
542 if ($name == 'catname') {
543 $value = $blog->getCategoryIdFromName($value);
544 if ($value == $catid)
545 return $blog->isValidCategory($catid);
549 if (($name == 'catid') && ($value == $catid))
550 return $blog->isValidCategory($catid);
557 * Different checks for an author
559 function _ifAuthor($name = '', $value='') {
560 global $member, $manager;
562 if ($this->currentComment['memberid'] == 0) return false;
564 $mem =& $manager->getMember($this->currentComment['memberid']);
565 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
566 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
568 // when no parameter is defined, just check if item author is current visitor
569 if (($name != 'isadmin' && $name != 'name' && $name != 'isauthor' && $name != 'isonteam')) {
570 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
573 // check comment author name
574 if ($name == 'name') {
575 $value = trim(strtolower($value));
578 if ($value == strtolower($mem->getDisplayName()))
582 // check if comment author is admin
583 if ($name == 'isadmin') {
584 $blogid = intval($b->getID());
588 return $mem->isBlogAdmin($blogid);
591 // check if comment author is item author
592 if ($name == 'isauthor') {
593 return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
596 // check if comment author is on team
597 if ($name == 'isonteam') {
598 return $mem->teamRights(intval($b->getID()));
605 * Different checks for a category
607 function _ifItemCategory($name = '', $value='') {
608 global $catid, $manager;
610 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
611 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
612 $icatid = $citem['catid'];
614 // when no parameter is defined, just check if a category is selected
615 if (($name != 'catname' && $name != 'catid') || ($value == ''))
616 return $b->isValidCategory($icatid);
618 // check category name
619 if ($name == 'catname') {
620 $value = $b->getCategoryIdFromName($value);
621 if ($value == $icatid)
622 return $b->isValidCategory($icatid);
626 if (($name == 'catid') && ($value == $icatid))
627 return $b->isValidCategory($icatid);
634 * Checks if a member is on the team of a blog and return his rights
636 function _ifOnTeam($blogName = '') {
637 global $blog, $member, $manager;
639 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
641 // when no blog found
642 if (($blogName == '') && (!is_object($b)))
645 // explicit blog selection
647 $blogid = getBlogIDFromName($blogName);
649 if (($blogName == '') || !$manager->existsBlogID($blogid))
651 $blogid = $b->getID();
653 return $member->teamRights($blogid);
657 * Checks if a member is admin of a blog
659 function _ifAdmin($blogName = '') {
660 global $blog, $member, $manager;
662 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
664 // when no blog found
665 if (($blogName == '') && (!is_object($b)))
668 // explicit blog selection
670 $blogid = getBlogIDFromName($blogName);
672 if (($blogName == '') || !$manager->existsBlogID($blogid))
674 $blogid = $b->getID();
676 return $member->isBlogAdmin($blogid);
682 * -> checks if plugin exists
683 * hasplugin,PlugName,OptionName
684 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
685 * hasplugin,PlugName,OptionName=value
686 * -> checks if the option OptionName from plugin PlugName is set to value
688 function _ifHasPlugin($name, $value) {
691 // (pluginInstalled method won't write a message in the actionlog on failure)
692 if ($manager->pluginInstalled('NP_'.$name)) {
693 $plugin =& $manager->getPlugin('NP_' . $name);
694 if ($plugin != NULL) {
698 list($name2, $value2) = i18n::explode('=', $value, 2);
699 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
701 } else if ($plugin->getOption($name2) == $value2) {
711 * Checks if a plugin exists and call its doIf function
713 function _ifPlugin($name, $key = '', $value = '') {
716 $plugin =& $manager->getPlugin('NP_' . $name);
717 if (!$plugin) return;
719 $params = func_get_args();
720 array_shift($params);
722 return call_user_func_array(array(&$plugin, 'doIf'), $params);