4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2009 The Nucleus Group
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation; either version 2
\r
10 * of the License, or (at your option) any later version.
\r
11 * (see nucleus/documentation/index.html#license for more info)
\r
14 * A class to parses plugin calls inside items
\r
16 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
17 * @copyright Copyright (C) 2002-2009 The Nucleus Group
\r
18 * @version $Id: BODYACTIONS.php 1757 2012-04-15 09:02:32Z sakamocchi $
\r
21 class BodyActions extends BaseActions
\r
23 private $currentItem;
\r
26 static private $defined_actions = array(
\r
34 * BodyActions::__construct()
\r
35 * Constructor of the BODYACTIONS
\r
40 public function __construct()
\r
42 parent::__construct();
47 * BodyActions::setCurrentItem()
\r
48 * Set the current item
\r
50 * @param object &$item reference to the current item
\r
53 public function setCurrentItem(&$item)
\r
55 global $currentitemid;
\r
56 $this->currentItem =& $item;
\r
57 $currentitemid = $this->currentItem->itemid;
\r
62 * BodyActions::setTemplate()
\r
63 * Set the current template
\r
65 * @param string $template Template to be used
\r
68 public function setTemplate($template)
\r
70 $this->template =& $template;
\r
75 * BodyActions::getDefinedActions()
\r
76 * Get the defined actions in an item
\r
80 * @return Array self::$defined_actions
\r
82 static public function getDefinedActions()
84 return array_merge(self::$defined_actions, parent::getDefinedActions());
88 * BodyActions::parse_plugin()
\r
89 * Parse a plugin var
\r
90 * Called if <%plugin(...)%> in an item appears
\r
92 * Calls the doItemVar function in the plugin
\r
94 public function parse_plugin($pluginName)
\r
98 $plugin =& $manager->getPlugin('NP_' . $pluginName);
\r
105 $params = func_get_args();
\r
107 // remove plugin name
\r
108 array_shift($params);
\r
110 // add item reference (array_unshift didn't work)
\r
111 $params = array_merge(array(&$this->currentItem),$params);
\r
113 call_user_func_array(array(&$plugin,'doItemVar'), $params);
\r
118 * BodyActions::parse_image()
\r
120 * Called if <%image(...)%> in an item appears
\r
123 * @return parsed image tag
\r
125 public function parse_image()
\r
127 // image/popup calls have arguments separated by |
\r
128 $args = func_get_args();
\r
129 $args = preg_split('#\|#',implode($args,', '));
\r
130 echo call_user_func_array(array(&$this,'createImageCode'),$args);
\r
134 * BodyActions::createImageCode()
\r
135 * Creates the code for an image
\r
137 * @param string $filename name of file from tag
\r
138 * @param integer $width width of file from tag
\r
139 * @param integer $height height of file from tag
\r
140 * @return string image element with anchor element
\r
142 public function createImageCode($filename, $width, $height, $text = '')
\r
146 // select private collection when no collection given
\r
147 if ( !strstr($filename,'/') )
\r
149 $filename = $this->currentItem->authorid . '/' . $filename;
\r
152 $windowwidth = $width;
\r
153 $windowheight = $height;
\r
155 $vars['link'] = Entity::hsc($CONF['MediaURL']. $filename);
\r
156 $vars['text'] = Entity::hsc($text);
\r
157 $vars['image'] = '<img src="' . $vars['link'] . '" width="' . $width . '" height="' . $height . '" alt="' . $vars['text'] . '" title="' . $vars['text'] . '" />';
\r
158 $vars['width'] = $width;
\r
159 $vars['height'] = $height;
\r
160 $vars['media'] = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';
\r
162 return Template::fill($this->template['IMAGE_CODE'], $vars);
166 * BodyActions::parse_media()
\r
168 * Called if <%media(...)%> in an item appears
\r
171 * @param parsed media tag
\r
173 public function parse_media()
\r
175 // image/popup calls have arguments separated by |
\r
176 $args = func_get_args();
\r
177 $args = preg_split('#\|#', implode($args,', '));
\r
178 echo call_user_func_array(array(&$this,'createMediaCode'), $args);
\r
182 * BodyActions::createMediaCode()
\r
183 * Creates the code for a media
\r
185 * @param string $filename name of file from tag
\r
186 * @param string $text alternative text from tag
\r
187 * @return string text element with anchor element
\r
189 public function createMediaCode($filename, $text = '')
\r
193 // select private collection when no collection given
\r
194 if ( !strstr($filename,'/') )
\r
196 $filename = $this->currentItem->authorid . '/' . $filename;
\r
199 $vars['link'] = Entity::hsc($CONF['MediaURL'] . $filename);
\r
200 $vars['text'] = Entity::hsc($text);
\r
201 $vars['media'] = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';
\r
203 return Template::fill($this->template['MEDIA_CODE'], $vars);;
\r
207 * BodyActions::parse_popup()
\r
209 * Called if <%popup(...)%> in an item appears
\r
212 * @return string parsed popup tag
\r
214 public function parse_popup()
\r
216 // image/popup calls have arguments separated by |
\r
217 $args = func_get_args();
\r
218 $args = preg_split('#\|#', implode($args,', '));
\r
219 echo call_user_func_array(array(&$this,'createPopupCode'), $args);
\r
223 * BodyActions::createPopupCode()
\r
224 * Creates the code for a popup
\r
226 * @param string $filename name of file from tag
\r
227 * @param integer $width width of file from tag
\r
228 * @param integer $height height of file from tag
\r
229 * @param string $text alternative text from tag
\r
230 * @return string text element with anchor element of JavaScript window.open
\r
232 public function createPopupCode($filename, $width, $height, $text = '')
\r
236 // select private collection when no collection given
\r
237 if ( !strstr($filename,'/') )
\r
239 $filename = $this->currentItem->authorid . '/' . $filename;
\r
242 $windowwidth = $width;
\r
243 $windowheight = $height;
\r
245 $vars['rawpopuplink'] = $CONF['Self'] . "?imagepopup=" . Entity::hsc($filename) . "&width=$width&height=$height&imagetext=" . urlencode(Entity::hsc($text));
\r
246 $vars['popupcode'] = "window.open(this.href,'imagepopup','status=no,toolbar=no,scrollbars=no,resizable=yes,width=$windowwidth,height=$windowheight');return false;";
\r
247 $vars['popuptext'] = Entity::hsc($text);
\r
248 $vars['popuplink'] = '<a href="' . $vars['rawpopuplink']. '" onclick="'. $vars['popupcode'].'" >' . $vars['popuptext'] . '</a>';
\r
249 $vars['width'] = $width;
\r
250 $vars['height'] = $height;
\r
251 $vars['text'] = $text;
\r
252 $vars['link'] = Entity::hsc($CONF['MediaURL'] . $filename);
\r
253 $vars['media'] = '<a href="' . $vars['link'] . '">' . $vars['popuptext'] . '</a>';
\r
255 return Template::fill($this->template['POPUP_CODE'], $vars);
\r
259 * BodyActions::checkCondition()
\r
260 * Checks conditions for if statements
\r
262 * @param string $field type of <%if%>
\r
263 * @param string $name property of field
\r
264 * @param string $value value of property
\r
265 * @return condition
\r
267 protected function checkCondition($field, $name='', $value = '')
\r
269 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
\r
275 $condition = ($blog && $this->ifCategory($name,$value));
\r
277 case 'itemcategory':
\r
278 $condition = ($this->ifItemCategory($name,$value));
\r
280 case 'blogsetting':
\r
281 $condition = ($blog && ($blog->getSetting($name) == $value));
\r
283 case 'itemblogsetting':
\r
284 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
\r
285 $condition = ($b && ($b->getSetting($name) == $value));
\r
288 $condition = $member->isLoggedIn();
\r
291 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
\r
294 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
\r
297 $condition = ($this->ifAuthor($name,$value));
\r
300 $condition = $this->ifHasPlugin($name, $value);
\r
303 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
\r
310 * BodyActions::ifCategory()
\r
311 * Different checks for a category
\r
313 * @param string $key key for data of category
\r
314 * @param string $value value for data of category
\r
317 private function ifCategory($key = '', $value = '')
\r
319 global $blog, $catid;
\r
321 // when no parameter is defined, just check if a category is selected
\r
322 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
\r
324 return $blog->isValidCategory($catid);
\r
327 // check category name
\r
328 if ( $key == 'catname' )
\r
330 $value = $blog->getCategoryIdFromName($value);
\r
331 if ( $value == $catid )
\r
333 return $blog->isValidCategory($catid);
\r
337 // check category id
\r
338 if ( ($key == 'catid') && ($value == $catid) )
\r
340 return $blog->isValidCategory($catid);
\r
347 * BodyActions::ifAuthor()
\r
348 * Different checks for an author
\r
350 * @param string $key key for data of author
\r
351 * @param string $value value for data of author
\r
354 private function ifAuthor($key = '', $value = '')
\r
356 global $member, $manager;
\r
358 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
\r
360 // when no parameter is defined, just check if author is current visitor
\r
361 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
\r
363 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));
\r
366 // check author name
\r
367 if ( $key == 'name' )
\r
369 $value = strtolower($value);
\r
370 if ( $value == strtolower($this->currentItem->author) )
\r
376 // check if author is admin
\r
377 if ( ($key == 'isadmin') )
\r
379 $aid = intval($this->currentItem->authorid);
\r
380 $blogid = intval($b->getID());
\r
381 $amember =& $manager->getMember($aid);
\r
382 if ( $amember->isAdmin() )
\r
386 return $amember->isBlogAdmin($blogid);
\r
393 * BodyActions::ifItemCategory()
\r
394 * Different checks for a category
\r
396 * @param string $key key for data of category
\r
397 * @param string $value value for data of category
\r
400 private function ifItemCategory($key = '', $value = '')
\r
402 global $catid, $manager;
\r
404 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
\r
406 // when no parameter is defined, just check if a category is selected
\r
407 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
\r
409 return $b->isValidCategory($catid);
\r
412 $icatid = $this->currentItem->catid;
\r
414 // check category name
\r
415 if ( $key == 'catname' )
\r
417 $value = $b->getCategoryIdFromName($value);
\r
418 if ( $value == $icatid )
\r
420 return $b->isValidCategory($icatid);
\r
424 // check category id
\r
425 if ( ($key == 'catid') && ($value == $icatid) )
\r
427 return $b->isValidCategory($icatid);
\r
433 * BodyActions::ifOnTeam()
\r
434 * Checks if a member is on the team of a blog and return his rights
\r
436 * @param string $blogName name of weblog
\r
439 private function ifOnTeam($blogName = '')
\r
441 global $blog, $member, $manager;
\r
443 // when no blog found
\r
444 if ( ($blogName == '') && (!is_object($blog)) )
\r
449 // explicit blog selection
\r
450 if ( $blogName != '' )
\r
452 $blogid = getBlogIDFromName($blogName);
\r
455 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
\r
457 // use current blog
\r
458 $blogid = $blog->getID();
\r
460 return $member->teamRights($blogid);
\r
464 * BodyActions::ifAdmin()
\r
465 * Checks if a member is admin of a blog
\r
467 * @param string $blogName name of weblog
\r
470 private function ifAdmin($blogName = '')
\r
472 global $blog, $member, $manager;
\r
474 // when no blog found
\r
475 if ( ($blogName == '') && (!is_object($blog)) )
\r
480 // explicit blog selection
\r
481 if ( $blogName != '' )
\r
483 $blogid = getBlogIDFromName($blogName);
\r
486 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
\r
488 // use current blog
\r
489 $blogid = $blog->getID();
\r
491 return $member->isBlogAdmin($blogid);
\r
496 * BodyActions::ifHasPlugin()
\r
497 * hasplugin,PlugName
\r
498 * -> checks if plugin exists
\r
499 * hasplugin,PlugName,OptionName
\r
500 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
\r
501 * hasplugin,PlugName,OptionName=value
\r
502 * -> checks if the option OptionName from plugin PlugName is set to value
\r
504 * @param string $name name of plugin
\r
505 * @param string $value value for plugin argument
\r
508 private function ifHasPlugin($name, $value)
\r
511 $condition = false;
\r
513 // (pluginInstalled method won't write a message in the actionlog on failure)
\r
514 if ( $manager->pluginInstalled("NP_{$name}") )
\r
516 $plugin =& $manager->getPlugin("NP_{$name}");
\r
517 if ( $plugin != NULL )
\r
519 if ( $value == "" )
\r
525 list($name2, $value2) = preg_split('#=#', $value, 2);
\r
526 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
\r
530 else if ( $plugin->getOption($name2) == $value2 )
\r
541 * BodyActions::ifPlugin()
\r
542 * Checks if a plugin exists and call its doIf function
\r
544 * @param string $name name of plugin
\r
545 * @param string $key ...
\r
546 * @param string $value ...
\r
547 * @return string result of plugin 'doIf'
\r
549 private function ifPlugin($name, $key = '', $value = '')
\r
553 $plugin =& $manager->getPlugin("NP_{$name}");
\r
559 $params = func_get_args();
\r
560 array_shift($params);
\r
562 return call_user_func_array(array(&$plugin, 'doIf'), $params);
\r