OSDN Git Service

本家Nucleus CMSの開発を補助するためにコミット
[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         function setCurrentComment(&$comment) {
96
97                 global $manager;
98
99                 // begin if: member comment
100                 if ($comment['memberid'] != 0)
101                 {
102                         $comment['authtext'] = $template['COMMENTS_AUTH'];
103
104                         $mem =& $manager->getMember($comment['memberid']);
105                         $comment['user'] = $mem->getDisplayName();
106
107                         // begin if: member URL exists, set it as the userid
108                         if ($mem->getURL() )
109                         {
110                                 $comment['userid'] = $mem->getURL();
111                         }
112                         // else: set the email as the userid
113                         else
114                         {
115                                 $comment['userid'] = $mem->getEmail();
116                         } // end if
117
118                         $comment['userlinkraw'] = createLink(
119                                                                                 'member',
120                                                                                 array(
121                                                                                         'memberid' => $comment['memberid'],
122                                                                                         'name' => $mem->getDisplayName(),
123                                                                                         'extra' => $this->commentsObj->itemActions->linkparams
124                                                                                 )
125                                                                         );
126
127                 }
128                 // else: non-member comment
129                 else
130                 {
131
132                         // create smart links
133
134                         // begin if: comment userid is not empty
135                         if (!empty($comment['userid']) )
136                         {
137
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) )
140                                 {
141                                         $comment['userlinkraw'] = $comment['userid'];
142                                 }
143                                 // else: prepend the "http://" (backwards compatibility before rev 1471)
144                                 else
145                                 {
146                                         $comment['userlinkraw'] = 'http://' . $comment['userid'];
147                                 } // end if
148
149                         }
150                         // else if: comment email is valid
151                         else if (isValidMailAddress($comment['email']) )
152                         {
153                                 $comment['userlinkraw'] = 'mailto:' . $comment['email'];
154                         }
155                         // else if: comment userid is a valid email
156                         else if (isValidMailAddress($comment['userid']) )
157                         {
158                                 $comment['userlinkraw'] = 'mailto:' . $comment['userid'];
159                         } // end if
160
161                 } // end if
162
163                 $this->currentComment =& $comment;
164                 global $currentcommentid, $currentcommentarray;
165                 $currentcommentid = $comment['commentid'];
166                 $currentcommentarray = $comment;
167         }
168
169         /**
170          * Parse templatevar authtext
171          */
172         function parse_authtext() {
173                 if ($this->currentComment['memberid'] != 0)
174                         $this->parser->parse($this->template['COMMENTS_AUTH']);
175         }
176
177         /**
178          * Parse templatevar blogid
179          */
180         function parse_blogid() {
181                 echo $this->currentComment['blogid'];
182         }
183
184         /**
185          * Parse templatevar blogurl
186          */
187         function parse_blogurl() {
188                 global $manager;
189                 $blogid = getBlogIDFromItemID($this->commentsObj->itemid);
190                 $blog =& $manager->getBlog($blogid);
191                 echo $blog->getURL();
192         }
193
194         /**
195          * Parse templatevar body
196          */
197         function parse_body() {
198                 echo $this->highlight($this->currentComment['body']);
199         }
200
201         /**
202          * Parse templatevar commentcount
203          */
204         function parse_commentcount() {
205                         echo $this->commentsObj->commentcount;
206         }
207
208         /**
209          * Parse templatevar commentid
210          */
211         function parse_commentid() {
212                 echo $this->currentComment['commentid'];
213         }
214
215         /**
216          * Parse templatevar commentword
217          */
218         function parse_commentword() {
219                 if ($this->commentsObj->commentcount == 1)
220                         echo $this->template['COMMENTS_ONE'];
221                 else
222                         echo $this->template['COMMENTS_MANY'];
223         }
224
225         /**
226          * Parse templatevar date
227          */
228         function parse_date($format = '') {
229                 echo formatDate($format, $this->currentComment['timestamp'], $this->template['FORMAT_DATE'], $this->commentsObj->itemActions->blog);
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          * Parse templatevar excerpt
244          */
245         function parse_excerpt() {
246                 echo stringToXML(shorten($this->currentComment['body'], 60, '...'));
247         }
248
249         /**
250          * Parse templatevar host
251          */
252         function parse_host() {
253                 echo $this->currentComment['host'];
254         }
255
256         /**
257          * Parse templatevar ip
258          */
259         function parse_ip() {
260                 echo $this->currentComment['ip'];
261         }
262
263         /**
264          * Parse templatevar itemid
265          */
266         function parse_itemid() {
267                 echo $this->commentsObj->itemid;
268         }
269
270         /**
271          * Parse templatevar itemlink
272          */
273         function parse_itemlink() {
274                 echo createLink(
275                         'item',
276                         array(
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
281                         )
282                 );
283         }
284
285         /**
286          * Parse templatevar itemtitle
287          */
288         function parse_itemtitle($maxLength = 0) {
289                 if ($maxLength == 0)
290                         $this->commentsObj->itemActions->parse_title();
291                 else
292                         $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
293         }
294
295         /**
296          * Parse templatevar memberid
297          */
298         function parse_memberid() {
299                 echo $this->currentComment['memberid'];
300         }
301
302         /**
303          * Parse templatevar short
304          */
305         function parse_short() {
306                 $tmp = strtok($this->currentComment['body'],"\n");
307                 $tmp = str_replace('<br />','',$tmp);
308                 echo $tmp;
309                 if ($tmp != $this->currentComment['body'])
310                         $this->parser->parse($this->template['COMMENTS_CONTINUED']);
311         }
312
313         /**
314          * Parse templatevar time
315          */
316         function parse_time($format = '') {
317                 echo i18n::strftime(
318                                 ($format == '') ? $this->template['FORMAT_TIME'] : $format,
319                                 $this->currentComment['timestamp']
320                         );
321         }
322
323         /**
324          * Parse templatevar timestamp
325          */
326         function parse_timestamp() {
327                 echo $this->currentComment['timestamp'];
328         }
329
330         /**
331           * Executes a plugin templatevar
332           *
333           * @param pluginName name of plugin (without the NP_)
334           *
335           * extra parameters can be added
336           */
337         function parse_plugin($pluginName) {
338                 global $manager;
339
340                 // only continue when the plugin is really installed
341                 if (!$manager->pluginInstalled('NP_' . $pluginName))
342                         return;
343
344                 $plugin =& $manager->getPlugin('NP_' . $pluginName);
345                 if (!$plugin) return;
346
347                 // get arguments
348                 $params = func_get_args();
349
350                 // remove plugin name
351                 array_shift($params);
352
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);
356
357                 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
358         }
359
360         /**
361          * Parse templatevar user
362          * @param string $mode
363          */
364         function parse_user($mode = '')
365         {
366                 global $manager;
367
368                 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
369                 {
370                         $member =& $manager->getMember($this->currentComment['memberid']);
371                         echo $member->getRealName();
372                 }
373                 else
374                 {
375                         echo i18n::hsc($this->currentComment['user']);
376                 }
377         }
378
379         /**
380          * Parse templatevar useremail
381          */
382         function parse_useremail() {
383                 global $manager;
384                 if ($this->currentComment['memberid'] > 0)
385                 {
386                         $member =& $manager->getMember($this->currentComment['memberid']);
387
388                         if ($member->email != '')
389                                 echo $member->email;
390                 }
391                 else
392                 {
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']);
399                 }
400         }
401
402         /**
403          * Parse templatevar userid
404          */
405         function parse_userid() {
406                         echo $this->currentComment['userid'];
407         }
408
409
410         /**
411          * Parse templatevar userlink
412          */
413         function parse_userlink() {
414                 if ($this->currentComment['userlinkraw']) {
415                         echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
416                 } else {
417                         echo $this->currentComment['user'];
418                 }
419         }
420
421         /**
422          * Parse templatevar userlinkraw
423          */
424         function parse_userlinkraw() {
425                 echo $this->currentComment['userlinkraw'];
426         }
427
428         /**
429          * Parse templatevar userwebsite
430          */
431         function parse_userwebsite() {
432                 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false))
433                         echo $this->currentComment['userlinkraw'];
434         }
435
436         /**
437          * Parse templatevar userwebsitelink
438          */
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>';
442                 } else {
443                         echo $this->currentComment['user'];
444                 }
445         }
446
447         // function to enable if-else-elseif-elseifnot-ifnot-endif to comment template fields
448
449         /**
450          * Checks conditions for if statements
451          *
452          * @param string $field type of <%if%>
453          * @param string $name property of field
454          * @param string $value value of property
455          */
456         function checkCondition($field, $name='', $value = '') {
457                 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
458
459                 $condition = 0;
460                 switch($field) {
461                         case 'category':
462                                 $condition = ($blog && $this->_ifCategory($name,$value));
463                                 break;
464                         case 'itemcategory':
465                                 $condition = ($this->_ifItemCategory($name,$value));
466                                 break;
467                         case 'blogsetting':
468                                 $condition = ($blog && ($blog->getSetting($name) == $value));
469                                 break;
470                         case 'itemblogsetting':
471                                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
472                                 $condition = ($b && ($b->getSetting($name) == $value));
473                                 break;
474                         case 'loggedin':
475                                 $condition = $member->isLoggedIn();
476                                 break;
477                         case 'onteam':
478                                 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
479                                 break;
480                         case 'admin':
481                                 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
482                                 break;
483                         case 'author':
484                                 $condition = ($this->_ifAuthor($name,$value));
485                                 break;
486 /*                      case 'nextitem':
487                                 $condition = ($itemidnext != '');
488                                 break;
489                         case 'previtem':
490                                 $condition = ($itemidprev != '');
491                                 break;
492                         case 'archiveprevexists':
493                                 $condition = ($archiveprevexists == true);
494                                 break;
495                         case 'archivenextexists':
496                                 $condition = ($archivenextexists == true);
497                                 break;
498                         case 'skintype':
499                                 $condition = ($name == $this->skintype);
500                                 break; */
501                         case 'hasplugin':
502                                 $condition = $this->_ifHasPlugin($name, $value);
503                                 break;
504                         default:
505                                 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
506                                 break;
507                 }
508                 return $condition;
509         }
510
511         /**
512          *  Different checks for a category
513          */
514         function _ifCategory($name = '', $value='') {
515                 global $blog, $catid;
516
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);
520
521                 // check category name
522                 if ($name == 'catname') {
523                         $value = $blog->getCategoryIdFromName($value);
524                         if ($value == $catid)
525                                 return $blog->isValidCategory($catid);
526                 }
527
528                 // check category id
529                 if (($name == 'catid') && ($value == $catid))
530                         return $blog->isValidCategory($catid);
531
532                 return false;
533         }
534
535
536         /**
537          *  Different checks for an author
538          */
539         function _ifAuthor($name = '', $value='') {
540                 global $member, $manager;
541
542                 if ($this->currentComment['memberid'] == 0) return false;
543
544                 $mem =& $manager->getMember($this->currentComment['memberid']);
545                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
546                 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
547
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']));
551                 }
552
553                 // check comment author name
554                 if ($name == 'name') {
555                         $value = trim(strtolower($value));
556                         if ($value == '')
557                                 return false;
558                         if ($value == strtolower($mem->getDisplayName()))
559                                 return true;
560                 }
561
562                 // check if comment author is admin
563                 if ($name == 'isadmin') {
564                         $blogid = intval($b->getID());
565                         if ($mem->isAdmin())
566                                 return true;
567
568                         return $mem->isBlogAdmin($blogid);
569                 }
570
571                 // check if comment author is item author
572                 if ($name == 'isauthor') {
573                         return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
574                 }
575
576                 // check if comment author is on team
577                 if ($name == 'isonteam') {
578                         return $mem->teamRights(intval($b->getID()));
579                 }
580
581                 return false;
582         }
583
584         /**
585          *  Different checks for a category
586          */
587         function _ifItemCategory($name = '', $value='') {
588                 global $catid, $manager;
589
590                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
591                 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
592                 $icatid = $citem['catid'];
593
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);
597
598                 // check category name
599                 if ($name == 'catname') {
600                         $value = $b->getCategoryIdFromName($value);
601                         if ($value == $icatid)
602                                 return $b->isValidCategory($icatid);
603                 }
604
605                 // check category id
606                 if (($name == 'catid') && ($value == $icatid))
607                         return $b->isValidCategory($icatid);
608
609                 return false;
610         }
611
612
613         /**
614          *  Checks if a member is on the team of a blog and return his rights
615          */
616         function _ifOnTeam($blogName = '') {
617                 global $blog, $member, $manager;
618
619                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
620
621                 // when no blog found
622                 if (($blogName == '') && (!is_object($b)))
623                         return 0;
624
625                 // explicit blog selection
626                 if ($blogName != '')
627                         $blogid = getBlogIDFromName($blogName);
628
629                 if (($blogName == '') || !$manager->existsBlogID($blogid))
630                         // use current blog
631                         $blogid = $b->getID();
632
633                 return $member->teamRights($blogid);
634         }
635
636         /**
637          *  Checks if a member is admin of a blog
638          */
639         function _ifAdmin($blogName = '') {
640                 global $blog, $member, $manager;
641
642                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
643
644                 // when no blog found
645                 if (($blogName == '') && (!is_object($b)))
646                         return 0;
647
648                 // explicit blog selection
649                 if ($blogName != '')
650                         $blogid = getBlogIDFromName($blogName);
651
652                 if (($blogName == '') || !$manager->existsBlogID($blogid))
653                         // use current blog
654                         $blogid = $b->getID();
655
656                 return $member->isBlogAdmin($blogid);
657         }
658
659
660         /**
661          *      hasplugin,PlugName
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
667          */
668         function _ifHasPlugin($name, $value) {
669                 global $manager;
670                 $condition = false;
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) {
675                                 if ($value == "") {
676                                         $condition = true;
677                                 } else {
678                                         list($name2, $value2) = i18n::explode('=', $value, 2);
679                                         if ($value2 == "" && $plugin->getOption($name2) != 'no') {
680                                                 $condition = true;
681                                         } else if ($plugin->getOption($name2) == $value2) {
682                                                 $condition = true;
683                                         }
684                                 }
685                         }
686                 }
687                 return $condition;
688         }
689
690         /**
691          * Checks if a plugin exists and call its doIf function
692          */
693         function _ifPlugin($name, $key = '', $value = '') {
694                 global $manager;
695
696                 $plugin =& $manager->getPlugin('NP_' . $name);
697                 if (!$plugin) return;
698
699                 $params = func_get_args();
700                 array_shift($params);
701
702                 return call_user_func_array(array(&$plugin, 'doIf'), $params);
703         }
704
705 }
706 ?>