3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2012 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)
12 * This class is used when parsing comment templates
15 class COMMENTACTIONS extends BaseActions {
17 // ref to COMMENTS object which is using this object to handle
21 // template to use to parse the comments
24 // comment currenlty being handled (mysql result assoc array; see COMMENTS::showComments())
27 function COMMENTACTIONS(&$comments) {
28 // call constructor of superclass first
31 // reference to the comments object
32 $this->setCommentsObj($comments);
35 function getDefinedActions() {
78 function setParser(&$parser) {
79 $this->parser =& $parser;
82 function setCommentsObj(&$commentsObj) {
83 $this->commentsObj =& $commentsObj;
86 function setTemplate($template) {
87 $this->template =& $template;
90 function setCurrentComment(&$comment) {
94 // begin if: member comment
95 if ($comment['memberid'] != 0)
97 $comment['authtext'] = $template['COMMENTS_AUTH'];
99 $mem =& $manager->getMember($comment['memberid']);
100 $comment['user'] = $mem->getDisplayName();
102 // begin if: member URL exists, set it as the userid
105 $comment['userid'] = $mem->getURL();
107 // else: set the email as the userid
110 $comment['userid'] = $mem->getEmail();
113 $comment['userlinkraw'] = createLink(
116 'memberid' => $comment['memberid'],
117 'name' => $mem->getDisplayName(),
118 'extra' => $this->commentsObj->itemActions->linkparams
123 // else: non-member comment
127 // create smart links
129 // begin if: comment userid is not empty
130 if (!empty($comment['userid']) )
133 // begin if: comment userid has either "http://" or "https://" at the beginning
134 if ( (strpos($comment['userid'], 'http://') === 0) || (strpos($comment['userid'], 'https://') === 0) )
136 $comment['userlinkraw'] = $comment['userid'];
138 // else: prepend the "http://" (backwards compatibility before rev 1471)
141 $comment['userlinkraw'] = 'http://' . $comment['userid'];
145 // else if: comment email is valid
146 else if (isValidMailAddress($comment['email']) )
148 $comment['userlinkraw'] = 'mailto:' . $comment['email'];
150 // else if: comment userid is a valid email
151 else if (isValidMailAddress($comment['userid']) )
153 $comment['userlinkraw'] = 'mailto:' . $comment['userid'];
158 $this->currentComment =& $comment;
159 global $currentcommentid, $currentcommentarray;
160 $currentcommentid = $comment['commentid'];
161 $currentcommentarray = $comment;
165 * Parse templatevar authtext
167 function parse_authtext() {
168 if ($this->currentComment['memberid'] != 0)
169 $this->parser->parse($this->template['COMMENTS_AUTH']);
173 * Parse templatevar blogid
175 function parse_blogid() {
176 echo $this->currentComment['blogid'];
180 * Parse templatevar blogurl
182 function parse_blogurl() {
184 $blogid = getBlogIDFromItemID($this->commentsObj->itemid);
185 $blog =& $manager->getBlog($blogid);
186 echo $blog->getURL();
190 * Parse templatevar body
192 function parse_body() {
193 echo $this->highlight($this->currentComment['body']);
197 * Parse templatevar commentcount
199 function parse_commentcount() {
200 echo $this->commentsObj->commentcount;
204 * Parse templatevar commentid
206 function parse_commentid() {
207 echo $this->currentComment['commentid'];
211 * Parse templatevar commentword
213 function parse_commentword() {
214 if ($this->commentsObj->commentcount == 1)
215 echo $this->template['COMMENTS_ONE'];
217 echo $this->template['COMMENTS_MANY'];
221 * Parse templatevar date
223 function parse_date($format = '') {
224 echo formatDate($format, $this->currentComment['timestamp'], $this->template['FORMAT_DATE'], $this->commentsObj->itemActions->blog);
228 * Parse templatevar email
230 function parse_email() {
231 $email = $this->currentComment['email'];
232 $email = str_replace('@', ' (at) ', $email);
233 $email = str_replace('.', ' (dot) ', $email);
238 * Parse templatevar excerpt
240 function parse_excerpt() {
241 echo stringToXML(shorten($this->currentComment['body'], 60, '...'));
245 * Parse templatevar host
247 function parse_host() {
248 echo $this->currentComment['host'];
252 * Parse templatevar ip
254 function parse_ip() {
255 echo $this->currentComment['ip'];
259 * Parse templatevar itemid
261 function parse_itemid() {
262 echo $this->commentsObj->itemid;
266 * Parse templatevar itemlink
268 function parse_itemlink() {
272 'itemid' => $this->commentsObj->itemid,
273 'timestamp' => $this->commentsObj->itemActions->currentItem->timestamp,
274 'title' => $this->commentsObj->itemActions->currentItem->title,
275 'extra' => $this->commentsObj->itemActions->linkparams
281 * Parse templatevar itemtitle
283 function parse_itemtitle($maxLength = 0) {
285 $this->commentsObj->itemActions->parse_title();
287 $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
291 * Parse templatevar memberid
293 function parse_memberid() {
294 echo $this->currentComment['memberid'];
298 * Parse templatevar short
300 function parse_short() {
301 $tmp = strtok($this->currentComment['body'],"\n");
302 $tmp = str_replace('<br />','',$tmp);
304 if ($tmp != $this->currentComment['body'])
305 $this->parser->parse($this->template['COMMENTS_CONTINUED']);
309 * Parse templatevar time
311 function parse_time($format = '') {
313 ($format == '') ? $this->template['FORMAT_TIME'] : $format,
314 $this->currentComment['timestamp']
319 * Parse templatevar timestamp
321 function parse_timestamp() {
322 echo $this->currentComment['timestamp'];
326 * Executes a plugin templatevar
328 * @param pluginName name of plugin (without the NP_)
330 * extra parameters can be added
332 function parse_plugin($pluginName) {
335 // only continue when the plugin is really installed
336 if (!$manager->pluginInstalled('NP_' . $pluginName))
339 $plugin =& $manager->getPlugin('NP_' . $pluginName);
340 if (!$plugin) return;
343 $params = func_get_args();
345 // remove plugin name
346 array_shift($params);
348 // pass info on current item and current comment as well
349 $params = array_merge(array(&$this->currentComment),$params);
350 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem),$params);
352 call_user_func_array(array($plugin, 'doTemplateCommentsVar'), $params);
356 * Parse templatevar user
357 * @param string $mode
359 function parse_user($mode = '')
\r
363 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
\r
365 $member =& $manager->getMember($this->currentComment['memberid']);
366 echo $member->getRealName();
370 echo htmlspecialchars($this->currentComment['user'], ENT_QUOTES);
375 * Parse templatevar useremail
377 function parse_useremail() {
379 if ($this->currentComment['memberid'] > 0)
381 $member =& $manager->getMember($this->currentComment['memberid']);
383 if ($member->email != '')
388 if (isValidMailAddress($this->currentComment['email']))
389 echo $this->currentComment['email'];
390 elseif (isValidMailAddress($this->currentComment['userid']))
391 echo $this->currentComment['userid'];
392 // if (!(strpos($this->currentComment['userlinkraw'], 'mailto:') === false))
393 // echo str_replace('mailto:', '', $this->currentComment['userlinkraw']);
398 * Parse templatevar userid
400 function parse_userid() {
401 echo $this->currentComment['userid'];
406 * Parse templatevar userlink
408 function parse_userlink() {
409 if ($this->currentComment['userlinkraw']) {
410 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
412 echo $this->currentComment['user'];
417 * Parse templatevar userlinkraw
419 function parse_userlinkraw() {
420 echo $this->currentComment['userlinkraw'];
424 * Parse templatevar userwebsite
426 function parse_userwebsite() {
427 if (!(strpos($this->currentComment['userlinkraw'], 'http://') === false))
428 echo $this->currentComment['userlinkraw'];
432 * Parse templatevar userwebsitelink
434 function parse_userwebsitelink() {
435 if (!(strpos($this->currentComment['userlinkraw'], 'http://') === false)) {
436 echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
438 echo $this->currentComment['user'];
442 // function to enable if-else-elseif-elseifnot-ifnot-endif to comment template fields
445 * Checks conditions for if statements
447 * @param string $field type of <%if%>
448 * @param string $name property of field
449 * @param string $value value of property
451 function checkCondition($field, $name='', $value = '') {
452 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
457 $condition = ($blog && $this->_ifCategory($name,$value));
460 $condition = ($this->_ifItemCategory($name,$value));
463 $condition = ($blog && ($blog->getSetting($name) == $value));
465 case 'itemblogsetting':
466 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
467 $condition = ($b && ($b->getSetting($name) == $value));
470 $condition = $member->isLoggedIn();
473 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
476 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
479 $condition = ($this->_ifAuthor($name,$value));
482 $condition = ($itemidnext != '');
485 $condition = ($itemidprev != '');
487 case 'archiveprevexists':
488 $condition = ($archiveprevexists == true);
490 case 'archivenextexists':
491 $condition = ($archivenextexists == true);
494 $condition = ($name == $this->skintype);
497 $condition = $this->_ifHasPlugin($name, $value);
500 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
507 * Different checks for a category
509 function _ifCategory($name = '', $value='') {
510 global $blog, $catid;
512 // when no parameter is defined, just check if a category is selected
513 if (($name != 'catname' && $name != 'catid') || ($value == ''))
514 return $blog->isValidCategory($catid);
516 // check category name
517 if ($name == 'catname') {
518 $value = $blog->getCategoryIdFromName($value);
519 if ($value == $catid)
520 return $blog->isValidCategory($catid);
524 if (($name == 'catid') && ($value == $catid))
525 return $blog->isValidCategory($catid);
532 * Different checks for an author
534 function _ifAuthor($name = '', $value='') {
535 global $member, $manager;
537 if ($this->currentComment['memberid'] == 0) return false;
539 $mem =& $manager->getMember($this->currentComment['memberid']);
540 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
541 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
543 // when no parameter is defined, just check if item author is current visitor
544 if (($name != 'isadmin' && $name != 'name' && $name != 'isauthor' && $name != 'isonteam')) {
545 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
548 // check comment author name
549 if ($name == 'name') {
550 $value = trim(strtolower($value));
553 if ($value == strtolower($mem->getDisplayName()))
557 // check if comment author is admin
558 if ($name == 'isadmin') {
559 $blogid = intval($b->getID());
563 return $mem->isBlogAdmin($blogid);
566 // check if comment author is item author
567 if ($name == 'isauthor') {
568 return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
571 // check if comment author is on team
572 if ($name == 'isonteam') {
573 return $mem->teamRights(intval($b->getID()));
580 * Different checks for a category
582 function _ifItemCategory($name = '', $value='') {
583 global $catid, $manager;
585 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
586 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
587 $icatid = $citem['catid'];
589 // when no parameter is defined, just check if a category is selected
590 if (($name != 'catname' && $name != 'catid') || ($value == ''))
591 return $b->isValidCategory($icatid);
593 // check category name
594 if ($name == 'catname') {
595 $value = $b->getCategoryIdFromName($value);
596 if ($value == $icatid)
597 return $b->isValidCategory($icatid);
601 if (($name == 'catid') && ($value == $icatid))
602 return $b->isValidCategory($icatid);
609 * Checks if a member is on the team of a blog and return his rights
611 function _ifOnTeam($blogName = '') {
612 global $blog, $member, $manager;
614 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
616 // when no blog found
617 if (($blogName == '') && (!is_object($b)))
620 // explicit blog selection
622 $blogid = getBlogIDFromName($blogName);
624 if (($blogName == '') || !$manager->existsBlogID($blogid))
626 $blogid = $b->getID();
628 return $member->teamRights($blogid);
632 * Checks if a member is admin of a blog
634 function _ifAdmin($blogName = '') {
635 global $blog, $member, $manager;
637 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
639 // when no blog found
640 if (($blogName == '') && (!is_object($b)))
643 // explicit blog selection
645 $blogid = getBlogIDFromName($blogName);
647 if (($blogName == '') || !$manager->existsBlogID($blogid))
649 $blogid = $b->getID();
651 return $member->isBlogAdmin($blogid);
657 * -> checks if plugin exists
658 * hasplugin,PlugName,OptionName
659 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
660 * hasplugin,PlugName,OptionName=value
661 * -> checks if the option OptionName from plugin PlugName is set to value
663 function _ifHasPlugin($name, $value) {
666 // (pluginInstalled method won't write a message in the actionlog on failure)
667 if ($manager->pluginInstalled('NP_'.$name)) {
668 $plugin =& $manager->getPlugin('NP_' . $name);
669 if ($plugin != NULL) {
673 list($name2, $value2) = explode('=', $value, 2);
674 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
676 } else if ($plugin->getOption($name2) == $value2) {
686 * Checks if a plugin exists and call its doIf function
688 function _ifPlugin($name, $key = '', $value = '') {
691 $plugin =& $manager->getPlugin('NP_' . $name);
692 if (!$plugin) return;
694 $params = func_get_args();
695 array_shift($params);
697 return call_user_func_array(array($plugin, 'doIf'), $params);