OSDN Git Service

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