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(
40 * BodyActions::__construct()
41 * Constructor of the BODYACTIONS
46 public function __construct()
48 parent::__construct();
53 * BodyActions::setCurrentItem()
54 * Set the current item
56 * @param object &$item reference to the current item
59 public function setCurrentItem(&$item)
61 global $currentitemid;
62 $this->currentItem =& $item;
63 $currentitemid = $this->currentItem->itemid;
68 * BodyActions::setTemplate()
69 * Set the current template
71 * @param string $template Template to be used
74 public function setTemplate($template)
76 $this->template =& $template;
81 * BodyActions::getDefinedActions()
82 * Get the defined actions in an item
85 * @return Array self::$defined_actions
87 public function getDefinedActions()
89 return self::$defined_actions;
93 * BodyActions::parse_plugin()
95 * Called if <%plugin(...)%> in an item appears
97 * Calls the doItemVar function in the plugin
99 public function parse_plugin($pluginName)
103 $plugin =& $manager->getPlugin('NP_' . $pluginName);
110 $params = func_get_args();
112 // remove plugin name
113 array_shift($params);
115 // add item reference (array_unshift didn't work)
116 $params = array_merge(array(&$this->currentItem),$params);
118 call_user_func_array(array(&$plugin,'doItemVar'), $params);
123 * BodyActions::parse_image()
125 * Called if <%image(...)%> in an item appears
128 * @return parsed image tag
130 public function parse_image()
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);
139 * BodyActions::createImageCode()
140 * Creates the code for an image
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
147 public function createImageCode($filename, $width, $height, $text = '')
151 // select private collection when no collection given
152 if ( !strstr($filename,'/') )
154 $filename = $this->currentItem->authorid . '/' . $filename;
157 $windowwidth = $width;
158 $windowheight = $height;
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>';
167 return Template::fill($this->template['IMAGE_CODE'],$vars);;
171 * BodyActions::parse_media()
173 * Called if <%media(...)%> in an item appears
176 * @param parsed media tag
178 public function parse_media()
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);
187 * BodyActions::createMediaCode()
188 * Creates the code for a media
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
194 public function createMediaCode($filename, $text = '')
198 // select private collection when no collection given
199 if ( !strstr($filename,'/') )
201 $filename = $this->currentItem->authorid . '/' . $filename;
204 $vars['link'] = Entity::hsc($CONF['MediaURL'] . $filename);
205 $vars['text'] = Entity::hsc($text);
206 $vars['media'] = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';
208 return Template::fill($this->template['MEDIA_CODE'], $vars);;
212 * BodyActions::parse_popup()
214 * Called if <%popup(...)%> in an item appears
217 * @return string parsed popup tag
219 public function parse_popup()
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);
228 * BodyActions::createPopupCode()
229 * Creates the code for a popup
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
237 public function createPopupCode($filename, $width, $height, $text = '')
241 // select private collection when no collection given
242 if ( !strstr($filename,'/') )
244 $filename = $this->currentItem->authorid . '/' . $filename;
247 $windowwidth = $width;
248 $windowheight = $height;
250 $vars['rawpopuplink'] = $CONF['Self'] . "?imagepopup=" . Entity::hsc($filename) . "&width=$width&height=$height&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>';
260 return Template::fill($this->template['POPUP_CODE'], $vars);
264 * BodyActions::checkCondition()
265 * Checks conditions for if statements
267 * @param string $field type of <%if%>
268 * @param string $name property of field
269 * @param string $value value of property
272 public function checkCondition($field, $name='', $value = '')
274 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
280 $condition = ($blog && $this->ifCategory($name,$value));
283 $condition = ($this->ifItemCategory($name,$value));
286 $condition = ($blog && ($blog->getSetting($name) == $value));
288 case 'itemblogsetting':
289 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
290 $condition = ($b && ($b->getSetting($name) == $value));
293 $condition = $member->isLoggedIn();
296 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
299 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
302 $condition = ($this->ifAuthor($name,$value));
305 $condition = $this->ifHasPlugin($name, $value);
308 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
315 * BodyActions::ifCategory()
316 * Different checks for a category
318 * @param string $key key for data of category
319 * @param string $value value for data of category
322 private function ifCategory($key = '', $value = '')
324 global $blog, $catid;
326 // when no parameter is defined, just check if a category is selected
327 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
329 return $blog->isValidCategory($catid);
332 // check category name
333 if ( $key == 'catname' )
335 $value = $blog->getCategoryIdFromName($value);
336 if ( $value == $catid )
338 return $blog->isValidCategory($catid);
343 if ( ($key == 'catid') && ($value == $catid) )
345 return $blog->isValidCategory($catid);
352 * BodyActions::ifAuthor()
353 * Different checks for an author
355 * @param string $key key for data of author
356 * @param string $value value for data of author
359 private function ifAuthor($key = '', $value = '')
361 global $member, $manager;
363 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
365 // when no parameter is defined, just check if author is current visitor
366 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
368 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));
372 if ( $key == 'name' )
374 $value = strtolower($value);
375 if ( $value == strtolower($this->currentItem->author) )
381 // check if author is admin
382 if ( ($key == 'isadmin') )
384 $aid = intval($this->currentItem->authorid);
385 $blogid = intval($b->getID());
386 $amember =& $manager->getMember($aid);
387 if ( $amember->isAdmin() )
391 return $amember->isBlogAdmin($blogid);
398 * BodyActions::ifItemCategory()
399 * Different checks for a category
401 * @param string $key key for data of category
402 * @param string $value value for data of category
405 private function ifItemCategory($key = '', $value = '')
407 global $catid, $manager;
409 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
411 // when no parameter is defined, just check if a category is selected
412 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
414 return $b->isValidCategory($catid);
417 $icatid = $this->currentItem->catid;
419 // check category name
420 if ( $key == 'catname' )
422 $value = $b->getCategoryIdFromName($value);
423 if ( $value == $icatid )
425 return $b->isValidCategory($icatid);
430 if ( ($key == 'catid') && ($value == $icatid) )
432 return $b->isValidCategory($icatid);
438 * BodyActions::ifOnTeam()
439 * Checks if a member is on the team of a blog and return his rights
441 * @param string $blogName name of weblog
444 private function ifOnTeam($blogName = '')
446 global $blog, $member, $manager;
448 // when no blog found
449 if ( ($blogName == '') && (!is_object($blog)) )
454 // explicit blog selection
455 if ( $blogName != '' )
457 $blogid = getBlogIDFromName($blogName);
460 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
463 $blogid = $blog->getID();
465 return $member->teamRights($blogid);
469 * BodyActions::ifAdmin()
470 * Checks if a member is admin of a blog
472 * @param string $blogName name of weblog
475 private function ifAdmin($blogName = '')
477 global $blog, $member, $manager;
479 // when no blog found
480 if ( ($blogName == '') && (!is_object($blog)) )
485 // explicit blog selection
486 if ( $blogName != '' )
488 $blogid = getBlogIDFromName($blogName);
491 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
494 $blogid = $blog->getID();
496 return $member->isBlogAdmin($blogid);
501 * BodyActions::ifHasPlugin()
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
509 * @param string $name name of plugin
510 * @param string $value value for plugin argument
513 private function ifHasPlugin($name, $value)
518 // (pluginInstalled method won't write a message in the actionlog on failure)
519 if ( $manager->pluginInstalled("NP_{$name}") )
521 $plugin =& $manager->getPlugin("NP_{$name}");
522 if ( $plugin != NULL )
530 list($name2, $value2) = preg_split('#=#', $value, 2);
531 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
535 else if ( $plugin->getOption($name2) == $value2 )
546 * BodyActions::ifPlugin()
547 * Checks if a plugin exists and call its doIf function
549 * @param string $name name of plugin
550 * @param string $key ...
551 * @param string $value ...
552 * @return string result of plugin 'doIf'
554 private function ifPlugin($name, $key = '', $value = '')
558 $plugin =& $manager->getPlugin("NP_{$name}");
564 $params = func_get_args();
565 array_shift($params);
567 return call_user_func_array(array(&$plugin, 'doIf'), $params);