4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2009 The Nucleus Group
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)
14 * A class to parses plugin calls inside items
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 $
21 class BodyActions extends BaseActions
26 static private $defined_actions = array(
34 * BodyActions::__construct()
35 * Constructor of the BODYACTIONS
40 public function __construct()
42 parent::__construct();
47 * BodyActions::getAvailableActions()
48 * Get the defined actions in an item
51 * @return array self::$defined_actions
53 public function getAvailableActions()
55 return array_merge(self::$defined_actions, parent::getAvailableActions());
59 * BodyActions::setCurrentItem()
60 * Set the current item
62 * @param object &$item reference to the current item
65 public function setCurrentItem(&$item)
67 global $currentitemid;
68 $this->currentItem =& $item;
69 $currentitemid = $this->currentItem['itemid'];
74 * BodyActions::setTemplate()
75 * Set the current template
77 * @param string $template Template to be used
80 public function setTemplate($template)
82 $this->template =& $template;
87 * BodyActions::parse_plugin()
89 * Called if <%plugin(...)%> in an item appears
91 * Calls the doItemVar function in the plugin
93 public function parse_plugin($pluginName)
97 $plugin =& $manager->getPlugin('NP_' . $pluginName);
104 $params = func_get_args();
106 // remove plugin name
107 array_shift($params);
109 // add item reference (array_unshift didn't work)
110 $params = array_merge(array(&$this->currentItem), $params);
112 call_user_func_array(array(&$plugin, 'doItemVar'), $params);
117 * BodyActions::parse_image()
119 * Called if <%image(...)%> in an item appears
122 * @return parsed image tag
124 public function parse_image()
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);
133 * BodyActions::createImageCode()
134 * Creates the code for an image
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
141 public function createImageCode($filename, $width, $height, $text = '')
145 // select private collection when no collection given
146 if ( i18n::strpos($filename, '/') === FALSE )
148 $filename = $this->currentItem['authorid'] . '/' . $filename;
151 $windowwidth = $width;
152 $windowheight = $height;
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>';
161 return Template::fill($this->template['IMAGE_CODE'], $vars);
165 * BodyActions::parse_media()
167 * Called if <%media(...)%> in an item appears
170 * @param parsed media tag
172 public function parse_media()
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);
181 * BodyActions::createMediaCode()
182 * Creates the code for a media
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
188 public function createMediaCode($filename, $text = '')
192 // select private collection when no collection given
193 if ( i18n::strpos($filename, '/') === FALSE )
195 $filename = $this->currentItem['authorid'] . '/' . $filename;
198 $vars['link'] = Entity::hsc($CONF['MediaURL'] . $filename);
199 $vars['text'] = Entity::hsc($text);
200 $vars['media'] = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';
202 return Template::fill($this->template['MEDIA_CODE'], $vars);;
206 * BodyActions::parse_popup()
208 * Called if <%popup(...)%> in an item appears
211 * @return string parsed popup tag
213 public function parse_popup()
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);
222 * BodyActions::createPopupCode()
223 * Creates the code for a popup
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
231 public function createPopupCode($filename, $width, $height, $text = '')
235 // select private collection when no collection given
236 if ( i18n::strpos($filename, '/') === FALSE )
238 $filename = $this->currentItem['authorid'] . '/' . $filename;
241 $windowwidth = $width;
242 $windowheight = $height;
244 $vars['rawpopuplink'] = $CONF['Self'] . "?imagepopup=" . Entity::hsc($filename) . "&width=$width&height=$height&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>';
254 return Template::fill($this->template['POPUP_CODE'], $vars);
258 * BodyActions::checkCondition()
259 * Checks conditions for if statements
261 * @param string $field type of <%if%>
262 * @param string $name property of field
263 * @param string $value value of property
266 protected function checkCondition($field, $name='', $value = '')
268 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
274 $condition = ($blog && $this->ifCategory($name,$value));
277 $condition = ($this->ifItemCategory($name,$value));
280 $condition = ($blog && ($blog->getSetting($name) == $value));
282 case 'itemblogsetting':
283 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem['itemid']));
284 $condition = ($b && ($b->getSetting($name) == $value));
287 $condition = $member->isLoggedIn();
290 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
293 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
296 $condition = ($this->ifAuthor($name,$value));
299 $condition = $this->ifHasPlugin($name, $value);
302 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
309 * BodyActions::ifCategory()
310 * Different checks for a category
312 * @param string $key key for data of category
313 * @param string $value value for data of category
316 private function ifCategory($key = '', $value = '')
318 global $blog, $catid;
320 // when no parameter is defined, just check if a category is selected
321 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
323 return $blog->isValidCategory($catid);
326 // check category name
327 if ( $key == 'catname' )
329 $value = $blog->getCategoryIdFromName($value);
330 if ( $value == $catid )
332 return $blog->isValidCategory($catid);
337 if ( ($key == 'catid') && ($value == $catid) )
339 return $blog->isValidCategory($catid);
346 * BodyActions::ifAuthor()
347 * Different checks for an author
349 * @param string $key key for data of author
350 * @param string $value value for data of author
353 private function ifAuthor($key = '', $value = '')
355 global $member, $manager;
357 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem['itemid']));
359 // when no parameter is defined, just check if author is current visitor
360 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
362 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem['authorid']));
366 if ( $key == 'name' )
368 $value = strtolower($value);
369 if ( $value == strtolower($this->currentItem['author']) )
375 // check if author is admin
376 if ( ($key == 'isadmin') )
378 $aid = intval($this->currentItem['authorid']);
379 $blogid = intval($b->getID());
380 $amember =& $manager->getMember($aid);
381 if ( $amember->isAdmin() )
385 return $amember->isBlogAdmin($blogid);
392 * BodyActions::ifItemCategory()
393 * Different checks for a category
395 * @param string $key key for data of category
396 * @param string $value value for data of category
399 private function ifItemCategory($key = '', $value = '')
401 global $catid, $manager;
403 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem['itemid']));
405 // when no parameter is defined, just check if a category is selected
406 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
408 return $b->isValidCategory($catid);
411 $icatid = $this->currentItem['catid'];
413 // check category name
414 if ( $key == 'catname' )
416 $value = $b->getCategoryIdFromName($value);
417 if ( $value == $icatid )
419 return $b->isValidCategory($icatid);
424 if ( ($key == 'catid') && ($value == $icatid) )
426 return $b->isValidCategory($icatid);
432 * BodyActions::ifOnTeam()
433 * Checks if a member is on the team of a blog and return his rights
435 * @param string $blogName name of weblog
438 private function ifOnTeam($blogName = '')
440 global $blog, $member, $manager;
442 // when no blog found
443 if ( ($blogName == '') && (!is_object($blog)) )
448 // explicit blog selection
449 if ( $blogName != '' )
451 $blogid = getBlogIDFromName($blogName);
454 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
457 $blogid = $blog->getID();
459 return $member->teamRights($blogid);
463 * BodyActions::ifAdmin()
464 * Checks if a member is admin of a blog
466 * @param string $blogName name of weblog
469 private function ifAdmin($blogName = '')
471 global $blog, $member, $manager;
473 // when no blog found
474 if ( ($blogName == '') && (!is_object($blog)) )
479 // explicit blog selection
480 if ( $blogName != '' )
482 $blogid = getBlogIDFromName($blogName);
485 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
488 $blogid = $blog->getID();
490 return $member->isBlogAdmin($blogid);
495 * BodyActions::ifHasPlugin()
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
503 * @param string $name name of plugin
504 * @param string $value value for plugin argument
507 private function ifHasPlugin($name, $value)
512 // (pluginInstalled method won't write a message in the actionlog on failure)
513 if ( $manager->pluginInstalled("NP_{$name}") )
515 $plugin =& $manager->getPlugin("NP_{$name}");
516 if ( $plugin != NULL )
524 list($name2, $value2) = preg_split('#=#', $value, 2);
525 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
529 else if ( $plugin->getOption($name2) == $value2 )
540 * BodyActions::ifPlugin()
541 * Checks if a plugin exists and call its doIf function
543 * @param string $name name of plugin
544 * @param string $key ...
545 * @param string $value ...
546 * @return string result of plugin 'doIf'
548 private function ifPlugin($name, $key = '', $value = '')
552 $plugin =& $manager->getPlugin("NP_{$name}");
558 $params = func_get_args();
559 array_shift($params);
561 return call_user_func_array(array(&$plugin, 'doIf'), $params);