OSDN Git Service

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