OSDN Git Service

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