OSDN Git Service

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