OSDN Git Service

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