OSDN Git Service

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