OSDN Git Service

CHANGE: Linkクラスを参照せずにglobalfunctions.phpのラッパー関数を参照しているスクリプトを修正。
[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          * Parse templatevar date
220          */
221         function parse_date($format = '') {
222                 echo formatDate($format, $this->currentComment['timestamp'], $this->template['FORMAT_DATE'], $this->commentsObj->itemActions->blog);
223         }
224
225         /**
226          * Parse templatevar email
227          */
228         function parse_email() {
229                 $email = $this->currentComment['email'];
230                 $email = str_replace('@', ' (at) ', $email);
231                 $email = str_replace('.', ' (dot) ', $email);
232                 echo $email;
233         }
234
235         /**
236          * COMMENTACTIONS::parse_excerpt()
237          * Parse templatevar excerpt
238          * 
239          * @param       Void
240          * @return      String  templatevar excerpt
241          */
242         function parse_excerpt()
243         {
244                 echo stringToXML(shorten($this->currentComment['body'], 60, '...'));
245         }
246
247         /**
248          * Parse templatevar host
249          */
250         function parse_host() {
251                 echo $this->currentComment['host'];
252         }
253
254         /**
255          * Parse templatevar ip
256          */
257         function parse_ip() {
258                 echo $this->currentComment['ip'];
259         }
260
261         /**
262          * Parse templatevar itemid
263          */
264         function parse_itemid() {
265                 echo $this->commentsObj->itemid;
266         }
267
268         /**
269          * Parse templatevar itemlink
270          */
271         function parse_itemlink() {
272                 echo Link::create_link(
273                         'item',
274                         array(
275                                 'itemid' => $this->commentsObj->itemid,
276                                 'timestamp' => $this->commentsObj->itemActions->currentItem->timestamp,
277                                 'title' => $this->commentsObj->itemActions->currentItem->title,
278                                 'extra' => $this->commentsObj->itemActions->linkparams
279                         )
280                 );
281         }
282
283         /**
284          * Parse templatevar itemtitle
285          */
286         function parse_itemtitle($maxLength = 0) {
287                 if ($maxLength == 0)
288                         $this->commentsObj->itemActions->parse_title();
289                 else
290                         $this->commentsObj->itemActions->parse_syndicate_title($maxLength);
291         }
292
293         /**
294          * Parse templatevar memberid
295          */
296         function parse_memberid() {
297                 echo $this->currentComment['memberid'];
298         }
299
300         /**
301          * Parse templatevar short
302          */
303         function parse_short() {
304                 $tmp = strtok($this->currentComment['body'],"\n");
305                 $tmp = str_replace('<br />','',$tmp);
306                 echo $tmp;
307                 if ($tmp != $this->currentComment['body'])
308                         $this->parser->parse($this->template['COMMENTS_CONTINUED']);
309         }
310
311         /**
312          * Parse templatevar time
313          */
314         function parse_time($format = '') {
315                 echo i18n::strftime(
316                                 ($format == '') ? $this->template['FORMAT_TIME'] : $format,
317                                 $this->currentComment['timestamp']
318                         );
319         }
320
321         /**
322          * Parse templatevar timestamp
323          */
324         function parse_timestamp() {
325                 echo $this->currentComment['timestamp'];
326         }
327
328         /**
329           * Executes a plugin templatevar
330           *
331           * @param pluginName name of plugin (without the NP_)
332           *
333           * extra parameters can be added
334           */
335         function parse_plugin($pluginName) {
336                 global $manager;
337
338                 // only continue when the plugin is really installed
339                 if (!$manager->pluginInstalled('NP_' . $pluginName))
340                         return;
341
342                 $plugin =& $manager->getPlugin('NP_' . $pluginName);
343                 if (!$plugin) return;
344
345                 // get arguments
346                 $params = func_get_args();
347
348                 // remove plugin name
349                 array_shift($params);
350
351                 // pass info on current item and current comment as well
352                 $params = array_merge(array(&$this->currentComment),$params);
353                 $params = array_merge(array(&$this->commentsObj->itemActions->currentItem),$params);
354
355                 call_user_func_array(array(&$plugin,'doTemplateCommentsVar'), $params);
356         }
357
358         /**
359          * Parse templatevar user
360          * @param string $mode
361          */
362         function parse_user($mode = '')
363         {
364                 global $manager;
365
366                 if ( $mode == 'realname' && $this->currentComment['memberid'] > 0 )
367                 {
368                         $member =& $manager->getMember($this->currentComment['memberid']);
369                         echo $member->getRealName();
370                 }
371                 else
372                 {
373                         echo ENTITY::hsc($this->currentComment['user']);
374                 }
375         }
376
377         /**
378          * COMMENTACTIONS::parse_useremail()
379          * Output mail address
380          * 
381          * @param       void
382          * @return      void
383          */
384         function parse_useremail() {
385                 global $manager;
386                 if ( $this->currentComment['memberid'] > 0 )
387                 {
388                         $member =& $manager->getMember($this->currentComment['memberid']);
389                         
390                         if ( $member->email != '' )
391                         {
392                                 echo $member->email;
393                         }
394                 }
395                 else
396                 {
397                         if ( NOTIFICATION::address_validation($this->currentComment['email']) )
398                         {
399                                 echo $this->currentComment['email'];
400                         }
401                         elseif ( NOTIFICATION::address_validation($this->currentComment['userid']) )
402                         {
403                                 echo $this->currentComment['userid'];
404                         }
405                 }
406                 return;
407         }
408         
409         /**
410          * Parse templatevar userid
411          */
412         function parse_userid() {
413                         echo $this->currentComment['userid'];
414         }
415
416
417         /**
418          * Parse templatevar userlink
419          */
420         function parse_userlink() {
421                 if ($this->currentComment['userlinkraw']) {
422                         echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
423                 } else {
424                         echo $this->currentComment['user'];
425                 }
426         }
427
428         /**
429          * Parse templatevar userlinkraw
430          */
431         function parse_userlinkraw() {
432                 echo $this->currentComment['userlinkraw'];
433         }
434
435         /**
436          * Parse templatevar userwebsite
437          */
438         function parse_userwebsite() {
439                 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false))
440                         echo $this->currentComment['userlinkraw'];
441         }
442
443         /**
444          * Parse templatevar userwebsitelink
445          */
446         function parse_userwebsitelink() {
447                 if (!(i18n::strpos($this->currentComment['userlinkraw'], 'http://') === false)) {
448                         echo '<a href="'.$this->currentComment['userlinkraw'].'" rel="nofollow">'.$this->currentComment['user'].'</a>';
449                 } else {
450                         echo $this->currentComment['user'];
451                 }
452         }
453
454         // function to enable if-else-elseif-elseifnot-ifnot-endif to comment template fields
455
456         /**
457          * Checks conditions for if statements
458          *
459          * @param string $field type of <%if%>
460          * @param string $name property of field
461          * @param string $value value of property
462          */
463         function checkCondition($field, $name='', $value = '') {
464                 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
465
466                 $condition = 0;
467                 switch($field) {
468                         case 'category':
469                                 $condition = ($blog && $this->_ifCategory($name,$value));
470                                 break;
471                         case 'itemcategory':
472                                 $condition = ($this->_ifItemCategory($name,$value));
473                                 break;
474                         case 'blogsetting':
475                                 $condition = ($blog && ($blog->getSetting($name) == $value));
476                                 break;
477                         case 'itemblogsetting':
478                                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
479                                 $condition = ($b && ($b->getSetting($name) == $value));
480                                 break;
481                         case 'loggedin':
482                                 $condition = $member->isLoggedIn();
483                                 break;
484                         case 'onteam':
485                                 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
486                                 break;
487                         case 'admin':
488                                 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
489                                 break;
490                         case 'author':
491                                 $condition = ($this->_ifAuthor($name,$value));
492                                 break;
493 /*                      case 'nextitem':
494                                 $condition = ($itemidnext != '');
495                                 break;
496                         case 'previtem':
497                                 $condition = ($itemidprev != '');
498                                 break;
499                         case 'archiveprevexists':
500                                 $condition = ($archiveprevexists == true);
501                                 break;
502                         case 'archivenextexists':
503                                 $condition = ($archivenextexists == true);
504                                 break;
505                         case 'skintype':
506                                 $condition = ($name == $this->skintype);
507                                 break; */
508                         case 'hasplugin':
509                                 $condition = $this->_ifHasPlugin($name, $value);
510                                 break;
511                         default:
512                                 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
513                                 break;
514                 }
515                 return $condition;
516         }
517
518         /**
519          *  Different checks for a category
520          */
521         function _ifCategory($name = '', $value='') {
522                 global $blog, $catid;
523
524                 // when no parameter is defined, just check if a category is selected
525                 if (($name != 'catname' && $name != 'catid') || ($value == ''))
526                         return $blog->isValidCategory($catid);
527
528                 // check category name
529                 if ($name == 'catname') {
530                         $value = $blog->getCategoryIdFromName($value);
531                         if ($value == $catid)
532                                 return $blog->isValidCategory($catid);
533                 }
534
535                 // check category id
536                 if (($name == 'catid') && ($value == $catid))
537                         return $blog->isValidCategory($catid);
538
539                 return false;
540         }
541
542
543         /**
544          *  Different checks for an author
545          */
546         function _ifAuthor($name = '', $value='') {
547                 global $member, $manager;
548
549                 if ($this->currentComment['memberid'] == 0) return false;
550
551                 $mem =& $manager->getMember($this->currentComment['memberid']);
552                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
553                 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
554
555                 // when no parameter is defined, just check if item author is current visitor
556                 if (($name != 'isadmin' && $name != 'name' && $name != 'isauthor' && $name != 'isonteam')) {
557                         return (intval($member->getID()) > 0 && intval($member->getID()) == intval($citem['authorid']));
558                 }
559
560                 // check comment author name
561                 if ($name == 'name') {
562                         $value = trim(strtolower($value));
563                         if ($value == '')
564                                 return false;
565                         if ($value == strtolower($mem->getDisplayName()))
566                                 return true;
567                 }
568
569                 // check if comment author is admin
570                 if ($name == 'isadmin') {
571                         $blogid = intval($b->getID());
572                         if ($mem->isAdmin())
573                                 return true;
574
575                         return $mem->isBlogAdmin($blogid);
576                 }
577
578                 // check if comment author is item author
579                 if ($name == 'isauthor') {
580                         return (intval($citem['authorid']) == intval($this->currentComment['memberid']));
581                 }
582
583                 // check if comment author is on team
584                 if ($name == 'isonteam') {
585                         return $mem->teamRights(intval($b->getID()));
586                 }
587
588                 return false;
589         }
590
591         /**
592          *  Different checks for a category
593          */
594         function _ifItemCategory($name = '', $value='') {
595                 global $catid, $manager;
596
597                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
598                 $citem =& $manager->getItem($this->currentComment['itemid'],1,1);
599                 $icatid = $citem['catid'];
600
601                 // when no parameter is defined, just check if a category is selected
602                 if (($name != 'catname' && $name != 'catid') || ($value == ''))
603                         return $b->isValidCategory($icatid);
604
605                 // check category name
606                 if ($name == 'catname') {
607                         $value = $b->getCategoryIdFromName($value);
608                         if ($value == $icatid)
609                                 return $b->isValidCategory($icatid);
610                 }
611
612                 // check category id
613                 if (($name == 'catid') && ($value == $icatid))
614                         return $b->isValidCategory($icatid);
615
616                 return false;
617         }
618
619
620         /**
621          *  Checks if a member is on the team of a blog and return his rights
622          */
623         function _ifOnTeam($blogName = '') {
624                 global $blog, $member, $manager;
625
626                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
627
628                 // when no blog found
629                 if (($blogName == '') && (!is_object($b)))
630                         return 0;
631
632                 // explicit blog selection
633                 if ($blogName != '')
634                         $blogid = getBlogIDFromName($blogName);
635
636                 if (($blogName == '') || !$manager->existsBlogID($blogid))
637                         // use current blog
638                         $blogid = $b->getID();
639
640                 return $member->teamRights($blogid);
641         }
642
643         /**
644          *  Checks if a member is admin of a blog
645          */
646         function _ifAdmin($blogName = '') {
647                 global $blog, $member, $manager;
648
649                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentComment['itemid']));
650
651                 // when no blog found
652                 if (($blogName == '') && (!is_object($b)))
653                         return 0;
654
655                 // explicit blog selection
656                 if ($blogName != '')
657                         $blogid = getBlogIDFromName($blogName);
658
659                 if (($blogName == '') || !$manager->existsBlogID($blogid))
660                         // use current blog
661                         $blogid = $b->getID();
662
663                 return $member->isBlogAdmin($blogid);
664         }
665
666
667         /**
668          *      hasplugin,PlugName
669          *         -> checks if plugin exists
670          *      hasplugin,PlugName,OptionName
671          *         -> checks if the option OptionName from plugin PlugName is not set to 'no'
672          *      hasplugin,PlugName,OptionName=value
673          *         -> checks if the option OptionName from plugin PlugName is set to value
674          */
675         function _ifHasPlugin($name, $value) {
676                 global $manager;
677                 $condition = false;
678                 // (pluginInstalled method won't write a message in the actionlog on failure)
679                 if ($manager->pluginInstalled('NP_'.$name)) {
680                         $plugin =& $manager->getPlugin('NP_' . $name);
681                         if ($plugin != NULL) {
682                                 if ($value == "") {
683                                         $condition = true;
684                                 } else {
685                                         list($name2, $value2) = i18n::explode('=', $value, 2);
686                                         if ($value2 == "" && $plugin->getOption($name2) != 'no') {
687                                                 $condition = true;
688                                         } else if ($plugin->getOption($name2) == $value2) {
689                                                 $condition = true;
690                                         }
691                                 }
692                         }
693                 }
694                 return $condition;
695         }
696
697         /**
698          * Checks if a plugin exists and call its doIf function
699          */
700         function _ifPlugin($name, $key = '', $value = '') {
701                 global $manager;
702
703                 $plugin =& $manager->getPlugin('NP_' . $name);
704                 if (!$plugin) return;
705
706                 $params = func_get_args();
707                 array_shift($params);
708
709                 return call_user_func_array(array(&$plugin, 'doIf'), $params);
710         }
711
712 }
713 ?>