OSDN Git Service

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