OSDN Git Service

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