OSDN Git Service

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