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