OSDN Git Service

408ba19f7d963b6dd8337d3aba5ff7960a8adbe4
[nucleus-jp/nucleus-next.git] / nucleus / libs / COMMENTACTIONS.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2009 The Nucleus Group
5  *
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)
11  */
12 /**
13  * This class is used when parsing comment templates
14  *
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 $
18  */
19
20 class COMMENTACTIONS extends BaseActions {
21
22         // ref to COMMENTS object which is using this object to handle
23         // its templatevars
24         var $commentsObj;
25
26         // template to use to parse the comments
27         var $template;
28
29         // comment currenlty being handled (mysql result assoc array; see COMMENTS::showComments())
30         var $currentComment;
31
32         function COMMENTACTIONS(&$comments) {
33                 // call constructor of superclass first
34                 $this->BaseActions();
35
36                 // reference to the comments object
37                 $this->setCommentsObj($comments);
38         }
39
40         function getDefinedActions() {
41                 return array(
42                         'blogurl',
43                         'commentcount',
44                         'commentword',
45                         'email',
46                         'itemlink',
47                         'itemid',
48                         'itemtitle',
49                         'date',
50                         'time',
51                         'commentid',
52                         'body',
53                         'memberid',
54                         'timestamp',
55                         'host',
56                         'ip',
57                         'blogid',
58                         'authtext',
59                         'user',
60                         'userid',
61                         'userlinkraw',
62                         'userlink',
63                         'useremail',
64                         'userwebsite',
65                         'userwebsitelink',
66                         'excerpt',
67                         'short',
68                         'skinfile',
69                         'set',
70                         'plugin',
71                         'include',
72                         'phpinclude',
73                         'parsedinclude',
74                         'if',
75                         'else',
76                         'endif',
77                         'elseif',
78                         'ifnot',
79                         'elseifnot'
80                 );
81         }
82
83         function setParser(&$parser) {
84                 $this->parser =& $parser;
85         }
86
87         function setCommentsObj(&$commentsObj) {
88                 $this->commentsObj =& $commentsObj;
89         }
90
91         function setTemplate($template) {
92                 $this->template =& $template;
93         }
94         
95         /**
96          * COMMENTACTIONS::setCurrentComment()
97          * Set $currentcommentid and $currentcommentarray
98          * 
99          * @param       Array   $comment        array with comment elements
100          * @return      void
101          * 
102          */
103         function setCurrentComment(&$comment)
104         {
105                 global $manager;
106                 
107                 if ( $comment['memberid'] != 0 )
108                 {
109                         $comment['authtext'] = $template['COMMENTS_AUTH'];
110                         $mem =& $manager->getMember($comment['memberid']);
111                         $comment['user'] = $mem->getDisplayName();
112                         
113                         if ( $mem->getURL() )
114                         {
115                                 $comment['userid'] = $mem->getURL();
116                         }
117                         else
118                         {
119                                 $comment['userid'] = $mem->getEmail();
120                         }
121                         
122                         $comment['userlinkraw'] = Link::create_link(
123                                                                                 'member',
124                                                                                 array(
125                                                                                         'memberid' => $comment['memberid'],
126                                                                                         'name' => $mem->getDisplayName(),
127                                                                                         'extra' => $this->commentsObj->itemActions->linkparams
128                                                                                 )
129                                                                         );
130                 }
131                 else
132                 {
133                         // create smart links
134                         if ( !empty($comment['userid']) )
135                         {
136                                 if ( (i18n::strpos($comment['userid'], 'http://') === 0) || (i18n::strpos($comment['userid'], 'https://') === 0) )
137                                 {
138                                         $comment['userlinkraw'] = $comment['userid'];
139                                 }
140                                 else
141                                 {
142                                         $comment['userlinkraw'] = 'http://' . $comment['userid'];
143                                 }
144                         }
145                         else if ( NOTIFICATION::address_validation($comment['email']) )
146                         {
147                                 $comment['userlinkraw'] = 'mailto:' . $comment['email'];
148                         }
149                         else if ( NOTIFICATION::address_validation($comment['userid']) )
150                         {
151                                 $comment['userlinkraw'] = 'mailto:' . $comment['userid'];
152                         }
153                 }
154                 
155                 $this->currentComment =& $comment;
156                 global $currentcommentid, $currentcommentarray;
157                 $currentcommentid = $comment['commentid'];
158                 $currentcommentarray = $comment;
159                 return;
160         }
161
162         /**
163          * Parse templatevar authtext
164          */
165         function parse_authtext() {
166                 if ($this->currentComment['memberid'] != 0)
167                         $this->parser->parse($this->template['COMMENTS_AUTH']);
168         }
169
170         /**
171          * Parse templatevar blogid
172          */
173         function parse_blogid() {
174                 echo $this->currentComment['blogid'];
175         }
176
177         /**
178          * Parse templatevar blogurl
179          */
180         function parse_blogurl() {
181                 global $manager;
182                 $blogid = getBlogIDFromItemID($this->commentsObj->itemid);
183                 $blog =& $manager->getBlog($blogid);
184                 echo $blog->getURL();
185         }
186
187         /**
188          * Parse templatevar body
189          */
190         function parse_body() {
191                 echo $this->highlight($this->currentComment['body']);
192         }
193
194         /**
195          * Parse templatevar commentcount
196          */
197         function parse_commentcount() {
198                         echo $this->commentsObj->commentcount;
199         }
200
201         /**
202          * Parse templatevar commentid
203          */
204         function parse_commentid() {
205                 echo $this->currentComment['commentid'];
206         }
207
208         /**
209          * Parse templatevar commentword
210          */
211         function parse_commentword() {
212                 if ($this->commentsObj->commentcount == 1)
213                         echo $this->template['COMMENTS_ONE'];
214                 else
215                         echo $this->template['COMMENTS_MANY'];
216         }
217
218         /**
219          * COMMENTACTIONS::parse_date()
220          * Parse templatevar date
221          * 
222          * @format      String  $format Date format according to PHP
223          * @return      String formatted datetime
224          * 
225          */
226         function parse_date($format = '')
227         {
228                 $offset = $this->commentsObj->itemActions->blog->getTimeOffset() * 3600;
229                 echo i18n::formatted_datetime($format, $this->currentComment['timestamp'], $this->template['FORMAT_DATE'], $offset);
230         }
231
232         /**
233          * Parse templatevar email
234          */
235         function parse_email() {
236                 $email = $this->currentComment['email'];
237                 $email = str_replace('@', ' (at) ', $email);
238                 $email = str_replace('.', ' (dot) ', $email);
239                 echo $email;
240         }
241
242         /**
243          * COMMENTACTIONS::parse_excerpt()
244          * Parse templatevar excerpt
245          * 
246          * @param       Void
247          * @return      String  templatevar excerpt
248          */
249         function parse_excerpt()
250         {
251                 echo ENTITY::hen(ENTITY::shorten($this->currentComment['body'], 60, '...'));
252         }
253
254         /**
255          * Parse templatevar host
256          */
257         function parse_host() {
258                 echo $this->currentComment['host'];
259         }
260
261         /**
262          * Parse templatevar ip
263          */
264         function parse_ip() {
265                 echo $this->currentComment['ip'];
266         }
267
268         /**
269          * Parse templatevar itemid
270          */
271         function parse_itemid() {
272                 echo $this->commentsObj->itemid;
273         }
274
275         /**
276          * Parse templatevar itemlink
277          */
278         function parse_itemlink() {
279                 echo Link::create_link(
280                         'item',
281                         array(
282                                 'itemid' => $this->commentsObj->itemid,
283                                 'timestamp' => $this->commentsObj->itemActions->currentItem->timestamp,
284                                 'title' => $this->commentsObj->itemActions->currentItem->title,
285                                 'extra' => $this->commentsObj->itemActions->linkparams
286                         )
287                 );
288         }
289
290         /**
291          * Parse templatevar itemtitle
292          */
293         function parse_itemtitle($maxLength = 0) {
294                 if ($maxLength == 0)
295                         $this->commentsObj->itemActions->parse_title();
296                 else
297                         $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
298         }
299
300         /**
301          * Parse templatevar memberid
302          */
303         function parse_memberid() {
304                 echo $this->currentComment['memberid'];
305         }
306
307         /**
308          * Parse templatevar short
309          */
310         function parse_short() {
311                 $tmp = strtok($this->currentComment['body'],"\n");
312                 $tmp = str_replace('<br />','',$tmp);
313                 echo $tmp;
314                 if ($tmp != $this->currentComment['body'])
315                         $this->parser->parse($this->template['COMMENTS_CONTINUED']);
316         }
317
318         /**
319          * Parse templatevar time
320          */
321         function parse_time($format = '') {
322                 echo i18n::strftime(
323                                 ($format == '') ? $this->template['FORMAT_TIME'] : $format,
324                                 $this->currentComment['timestamp']
325                         );
326         }
327
328         /**
329          * Parse templatevar timestamp
330          */
331         function parse_timestamp() {
332                 echo $this->currentComment['timestamp'];
333         }
334
335         /**
336           * Executes a plugin templatevar
337           *
338           * @param pluginName name of plugin (without the NP_)
339           *
340           * extra parameters can be added
341           */
342         function parse_plugin($pluginName) {
343                 global $manager;
344
345                 // only continue when the plugin is really installed
346                 if (!$manager->pluginInstalled('NP_' . $pluginName))
347                         return;
348
349                 $plugin =& $manager->getPlugin('NP_' . $pluginName);
350                 if (!$plugin) return;
351
352                 // get arguments
353                 $params = func_get_args();
354
355                 // remove plugin name
356                 array_shift($params);
357
358                 // pass info on current item and current comment as well
359                 $params = array_merge(array(&$this->currentComment),$params);
360                 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem),$params);
361
362                 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
363         }
364
365         /**
366          * Parse templatevar user
367          * @param string $mode
368          */
369         function parse_user($mode = '')
370         {
371                 global $manager;
372
373                 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
374                 {
375                         $member =& $manager->getMember($this->currentComment['memberid']);
376                         echo $member->getRealName();
377                 }
378                 else
379                 {
380                         echo ENTITY::hsc($this->currentComment['user']);
381                 }
382         }
383
384         /**
385          * COMMENTACTIONS::parse_useremail()
386          * Output mail address
387          * 
388          * @param       void
389          * @return      void
390          */
391         function parse_useremail() {
392                 global $manager;
393                 if ( $this->currentComment['memberid'] > 0 )
394                 {
395                         $member =& $manager->getMember($this->currentComment['memberid']);
396                         
397                         if ( $member->email != '' )
398                         {
399                                 echo $member->email;
400                         }
401                 }
402                 else
403                 {
404                         if ( NOTIFICATION::address_validation($this->currentComment['email']) )
405                         {
406                                 echo $this->currentComment['email'];
407                         }
408                         elseif ( NOTIFICATION::address_validation($this->currentComment['userid']) )
409                         {
410                                 echo $this->currentComment['userid'];
411                         }
412                 }
413                 return;
414         }
415         
416         /**
417          * Parse templatevar userid
418          */
419         function parse_userid() {
420                         echo $this->currentComment['userid'];
421         }
422
423
424         /**
425          * Parse templatevar userlink
426          */
427         function parse_userlink() {
428                 if ($this->currentComment['userlinkraw']) {
429                         echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
430                 } else {
431                         echo $this->currentComment['user'];
432                 }
433         }
434
435         /**
436          * Parse templatevar userlinkraw
437          */
438         function parse_userlinkraw() {
439                 echo $this->currentComment['userlinkraw'];
440         }
441
442         /**
443          * Parse templatevar userwebsite
444          */
445         function parse_userwebsite() {
446                 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false))
447                         echo $this->currentComment['userlinkraw'];
448         }
449
450         /**
451          * Parse templatevar userwebsitelink
452          */
453         function parse_userwebsitelink() {
454                 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false)) {
455                         echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
456                 } else {
457                         echo $this->currentComment['user'];
458                 }
459         }
460
461         // function to enable if-else-elseif-elseifnot-ifnot-endif to comment template fields
462
463         /**
464          * Checks conditions for if statements
465          *
466          * @param string $field type of <%if%>
467          * @param string $name property of field
468          * @param string $value value of property
469          */
470         function checkCondition($field, $name='', $value = '') {
471                 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
472
473                 $condition = 0;
474                 switch($field) {
475                         case 'category':
476                                 $condition = ($blog && $this->_ifCategory($name,$value));
477                                 break;
478                         case 'itemcategory':
479                                 $condition = ($this->_ifItemCategory($name,$value));
480                                 break;
481                         case 'blogsetting':
482                                 $condition = ($blog && ($blog->getSetting($name) == $value));
483                                 break;
484                         case 'itemblogsetting':
485                                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
486                                 $condition = ($b && ($b->getSetting($name) == $value));
487                                 break;
488                         case 'loggedin':
489                                 $condition = $member->isLoggedIn();
490                                 break;
491                         case 'onteam':
492                                 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
493                                 break;
494                         case 'admin':
495                                 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
496                                 break;
497                         case 'author':
498                                 $condition = ($this->_ifAuthor($name,$value));
499                                 break;
500 /*                      case 'nextitem':
501                                 $condition = ($itemidnext != '');
502                                 break;
503                         case 'previtem':
504                                 $condition = ($itemidprev != '');
505                                 break;
506                         case 'archiveprevexists':
507                                 $condition = ($archiveprevexists == true);
508                                 break;
509                         case 'archivenextexists':
510                                 $condition = ($archivenextexists == true);
511                                 break;
512                         case 'skintype':
513                                 $condition = ($name == $this->skintype);
514                                 break; */
515                         case 'hasplugin':
516                                 $condition = $this->_ifHasPlugin($name, $value);
517                                 break;
518                         default:
519                                 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
520                                 break;
521                 }
522                 return $condition;
523         }
524
525         /**
526          *  Different checks for a category
527          */
528         function _ifCategory($name = '', $value='') {
529                 global $blog, $catid;
530
531                 // when no parameter is defined, just check if a category is selected
532                 if (($name != 'catname' && $name != 'catid') || ($value == ''))
533                         return $blog->isValidCategory($catid);
534
535                 // check category name
536                 if ($name == 'catname') {
537                         $value = $blog->getCategoryIdFromName($value);
538                         if ($value == $catid)
539                                 return $blog->isValidCategory($catid);
540                 }
541
542                 // check category id
543                 if (($name == 'catid') && ($value == $catid))
544                         return $blog->isValidCategory($catid);
545
546                 return false;
547         }
548
549
550         /**
551          *  Different checks for an author
552          */
553         function _ifAuthor($name = '', $value='') {
554                 global $member, $manager;
555
556                 if ($this->currentComment['memberid'] == 0) return false;
557
558                 $mem =& $manager->getMember($this->currentComment['memberid']);
559                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
560                 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
561
562                 // when no parameter is defined, just check if item author is current visitor
563                 if (($name != 'isadmin' && $name != 'name' && $name != 'isauthor' && $name != 'isonteam')) {
564                         return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
565                 }
566
567                 // check comment author name
568                 if ($name == 'name') {
569                         $value = trim(strtolower($value));
570                         if ($value == '')
571                                 return false;
572                         if ($value == strtolower($mem->getDisplayName()))
573                                 return true;
574                 }
575
576                 // check if comment author is admin
577                 if ($name == 'isadmin') {
578                         $blogid = intval($b->getID());
579                         if ($mem->isAdmin())
580                                 return true;
581
582                         return $mem->isBlogAdmin($blogid);
583                 }
584
585                 // check if comment author is item author
586                 if ($name == 'isauthor') {
587                         return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
588                 }
589
590                 // check if comment author is on team
591                 if ($name == 'isonteam') {
592                         return $mem->teamRights(intval($b->getID()));
593                 }
594
595                 return false;
596         }
597
598         /**
599          *  Different checks for a category
600          */
601         function _ifItemCategory($name = '', $value='') {
602                 global $catid, $manager;
603
604                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
605                 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
606                 $icatid = $citem['catid'];
607
608                 // when no parameter is defined, just check if a category is selected
609                 if (($name != 'catname' && $name != 'catid') || ($value == ''))
610                         return $b->isValidCategory($icatid);
611
612                 // check category name
613                 if ($name == 'catname') {
614                         $value = $b->getCategoryIdFromName($value);
615                         if ($value == $icatid)
616                                 return $b->isValidCategory($icatid);
617                 }
618
619                 // check category id
620                 if (($name == 'catid') && ($value == $icatid))
621                         return $b->isValidCategory($icatid);
622
623                 return false;
624         }
625
626
627         /**
628          *  Checks if a member is on the team of a blog and return his rights
629          */
630         function _ifOnTeam($blogName = '') {
631                 global $blog, $member, $manager;
632
633                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
634
635                 // when no blog found
636                 if (($blogName == '') && (!is_object($b)))
637                         return 0;
638
639                 // explicit blog selection
640                 if ($blogName != '')
641                         $blogid = getBlogIDFromName($blogName);
642
643                 if (($blogName == '') || !$manager->existsBlogID($blogid))
644                         // use current blog
645                         $blogid = $b->getID();
646
647                 return $member->teamRights($blogid);
648         }
649
650         /**
651          *  Checks if a member is admin of a blog
652          */
653         function _ifAdmin($blogName = '') {
654                 global $blog, $member, $manager;
655
656                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
657
658                 // when no blog found
659                 if (($blogName == '') && (!is_object($b)))
660                         return 0;
661
662                 // explicit blog selection
663                 if ($blogName != '')
664                         $blogid = getBlogIDFromName($blogName);
665
666                 if (($blogName == '') || !$manager->existsBlogID($blogid))
667                         // use current blog
668                         $blogid = $b->getID();
669
670                 return $member->isBlogAdmin($blogid);
671         }
672
673
674         /**
675          *      hasplugin,PlugName
676          *         -> checks if plugin exists
677          *      hasplugin,PlugName,OptionName
678          *         -> checks if the option OptionName from plugin PlugName is not set to 'no'
679          *      hasplugin,PlugName,OptionName=value
680          *         -> checks if the option OptionName from plugin PlugName is set to value
681          */
682         function _ifHasPlugin($name, $value) {
683                 global $manager;
684                 $condition = false;
685                 // (pluginInstalled method won't write a message in the actionlog on failure)
686                 if ($manager->pluginInstalled('NP_'.$name)) {
687                         $plugin =& $manager->getPlugin('NP_' . $name);
688                         if ($plugin != NULL) {
689                                 if ($value == "") {
690                                         $condition = true;
691                                 } else {
692                                         list($name2, $value2) = i18n::explode('=', $value, 2);
693                                         if ($value2 == "" && $plugin->getOption($name2) != 'no') {
694                                                 $condition = true;
695                                         } else if ($plugin->getOption($name2) == $value2) {
696                                                 $condition = true;
697                                         }
698                                 }
699                         }
700                 }
701                 return $condition;
702         }
703
704         /**
705          * Checks if a plugin exists and call its doIf function
706          */
707         function _ifPlugin($name, $key = '', $value = '') {
708                 global $manager;
709
710                 $plugin =& $manager->getPlugin('NP_' . $name);
711                 if (!$plugin) return;
712
713                 $params = func_get_args();
714                 array_shift($params);
715
716                 return call_user_func_array(array(&$plugin, 'doIf'), $params);
717         }
718
719 }
720 ?>