OSDN Git Service

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