OSDN Git Service

主だったスクリプトの改行コードを統一
[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 1626 2012-01-09 15:46:54Z sakamocchi $\r
19  */\r
20 \r
21 class BODYACTIONS extends BaseActions {\r
22 \r
23         var $currentItem;\r
24 \r
25         var $template;\r
26 \r
27         /**\r
28          * Constructor of the BODYACTIONS\r
29          */\r
30         function BODYACTIONS () {\r
31                 $this->BaseActions();   \r
32         }\r
33 \r
34         /**\r
35          * Set the current item\r
36          * \r
37          * @param &$item\r
38          *                      reference to the current item\r
39          */\r
40         function setCurrentItem(&$item) {\r
41                 $this->currentItem =& $item;\r
42                 global $currentitemid;\r
43                 $currentitemid = $this->currentItem->itemid;\r
44         }\r
45 \r
46         /**\r
47          * Set the current template\r
48          * \r
49          * @param $template\r
50          *                      Template to be used\r
51          */\r
52         function setTemplate($template) {\r
53                 $this->template =& $template;\r
54         }\r
55 \r
56         /**\r
57          * Get the defined actions in an item\r
58          */\r
59         function getDefinedActions() {\r
60                 return array('image', 'media', 'popup', 'plugin', 'if', 'else', 'endif', 'elseif', 'ifnot', 'elseifnot');\r
61         }\r
62 \r
63         /**\r
64          * Parse a plugin var\r
65          * Called if <%plugin(...)%> in an item appears\r
66          * \r
67          * Calls the doItemVar function in the plugin\r
68          */\r
69         function parse_plugin($pluginName) {\r
70                 global $manager;\r
71 \r
72                 // should be already tested from the parser (PARSER.php)\r
73                 // only continue when the plugin is really installed\r
74                 /*if (!$manager->pluginInstalled('NP_' . $pluginName)) {\r
75                         return;\r
76                 }*/\r
77 \r
78                 $plugin =& $manager->getPlugin('NP_' . $pluginName);\r
79                 if (!$plugin) return;\r
80 \r
81                 // get arguments\r
82                 $params = func_get_args();\r
83 \r
84                 // remove plugin name\r
85                 array_shift($params);\r
86 \r
87                 // add item reference (array_unshift didn't work)\r
88                 $params = array_merge(array(&$this->currentItem),$params);\r
89 \r
90                 call_user_func_array(array(&$plugin,'doItemVar'), $params);\r
91         }\r
92 \r
93         /**\r
94          * Parse image\r
95          * Called if <%image(...)%> in an item appears\r
96          */\r
97         function parse_image() {\r
98                 // image/popup calls have arguments separated by |\r
99                 $args = func_get_args();\r
100                 $args = i18n::explode('|',implode($args,', '));\r
101                 call_user_func_array(array(&$this,'createImageCode'),$args);\r
102         }\r
103         \r
104         /**\r
105          * Creates the code for an image\r
106          */\r
107         function createImageCode($filename, $width, $height, $text = '') {\r
108                 global $CONF;\r
109 \r
110                 // select private collection when no collection given\r
111                 if (!strstr($filename,'/')) {\r
112                         $filename = $this->currentItem->authorid . '/' . $filename;\r
113                 }\r
114 \r
115                 $windowwidth = $width;\r
116                 $windowheight = $height;\r
117 \r
118                 $vars['link']                   = ENTITY::hsc($CONF['MediaURL']. $filename);\r
119                 $vars['text']                   = ENTITY::hsc($text);\r
120                 $vars['image'] = '<img src="' . $vars['link'] . '" width="' . $width . '" height="' . $height . '" alt="' . $vars['text'] . '" title="' . $vars['text'] . '" />';\r
121                 $vars['width']                  = $width;\r
122                 $vars['height']                 = $height;\r
123                 $vars['media']                  = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';\r
124 \r
125 \r
126                 echo TEMPLATE::fill($this->template['IMAGE_CODE'],$vars);;\r
127 \r
128         }\r
129 \r
130         /**\r
131          * Parse media\r
132          * Called if <%media(...)%> in an item appears\r
133          */\r
134         function parse_media() {\r
135                 // image/popup calls have arguments separated by |\r
136                 $args = func_get_args();\r
137                 $args = i18n::explode('|',implode($args,', '));\r
138                 call_user_func_array(array(&$this,'createMediaCode'),$args);\r
139         }\r
140 \r
141         /**\r
142          * Creates the code for a media\r
143          */\r
144         function createMediaCode($filename, $text = '') {\r
145                 global $CONF;\r
146 \r
147                 // select private collection when no collection given\r
148                 if (!strstr($filename,'/')) {\r
149                         $filename = $this->currentItem->authorid . '/' . $filename;\r
150                 }\r
151 \r
152                 $vars['link']                   = ENTITY::hsc($CONF['MediaURL'] . $filename);\r
153                 $vars['text']                   = ENTITY::hsc($text);\r
154                 $vars['media']                  = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';\r
155 \r
156                 echo TEMPLATE::fill($this->template['MEDIA_CODE'],$vars);;\r
157         }\r
158 \r
159         /**\r
160          * Parse popup\r
161          * Called if <%popup(...)%> in an item appears\r
162          */\r
163         function parse_popup() {\r
164                 // image/popup calls have arguments separated by |\r
165                 $args = func_get_args();\r
166                 $args = i18n::explode('|',implode($args,', '));\r
167                 call_user_func_array(array(&$this,'createPopupCode'),$args);\r
168         }\r
169 \r
170         /**\r
171          * Creates the code for a popup\r
172          */\r
173         function createPopupCode($filename, $width, $height, $text = '') {\r
174                 global $CONF;\r
175 \r
176                 // select private collection when no collection given\r
177                 if (!strstr($filename,'/')) {\r
178                         $filename = $this->currentItem->authorid . '/' . $filename;\r
179                 }\r
180 \r
181                 $windowwidth = $width;\r
182                 $windowheight = $height;\r
183 \r
184                 $vars['rawpopuplink']   = $CONF['Self'] . "?imagepopup=" . ENTITY::hsc($filename) . "&amp;width=$width&amp;height=$height&amp;imagetext=" . urlencode(ENTITY::hsc($text));\r
185                 $vars['popupcode']              = "window.open(this.href,'imagepopup','status=no,toolbar=no,scrollbars=no,resizable=yes,width=$windowwidth,height=$windowheight');return false;";\r
186                 $vars['popuptext']              = ENTITY::hsc($text);\r
187                 $vars['popuplink']              = '<a href="' . $vars['rawpopuplink']. '" onclick="'. $vars['popupcode'].'" >' . $vars['popuptext'] . '</a>';\r
188                 $vars['width']                  = $width;\r
189                 $vars['height']                 = $height;\r
190                 $vars['text']                   = $text;\r
191                 $vars['link']                   = ENTITY::hsc($CONF['MediaURL'] . $filename);\r
192                 $vars['media']                  = '<a href="' . $vars['link'] . '">' . $vars['popuptext'] . '</a>';\r
193 \r
194                 echo TEMPLATE::fill($this->template['POPUP_CODE'],$vars);\r
195         }\r
196         \r
197         // function to enable if-else-elseif-elseifnot-ifnot-endif to item template fields\r
198         \r
199         /**\r
200          * Checks conditions for if statements\r
201          *\r
202          * @param string $field type of <%if%>\r
203          * @param string $name property of field\r
204          * @param string $value value of property\r
205          */\r
206         function checkCondition($field, $name='', $value = '') {\r
207                 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;\r
208 \r
209                 $condition = 0;\r
210                 switch($field) {\r
211                         case 'category':\r
212                                 $condition = ($blog && $this->_ifCategory($name,$value));\r
213                                 break;\r
214                         case 'itemcategory':\r
215                                 $condition = ($this->_ifItemCategory($name,$value));\r
216                                 break;\r
217                         case 'blogsetting':\r
218                                 $condition = ($blog && ($blog->getSetting($name) == $value));\r
219                                 break;\r
220                         case 'itemblogsetting':\r
221                                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));\r
222                                 $condition = ($b && ($b->getSetting($name) == $value));\r
223                                 break;\r
224                         case 'loggedin':\r
225                                 $condition = $member->isLoggedIn();\r
226                                 break;\r
227                         case 'onteam':\r
228                                 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);\r
229                                 break;\r
230                         case 'admin':\r
231                                 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);\r
232                                 break;\r
233                         case 'author':\r
234                                 $condition = ($this->_ifAuthor($name,$value));\r
235                                 break;\r
236 /*                      case 'nextitem':\r
237                                 $condition = ($itemidnext != '');\r
238                                 break;\r
239                         case 'previtem':\r
240                                 $condition = ($itemidprev != '');\r
241                                 break;\r
242                         case 'archiveprevexists':\r
243                                 $condition = ($archiveprevexists == true);\r
244                                 break;\r
245                         case 'archivenextexists':\r
246                                 $condition = ($archivenextexists == true);\r
247                                 break; \r
248                         case 'skintype':\r
249                                 $condition = ($name == $this->skintype);\r
250                                 break; */\r
251                         case 'hasplugin':\r
252                                 $condition = $this->_ifHasPlugin($name, $value);\r
253                                 break;\r
254                         default:\r
255                                 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);\r
256                                 break;\r
257                 }\r
258                 return $condition;\r
259         }       \r
260         \r
261         /**\r
262          *  Different checks for a category\r
263          */\r
264         function _ifCategory($name = '', $value='') {\r
265                 global $blog, $catid;\r
266 \r
267                 // when no parameter is defined, just check if a category is selected\r
268                 if (($name != 'catname' && $name != 'catid') || ($value == ''))\r
269                         return $blog->isValidCategory($catid);\r
270 \r
271                 // check category name\r
272                 if ($name == 'catname') {\r
273                         $value = $blog->getCategoryIdFromName($value);\r
274                         if ($value == $catid)\r
275                                 return $blog->isValidCategory($catid);\r
276                 }\r
277 \r
278                 // check category id\r
279                 if (($name == 'catid') && ($value == $catid))\r
280                         return $blog->isValidCategory($catid);\r
281 \r
282                 return false;\r
283         }\r
284         \r
285                 \r
286         /**\r
287          *  Different checks for an author\r
288          */\r
289         function _ifAuthor($name = '', $value='') {\r
290                 global $member, $manager;\r
291                 \r
292                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));\r
293 \r
294                 // when no parameter is defined, just check if author is current visitor\r
295                 if (($name != 'isadmin' && $name != 'name') || ($name == 'name' && $value == '')) {\r
296                         return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));\r
297                 }\r
298 \r
299                 // check author name\r
300                 if ($name == 'name') {\r
301                         $value = strtolower($value);\r
302                         if ($value == strtolower($this->currentItem->author))\r
303                                 return true;\r
304                 }\r
305 \r
306                 // check if author is admin\r
307                 if (($name == 'isadmin')) {                     \r
308                         $aid = intval($this->currentItem->authorid);\r
309                         $blogid = intval($b->getID());                  \r
310                         $amember =& $manager->getMember($aid);\r
311                         if ($amember->isAdmin())\r
312                                 return true;\r
313                                 \r
314                         return $amember->isBlogAdmin($blogid);\r
315                 }\r
316 \r
317                 return false;\r
318         }\r
319         \r
320         /**\r
321          *  Different checks for a category\r
322          */\r
323         function _ifItemCategory($name = '', $value='') {\r
324                 global $catid, $manager;\r
325                 \r
326                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));\r
327 \r
328                 // when no parameter is defined, just check if a category is selected\r
329                 if (($name != 'catname' && $name != 'catid') || ($value == ''))\r
330                         return $b->isValidCategory($catid);\r
331                         \r
332                 $icatid = $this->currentItem->catid;\r
333                 //$icategory = $this->currentItem->category;\r
334 \r
335                 // check category name\r
336                 if ($name == 'catname') {\r
337                         $value = $b->getCategoryIdFromName($value);\r
338                         if ($value == $icatid)\r
339                                 return $b->isValidCategory($icatid);\r
340                 }\r
341 \r
342                 // check category id\r
343                 if (($name == 'catid') && ($value == $icatid))\r
344                         return $b->isValidCategory($icatid);\r
345 \r
346                 return false;\r
347         }\r
348 \r
349         \r
350         /**\r
351          *  Checks if a member is on the team of a blog and return his rights\r
352          */\r
353         function _ifOnTeam($blogName = '') {\r
354                 global $blog, $member, $manager;\r
355 \r
356                 // when no blog found\r
357                 if (($blogName == '') && (!is_object($blog)))\r
358                         return 0;\r
359 \r
360                 // explicit blog selection\r
361                 if ($blogName != '')\r
362                         $blogid = getBlogIDFromName($blogName);\r
363 \r
364                 if (($blogName == '') || !$manager->existsBlogID($blogid))\r
365                         // use current blog\r
366                         $blogid = $blog->getID();\r
367 \r
368                 return $member->teamRights($blogid);\r
369         }\r
370 \r
371         /**\r
372          *  Checks if a member is admin of a blog\r
373          */\r
374         function _ifAdmin($blogName = '') {\r
375                 global $blog, $member, $manager;\r
376 \r
377                 // when no blog found\r
378                 if (($blogName == '') && (!is_object($blog)))\r
379                         return 0;\r
380 \r
381                 // explicit blog selection\r
382                 if ($blogName != '')\r
383                         $blogid = getBlogIDFromName($blogName);\r
384 \r
385                 if (($blogName == '') || !$manager->existsBlogID($blogid))\r
386                         // use current blog\r
387                         $blogid = $blog->getID();\r
388 \r
389                 return $member->isBlogAdmin($blogid);\r
390         }\r
391 \r
392         \r
393         /**\r
394          *      hasplugin,PlugName\r
395          *         -> checks if plugin exists\r
396          *      hasplugin,PlugName,OptionName\r
397          *         -> checks if the option OptionName from plugin PlugName is not set to 'no'\r
398          *      hasplugin,PlugName,OptionName=value\r
399          *         -> checks if the option OptionName from plugin PlugName is set to value\r
400          */\r
401         function _ifHasPlugin($name, $value) {\r
402                 global $manager;\r
403                 $condition = false;\r
404                 // (pluginInstalled method won't write a message in the actionlog on failure)\r
405                 if ($manager->pluginInstalled('NP_'.$name)) {\r
406                         $plugin =& $manager->getPlugin('NP_' . $name);\r
407                         if ($plugin != NULL) {\r
408                                 if ($value == "") {\r
409                                         $condition = true;\r
410                                 } else {\r
411                                         list($name2, $value2) = i18n::explode('=', $value, 2);\r
412                                         if ($value2 == "" && $plugin->getOption($name2) != 'no') {\r
413                                                 $condition = true;\r
414                                         } else if ($plugin->getOption($name2) == $value2) {\r
415                                                 $condition = true;\r
416                                         }\r
417                                 }\r
418                         }\r
419                 }\r
420                 return $condition;\r
421         }\r
422 \r
423         /**\r
424          * Checks if a plugin exists and call its doIf function\r
425          */\r
426         function _ifPlugin($name, $key = '', $value = '') {\r
427                 global $manager;\r
428 \r
429                 $plugin =& $manager->getPlugin('NP_' . $name);\r
430                 if (!$plugin) return;\r
431 \r
432                 $params = func_get_args();\r
433                 array_shift($params);\r
434 \r
435                 return call_user_func_array(array(&$plugin, 'doIf'), $params);\r
436         }\r
437 \r
438 }\r
439 ?>\r