OSDN Git Service

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