3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2011 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-2011 The Nucleus Group
18 * @version $NucleusJP: COMMENTACTIONS.php,v 1.5.2.1 2007/08/08 05:31:31 kimitake Exp $
21 class COMMENTACTIONS extends BaseActions {
23 // ref to COMMENTS object which is using this object to handle
27 // template to use to parse the comments
30 // comment currenlty being handled (mysql result assoc array; see COMMENTS::showComments())
33 function COMMENTACTIONS(&$comments) {
34 // call constructor of superclass first
37 // reference to the comments object
38 $this->setCommentsObj($comments);
41 function getDefinedActions() {
84 function setParser(&$parser) {
85 $this->parser =& $parser;
88 function setCommentsObj(&$commentsObj) {
89 $this->commentsObj =& $commentsObj;
92 function setTemplate($template) {
93 $this->template =& $template;
96 function setCurrentComment(&$comment) {
100 // begin if: member comment
101 if ($comment['memberid'] != 0)
103 $comment['authtext'] = $template['COMMENTS_AUTH'];
105 $mem =& $manager->getMember($comment['memberid']);
106 $comment['user'] = $mem->getDisplayName();
108 // begin if: member URL exists, set it as the userid
111 $comment['userid'] = $mem->getURL();
113 // else: set the email as the userid
116 $comment['userid'] = $mem->getEmail();
119 $comment['userlinkraw'] = createLink(
122 'memberid' => $comment['memberid'],
123 'name' => $mem->getDisplayName(),
124 'extra' => $this->commentsObj->itemActions->linkparams
129 // else: non-member comment
133 // create smart links
135 // begin if: comment userid is not empty
136 if (!empty($comment['userid']) )
139 // begin if: comment userid has either "http://" or "https://" at the beginning
140 if ( (strpos($comment['userid'], 'http://') === 0) || (strpos($comment['userid'], 'https://') === 0) )
142 $comment['userlinkraw'] = $comment['userid'];
144 // else: prepend the "http://" (backwards compatibility before rev 1471)
147 $comment['userlinkraw'] = 'http://' . $comment['userid'];
151 // else if: comment email is valid
152 else if (isValidMailAddress($comment['email']) )
154 $comment['userlinkraw'] = 'mailto:' . $comment['email'];
156 // else if: comment userid is a valid email
157 else if (isValidMailAddress($comment['userid']) )
159 $comment['userlinkraw'] = 'mailto:' . $comment['userid'];
164 $this->currentComment =& $comment;
165 global $currentcommentid, $currentcommentarray;
166 $currentcommentid = $comment['commentid'];
167 $currentcommentarray = $comment;
171 * Parse templatevar authtext
173 function parse_authtext() {
174 if ($this->currentComment['memberid'] != 0)
175 $this->parser->parse($this->template['COMMENTS_AUTH']);
179 * Parse templatevar blogid
181 function parse_blogid() {
182 echo $this->currentComment['blogid'];
186 * Parse templatevar blogurl
188 function parse_blogurl() {
190 $blogid = getBlogIDFromItemID($this->commentsObj->itemid);
191 $blog =& $manager->getBlog($blogid);
192 echo $blog->getURL();
196 * Parse templatevar body
198 function parse_body() {
199 echo $this->highlight($this->currentComment['body']);
203 * Parse templatevar commentcount
205 function parse_commentcount() {
206 echo $this->commentsObj->commentcount;
210 * Parse templatevar commentid
212 function parse_commentid() {
213 echo $this->currentComment['commentid'];
217 * Parse templatevar commentword
219 function parse_commentword() {
220 if ($this->commentsObj->commentcount == 1)
221 echo $this->template['COMMENTS_ONE'];
223 echo $this->template['COMMENTS_MANY'];
227 * Parse templatevar date
229 function parse_date($format = '') {
230 echo formatDate($format, $this->currentComment['timestamp'], $this->template['FORMAT_DATE'], $this->commentsObj->itemActions->blog);
234 * Parse templatevar email
236 function parse_email() {
237 $email = $this->currentComment['email'];
238 $email = str_replace('@', ' (at) ', $email);
239 $email = str_replace('.', ' (dot) ', $email);
244 * Parse templatevar excerpt
246 function parse_excerpt() {
247 echo stringToXML(shorten($this->currentComment['body'], 60, '...'));
251 * Parse templatevar host
253 function parse_host() {
254 echo $this->currentComment['host'];
258 * Parse templatevar ip
260 function parse_ip() {
261 echo $this->currentComment['ip'];
265 * Parse templatevar itemid
267 function parse_itemid() {
268 echo $this->commentsObj->itemid;
272 * Parse templatevar itemlink
274 function parse_itemlink() {
278 'itemid' => $this->commentsObj->itemid,
279 'timestamp' => $this->commentsObj->itemActions->currentItem->timestamp,
280 'title' => $this->commentsObj->itemActions->currentItem->title,
281 'extra' => $this->commentsObj->itemActions->linkparams
287 * Parse templatevar itemtitle
289 function parse_itemtitle($maxLength = 0) {
291 $this->commentsObj->itemActions->parse_title();
293 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
297 * Parse templatevar memberid
299 function parse_memberid() {
300 echo $this->currentComment['memberid'];
304 * Parse templatevar short
306 function parse_short() {
307 $tmp = strtok($this->currentComment['body'],"\n");
308 $tmp = str_replace('<br />','',$tmp);
310 if ($tmp != $this->currentComment['body'])
311 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
315 * Parse templatevar time
317 function parse_time($format = '') {
319 ($format == '') ? $this->template['FORMAT_TIME'] : $format,
320 $this->currentComment['timestamp']
325 * Parse templatevar timestamp
327 function parse_timestamp() {
328 echo $this->currentComment['timestamp'];
332 * Executes a plugin templatevar
334 * @param pluginName name of plugin (without the NP_)
336 * extra parameters can be added
338 function parse_plugin($pluginName) {
341 // only continue when the plugin is really installed
342 if (!$manager->pluginInstalled('NP_' . $pluginName))
345 $plugin =& $manager->getPlugin('NP_' . $pluginName);
346 if (!$plugin) return;
349 $params = func_get_args();
351 // remove plugin name
352 array_shift($params);
354 // pass info on current item and current comment as well
355 $params = array_merge(array(&$this->currentComment),$params);
356 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem),$params);
358 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
362 * Parse templatevar user
363 * @param string $mode
365 function parse_user($mode = '')
\r
369 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
\r
371 $member =& $manager->getMember($this->currentComment['memberid']);
372 echo $member->getRealName();
376 echo htmlspecialchars($this->currentComment['user'], ENT_QUOTES);
381 * Parse templatevar useremail
383 function parse_useremail() {
385 if ($this->currentComment['memberid'] > 0)
387 $member =& $manager->getMember($this->currentComment['memberid']);
389 if ($member->email != '')
394 if (isValidMailAddress($this->currentComment['email']))
395 echo $this->currentComment['email'];
396 elseif (isValidMailAddress($this->currentComment['userid']))
397 echo $this->currentComment['userid'];
398 // if (!(strpos($this->currentComment['userlinkraw'], 'mailto:') === false))
399 // echo str_replace('mailto:', '', $this->currentComment['userlinkraw']);
404 * Parse templatevar userid
406 function parse_userid() {
407 echo $this->currentComment['userid'];
412 * Parse templatevar userlink
414 function parse_userlink() {
415 if ($this->currentComment['userlinkraw']) {
416 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
418 echo $this->currentComment['user'];
423 * Parse templatevar userlinkraw
425 function parse_userlinkraw() {
426 echo $this->currentComment['userlinkraw'];
430 * Parse templatevar userwebsite
432 function parse_userwebsite() {
433 if (!(strpos($this->currentComment['userlinkraw'], 'http://') === false))
434 echo $this->currentComment['userlinkraw'];
438 * Parse templatevar userwebsitelink
440 function parse_userwebsitelink() {
441 if (!(strpos($this->currentComment['userlinkraw'], 'http://') === false)) {
442 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
444 echo $this->currentComment['user'];
448 // function to enable if-else-elseif-elseifnot-ifnot-endif to comment template fields
451 * Checks conditions for if statements
453 * @param string $field type of <%if%>
454 * @param string $name property of field
455 * @param string $value value of property
457 function checkCondition($field, $name='', $value = '') {
458 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
463 $condition = ($blog && $this->_ifCategory($name,$value));
466 $condition = ($this->_ifItemCategory($name,$value));
469 $condition = ($blog && ($blog->getSetting($name) == $value));
471 case 'itemblogsetting':
472 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
473 $condition = ($b && ($b->getSetting($name) == $value));
476 $condition = $member->isLoggedIn();
479 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
482 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
485 $condition = ($this->_ifAuthor($name,$value));
488 $condition = ($itemidnext != '');
491 $condition = ($itemidprev != '');
493 case 'archiveprevexists':
494 $condition = ($archiveprevexists == true);
496 case 'archivenextexists':
497 $condition = ($archivenextexists == true);
500 $condition = ($name == $this->skintype);
503 $condition = $this->_ifHasPlugin($name, $value);
506 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
513 * Different checks for a category
515 function _ifCategory($name = '', $value='') {
516 global $blog, $catid;
518 // when no parameter is defined, just check if a category is selected
519 if (($name != 'catname' && $name != 'catid') || ($value == ''))
520 return $blog->isValidCategory($catid);
522 // check category name
523 if ($name == 'catname') {
524 $value = $blog->getCategoryIdFromName($value);
525 if ($value == $catid)
526 return $blog->isValidCategory($catid);
530 if (($name == 'catid') && ($value == $catid))
531 return $blog->isValidCategory($catid);
538 * Different checks for an author
540 function _ifAuthor($name = '', $value='') {
541 global $member, $manager;
543 if ($this->currentComment['memberid'] == 0) return false;
545 $mem =& $manager->getMember($this->currentComment['memberid']);
546 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
547 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
549 // when no parameter is defined, just check if item author is current visitor
550 if (($name != 'isadmin' && $name != 'name' && $name != 'isauthor' && $name != 'isonteam')) {
551 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
554 // check comment author name
555 if ($name == 'name') {
556 $value = trim(strtolower($value));
559 if ($value == strtolower($mem->getDisplayName()))
563 // check if comment author is admin
564 if ($name == 'isadmin') {
565 $blogid = intval($b->getID());
569 return $mem->isBlogAdmin($blogid);
572 // check if comment author is item author
573 if ($name == 'isauthor') {
574 return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
577 // check if comment author is on team
578 if ($name == 'isonteam') {
579 return $mem->teamRights(intval($b->getID()));
586 * Different checks for a category
588 function _ifItemCategory($name = '', $value='') {
589 global $catid, $manager;
591 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
592 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
593 $icatid = $citem['catid'];
595 // when no parameter is defined, just check if a category is selected
596 if (($name != 'catname' && $name != 'catid') || ($value == ''))
597 return $b->isValidCategory($icatid);
599 // check category name
600 if ($name == 'catname') {
601 $value = $b->getCategoryIdFromName($value);
602 if ($value == $icatid)
603 return $b->isValidCategory($icatid);
607 if (($name == 'catid') && ($value == $icatid))
608 return $b->isValidCategory($icatid);
615 * Checks if a member is on the team of a blog and return his rights
617 function _ifOnTeam($blogName = '') {
618 global $blog, $member, $manager;
620 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
622 // when no blog found
623 if (($blogName == '') && (!is_object($b)))
626 // explicit blog selection
628 $blogid = getBlogIDFromName($blogName);
630 if (($blogName == '') || !$manager->existsBlogID($blogid))
632 $blogid = $b->getID();
634 return $member->teamRights($blogid);
638 * Checks if a member is admin of a blog
640 function _ifAdmin($blogName = '') {
641 global $blog, $member, $manager;
643 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
645 // when no blog found
646 if (($blogName == '') && (!is_object($b)))
649 // explicit blog selection
651 $blogid = getBlogIDFromName($blogName);
653 if (($blogName == '') || !$manager->existsBlogID($blogid))
655 $blogid = $b->getID();
657 return $member->isBlogAdmin($blogid);
663 * -> checks if plugin exists
664 * hasplugin,PlugName,OptionName
665 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
666 * hasplugin,PlugName,OptionName=value
667 * -> checks if the option OptionName from plugin PlugName is set to value
669 function _ifHasPlugin($name, $value) {
672 // (pluginInstalled method won't write a message in the actionlog on failure)
673 if ($manager->pluginInstalled('NP_'.$name)) {
674 $plugin =& $manager->getPlugin('NP_' . $name);
675 if ($plugin != NULL) {
679 list($name2, $value2) = explode('=', $value, 2);
680 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
682 } else if ($plugin->getOption($name2) == $value2) {
692 * Checks if a plugin exists and call its doIf function
694 function _ifPlugin($name, $key = '', $value = '') {
697 $plugin =& $manager->getPlugin('NP_' . $name);
698 if (!$plugin) return;
700 $params = func_get_args();
701 array_shift($params);
703 return call_user_func_array(array(&$plugin, 'doIf'), $params);