OSDN Git Service

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