OSDN Git Service

MERGE: リビジョン1782。checkCondition()のアクセス修飾子をprotectedに変更。
[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 1757 2012-04-15 09:02:32Z sakamocchi $
19  */
20
21 class BodyActions extends BaseActions
22 {
23         private $currentItem;
24         private $template;
25         
26         static private $defined_actions = array(
27                 'image',
28                 'media',
29                 'popup',
30                 'plugin',
31         );
32         
33         /**
34          * BodyActions::__construct()
35          * Constructor of the BODYACTIONS
36          * 
37          * @param       void
38          * @return      void
39          */
40         public function __construct()
41         {
42                 parent::__construct();  
43                 return;
44         }
45         
46         /**
47          * BodyActions::setCurrentItem()
48          * Set the current item
49          * 
50          * @param       object  &$item  reference to the current item
51          * @return      void
52          */
53         public function setCurrentItem(&$item)
54         {
55                 global $currentitemid;
56                 $this->currentItem =& $item;
57                 $currentitemid = $this->currentItem->itemid;
58                 return;
59         }
60         
61         /**
62          * BodyActions::setTemplate()
63          * Set the current template
64          * 
65          * @param       string  $template       Template to be used
66          * @return      void
67          */
68         public function setTemplate($template)
69         {
70                 $this->template =& $template;
71                 return;
72         }
73         
74         /**
75          * BodyActions::getDefinedActions()
76          * Get the defined actions in an item
77          * 
78          * @static
79          * @param       void
80          * @return      Array   self::$defined_actions
81          */
82         static public function getDefinedActions()
83         {
84                 return array_merge(self::$defined_actions, parent::getDefinedActions());
85         }
86         
87         /**
88          * BodyActions::parse_plugin()
89          * Parse a plugin var
90          * Called if <%plugin(...)%> in an item appears
91          * 
92          * Calls the doItemVar function in the plugin
93          */
94         public function parse_plugin($pluginName)
95         {
96                 global $manager;
97                 
98                 $plugin =& $manager->getPlugin('NP_' . $pluginName);
99                 if ( !$plugin )
100                 {
101                         return; 
102                 }
103                 
104                 // get arguments
105                 $params = func_get_args();
106                 
107                 // remove plugin name
108                 array_shift($params);
109                 
110                 // add item reference (array_unshift didn't work)
111                 $params = array_merge(array(&$this->currentItem), $params);
112                 
113                 call_user_func_array(array(&$plugin, 'doItemVar'), $params);
114                 return;
115         }
116         
117         /**
118          * BodyActions::parse_image()
119          * Parse image
120          * Called if <%image(...)%> in an item appears
121          * 
122          * @param       void
123          * @return      parsed image tag
124          */
125         public function parse_image()
126         {
127                 // image/popup calls have arguments separated by |
128                 $args = func_get_args();
129                 $args = preg_split('#\|#', implode($args, ', '));
130                 echo call_user_func_array(array(&$this, 'createImageCode'), $args);
131         }
132         
133         /**
134          * BodyActions::createImageCode()
135          * Creates the code for an image
136          * 
137          * @param       string  $filename       name of file from tag
138          * @param       integer $width          width of file from tag
139          * @param       integer $height         height of file from tag
140          * @return      string  image element with anchor element
141          */
142         public function createImageCode($filename, $width, $height, $text = '')
143         {
144                 global $CONF;
145                 
146                 // select private collection when no collection given
147                 if ( !strstr($filename, '/') )
148                 {
149                         $filename = $this->currentItem->authorid . '/' . $filename;
150                 }
151                 
152                 $windowwidth = $width;
153                 $windowheight = $height;
154                 
155                 $vars['link']   = Entity::hsc($CONF['MediaURL']. $filename);
156                 $vars['text']   = Entity::hsc($text);
157                 $vars['image']  = '<img src="' . $vars['link'] . '" width="' . $width . '" height="' . $height . '" alt="' . $vars['text'] . '" title="' . $vars['text'] . '" />';
158                 $vars['width']  = $width;
159                 $vars['height'] = $height;
160                 $vars['media']  = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';
161                 
162                 return Template::fill($this->template['IMAGE_CODE'], $vars);
163         }
164         
165         /**
166          * BodyActions::parse_media()
167          * Parse media
168          * Called if <%media(...)%> in an item appears
169          * 
170          * @param       void
171          * @param       parsed media tag
172          */
173         public function parse_media()
174         {
175                 // image/popup calls have arguments separated by |
176                 $args = func_get_args();
177                 $args = preg_split('#\|#', implode($args, ', '));
178                 echo call_user_func_array(array(&$this, 'createMediaCode'), $args);
179         }
180         
181         /**
182          * BodyActions::createMediaCode()
183          * Creates the code for a media
184          * 
185          * @param       string  $filename       name of file from tag
186          * @param       string  $text           alternative text from tag
187          * @return      string  text element with anchor element
188          */
189         public function createMediaCode($filename, $text = '')
190         {
191                 global $CONF;
192                 
193                 // select private collection when no collection given
194                 if ( !strstr($filename, '/') )
195                 {
196                         $filename = $this->currentItem->authorid . '/' . $filename;
197                 }
198                 
199                 $vars['link']                   = Entity::hsc($CONF['MediaURL'] . $filename);
200                 $vars['text']                   = Entity::hsc($text);
201                 $vars['media']                  = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';
202                 
203                 return Template::fill($this->template['MEDIA_CODE'], $vars);;
204         }
205         
206         /**
207          * BodyActions::parse_popup()
208          * Parse popup
209          * Called if <%popup(...)%> in an item appears
210          * 
211          * @param       void
212          * @return      string  parsed popup tag
213          */
214         public function parse_popup()
215         {
216                 // image/popup calls have arguments separated by |
217                 $args = func_get_args();
218                 $args = preg_split('#\|#', implode($args, ', '));
219                 echo call_user_func_array(array(&$this, 'createPopupCode'), $args);
220         }
221         
222         /**
223          * BodyActions::createPopupCode()
224          * Creates the code for a popup
225          * 
226          * @param       string  $filename       name of file from tag
227          * @param       integer $width          width of file from tag
228          * @param       integer $height         height of file from tag
229          * @param       string  $text           alternative text from tag
230          * @return      string  text element with anchor element of JavaScript window.open
231          */
232         public function createPopupCode($filename, $width, $height, $text = '')
233         {
234                 global $CONF;
235                 
236                 // select private collection when no collection given
237                 if ( !strstr($filename, '/') )
238                 {
239                         $filename = $this->currentItem->authorid . '/' . $filename;
240                 }
241                 
242                 $windowwidth = $width;
243                 $windowheight = $height;
244                 
245                 $vars['rawpopuplink']   = $CONF['Self'] . "?imagepopup=" . Entity::hsc($filename) . "&amp;width=$width&amp;height=$height&amp;imagetext=" . urlencode(Entity::hsc($text));
246                 $vars['popupcode']              = "window.open(this.href,'imagepopup','status=no,toolbar=no,scrollbars=no,resizable=yes,width=$windowwidth,height=$windowheight');return false;";
247                 $vars['popuptext']              = Entity::hsc($text);
248                 $vars['popuplink']              = '<a href="' . $vars['rawpopuplink']. '" onclick="'. $vars['popupcode'].'" >' . $vars['popuptext'] . '</a>';
249                 $vars['width']                  = $width;
250                 $vars['height']                 = $height;
251                 $vars['text']                   = $text;
252                 $vars['link']                   = Entity::hsc($CONF['MediaURL'] . $filename);
253                 $vars['media']                  = '<a href="' . $vars['link'] . '">' . $vars['popuptext'] . '</a>';
254                 
255                 return Template::fill($this->template['POPUP_CODE'], $vars);
256         }
257         
258         /**
259          * BodyActions::checkCondition()
260          * Checks conditions for if statements
261          *
262          * @param       string  $field  type of <%if%>
263          * @param       string  $name   property of field
264          * @param       string  $value  value of property
265          * @return      condition
266          */
267         protected function checkCondition($field, $name='', $value = '')
268         {
269                 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
270                 
271                 $condition = 0;
272                 switch ( $field )
273                 {
274                         case 'category':
275                                 $condition = ($blog && $this->ifCategory($name,$value));
276                                 break;
277                         case 'itemcategory':
278                                 $condition = ($this->ifItemCategory($name,$value));
279                                 break;
280                         case 'blogsetting':
281                                 $condition = ($blog && ($blog->getSetting($name) == $value));
282                                 break;
283                         case 'itemblogsetting':
284                                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
285                                 $condition = ($b && ($b->getSetting($name) == $value));
286                                 break;
287                         case 'loggedin':
288                                 $condition = $member->isLoggedIn();
289                                 break;
290                         case 'onteam':
291                                 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
292                                 break;
293                         case 'admin':
294                                 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
295                                 break;
296                         case 'author':
297                                 $condition = ($this->ifAuthor($name,$value));
298                                 break;
299                         case 'hasplugin':
300                                 $condition = $this->ifHasPlugin($name, $value);
301                                 break;
302                         default:
303                                 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
304                                 break;
305                 }
306                 return $condition;
307         }       
308         
309         /**
310          * BodyActions::ifCategory()
311          *  Different checks for a category
312          *  
313          * @param       string  $key    key for data of category
314          * @param       string  $value  value for data of category
315          * @return      boolean
316          */
317         private function ifCategory($key = '', $value = '')
318         {
319                 global $blog, $catid;
320                 
321                 // when no parameter is defined, just check if a category is selected
322                 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
323                 {
324                         return $blog->isValidCategory($catid);
325                 }
326                 
327                 // check category name
328                 if ( $key == 'catname' )
329                 {
330                         $value = $blog->getCategoryIdFromName($value);
331                         if ( $value == $catid )
332                         {
333                                 return $blog->isValidCategory($catid);
334                         }
335                 }
336                 
337                 // check category id
338                 if ( ($key == 'catid') && ($value == $catid) )
339                 {
340                         return $blog->isValidCategory($catid);
341                 }
342                 
343                 return FALSE;
344         }
345         
346         /**
347          * BodyActions::ifAuthor()
348          * Different checks for an author
349          * 
350          * @param       string  $key    key for data of author
351          * @param       string  $value  value for data of author
352          * @return      boolean
353          */
354         private function ifAuthor($key = '', $value = '')
355         {
356                 global $member, $manager;
357                 
358                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
359                 
360                 // when no parameter is defined, just check if author is current visitor
361                 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
362                 {
363                         return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));
364                 }
365                 
366                 // check author name
367                 if ( $key == 'name' )
368                 {
369                         $value = strtolower($value);
370                         if ( $value == strtolower($this->currentItem->author) )
371                         {
372                                 return TRUE;
373                         }
374                 }
375                 
376                 // check if author is admin
377                 if ( ($key == 'isadmin') )
378                 {
379                         $aid = intval($this->currentItem->authorid);
380                         $blogid = intval($b->getID());                  
381                         $amember =& $manager->getMember($aid);
382                         if ( $amember->isAdmin() )
383                         {
384                                 return TRUE;
385                         }       
386                         return $amember->isBlogAdmin($blogid);
387                 }
388                 
389                 return FALSE;
390         }
391         
392         /**
393          * BodyActions::ifItemCategory()
394          * Different checks for a category
395          * 
396          * @param       string  $key    key for data of category
397          * @param       string  $value  value for data of category
398          * @return      boolean 
399          */
400         private function ifItemCategory($key = '', $value = '')
401         {
402                 global $catid, $manager;
403                 
404                 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
405                 
406                 // when no parameter is defined, just check if a category is selected
407                 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
408                 {
409                         return $b->isValidCategory($catid);
410                 }
411                         
412                 $icatid = $this->currentItem->catid;
413                 
414                 // check category name
415                 if ( $key == 'catname' )
416                 {
417                         $value = $b->getCategoryIdFromName($value);
418                         if ( $value == $icatid )
419                         {
420                                 return $b->isValidCategory($icatid);
421                         }
422                 }
423                 
424                 // check category id
425                 if ( ($key == 'catid') && ($value == $icatid) )
426                 {
427                         return $b->isValidCategory($icatid);
428                 }
429                 return FALSE;
430         }
431         
432         /**
433          * BodyActions::ifOnTeam()
434          * Checks if a member is on the team of a blog and return his rights
435          * 
436          * @param       string  $blogName       name of weblog
437          * @return      boolean
438          */
439         private function ifOnTeam($blogName = '')
440         {
441                 global $blog, $member, $manager;
442                 
443                 // when no blog found
444                 if ( ($blogName == '') && (!is_object($blog)) )
445                 {
446                         return 0;
447                 }
448                 
449                 // explicit blog selection
450                 if ( $blogName != '' )
451                 {
452                         $blogid = getBlogIDFromName($blogName);
453                 }
454                 
455                 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
456                 {
457                         // use current blog
458                         $blogid = $blog->getID();
459                 }
460                 return $member->teamRights($blogid);
461         }
462         
463         /**
464          * BodyActions::ifAdmin()
465          * Checks if a member is admin of a blog
466          * 
467          * @param       string  $blogName       name of weblog
468          * @return      boolean
469          */
470         private function ifAdmin($blogName = '')
471         {
472                 global $blog, $member, $manager;
473                 
474                 // when no blog found
475                 if ( ($blogName == '') && (!is_object($blog)) )
476                 {
477                         return 0;
478                 }
479                 
480                 // explicit blog selection
481                 if ( $blogName != '' )
482                 {
483                         $blogid = getBlogIDFromName($blogName);
484                 }
485                 
486                 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
487                 {
488                         // use current blog
489                         $blogid = $blog->getID();
490                 }
491                 return $member->isBlogAdmin($blogid);
492         }
493         
494         
495         /**
496          * BodyActions::ifHasPlugin()
497          *      hasplugin,PlugName
498          *         -> checks if plugin exists
499          *      hasplugin,PlugName,OptionName
500          *         -> checks if the option OptionName from plugin PlugName is not set to 'no'
501          *      hasplugin,PlugName,OptionName=value
502          *         -> checks if the option OptionName from plugin PlugName is set to value
503          *
504          * @param       string  $name   name of plugin
505          * @param       string  $value  value for plugin argument
506          * @return      boolean
507          */
508         private function ifHasPlugin($name, $value)
509         {
510                 global $manager;
511                 $condition = false;
512                 
513                 // (pluginInstalled method won't write a message in the actionlog on failure)
514                 if ( $manager->pluginInstalled("NP_{$name}") )
515                 {
516                         $plugin =& $manager->getPlugin("NP_{$name}");
517                         if ( $plugin != NULL )
518                         {
519                                 if ( $value == "" )
520                                 {
521                                         $condition = TRUE;
522                                 }
523                                 else
524                                 {
525                                         list($name2, $value2) = preg_split('#=#', $value, 2);
526                                         if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
527                                         {
528                                                 $condition = TRUE;
529                                         }
530                                         else if ( $plugin->getOption($name2) == $value2 )
531                                         {
532                                                 $condition = TRUE;
533                                         }
534                                 }
535                         }
536                 }
537                 return $condition;
538         }
539         
540         /**
541          * BodyActions::ifPlugin()
542          * Checks if a plugin exists and call its doIf function
543          * 
544          * @param       string  $name   name of plugin
545          * @param       string  $key    ...
546          * @param       string  $value  ...
547          * @return      string  result of plugin 'doIf'
548          */
549         private function ifPlugin($name, $key = '', $value = '')
550         {
551                 global $manager;
552                 
553                 $plugin =& $manager->getPlugin("NP_{$name}");
554                 if ( !$plugin )
555                 {
556                         return;
557                 }
558                 
559                 $params = func_get_args();
560                 array_shift($params);
561                 
562                 return call_user_func_array(array(&$plugin, 'doIf'), $params);
563         }
564 }