OSDN Git Service

ADD: 不足していたアクセス修飾子を追加
[nucleus-jp/nucleus-next.git] / nucleus / libs / ITEMACTIONS.php
1 <?php\r
2 /*\r
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
4  * Copyright (C) 2002-2009 The Nucleus Group\r
5  *\r
6  * This program is free software; you can redistribute it and/or\r
7  * modify it under the terms of the GNU General Public License\r
8  * as published by the Free Software Foundation; either version 2\r
9  * of the License, or (at your option) any later version.\r
10  * (see nucleus/documentation/index.html#license for more info)\r
11  */\r
12 /**\r
13  * This class is used to parse item templates\r
14  *\r
15  * @license http://nucleuscms.org/license.txt GNU General Public License\r
16  * @copyright Copyright (C) 2002-2009 The Nucleus Group\r
17  * @version $Id: ITEMACTIONS.php 1757 2012-04-15 09:02:32Z sakamocchi $\r
18  */\r
19 class ItemActions extends BaseActions\r
20 {\r
21         /**\r
22          * ItemActions::$currentItem\r
23          * item currently being handled (mysql result object, see Blog::showUsingQuery)\r
24          */\r
25         public $currentItem;\r
26         \r
27         /**\r
28          * ItemActions::$linkparams\r
29          * contains an assoc array with parameters that need to be included when\r
30          * generating links to items/archives/... (e.g. catid)\r
31          */\r
32         public $linkparams;\r
33         \r
34         /**\r
35          * ItemActions::$allowEditAll\r
36          * true when the current user is a blog admin (and thus allowed to edit all items) \r
37          */\r
38         private $allowEditAll;\r
39         \r
40         /**\r
41          * ItemActions::$lastVisit\r
42          * timestamp of last visit\r
43          */\r
44         private $lastVisit;\r
45         \r
46         /**\r
47          * ItemActions::$blog\r
48          * reference to the blog currently being displayed\r
49          */\r
50         public $blog;
51         \r
52         /**\r
53          * ItemActions::$template\r
54          * associative array with template info (part name => contents)\r
55          */\r
56         private $template;\r
57         \r
58         /**\r
59          * ItemActions::$showComments\r
60          * true when comments need to be displayed\r
61          */\r
62         private $showComments;\r
63         \r
64         /**\r
65          * ItemActions::$defined_actions\r
66          * defined actions in this class\r
67          */\r
68         static private $defined_actions = array(\r
69                 'author',
70                 'authorid',
71                 'authorlink',
72                 'blogid',\r
73                 'blogurl',
74                 'body',\r
75                 'category',\r
76                 'categorylink',\r
77                 'catid',\r
78                 'closed',
79                 'comments',
80                 'date',\r
81                 'daylink',
82                 'edit',
83                 'editlink',
84                 'editpopupcode',
85                 'itemid',
86                 'itemlink',\r
87                 'karma',
88                 'karmaneglink',
89                 'karmaposlink',\r
90                 'more',
91                 'morelink',
92                 'new',\r
93                 'plugin',
94                 'query',
95                 'relevance',
96                 'smartbody',
97                 'syndicate_description',
98                 'syndicate_title',
99                 'time',
100                 'title',
101         /* actions defined in BodyAction class */
102                 'image',\r
103                 'media',
104                 'popup',\r
105         );\r
106         \r
107         /**\r
108          * ItemActions::__construct\r
109          * Enter description here ...\r
110          * @param unknown_type $blog\r
111          */\r
112         public function __construct(&$blog)\r
113         {\r
114                 global $catid, $member;\r
115                 // call constructor of superclass first\r
116                 parent::__construct();
117                 \r
118                 // extra parameters for created links\r
119                 if ( $catid )\r
120                 {\r
121                         $this->linkparams = array('catid' => $catid);\r
122                 }\r
123                 \r
124                 // check if member is blog admin (and thus allowed to edit all items)\r
125                 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));\r
126                 $this->setBlog($blog);\r
127                 return;\r
128         }\r
129         \r
130         /**\r
131          * ItemActions::getAvailableActions()\r
132          * Returns an array with the actions that are defined\r
133          * in the ItemActions class\r
134          * \r
135          * @param       void\r
136          * @return      void\r
137          */\r
138         public function getAvailableActions()\r
139         {\r
140                 return array_merge(self::$defined_actions, parent::getAvailableActions());\r
141         }\r
142         \r
143         /**\r
144          * ItemActions::setLastVisit()\r
145          * \r
146          * @param       timestamp       $lastVisit      timestamp of latest visit\r
147          * @return      void\r
148          */\r
149         public function setLastVisit($lastVisit)\r
150         {\r
151                 $this->lastVisit = $lastVisit;\r
152                 return;\r
153         }\r
154         \r
155         /**\r
156          * ItemActions::setCurrentItem()\r
157          * \r
158          * @param       object  $item   instance of Item class\r
159          * @return      void\r
160          */\r
161         public function setCurrentItem(&$item)\r
162         {\r
163                 global $currentitemid;\r
164                 $this->currentItem =& $item;\r
165                 $currentitemid = $this->currentItem->itemid;\r
166                 return;\r
167         }\r
168         \r
169         /**\r
170          * ItemActions::setBlog()\r
171          * \r
172          * @param       object  &$blog  instance of Blog class\r
173          * @return      void\r
174          */\r
175         public function setBlog(&$blog)\r
176         {\r
177                 $this->blog =& $blog;\r
178                 return;\r
179         }\r
180         \r
181         /**\r
182          * ItemActions::setTemplate()\r
183          * \r
184          * @param       array   $template       array including templates\r
185          * @return      void\r
186          */\r
187         public function setTemplate($template)\r
188         {\r
189                 $this->template =& $template;\r
190                 return;\r
191         }\r
192         \r
193         /**\r
194          * ItemActions::setShowComments()\r
195          * \r
196          * @param       boolean $val    need to be displayed or not\r
197          * @return      void\r
198          */\r
199         public function setShowComments($val)\r
200         {\r
201                 $this->showComments = (boolean) $val;\r
202                 return;\r
203         }\r
204         \r
205         /**\r
206          * ItemActions::parse_blogid()\r
207          * Parse templatevar blogid\r
208          * \r
209          * @param       void\r
210          * @return      void\r
211          */\r
212         public function parse_blogid()\r
213         {\r
214                 echo $this->blog->getID();\r
215         }\r
216 \r
217         /**\r
218          * ItemActions::parse_body()\r
219          * Parse templatevar body\r
220          * \r
221          * @param       void\r
222          * @return      void\r
223          */\r
224         public function parse_body()\r
225         {\r
226                 $this->highlightAndParse($this->currentItem->body);\r
227                 return;\r
228         }\r
229         \r
230         /**\r
231          * ItemActions::parse_more()\r
232          * Parse templatevar more\r
233          * \r
234          * @param       void\r
235          * @return      void\r
236          */\r
237         public function parse_more()\r
238         {\r
239                 $this->highlightAndParse($this->currentItem->more);\r
240                 return;\r
241         }\r
242         \r
243         /**\r
244          * ItemActions::parse_itemid()\r
245          * Parse templatevar itemid\r
246          * \r
247          * @param       void\r
248          * @return      void\r
249          */\r
250         public function parse_itemid()\r
251         {\r
252                 echo $this->currentItem->itemid;\r
253                 return;\r
254         }\r
255         \r
256         /**\r
257          * ItemActions::parse_category()\r
258          * Parse templatevar category\r
259          * \r
260          * @param       void\r
261          * @return      void\r
262          */\r
263         public function parse_category()\r
264         {\r
265                 echo $this->currentItem->category;\r
266                 return;\r
267         }\r
268         \r
269         /**\r
270          * ItemActions::parse_categorylink()\r
271          * Parse templatevar categorylink\r
272          * \r
273          * @param       void\r
274          * @return      void\r
275          */\r
276         public function parse_categorylink()\r
277         {\r
278                 echo Link::create_link('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));\r
279                 return;\r
280         }\r
281         \r
282         /**\r
283          * ItemActions::parse_catid()\r
284          * Parse templatevar catid\r
285          * \r
286          * @param       void\r
287          * @return      void\r
288          */\r
289         public function parse_catid()\r
290         {\r
291                 echo $this->currentItem->catid;\r
292                 return;\r
293         }\r
294         \r
295         /**\r
296          * ItemActions::parse_authorid()\r
297          * Parse templatevar authorid\r
298          * \r
299          * @param       void\r
300          * @return      void\r
301          */\r
302         public function parse_authorid()\r
303         {\r
304                 echo $this->currentItem->authorid;\r
305                 return;\r
306         }\r
307         \r
308         /**\r
309          * ItemActions::parse_authorlink()\r
310          * Parse templatevar authorlink\r
311          * \r
312          * @param       void\r
313          * @return      void\r
314          */\r
315         public function parse_authorlink()\r
316         {\r
317                 $data = array(\r
318                                 'memberid' => $this->currentItem->authorid,\r
319                                 'name' => $this->currentItem->author,\r
320                                 'extra' => $this->linkparams\r
321                         );\r
322                 \r
323                 echo Link::create_link('member', $data);\r
324                 return;\r
325         }\r
326         \r
327         /**\r
328          * ItemActions::parse_query()\r
329          * Parse templatevar query\r
330          * \r
331          * @param       void\r
332          * @return      void\r
333          */\r
334         public function parse_query()\r
335         {\r
336                 echo $this->strHighlight;\r
337                 return;\r
338         }\r
339         \r
340         /**\r
341          * ItemActions::parse_itemlink()\r
342          * Parse templatevar itemlink\r
343          * \r
344          * @param       void\r
345          * @return      void\r
346          */\r
347         public function parse_itemlink()\r
348         {\r
349                 $data = array(\r
350                         'itemid'        => $this->currentItem->itemid,\r
351                         'title'         => $this->currentItem->title,\r
352                         'timestamp'     => $this->currentItem->timestamp,\r
353                         'extra'         => $this->linkparams\r
354                 );\r
355                 \r
356                 echo Link::create_link('item', $data);\r
357                 return;\r
358         }\r
359         \r
360         /**\r
361          * ItemActions::parse_blogurl()\r
362          * Parse templatevar blogurl\r
363          * \r
364          * @param       void\r
365          * @return      void\r
366          */\r
367         public function parse_blogurl()\r
368         {\r
369                 echo $this->blog->getURL();\r
370                 return;\r
371         }\r
372         \r
373         /**\r
374          * ItemActions::parse_closed()\r
375          * Parse templatevar closed\r
376          * \r
377          * @param       void\r
378          * @return      void\r
379          */\r
380         public function parse_closed()\r
381         {\r
382                 echo $this->currentItem->closed;\r
383                 return;\r
384         }\r
385         \r
386         /**\r
387          * ItemActions::parse_relevance()\r
388          * Parse templatevar relevance\r
389          * \r
390          * @param       void\r
391          * @return      void\r
392          */\r
393         public function parse_relevance()\r
394         {\r
395                 echo round($this->currentItem->score,2);\r
396                 return;\r
397         }\r
398         \r
399         /**\r
400          * ItemActions::parse_title()\r
401          * Parse templatevar title\r
402          *\r
403          * @param       string  $format defines in which format the title is shown\r
404          * @return      void\r
405          */\r
406         public function parse_title($format = '')\r
407         {\r
408                 if ( is_array($this->currentItem) )\r
409                 {\r
410                         $itemtitle = $this->currentItem['title'];\r
411                 }\r
412                 elseif ( is_object($this->currentItem) )\r
413                 {\r
414                         $itemtitle = $this->currentItem->title;\r
415                 }\r
416                 switch ( $format )\r
417                 {\r
418                         case 'xml':\r
419                                 echo Entity::hen($itemtitle);\r
420                                 break;\r
421                         case 'attribute':\r
422                                 echo Entity::hsc($itemtitle);\r
423                                 break;\r
424                         case 'raw':\r
425                                 echo $itemtitle;\r
426                                 break;\r
427                         default:\r
428                                 $this->highlightAndParse($itemtitle);\r
429                                 break;\r
430                 }\r
431                 return;\r
432         }\r
433         \r
434         /**\r
435          * ItemActions::parse_karma()\r
436          * Parse templatevar karma\r
437          * \r
438          * @param       string  $type   type of data for karma\r
439          * @return      void\r
440          */\r
441         public function parse_karma($type = 'totalscore')\r
442         {\r
443                 global $manager;\r
444                 \r
445                 // get karma object\r
446                 $karma =& $manager->getKarma($this->currentItem->itemid);\r
447                 \r
448                 switch ( $type )\r
449                 {\r
450                         case 'pos':\r
451                                 echo $karma->getNbPosVotes();\r
452                                 break;\r
453                         case 'neg':\r
454                                 echo $karma->getNbNegVotes();\r
455                                 break;\r
456                         case 'votes':\r
457                                 echo $karma->getNbOfVotes();\r
458                                 break;\r
459                         case 'posp':\r
460                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;\r
461                                 echo number_format($percentage,2), '%';\r
462                                 break;\r
463                         case 'negp':\r
464                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;\r
465                                 echo number_format($percentage,2), '%';\r
466                                 break;\r
467                         case 'totalscore':\r
468                         default:\r
469                                 echo $karma->getTotalScore();\r
470                                 break;\r
471                 }\r
472                 return;\r
473         }\r
474         \r
475         /**\r
476          * ItemActions::parse_author()\r
477          * Parse templatevar author\r
478          * \r
479          * @param       string  $which  key of data for author\r
480          * @return      void\r
481          */\r
482         public function parse_author($which = '')\r
483         {\r
484                 switch ( $which )\r
485                 {\r
486                         case 'realname':\r
487                                 echo $this->currentItem->authorname;\r
488                                 break;\r
489                         case 'id':\r
490                                 echo $this->currentItem->authorid;\r
491                                 break;\r
492                         case 'email':\r
493                                 echo $this->currentItem->authormail;\r
494                                 break;\r
495                         case 'url':\r
496                                 echo $this->currentItem->authorurl;\r
497                                 break;\r
498                         case 'name':\r
499                         default:\r
500                                 echo $this->currentItem->author;\r
501                 }\r
502                 return;\r
503         }\r
504         \r
505         /**\r
506          * ItemActions::parse_smartbody()\r
507          * Parse templatevar smartbody\r
508          * \r
509          * @param       void\r
510          * @return      void\r
511          */\r
512         public function parse_smartbody()\r
513         {\r
514                 if ( !$this->currentItem->more )\r
515                 {\r
516                         $this->highlightAndParse($this->currentItem->body);\r
517                 }\r
518                 else\r
519                 {\r
520                         $this->highlightAndParse($this->currentItem->more);\r
521                 }\r
522                 return;\r
523         }\r
524         \r
525         /**\r
526          * ItemActions::parse_morelink()\r
527          * Parse templatevar morelink\r
528          */\r
529         public function parse_morelink()\r
530         {\r
531                 if ( $this->currentItem->more )\r
532                 {\r
533                         $this->parser->parse($this->template['MORELINK']);\r
534                 }\r
535                 return;\r
536         }\r
537         \r
538         /**\r
539          * ItemActions::parse_date()\r
540          * Parse templatevar date\r
541          *\r
542          * @param       string  $format format optional strftime format\r
543          * @return      void\r
544          */\r
545         public function parse_date($format = '')\r
546         {\r
547                 if ( $format !== '' )\r
548                 {\r
549                         /* do nothing */\r
550                         ;\r
551                 }\r
552                 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )\r
553                 {\r
554                         /* depends on the PHP's current locale */\r
555                         $format = '%X';\r
556                 }\r
557                 else\r
558                 {\r
559                         $format = $this->template['FORMAT_DATE'];\r
560                 }\r
561                 \r
562                 $offset = 0;\r
563                 if ( $this->blog )\r
564                 {\r
565                         $offset = $this->blog->getTimeOffset() * 3600;\r
566                 }\r
567                 \r
568                 echo i18n::formatted_datetime($format, $this->currentItem->timestamp, $offset);\r
569                 return;\r
570         }\r
571         \r
572         /**\r
573          * ItemActions::parse_time()\r
574          * Parse templatevar time\r
575          *\r
576          * @param       string  $format format optional strftime format\r
577          * @return      void\r
578          */\r
579         public function parse_time($format = '')\r
580         {\r
581                 if ( $format !== '' )\r
582                 {\r
583                         /* do nothing */\r
584                         ;\r
585                 }\r
586                 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )\r
587                 {\r
588                         /* depends on the PHP's current locale */\r
589                         $format = '%x';\r
590                 }\r
591                 else\r
592                 {\r
593                         $format = $this->template['FORMAT_TIME'];\r
594                 }\r
595                 echo i18n::formatted_datetime($format, $this->currentItem->timestamp);\r
596                 return;\r
597         }\r
598         \r
599         /**\r
600          * ItemActions::parse_syndicate_title()\r
601          * Parse templatevar syndicate_title\r
602          *\r
603          * @param       string  $maxLength      maxLength optional maximum length\r
604          * @return      string  syndicated      title\r
605          */\r
606         public function parse_syndicate_title($maxLength = 100) {\r
607                 $syndicated = strip_tags($this->currentItem->title);\r
608                 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));\r
609         }\r
610         \r
611         /**\r
612          * ItemActions::parse_syndicate_description()\r
613          * Parse templatevar syndicate_description\r
614          *\r
615          * @param       stromg  $maxLength              maxlength optional maximum length\r
616          * @param       string  $addHighlight   highlighted string\r
617          * @return      void\r
618          */\r
619         public function parse_syndicate_description($maxLength = 250, $addHighlight = 0)\r
620         {\r
621                 $syndicated = strip_tags($this->currentItem->body);\r
622                 if ( $addHighlight )\r
623                 {\r
624                         $tmp_highlight = Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));\r
625                         echo $this->highlightAndParse($tmp_highlight);\r
626                 }\r
627                 else\r
628                 {\r
629                         echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));\r
630                 }\r
631                 return;\r
632         }\r
633         \r
634         /**\r
635          * ItemActions::parse_karmaposlink()\r
636          * Parse templatevar karmaposlink\r
637          *\r
638          * @param       string  $text   text element for anchor element\r
639          * @return      void\r
640          */\r
641         public function parse_karmaposlink($text = '')\r
642         {\r
643                 global $CONF;\r
644                 $link = $CONF['ActionURL'] . '?action=votepositive&amp;itemid=' . $this->currentItem->itemid;\r
645                 if ( !$text )\r
646                 {\r
647                         echo '<a href="'.$link.'">' . $text . '</a>';\r
648                 }\r
649                 else\r
650                 {\r
651                         echo $link;\r
652                 }\r
653                 \r
654                 return;\r
655         }\r
656         \r
657         /**\r
658          * ItemActions::parse_karmaneglink()\r
659          * Parse templatevar karmaneglink\r
660          *\r
661          * @param       string $text    text element for anchor element\r
662          * @return      void\r
663          */\r
664         public function parse_karmaneglink($text = '')\r
665         {\r
666                 global $CONF;\r
667                 $link = $CONF['ActionURL'] . '?action=votenegative&amp;itemid='.$this->currentItem->itemid;\r
668                 \r
669                 if ( !$text )\r
670                 {\r
671                         echo '<a href="'.$link.'">' . $text . '</a>';\r
672                 }\r
673                 else\r
674                 {\r
675                         echo $link;\r
676                 }\r
677                 \r
678                 return;\r
679         }\r
680 \r
681         /**\r
682          * ItemActions::parse_new()\r
683          * Parse templatevar new\r
684          * \r
685          * @param       void\r
686          * @return      void\r
687          */\r
688         public function parse_new()\r
689         {\r
690                 if ( ($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit) )\r
691                 {\r
692                         echo $this->template['NEW'];\r
693                 }\r
694                 return;\r
695         }\r
696         \r
697         /**\r
698          * ItemActions::parse_daylink()\r
699          * Parse templatevar daylink\r
700          * \r
701          * @param       void\r
702          * @return      void\r
703          */\r
704         public function parse_daylink()\r
705         {\r
706                 echo Link::create_archive_link($this->blog->getID(), i18n::formatted_datetime('%Y-%m-%d', $this->currentItem->timestamp), $this->linkparams);\r
707                 return;\r
708         }\r
709         \r
710         /**\r
711          * ItemActions::parse_comments(\r
712          * Parse templatevar comments\r
713          * \r
714          * @param       integer $maxToShow      maximum number of comments in a display\r
715          * @return      void\r
716          */\r
717         public function parse_comments($maxToShow = 0)\r
718         {\r
719                 if ( $maxToShow == 0 )\r
720                 {\r
721                         $maxToShow = $this->blog->getMaxComments();\r
722                 }\r
723                 \r
724                 // add comments\r
725                 if ( $this->showComments && $this->blog->commentsEnabled() )\r
726                 {\r
727                         $comments = new Comments($this->currentItem->itemid);\r
728                         $comments->setItemActions($this);\r
729                         $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);\r
730                 }\r
731                 return;\r
732         }\r
733         \r
734         /**\r
735          * ItemActions::parse_plugin()\r
736          * Executes a plugin templatevar\r
737          *\r
738          * @param       string  $pluginName     name of plugin (without the NP_)\r
739          * @param       extra parameters can be added\r
740          * @return      void\r
741          */\r
742         public function parse_plugin($pluginName)\r
743         {\r
744                 global $manager;\r
745                 \r
746                 $plugin =& $manager->getPlugin("NP_{$pluginName}");\r
747                 if ( !$plugin )\r
748                 {\r
749                         return;\r
750                 }\r
751                 \r
752                 // get arguments\r
753                 $params = func_get_args();\r
754                 \r
755                 // remove plugin name\r
756                 array_shift($params);\r
757                 \r
758                 // add item reference (array_unshift didn't work)\r
759                 $params = array_merge(array(&$this->currentItem),$params);\r
760                 \r
761                 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);\r
762                 return;\r
763         }\r
764         \r
765         /**\r
766          * ItemActions::parse_edit()\r
767          * Parse templatevar edit\r
768          * \r
769          * @param       void\r
770          * @return      void\r
771          */\r
772         public function parse_edit()\r
773         {\r
774                 global $member, $CONF;\r
775                 if ( $this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) )\r
776                 {\r
777                         $this->parser->parse($this->template['EDITLINK']);\r
778                 }\r
779                 return;\r
780         }\r
781         \r
782         /**\r
783          * ItemActions::parse_editlink()\r
784          * Parse templatevar editlink\r
785          */\r
786         public function parse_editlink()\r
787         {\r
788                 global $CONF;\r
789                 echo $CONF['AdminURL'] . 'bookmarklet.php?action=edit&amp;itemid=' . $this->currentItem->itemid;\r
790                 return;\r
791         }\r
792         \r
793         /**\r
794          * ItemActions::parse_editpopupcode()\r
795          * Parse templatevar editpopupcode\r
796          * \r
797          * @param       void\r
798          * @return      void\r
799          */\r
800         public function parse_editpopupcode()\r
801         {\r
802                 echo "if (event &amp;&amp; event.preventDefault) event.preventDefault();winbm=window.open(this.href,'nucleusbm','scrollbars=yes,width=600,height=550,left=10,top=10,status=yes,resizable=yes');winbm.focus();return false;";\r
803                 return;\r
804         }\r
805         \r
806         /**\r
807          * ItemActions::highlightAndParse()\r
808          * Parses highlighted text, with limited actions only (to prevent not fully trusted team members\r
809          * from hacking your weblog.\r
810          * 'plugin variables in items' implementation by Andy\r
811          * \r
812          * @param       array   $data   \r
813          * @return      void\r
814          */\r
815         public function highlightAndParse(&$data)\r
816         {\r
817                 $handler = new BodyActions($this->blog);
818                 $handler->setTemplate($this->template);
819                 $handler->setHighlight($this->strHighlight);
820                 $handler->setCurrentItem($this->currentItem);
821                 \r
822                 $parser = new Parser($handler);\r
823                 $parser->parse($handler->highlight($data));
824                 return;\r
825         }\r
826         \r
827         /**\r
828          * ItemActions::checkCondition()\r
829          * Checks conditions for if statements\r
830          *\r
831          * @param       string  $field  type of <%if%>\r
832          * @param       string  $name   property of field\r
833          * @param       string  $value  value of property\r
834          * @return      boolean\r
835          */\r
836         protected function checkCondition($field, $name='', $value = '')\r
837         {\r
838                 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;\r
839                 \r
840                 $condition = 0;\r
841                 switch ( $field )\r
842                 {\r
843                         case 'category':\r
844                                 $condition = ($blog && $this->ifCategory($name,$value));\r
845                                 break;\r
846                         case 'itemcategory':\r
847                                 $condition = ($this->ifItemCategory($name,$value));\r
848                                 break;\r
849                         case 'blogsetting':\r
850                                 $condition = ($blog && ($blog->getSetting($name) == $value));\r
851                                 break;\r
852                         case 'itemblogsetting':\r
853                                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));\r
854                                 $condition = ($b && ($b->getSetting($name) == $value));\r
855                                 break;\r
856                         case 'loggedin':\r
857                                 $condition = $member->isLoggedIn();\r
858                                 break;\r
859                         case 'onteam':\r
860                                 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);\r
861                                 break;\r
862                         case 'admin':\r
863                                 $condition = $member->isLoggedIn() && $this->ifAdmin($name);\r
864                                 break;\r
865                         case 'author':\r
866                                 $condition = ($this->ifAuthor($name,$value));\r
867                                 break;\r
868                         case 'hasplugin':\r
869                                 $condition = $this->ifHasPlugin($name, $value);\r
870                                 break;\r
871                         default:\r
872                                 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);\r
873                                 break;\r
874                 }\r
875                 return $condition;\r
876         }       \r
877         \r
878         /**\r
879          * ItemActions::ifCategory()\r
880          *  Different checks for a category\r
881          *  \r
882          * @param       string  $key    key of category\r
883          * @param       string  $value  value for key of category\r
884          * @return      boolean\r
885          */\r
886         private function ifCategory($key = '', $value = '')\r
887         {\r
888                 global $blog, $catid;\r
889                 \r
890                 // when no parameter is defined, just check if a category is selected\r
891                 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )\r
892                 {\r
893                         return (boolean) $blog->isValidCategory($catid);\r
894                 }\r
895                 \r
896                 // check category name\r
897                 if ( $key == 'catname' )\r
898                 {\r
899                         $value = $blog->getCategoryIdFromName($value);\r
900                         if ( $value == $catid )\r
901                         {\r
902                                 return (boolean) $blog->isValidCategory($catid);\r
903                         }\r
904                 }\r
905                 \r
906                 // check category id\r
907                 if ( ($key == 'catid') && ($value == $catid) )\r
908                 {\r
909                         return (boolean) $blog->isValidCategory($catid);\r
910                 }\r
911                 return FALSE;\r
912         }\r
913         \r
914         /**\r
915          * ItemActions::ifAuthor()\r
916          * Different checks for an author\r
917          * \r
918          * @param       string  $key    key of data for author\r
919          * @param       string  $value  value of data for author\r
920          * @return      boolean correct or not\r
921          */\r
922         private function ifAuthor($key = '', $value = '')\r
923         {\r
924                 global $member, $manager;\r
925                 \r
926                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));\r
927                 \r
928                 // when no parameter is defined, just check if author is current visitor\r
929                 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )\r
930                 {\r
931                         return (boolean) ((integer) $member->getID() > 0 && (integer) $member->getID() == (integer) $this->currentItem->authorid);\r
932                 }\r
933                 \r
934                 // check author name\r
935                 if ( $key == 'name' )\r
936                 {\r
937                         $value = strtolower($value);\r
938                         if ( $value == strtolower($this->currentItem->author) )\r
939                         {\r
940                                 return TRUE;\r
941                         }\r
942                 }\r
943                 \r
944                 // check if author is admin\r
945                 if ( ($key == 'isadmin') )\r
946                 {\r
947                         $aid = intval($this->currentItem->authorid);\r
948                         $blogid = intval($b->getID());                  \r
949                         $amember =& $manager->getMember($aid);\r
950                         if ( $amember->isAdmin() )\r
951                         {\r
952                                 return TRUE;\r
953                         }\r
954                         return (boolean) $amember->isBlogAdmin($blogid);\r
955                 }\r
956                 \r
957                 return FALSE;\r
958         }\r
959         \r
960         /**\r
961          * ItemActions::ifItemCategory()\r
962          * Different checks for a category\r
963          * \r
964          * @param       string  $key    key of data for category to which item belongs\r
965          * @param       string  $value  value of data for category to which item belongs\r
966          * @return boolean      correct or not\r
967          */\r
968         private function ifItemCategory($key = '', $value='')\r
969         {\r
970                 global $catid, $manager;\r
971                 \r
972                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));\r
973                 \r
974                 // when no parameter is defined, just check if a category is selected\r
975                 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )\r
976                 {\r
977                         return (boolean) $b->isValidCategory($catid);\r
978                 }\r
979                 \r
980                 $icatid = $this->currentItem->catid;\r
981                 \r
982                 // check category name\r
983                 if ( $key == 'catname' )\r
984                 {\r
985                         $value = $b->getCategoryIdFromName($value);\r
986                         if ( $value == $icatid )\r
987                         {\r
988                                 return (boolean) $b->isValidCategory($icatid);\r
989                         }\r
990                 }\r
991                 \r
992                 // check category id\r
993                 if ( ($key == 'catid') && ($value == $icatid) )\r
994                 {\r
995                         return (boolean) $b->isValidCategory($icatid);\r
996                 }\r
997                 return FALSE;\r
998         }\r
999 \r
1000         \r
1001         /**\r
1002          * ItemActions::ifOnTeam()\r
1003          * Checks if a member is on the team of a blog and return his rights\r
1004          * \r
1005          * @param       string  $blogName       name of weblog\r
1006          * @return      boolean correct or not\r
1007          */\r
1008         private function ifOnTeam($blogName = '')\r
1009         {\r
1010                 global $blog, $member, $manager;\r
1011                 \r
1012                 // when no blog found\r
1013                 if ( ($blogName == '') && (!is_object($blog)) )\r
1014                 {\r
1015                         return 0;\r
1016                 }\r
1017                 \r
1018                 // explicit blog selection\r
1019                 if ( $blogName != '' )\r
1020                 {\r
1021                         $blogid = getBlogIDFromName($blogName);\r
1022                 }\r
1023                 \r
1024                 // use current blog\r
1025                 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )\r
1026                 {\r
1027                         $blogid = $blog->getID();\r
1028                 }\r
1029                 return (boolean) $member->teamRights($blogid);\r
1030         }\r
1031         \r
1032         /**\r
1033          * ItemActions::ifAdmin()\r
1034          * Checks if a member is admin of a blog\r
1035          * \r
1036          * @param       string  $blogName       name of weblog\r
1037          * @return      boolean correct or not\r
1038          */\r
1039         private function ifAdmin($blogName = '')\r
1040         {\r
1041                 global $blog, $member, $manager;\r
1042                 \r
1043                 // when no blog found\r
1044                 if ( ($blogName == '') && (!is_object($blog)) )\r
1045                 {\r
1046                         return 0;\r
1047                 }\r
1048                 \r
1049                 // explicit blog selection\r
1050                 if ( $blogName != '' )\r
1051                 {\r
1052                         $blogid = getBlogIDFromName($blogName);\r
1053                 }\r
1054                 \r
1055                 // use current blog\r
1056                 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )\r
1057                 {\r
1058                         $blogid = $blog->getID();\r
1059                 }\r
1060                 return (boolean) $member->isBlogAdmin($blogid);\r
1061         }\r
1062         \r
1063         \r
1064         /**\r
1065          * ItemActions::ifHasPlugin()\r
1066          *      hasplugin,PlugName\r
1067          *         -> checks if plugin exists\r
1068          *      hasplugin,PlugName,OptionName\r
1069          *         -> checks if the option OptionName from plugin PlugName is not set to 'no'\r
1070          *      hasplugin,PlugName,OptionName=value\r
1071          *         -> checks if the option OptionName from plugin PlugName is set to value\r
1072          *\r
1073          * @param       string  $name   name of plugin\r
1074          * @param       string  $value  key (and value) of plugin option\r
1075          * @return      boolean correct or not\r
1076          */\r
1077         private function ifHasPlugin($name, $value)\r
1078         {\r
1079                 global $manager;\r
1080                 $condition = FALSE;\r
1081                 // (pluginInstalled method won't write a message in the actionlog on failure)\r
1082                 if ( $manager->pluginInstalled("NP_{$name}"))\r
1083                 {\r
1084                         $plugin =& $manager->getPlugin('NP_' . $name);\r
1085                         if ( $plugin != NULL )\r
1086                         {\r
1087                                 if ( $value == "" )\r
1088                                 {\r
1089                                         $condition = TRUE;\r
1090                                 }\r
1091                                 else\r
1092                                 {\r
1093                                         list($name2, $value2) = preg_split('#=#', $value, 2);\r
1094                                         if ( $value2 == "" && $plugin->getOption($name2) != 'no' )\r
1095                                         {\r
1096                                                 $condition = TRUE;\r
1097                                         }\r
1098                                         else if ( $plugin->getOption($name2) == $value2 )\r
1099                                         {\r
1100                                                 $condition = TRUE;\r
1101                                         }\r
1102                                 }\r
1103                         }\r
1104                 }\r
1105                 return (boolean) $condition;\r
1106         }\r
1107         \r
1108         /**\r
1109          * ItemActions::ifPlugin()\r
1110          * Checks if a plugin exists and call its doIf function\r
1111          * \r
1112          * @param       string  $name   name of plugin\r
1113          * @param       string  $key    key of plugin option\r
1114          * @param       string  $value  value of plugin option\r
1115          * @return      boolean callback output from plugin\r
1116          */\r
1117         private function ifPlugin($name, $key = '', $value = '')\r
1118         {\r
1119                 global $manager;\r
1120                 \r
1121                 $plugin =& $manager->getPlugin("NP_{$name}");\r
1122                 if ( !$plugin )\r
1123                 {\r
1124                         return;\r
1125                 }\r
1126                 $params = func_get_args();\r
1127                 array_shift($params);\r
1128                 \r
1129                 return (boolean) call_user_func_array(array(&$plugin, 'doIf'), $params);\r
1130         }\r
1131 }