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 1626 2012-01-09 15:46:54Z sakamocchi $
21 class BODYACTIONS extends BaseActions {
28 * Constructor of the BODYACTIONS
30 function BODYACTIONS () {
35 * Set the current item
38 * reference to the current item
40 function setCurrentItem(&$item) {
41 $this->currentItem =& $item;
42 global $currentitemid;
43 $currentitemid = $this->currentItem->itemid;
47 * Set the current template
52 function setTemplate($template) {
53 $this->template =& $template;
57 * Get the defined actions in an item
59 function getDefinedActions() {
60 return array('image', 'media', 'popup', 'plugin', 'if', 'else', 'endif', 'elseif', 'ifnot', 'elseifnot');
65 * Called if <%plugin(...)%> in an item appears
67 * Calls the doItemVar function in the plugin
69 function parse_plugin($pluginName) {
72 // should be already tested from the parser (PARSER.php)
73 // only continue when the plugin is really installed
74 /*if (!$manager->pluginInstalled('NP_' . $pluginName)) {
78 $plugin =& $manager->getPlugin('NP_' . $pluginName);
82 $params = func_get_args();
87 // add item reference (array_unshift didn't work)
88 $params = array_merge(array(&$this->currentItem),$params);
90 call_user_func_array(array(&$plugin,'doItemVar'), $params);
95 * Called if <%image(...)%> in an item appears
97 function parse_image() {
98 // image/popup calls have arguments separated by |
99 $args = func_get_args();
100 $args = i18n::explode('|',implode($args,', '));
101 call_user_func_array(array(&$this,'createImageCode'),$args);
105 * Creates the code for an image
107 function createImageCode($filename, $width, $height, $text = '') {
110 // select private collection when no collection given
111 if (!strstr($filename,'/')) {
112 $filename = $this->currentItem->authorid . '/' . $filename;
115 $windowwidth = $width;
116 $windowheight = $height;
118 $vars['link'] = ENTITY::hsc($CONF['MediaURL']. $filename);
119 $vars['text'] = ENTITY::hsc($text);
120 $vars['image'] = '<img src="' . $vars['link'] . '" width="' . $width . '" height="' . $height . '" alt="' . $vars['text'] . '" title="' . $vars['text'] . '" />';
121 $vars['width'] = $width;
122 $vars['height'] = $height;
123 $vars['media'] = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';
126 echo TEMPLATE::fill($this->template['IMAGE_CODE'],$vars);;
132 * Called if <%media(...)%> in an item appears
134 function parse_media() {
135 // image/popup calls have arguments separated by |
136 $args = func_get_args();
137 $args = i18n::explode('|',implode($args,', '));
138 call_user_func_array(array(&$this,'createMediaCode'),$args);
142 * Creates the code for a media
144 function createMediaCode($filename, $text = '') {
147 // select private collection when no collection given
148 if (!strstr($filename,'/')) {
149 $filename = $this->currentItem->authorid . '/' . $filename;
152 $vars['link'] = ENTITY::hsc($CONF['MediaURL'] . $filename);
153 $vars['text'] = ENTITY::hsc($text);
154 $vars['media'] = '<a href="' . $vars['link'] . '">' . $vars['text'] . '</a>';
156 echo TEMPLATE::fill($this->template['MEDIA_CODE'],$vars);;
161 * Called if <%popup(...)%> in an item appears
163 function parse_popup() {
164 // image/popup calls have arguments separated by |
165 $args = func_get_args();
166 $args = i18n::explode('|',implode($args,', '));
167 call_user_func_array(array(&$this,'createPopupCode'),$args);
171 * Creates the code for a popup
173 function createPopupCode($filename, $width, $height, $text = '') {
176 // select private collection when no collection given
177 if (!strstr($filename,'/')) {
178 $filename = $this->currentItem->authorid . '/' . $filename;
181 $windowwidth = $width;
182 $windowheight = $height;
184 $vars['rawpopuplink'] = $CONF['Self'] . "?imagepopup=" . ENTITY::hsc($filename) . "&width=$width&height=$height&imagetext=" . urlencode(ENTITY::hsc($text));
185 $vars['popupcode'] = "window.open(this.href,'imagepopup','status=no,toolbar=no,scrollbars=no,resizable=yes,width=$windowwidth,height=$windowheight');return false;";
186 $vars['popuptext'] = ENTITY::hsc($text);
187 $vars['popuplink'] = '<a href="' . $vars['rawpopuplink']. '" onclick="'. $vars['popupcode'].'" >' . $vars['popuptext'] . '</a>';
188 $vars['width'] = $width;
189 $vars['height'] = $height;
190 $vars['text'] = $text;
191 $vars['link'] = ENTITY::hsc($CONF['MediaURL'] . $filename);
192 $vars['media'] = '<a href="' . $vars['link'] . '">' . $vars['popuptext'] . '</a>';
194 echo TEMPLATE::fill($this->template['POPUP_CODE'],$vars);
197 // function to enable if-else-elseif-elseifnot-ifnot-endif to item template fields
200 * Checks conditions for if statements
202 * @param string $field type of <%if%>
203 * @param string $name property of field
204 * @param string $value value of property
206 function checkCondition($field, $name='', $value = '') {
207 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
212 $condition = ($blog && $this->_ifCategory($name,$value));
215 $condition = ($this->_ifItemCategory($name,$value));
218 $condition = ($blog && ($blog->getSetting($name) == $value));
220 case 'itemblogsetting':
221 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
222 $condition = ($b && ($b->getSetting($name) == $value));
225 $condition = $member->isLoggedIn();
228 $condition = $member->isLoggedIn() && $this->_ifOnTeam($name);
231 $condition = $member->isLoggedIn() && $this->_ifAdmin($name);
234 $condition = ($this->_ifAuthor($name,$value));
237 $condition = ($itemidnext != '');
240 $condition = ($itemidprev != '');
242 case 'archiveprevexists':
243 $condition = ($archiveprevexists == true);
245 case 'archivenextexists':
246 $condition = ($archivenextexists == true);
249 $condition = ($name == $this->skintype);
252 $condition = $this->_ifHasPlugin($name, $value);
255 $condition = $manager->pluginInstalled('NP_' . $field) && $this->_ifPlugin($field, $name, $value);
262 * Different checks for a category
264 function _ifCategory($name = '', $value='') {
265 global $blog, $catid;
267 // when no parameter is defined, just check if a category is selected
268 if (($name != 'catname' && $name != 'catid') || ($value == ''))
269 return $blog->isValidCategory($catid);
271 // check category name
272 if ($name == 'catname') {
273 $value = $blog->getCategoryIdFromName($value);
274 if ($value == $catid)
275 return $blog->isValidCategory($catid);
279 if (($name == 'catid') && ($value == $catid))
280 return $blog->isValidCategory($catid);
287 * Different checks for an author
289 function _ifAuthor($name = '', $value='') {
290 global $member, $manager;
292 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
294 // when no parameter is defined, just check if author is current visitor
295 if (($name != 'isadmin' && $name != 'name') || ($name == 'name' && $value == '')) {
296 return (intval($member->getID()) > 0 && intval($member->getID()) == intval($this->currentItem->authorid));
300 if ($name == 'name') {
301 $value = strtolower($value);
302 if ($value == strtolower($this->currentItem->author))
306 // check if author is admin
307 if (($name == 'isadmin')) {
308 $aid = intval($this->currentItem->authorid);
309 $blogid = intval($b->getID());
310 $amember =& $manager->getMember($aid);
311 if ($amember->isAdmin())
314 return $amember->isBlogAdmin($blogid);
321 * Different checks for a category
323 function _ifItemCategory($name = '', $value='') {
324 global $catid, $manager;
326 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
328 // when no parameter is defined, just check if a category is selected
329 if (($name != 'catname' && $name != 'catid') || ($value == ''))
330 return $b->isValidCategory($catid);
332 $icatid = $this->currentItem->catid;
333 //$icategory = $this->currentItem->category;
335 // check category name
336 if ($name == 'catname') {
337 $value = $b->getCategoryIdFromName($value);
338 if ($value == $icatid)
339 return $b->isValidCategory($icatid);
343 if (($name == 'catid') && ($value == $icatid))
344 return $b->isValidCategory($icatid);
351 * Checks if a member is on the team of a blog and return his rights
353 function _ifOnTeam($blogName = '') {
354 global $blog, $member, $manager;
356 // when no blog found
357 if (($blogName == '') && (!is_object($blog)))
360 // explicit blog selection
362 $blogid = getBlogIDFromName($blogName);
364 if (($blogName == '') || !$manager->existsBlogID($blogid))
366 $blogid = $blog->getID();
368 return $member->teamRights($blogid);
372 * Checks if a member is admin of a blog
374 function _ifAdmin($blogName = '') {
375 global $blog, $member, $manager;
377 // when no blog found
378 if (($blogName == '') && (!is_object($blog)))
381 // explicit blog selection
383 $blogid = getBlogIDFromName($blogName);
385 if (($blogName == '') || !$manager->existsBlogID($blogid))
387 $blogid = $blog->getID();
389 return $member->isBlogAdmin($blogid);
395 * -> checks if plugin exists
396 * hasplugin,PlugName,OptionName
397 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
398 * hasplugin,PlugName,OptionName=value
399 * -> checks if the option OptionName from plugin PlugName is set to value
401 function _ifHasPlugin($name, $value) {
404 // (pluginInstalled method won't write a message in the actionlog on failure)
405 if ($manager->pluginInstalled('NP_'.$name)) {
406 $plugin =& $manager->getPlugin('NP_' . $name);
407 if ($plugin != NULL) {
411 list($name2, $value2) = i18n::explode('=', $value, 2);
412 if ($value2 == "" && $plugin->getOption($name2) != 'no') {
414 } else if ($plugin->getOption($name2) == $value2) {
424 * Checks if a plugin exists and call its doIf function
426 function _ifPlugin($name, $key = '', $value = '') {
429 $plugin =& $manager->getPlugin('NP_' . $name);
430 if (!$plugin) return;
432 $params = func_get_args();
433 array_shift($params);
435 return call_user_func_array(array(&$plugin, 'doIf'), $params);