OSDN Git Service

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