OSDN Git Service

MERGE: リビジョン1757のマージ。BaseActionsクラスとその派生クラスを修正。
[nucleus-jp/nucleus-next.git] / nucleus / libs / BODYACTIONS.php
1 <?php\r
2 \r
3 /*\r
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
5  * Copyright (C) 2002-2009 The Nucleus Group\r
6  *\r
7  * This program is free software; you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License\r
9  * as published by the Free Software Foundation; either version 2\r
10  * of the License, or (at your option) any later version.\r
11  * (see nucleus/documentation/index.html#license for more info)\r
12  */\r
13 /**\r
14  * A class to parses plugin calls inside items\r
15  *\r
16  * @license http://nucleuscms.org/license.txt GNU General Public License\r
17  * @copyright Copyright (C) 2002-2009 The Nucleus Group\r
18  * @version $Id: BODYACTIONS.php 1757 2012-04-15 09:02:32Z sakamocchi $\r
19  */\r
20 \r
21 class BodyActions extends BaseActions\r
22 {\r
23         private $currentItem;\r
24         private $template;\r
25         \r
26         static private $defined_actions = array(\r
27                 'image',\r
28                 'media',\r
29                 'popup',\r
30                 'plugin',\r
31                 'if',\r
32                 'else',\r
33                 'endif',\r
34                 'elseif',\r
35                 'ifnot',\r
36                 'elseifnot'\r
37         );\r
38         \r
39         /**\r
40          * BodyActions::__construct()\r
41          * Constructor of the BODYACTIONS\r
42          * \r
43          * @param       void\r
44          * @return      void\r
45          */\r
46         public function __construct()\r
47         {\r
48                 $this->initialize();    \r
49                 return;\r
50         }\r
51         \r
52         /**\r
53          * BodyActions::setCurrentItem()\r
54          * Set the current item\r
55          * \r
56          * @param       object  &$item  reference to the current item\r
57          * @return      void\r
58          */\r
59         public function setCurrentItem(&$item)\r
60         {\r
61                 global $currentitemid;\r
62                 $this->currentItem =& $item;\r
63                 $currentitemid = $this->currentItem->itemid;\r
64                 return;\r
65         }\r
66         \r
67         /**\r
68          * BodyActions::setTemplate()\r
69          * Set the current template\r
70          * \r
71          * @param       string  $template       Template to be used\r
72          * @return      void\r
73          */\r
74         public function setTemplate($template)\r
75         {\r
76                 $this->template =& $template;\r
77                 return;\r
78         }\r
79         \r
80         /**\r
81          * BodyActions::getDefinedActions()\r
82          * Get the defined actions in an item\r
83          * \r
84          * @param       void\r
85          * @return      Array   self::$defined_actions\r
86          */\r
87         public function getDefinedActions()\r
88         {\r
89                 return self::$defined_actions;\r
90         }\r
91         \r
92         /**\r
93          * BodyActions::parse_plugin()\r
94          * Parse a plugin var\r
95          * Called if <%plugin(...)%> in an item appears\r
96          * \r
97          * Calls the doItemVar function in the plugin\r
98          */\r
99         public function parse_plugin($pluginName)\r
100         {\r
101                 global $manager;\r
102                 \r
103                 $plugin =& $manager->getPlugin('NP_' . $pluginName);\r
104                 if ( !$plugin )\r
105                 {\r
106                         return; \r
107                 }\r
108                 \r
109                 // get arguments\r
110                 $params = func_get_args();\r
111                 \r
112                 // remove plugin name\r
113                 array_shift($params);\r
114                 \r
115                 // add item reference (array_unshift didn't work)\r
116                 $params = array_merge(array(&$this->currentItem),$params);\r
117                 \r
118                 call_user_func_array(array(&$plugin,'doItemVar'), $params);\r
119                 return;\r
120         }\r
121         \r
122         /**\r
123          * BodyActions::parse_image()\r
124          * Parse image\r
125          * Called if <%image(...)%> in an item appears\r
126          * \r
127          * @param       void\r
128          * @return      parsed image tag\r
129          */\r
130         public function parse_image()\r
131         {\r
132                 // image/popup calls have arguments separated by |\r
133                 $args = func_get_args();\r
134                 $args = preg_split('#\|#',implode($args,', '));\r
135                 echo call_user_func_array(array(&$this,'createImageCode'),$args);\r
136         }\r
137         \r
138         /**\r
139          * BodyActions::createImageCode()\r
140          * Creates the code for an image\r
141          * \r
142          * @param       string  $filename       name of file from tag\r
143          * @param       integer $width          width of file from tag\r
144          * @param       integer $height         height of file from tag\r
145          * @return      string  image element with anchor element\r
146          */\r
147         public function createImageCode($filename, $width, $height, $text = '')\r
148         {\r
149                 global $CONF;\r
150                 \r
151                 // select private collection when no collection given\r
152                 if ( !strstr($filename,'/') )\r
153                 {\r
154                         $filename = $this->currentItem->authorid . '/' . $filename;\r
155                 }\r
156                 \r
157                 $windowwidth = $width;\r
158                 $windowheight = $height;\r
159                 \r
160                 $vars['link']                   = Entity::hsc($CONF['MediaURL']. $filename);\r
161                 $vars['text']                   = Entity::hsc($text);\r
162                 $vars['image'] = '<img src="' . $vars['link'] . '" width="' . $width . '" height="' . $height . '" alt="' . $vars['text'] . '" title="' . $vars['text'] . '" />';\r
163                 $vars['width']                  = $width;\r
164                 $vars['height']                 = $height;\r
165                 $vars['media']                  = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';\r
166                 \r
167                 return Template::fill($this->template['IMAGE_CODE'],$vars);;\r
168         }\r
169         \r
170         /**\r
171          * BodyActions::parse_media()\r
172          * Parse media\r
173          * Called if <%media(...)%> in an item appears\r
174          * \r
175          * @param       void\r
176          * @param       parsed media tag\r
177          */\r
178         public function parse_media()\r
179         {\r
180                 // image/popup calls have arguments separated by |\r
181                 $args = func_get_args();\r
182                 $args = preg_split('#\|#', implode($args,', '));\r
183                 echo call_user_func_array(array(&$this,'createMediaCode'), $args);\r
184         }\r
185         \r
186         /**\r
187          * BodyActions::createMediaCode()\r
188          * Creates the code for a media\r
189          * \r
190          * @param       string  $filename       name of file from tag\r
191          * @param       string  $text           alternative text from tag\r
192          * @return      string  text element with anchor element\r
193          */\r
194         public function createMediaCode($filename, $text = '')\r
195         {\r
196                 global $CONF;\r
197                 \r
198                 // select private collection when no collection given\r
199                 if ( !strstr($filename,'/') )\r
200                 {\r
201                         $filename = $this->currentItem->authorid . '/' . $filename;\r
202                 }\r
203                 \r
204                 $vars['link']                   = Entity::hsc($CONF['MediaURL'] . $filename);\r
205                 $vars['text']                   = Entity::hsc($text);\r
206                 $vars['media']                  = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';\r
207                 \r
208                 return Template::fill($this->template['MEDIA_CODE'], $vars);;\r
209         }\r
210         \r
211         /**\r
212          * BodyActions::parse_popup()\r
213          * Parse popup\r
214          * Called if <%popup(...)%> in an item appears\r
215          * \r
216          * @param       void\r
217          * @return      string  parsed popup tag\r
218          */\r
219         public function parse_popup()\r
220         {\r
221                 // image/popup calls have arguments separated by |\r
222                 $args = func_get_args();\r
223                 $args = preg_split('#\|#', implode($args,', '));\r
224                 echo call_user_func_array(array(&$this,'createPopupCode'), $args);\r
225         }\r
226         \r
227         /**\r
228          * BodyActions::createPopupCode()\r
229          * Creates the code for a popup\r
230          * \r
231          * @param       string  $filename       name of file from tag\r
232          * @param       integer $width          width of file from tag\r
233          * @param       integer $height         height of file from tag\r
234          * @param       string  $text           alternative text from tag\r
235          * @return      string  text element with anchor element of JavaScript window.open\r
236          */\r
237         public function createPopupCode($filename, $width, $height, $text = '')\r
238         {\r
239                 global $CONF;\r
240                 \r
241                 // select private collection when no collection given\r
242                 if ( !strstr($filename,'/') )\r
243                 {\r
244                         $filename = $this->currentItem->authorid . '/' . $filename;\r
245                 }\r
246                 \r
247                 $windowwidth = $width;\r
248                 $windowheight = $height;\r
249                 \r
250                 $vars['rawpopuplink']   = $CONF['Self'] . "?imagepopup=" . Entity::hsc($filename) . "&amp;width=$width&amp;height=$height&amp;imagetext=" . urlencode(Entity::hsc($text));\r
251                 $vars['popupcode']              = "window.open(this.href,'imagepopup','status=no,toolbar=no,scrollbars=no,resizable=yes,width=$windowwidth,height=$windowheight');return false;";\r
252                 $vars['popuptext']              = Entity::hsc($text);\r
253                 $vars['popuplink']              = '<a href="' . $vars['rawpopuplink']. '" onclick="'. $vars['popupcode'].'" >' . $vars['popuptext'] . '</a>';\r
254                 $vars['width']                  = $width;\r
255                 $vars['height']                 = $height;\r
256                 $vars['text']                   = $text;\r
257                 $vars['link']                   = Entity::hsc($CONF['MediaURL'] . $filename);\r
258                 $vars['media']                  = '<a href="' . $vars['link'] . '">' . $vars['popuptext'] . '</a>';\r
259                 \r
260                 return Template::fill($this->template['POPUP_CODE'], $vars);\r
261         }\r
262         \r
263         /**\r
264          * BodyActions::checkCondition()\r
265          * Checks conditions for if statements\r
266          *\r
267          * @param       string  $field  type of <%if%>\r
268          * @param       string  $name   property of field\r
269          * @param       string  $value  value of property\r
270          * @return      condition\r
271          */\r
272         public function checkCondition($field, $name='', $value = '')\r
273         {\r
274                 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;\r
275                 \r
276                 $condition = 0;\r
277                 switch ( $field )\r
278                 {\r
279                         case 'category':\r
280                                 $condition = ($blog && $this->ifCategory($name,$value));\r
281                                 break;\r
282                         case 'itemcategory':\r
283                                 $condition = ($this->ifItemCategory($name,$value));\r
284                                 break;\r
285                         case 'blogsetting':\r
286                                 $condition = ($blog && ($blog->getSetting($name) == $value));\r
287                                 break;\r
288                         case 'itemblogsetting':\r
289                                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));\r
290                                 $condition = ($b && ($b->getSetting($name) == $value));\r
291                                 break;\r
292                         case 'loggedin':\r
293                                 $condition = $member->isLoggedIn();\r
294                                 break;\r
295                         case 'onteam':\r
296                                 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);\r
297                                 break;\r
298                         case 'admin':\r
299                                 $condition = $member->isLoggedIn() && $this->ifAdmin($name);\r
300                                 break;\r
301                         case 'author':\r
302                                 $condition = ($this->ifAuthor($name,$value));\r
303                                 break;\r
304                         case 'hasplugin':\r
305                                 $condition = $this->ifHasPlugin($name, $value);\r
306                                 break;\r
307                         default:\r
308                                 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);\r
309                                 break;\r
310                 }\r
311                 return $condition;\r
312         }       \r
313         \r
314         /**\r
315          * BodyActions::ifCategory()\r
316          *  Different checks for a category\r
317          *  \r
318          * @param       string  $key    key for data of category\r
319          * @param       string  $value  value for data of category\r
320          * @return      boolean\r
321          */\r
322         private function ifCategory($key = '', $value = '')\r
323         {\r
324                 global $blog, $catid;\r
325                 \r
326                 // when no parameter is defined, just check if a category is selected\r
327                 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )\r
328                 {\r
329                         return $blog->isValidCategory($catid);\r
330                 }\r
331                 \r
332                 // check category name\r
333                 if ( $key == 'catname' )\r
334                 {\r
335                         $value = $blog->getCategoryIdFromName($value);\r
336                         if ( $value == $catid )\r
337                         {\r
338                                 return $blog->isValidCategory($catid);\r
339                         }\r
340                 }\r
341                 \r
342                 // check category id\r
343                 if ( ($key == 'catid') && ($value == $catid) )\r
344                 {\r
345                         return $blog->isValidCategory($catid);\r
346                 }\r
347                 \r
348                 return FALSE;\r
349         }\r
350         \r
351         /**\r
352          * BodyActions::ifAuthor()\r
353          * Different checks for an author\r
354          * \r
355          * @param       string  $key    key for data of author\r
356          * @param       string  $value  value for data of author\r
357          * @return      boolean\r
358          */\r
359         private function ifAuthor($key = '', $value = '')\r
360         {\r
361                 global $member, $manager;\r
362                 \r
363                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));\r
364                 \r
365                 // when no parameter is defined, just check if author is current visitor\r
366                 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )\r
367                 {\r
368                         return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));\r
369                 }\r
370                 \r
371                 // check author name\r
372                 if ( $key == 'name' )\r
373                 {\r
374                         $value = strtolower($value);\r
375                         if ( $value == strtolower($this->currentItem->author) )\r
376                         {\r
377                                 return TRUE;\r
378                         }\r
379                 }\r
380                 \r
381                 // check if author is admin\r
382                 if ( ($key == 'isadmin') )\r
383                 {\r
384                         $aid = intval($this->currentItem->authorid);\r
385                         $blogid = intval($b->getID());                  \r
386                         $amember =& $manager->getMember($aid);\r
387                         if ( $amember->isAdmin() )\r
388                         {\r
389                                 return TRUE;\r
390                         }       \r
391                         return $amember->isBlogAdmin($blogid);\r
392                 }\r
393                 \r
394                 return FALSE;\r
395         }\r
396         \r
397         /**\r
398          * BodyActions::ifItemCategory()\r
399          * Different checks for a category\r
400          * \r
401          * @param       string  $key    key for data of category\r
402          * @param       string  $value  value for data of category\r
403          * @return      boolean \r
404          */\r
405         private function ifItemCategory($key = '', $value = '')\r
406         {\r
407                 global $catid, $manager;\r
408                 \r
409                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));\r
410                 \r
411                 // when no parameter is defined, just check if a category is selected\r
412                 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )\r
413                 {\r
414                         return $b->isValidCategory($catid);\r
415                 }\r
416                         \r
417                 $icatid = $this->currentItem->catid;\r
418                 \r
419                 // check category name\r
420                 if ( $key == 'catname' )\r
421                 {\r
422                         $value = $b->getCategoryIdFromName($value);\r
423                         if ( $value == $icatid )\r
424                         {\r
425                                 return $b->isValidCategory($icatid);\r
426                         }\r
427                 }\r
428                 \r
429                 // check category id\r
430                 if ( ($key == 'catid') && ($value == $icatid) )\r
431                 {\r
432                         return $b->isValidCategory($icatid);\r
433                 }\r
434                 return FALSE;\r
435         }\r
436         \r
437         /**\r
438          * BodyActions::ifOnTeam()\r
439          * Checks if a member is on the team of a blog and return his rights\r
440          * \r
441          * @param       string  $blogName       name of weblog\r
442          * @return      boolean\r
443          */\r
444         private function ifOnTeam($blogName = '')\r
445         {\r
446                 global $blog, $member, $manager;\r
447                 \r
448                 // when no blog found\r
449                 if ( ($blogName == '') && (!is_object($blog)) )\r
450                 {\r
451                         return 0;\r
452                 }\r
453                 \r
454                 // explicit blog selection\r
455                 if ( $blogName != '' )\r
456                 {\r
457                         $blogid = getBlogIDFromName($blogName);\r
458                 }\r
459                 \r
460                 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )\r
461                 {\r
462                         // use current blog\r
463                         $blogid = $blog->getID();\r
464                 }\r
465                 return $member->teamRights($blogid);\r
466         }\r
467         \r
468         /**\r
469          * BodyActions::ifAdmin()\r
470          * Checks if a member is admin of a blog\r
471          * \r
472          * @param       string  $blogName       name of weblog\r
473          * @return      boolean\r
474          */\r
475         private function ifAdmin($blogName = '')\r
476         {\r
477                 global $blog, $member, $manager;\r
478                 \r
479                 // when no blog found\r
480                 if ( ($blogName == '') && (!is_object($blog)) )\r
481                 {\r
482                         return 0;\r
483                 }\r
484                 \r
485                 // explicit blog selection\r
486                 if ( $blogName != '' )\r
487                 {\r
488                         $blogid = getBlogIDFromName($blogName);\r
489                 }\r
490                 \r
491                 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )\r
492                 {\r
493                         // use current blog\r
494                         $blogid = $blog->getID();\r
495                 }\r
496                 return $member->isBlogAdmin($blogid);\r
497         }\r
498         \r
499         \r
500         /**\r
501          * BodyActions::ifHasPlugin()\r
502          *      hasplugin,PlugName\r
503          *         -> checks if plugin exists\r
504          *      hasplugin,PlugName,OptionName\r
505          *         -> checks if the option OptionName from plugin PlugName is not set to 'no'\r
506          *      hasplugin,PlugName,OptionName=value\r
507          *         -> checks if the option OptionName from plugin PlugName is set to value\r
508          *\r
509          * @param       string  $name   name of plugin\r
510          * @param       string  $value  value for plugin argument\r
511          * @return      boolean\r
512          */\r
513         private function ifHasPlugin($name, $value)\r
514         {\r
515                 global $manager;\r
516                 $condition = false;\r
517                 \r
518                 // (pluginInstalled method won't write a message in the actionlog on failure)\r
519                 if ( $manager->pluginInstalled("NP_{$name}") )\r
520                 {\r
521                         $plugin =& $manager->getPlugin("NP_{$name}");\r
522                         if ( $plugin != NULL )\r
523                         {\r
524                                 if ( $value == "" )\r
525                                 {\r
526                                         $condition = TRUE;\r
527                                 }\r
528                                 else\r
529                                 {\r
530                                         list($name2, $value2) = preg_split('#=#', $value, 2);\r
531                                         if ( $value2 == "" && $plugin->getOption($name2) != 'no' )\r
532                                         {\r
533                                                 $condition = TRUE;\r
534                                         }\r
535                                         else if ( $plugin->getOption($name2) == $value2 )\r
536                                         {\r
537                                                 $condition = TRUE;\r
538                                         }\r
539                                 }\r
540                         }\r
541                 }\r
542                 return $condition;\r
543         }\r
544         \r
545         /**\r
546          * BodyActions::ifPlugin()\r
547          * Checks if a plugin exists and call its doIf function\r
548          * \r
549          * @param       string  $name   name of plugin\r
550          * @param       string  $key    ...\r
551          * @param       string  $value  ...\r
552          * @return      string  result of plugin 'doIf'\r
553          */\r
554         private function ifPlugin($name, $key = '', $value = '')\r
555         {\r
556                 global $manager;\r
557                 \r
558                 $plugin =& $manager->getPlugin("NP_{$name}");\r
559                 if ( !$plugin )\r
560                 {\r
561                         return;\r
562                 }\r
563                 \r
564                 $params = func_get_args();\r
565                 array_shift($params);\r
566                 \r
567                 return call_user_func_array(array(&$plugin, 'doIf'), $params);\r
568         }\r
569 }\r