3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2012 The Nucleus Group
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * (see nucleus/documentation/index.html#license for more info)
13 * This class contains parse actions that are available in all ACTION classes
14 * e.g. include, phpinclude, parsedinclude, skinfile, ...
16 * It should never be used on it's own
18 * @license http://nucleuscms.org/license.txt GNU General Public License
19 * @copyright Copyright (C) 2002-2012 The Nucleus Group
20 * @version $Id: BaseActions.php 1729 2012-04-07 05:07:08Z sakamocchi $
25 // depth level for includes (max. level is 3)
28 // array of evaluated conditions (true/false). The element at the end is the one for the most nested
30 public $if_conditions;
32 // in the "elseif" / "elseifnot" sequences, if one of the conditions become "true" remained conditions should not
33 // be tested. this variable (actually a stack) holds this information.
36 // at all times, can be evaluated to either true if the current block needs to be displayed. This
37 // variable is used to decide to skip skinvars in parts that will never be outputted.
38 public $if_currentlevel;
40 // contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight
43 // array of keywords that need to be highlighted in search results (see the highlight()
44 // and parseHighlight() methods)
47 // reference to the parser object that is using this object as actions-handler
51 * BaseActions::BaseActions()
52 * Constructor for a new BaseAction object
54 protected function BaseActions()
59 $this->if_conditions = array(); // array on which condition values are pushed/popped
60 $this->if_execute = array(); // array on which condition values are pushed/popped
61 $this->if_currentlevel = 1; // 1 = current level is displayed; 0 = current level not displayed
64 $this->strHighlight = ''; // full highlight
65 $this->aHighlight = array(); // parsed highlight
70 * BaseActions::parse_include()
71 * include file (no parsing of php)
73 * ToDo: function returns nothing and refering to the cross reference it
74 * isn't called from anywhere
76 * @param string $filename filename to be included
79 public function parse_include($filename)
81 @readfile($this->getIncludeFileName($filename));
86 * BaseActions::parse_phpinclude()
89 * @param string $filename filename to be included
92 public function parse_phpinclude($filename)
94 includephp($this->getIncludeFileName($filename));
100 * BaseActions::parse_parsedinclude()
103 * @param string $filename filename to be included
106 public function parse_parsedinclude($filename)
108 // check current level
109 if ( $this->level > 3 )
111 // max. depth reached (avoid endless loop)
115 $file = $this->getIncludeFileName($filename);
117 if ( !file_exists($file) )
122 $contents = file_get_contents($file);
124 if ( empty($contents) )
129 $this->level = $this->level + 1;
130 // parse file contents
131 $this->parser->parse($contents);
133 $this->level = $this->level - 1;
138 * BaseActions::getIncludeFileName()
139 * Returns the correct location of the file to be included, according to
142 * IF IncludeMode = 'skindir' => use skindir
144 * @param string $filename name of file to be inclluded
145 * @return string name of file with relative path
147 public function getIncludeFileName($filename)
149 // leave absolute filenames and http urls as they are
151 (i18n::substr($filename,0,1) == '/')
152 || (i18n::substr($filename,0,7) == 'http://')
153 || (i18n::substr($filename,0,6) == 'ftp://')
159 $filename = Parser::getProperty('IncludePrefix') . $filename;
161 if ( Parser::getProperty('IncludeMode') == 'skindir' )
164 return $DIR_SKINS . $filename;
170 * BaseActions::parse_skinfile()
171 * Inserts an url relative to the skindir (useful when doing import/export)
173 * e.g. <skinfile(default/myfile.sth)>
175 * @param string $filename name of file to be inclluded
178 public function parse_skinfile($filename)
181 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;
186 * BaseActions::parse_set()
187 * Sets a property for the parser
189 * @param string $property name of property
190 * @param string $value value of property
193 public function parse_set($property, $value)
195 Parser::setProperty($property, $value);
200 * BaseActions::_addIfCondition()
201 * Helper function: add if condition
203 * @param string $condition condition for if context
206 protected function _addIfCondition($condition)
208 array_push($this->if_conditions,$condition);
209 $this->_updateTopIfCondition();
215 * BaseActions::_updateTopIfCondition()
216 * Helper function: update the Top of the If Conditions Array
221 protected function _updateTopIfCondition()
223 if ( sizeof($this->if_conditions) == 0 )
225 $this->if_currentlevel = 1;
229 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
235 * BaseActions::_addIfExecute()
236 * Helper function for elseif / elseifnot
241 protected function _addIfExecute()
243 array_push($this->if_execute, 0);
248 * BaseActions::_updateIfExecute()
249 * Helper function for elseif / elseifnot
251 * @param string $condition condition to be fullfilled
254 protected function _updateIfExecute($condition)
256 $index = sizeof($this->if_execute) - 1;
257 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
262 * BaseActions::_getTopIfCondition()()
263 * returns the currently top if condition
266 * @return string level
268 protected function _getTopIfCondition()
270 return $this->if_currentlevel;
274 * BaseActions::setHighlight(()
275 * Sets the search terms to be highlighted
277 * @param string $highlight A series of search terms
280 public function setHighlight($highlight)
282 $this->strHighlight = $highlight;
285 $this->aHighlight = parseHighlight($highlight);
291 * BaseActions::highlight()
292 * Applies the highlight to the given piece of text
295 * @param string $data Data that needs to be highlighted
296 * @return string hilighted data
298 public function highlight($data)
300 if ( $this->aHighlight )
302 $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
308 * BaseActions::parse_if()
309 * Parses <%if%> statements
314 public function parse_if()
316 $this->_addIfExecute();
317 $args = func_get_args();
318 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
319 $this->_addIfCondition($condition);
324 * BaseActions::parse_else()
325 * Parses <%else%> statements
330 public function parse_else()
332 if ( sizeof($this->if_conditions) == 0 )
337 array_pop($this->if_conditions);
339 if ( $this->if_currentlevel )
342 $this->_updateIfExecute(1);
343 $this->_addIfCondition(0);
345 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
348 $this->_addIfCondition(0);
353 $this->_addIfCondition(1);
359 * BaseActions::parse_elseif()
360 * Parses <%elseif%> statements
365 public function parse_elseif()
367 if ( sizeof($this->if_conditions) == 0 )
372 array_pop($this->if_conditions);
374 if ( $this->if_currentlevel )
377 $this->_updateIfExecute(1);
378 $this->_addIfCondition(0);
380 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
383 $this->_addIfCondition(0);
388 $args = func_get_args();
389 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
390 $this->_addIfCondition($condition);
396 * BaseActions::parse_ifnot()
397 * Parses <%ifnot%> statements
402 public function parse_ifnot()
404 $this->_addIfExecute();
406 $args = func_get_args();
407 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
408 $this->_addIfCondition(!$condition);
413 * BaseActions::parse_elseifnot()
414 * Parses <%elseifnot%> statements
419 public function parse_elseifnot()
421 if ( sizeof($this->if_conditions) == 0 )
426 array_pop($this->if_conditions);
428 if ( $this->if_currentlevel )
431 $this->_updateIfExecute(1);
432 $this->_addIfCondition(0);
434 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
437 $this->_addIfCondition(0);
442 $args = func_get_args();
443 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
444 $this->_addIfCondition(!$condition);
450 * BaseActions::parse_endif()
451 * Ends a conditional if-block
452 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
457 public function parse_endif()
459 // we can only close what has been opened
460 if ( sizeof($this->if_conditions) == 0 )
465 if ( $this->if_currentlevel )
474 array_pop($this->if_conditions);
475 array_pop($this->if_execute);
477 $this->_updateTopIfCondition();