OSDN Git Service

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