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 1886 2012-06-17 08:27:27Z 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 $target = array(&$this->currentItem);
111 $params = array_merge($target, $params);
113 call_user_func_array(array($plugin, 'doItemVar'), $params);
118 * BodyActions::parse_image()
120 * Called if <%image(...)%> in an item appears
123 * @return parsed image tag
125 public function parse_image()
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);
134 * BodyActions::createImageCode()
135 * Creates the code for an image
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
142 public function createImageCode($filename, $width, $height, $text = '')
146 // select private collection when no collection given
147 if ( i18n::strpos($filename, '/') === FALSE )
149 $filename = $this->currentItem['authorid'] . '/' . $filename;
152 $windowwidth = $width;
153 $windowheight = $height;
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>';
162 return Template::fill($this->template['IMAGE_CODE'], $vars);
166 * BodyActions::parse_media()
168 * Called if <%media(...)%> in an item appears
171 * @param parsed media tag
173 public function parse_media()
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);
182 * BodyActions::createMediaCode()
183 * Creates the code for a media
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
189 public function createMediaCode($filename, $text = '')
193 // select private collection when no collection given
194 if ( i18n::strpos($filename, '/') === FALSE )
196 $filename = $this->currentItem['authorid'] . '/' . $filename;
199 $vars['link'] = Entity::hsc($CONF['MediaURL'] . $filename);
200 $vars['text'] = Entity::hsc($text);
201 $vars['media'] = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';
203 return Template::fill($this->template['MEDIA_CODE'], $vars);;
207 * BodyActions::parse_popup()
209 * Called if <%popup(...)%> in an item appears
212 * @return string parsed popup tag
214 public function parse_popup()
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);
223 * BodyActions::createPopupCode()
224 * Creates the code for a popup
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
232 public function createPopupCode($filename, $width, $height, $text = '')
236 // select private collection when no collection given
237 if ( i18n::strpos($filename, '/') === FALSE )
239 $filename = $this->currentItem['authorid'] . '/' . $filename;
242 $windowwidth = $width;
243 $windowheight = $height;
245 $vars['rawpopuplink'] = $CONF['Self'] . "?imagepopup=" . Entity::hsc($filename) . "&width=$width&height=$height&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>';
255 return Template::fill($this->template['POPUP_CODE'], $vars);
259 * BodyActions::checkCondition()
260 * Checks conditions for if statements
262 * @param string $field type of <%if%>
263 * @param string $name property of field
264 * @param string $value value of property
267 protected function checkCondition($field, $name='', $value = '')
269 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
275 $condition = ($blog && $this->ifCategory($name,$value));
278 $condition = ($this->ifItemCategory($name,$value));
281 $condition = ($blog && ($blog->getSetting($name) == $value));
283 case 'itemblogsetting':
284 $b =& $manager->getBlog($this->currentItem['blogid']);
285 $condition = ($b && ($b->getSetting($name) == $value));
288 $condition = $member->isLoggedIn();
291 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
294 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
297 $condition = ($this->ifAuthor($name,$value));
300 $condition = $this->ifHasPlugin($name, $value);
303 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
310 * BodyActions::ifCategory()
311 * Different checks for a category
313 * @param string $key key for data of category
314 * @param string $value value for data of category
317 private function ifCategory($key = '', $value = '')
319 global $blog, $catid;
321 // when no parameter is defined, just check if a category is selected
322 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
324 return $blog->isValidCategory($catid);
327 // check category name
328 if ( $key == 'catname' )
330 $value = $blog->getCategoryIdFromName($value);
331 if ( $value == $catid )
333 return $blog->isValidCategory($catid);
338 if ( ($key == 'catid') && ($value == $catid) )
340 return $blog->isValidCategory($catid);
347 * BodyActions::ifAuthor()
348 * Different checks for an author
350 * @param string $key key for data of author
351 * @param string $value value for data of author
354 private function ifAuthor($key = '', $value = '')
356 global $member, $manager;
358 $b =& $manager->getBlog($this->currentItem['blogid']);
360 // when no parameter is defined, just check if author is current visitor
361 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
363 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem['authorid']));
367 if ( $key == 'name' )
369 $value = strtolower($value);
370 if ( $value == strtolower($this->currentItem['author']) )
376 // check if author is admin
377 if ( ($key == 'isadmin') )
379 $aid = intval($this->currentItem['authorid']);
380 $blogid = intval($b->getID());
381 $amember =& $manager->getMember($aid);
382 if ( $amember->isAdmin() )
386 return $amember->isBlogAdmin($blogid);
393 * BodyActions::ifItemCategory()
394 * Different checks for a category
396 * @param string $key key for data of category
397 * @param string $value value for data of category
400 private function ifItemCategory($key = '', $value = '')
402 global $catid, $manager;
404 $b =& $manager->getBlog($this->currentItem['blogid']);
406 // when no parameter is defined, just check if a category is selected
407 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
409 return $b->isValidCategory($catid);
412 $icatid = $this->currentItem['catid'];
414 // check category name
415 if ( $key == 'catname' )
417 $value = $b->getCategoryIdFromName($value);
418 if ( $value == $icatid )
420 return $b->isValidCategory($icatid);
425 if ( ($key == 'catid') && ($value == $icatid) )
427 return $b->isValidCategory($icatid);
433 * BodyActions::ifOnTeam()
434 * Checks if a member is on the team of a blog and return his rights
436 * @param string $blogName name of weblog
439 private function ifOnTeam($blogName = '')
441 global $blog, $member, $manager;
443 // when no blog found
444 if ( ($blogName == '') && (!is_object($blog)) )
449 // explicit blog selection
450 if ( $blogName != '' )
452 $blogid = getBlogIDFromName($blogName);
455 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
458 $blogid = $blog->getID();
460 return $member->teamRights($blogid);
464 * BodyActions::ifAdmin()
465 * Checks if a member is admin of a blog
467 * @param string $blogName name of weblog
470 private function ifAdmin($blogName = '')
472 global $blog, $member, $manager;
474 // when no blog found
475 if ( ($blogName == '') && (!is_object($blog)) )
480 // explicit blog selection
481 if ( $blogName != '' )
483 $blogid = getBlogIDFromName($blogName);
486 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
489 $blogid = $blog->getID();
491 return $member->isBlogAdmin($blogid);
496 * BodyActions::ifHasPlugin()
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
504 * @param string $name name of plugin
505 * @param string $value value for plugin argument
508 private function ifHasPlugin($name, $value)
513 // (pluginInstalled method won't write a message in the actionlog on failure)
514 if ( $manager->pluginInstalled("NP_{$name}") )
516 $plugin =& $manager->getPlugin("NP_{$name}");
517 if ( $plugin != NULL )
525 list($name2, $value2) = preg_split('#=#', $value, 2);
526 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
530 else if ( $plugin->getOption($name2) == $value2 )
541 * BodyActions::ifPlugin()
542 * Checks if a plugin exists and call its doIf function
544 * @param string $name name of plugin
545 * @param string $key ...
546 * @param string $value ...
547 * @return string result of plugin 'doIf'
549 private function ifPlugin($name, $key = '', $value = '')
553 $plugin =& $manager->getPlugin("NP_{$name}");
559 $params = func_get_args();
560 array_shift($params);
562 return call_user_func_array(array($plugin, 'doIf'), $params);