3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2009 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-2009 The Nucleus Group
20 * @version $Id: BaseActions.php 1757 2012-04-15 09:02:32Z sakamocchi $
26 * BaseActions::$parser
27 * Skin class calls parser with action classes succeeded to this class
32 // depth level for includes (max. level is 3)
35 // array of evaluated conditions (true/false). The element at the end is the one for the most nested
37 private $if_conditions;
39 // in the "elseif" / "elseifnot" sequences, if one of the conditions become "true" remained conditions should not
40 // be tested. this variable (actually a stack) holds this information.
43 // at all times, can be evaluated to either true if the current block needs to be displayed. This
44 // variable is used to decide to skip skinvars in parts that will never be outputted.
45 private $if_currentlevel;
48 // contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight
49 protected $strHighlight;
51 // array of keywords that need to be highlighted in search results (see the highlight()
52 // and parseHighlight() methods)
56 * BaseActions::BaseActions()
57 * Constructor for a new BaseAction object
59 protected function initialize()
64 $this->if_conditions = array(); // array on which condition values are pushed/popped
65 $this->if_execute = array(); // array on which condition values are pushed/popped
66 $this->if_currentlevel = 1; // 1 = current level is displayed; 0 = current level not displayed
69 $this->strHighlight = ''; // full highlight
70 $this->aHighlight = array(); // parsed highlight
75 * BaseActions::parse_include()
76 * include file (no parsing of php)
78 * ToDo: function returns nothing and refering to the cross reference it
79 * isn't called from anywhere
81 * @param string $filename filename to be included
84 public function parse_include($filename)
86 @readfile($this->getIncludeFileName($filename));
91 * BaseActions::parse_phpinclude()
94 * @param string $filename filename to be included
97 public function parse_phpinclude($filename)
99 includephp($this->getIncludeFileName($filename));
105 * BaseActions::parse_parsedinclude()
108 * @param string $filename filename to be included
111 public function parse_parsedinclude($filename)
113 // check current level
114 if ( $this->level > 3 )
116 // max. depth reached (avoid endless loop)
120 $file = $this->getIncludeFileName($filename);
122 if ( !file_exists($file) )
127 $contents = file_get_contents($file);
129 if ( empty($contents) )
134 $this->level = $this->level + 1;
135 // parse file contents
136 $this->parser->parse($contents);
138 $this->level = $this->level - 1;
143 * BaseActions::getIncludeFileName()
144 * Returns the correct location of the file to be included, according to
147 * IF IncludeMode = 'skindir' => use skindir
149 * @param string $filename name of file to be inclluded
150 * @return string name of file with relative path
152 private function getIncludeFileName($filename)
154 // leave absolute filenames and http urls as they are
156 (i18n::substr($filename,0,1) == '/')
157 || (i18n::substr($filename,0,7) == 'http://')
158 || (i18n::substr($filename,0,6) == 'ftp://')
164 $filename = Parser::getProperty('IncludePrefix') . $filename;
166 if ( Parser::getProperty('IncludeMode') == 'skindir' )
169 return $DIR_SKINS . $filename;
175 * BaseActions::parse_skinfile()
176 * Inserts an url relative to the skindir (useful when doing import/export)
178 * e.g. <skinfile(default/myfile.sth)>
180 * @param string $filename name of file to be inclluded
183 public function parse_skinfile($filename)
186 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;
191 * BaseActions::parse_set()
192 * Sets a property for the parser
194 * @param string $property name of property
195 * @param string $value value of property
198 public function parse_set($property, $value)
200 Parser::setProperty($property, $value);
205 * BaseActions::addIfCondition()
206 * Helper function: add if condition
208 * @param string $condition condition for if context
211 protected function addIfCondition($condition)
213 array_push($this->if_conditions,$condition);
214 $this->updateTopIfCondition();
220 * BaseActions::updateTopIfCondition()
221 * Helper function: update the Top of the If Conditions Array
226 private function updateTopIfCondition()
228 if ( sizeof($this->if_conditions) == 0 )
230 $this->if_currentlevel = 1;
234 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
240 * BaseActions::addIfExecute()
241 * Helper function for elseif / elseifnot
246 private function addIfExecute()
248 array_push($this->if_execute, 0);
253 * BaseActions::updateIfExecute()
254 * Helper function for elseif / elseifnot
256 * @param string $condition condition to be fullfilled
259 private function updateIfExecute($condition)
261 $index = sizeof($this->if_execute) - 1;
262 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
267 * BaseActions::getTopIfCondition()()
268 * returns the currently top if condition
271 * @return string level
273 public function getTopIfCondition()
275 return $this->if_currentlevel;
279 * BaseActions::setHighlight(()
280 * Sets the search terms to be highlighted
282 * @param string $highlight A series of search terms
285 public function setHighlight($highlight)
287 $this->strHighlight = $highlight;
290 $this->aHighlight = parseHighlight($highlight);
296 * BaseActions::highlight()
297 * Applies the highlight to the given piece of text
300 * @param string $data Data that needs to be highlighted
301 * @return string hilighted data
303 public function highlight($data)
305 if ( $this->aHighlight )
307 $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
313 * BaseActions::parse_if()
314 * Parses <%if%> statements
319 public function parse_if()
321 $this->addIfExecute();
322 $args = func_get_args();
323 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
324 $this->addIfCondition($condition);
329 * BaseActions::parse_else()
330 * Parses <%else%> statements
335 public function parse_else()
337 if ( sizeof($this->if_conditions) == 0 )
342 array_pop($this->if_conditions);
344 if ( $this->if_currentlevel )
347 $this->updateIfExecute(1);
348 $this->addIfCondition(0);
350 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
353 $this->addIfCondition(0);
358 $this->addIfCondition(1);
364 * BaseActions::parse_elseif()
365 * Parses <%elseif%> statements
370 public function parse_elseif()
372 if ( sizeof($this->if_conditions) == 0 )
377 array_pop($this->if_conditions);
379 if ( $this->if_currentlevel )
382 $this->updateIfExecute(1);
383 $this->addIfCondition(0);
385 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
388 $this->addIfCondition(0);
393 $args = func_get_args();
394 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
395 $this->addIfCondition($condition);
401 * BaseActions::parse_ifnot()
402 * Parses <%ifnot%> statements
407 public function parse_ifnot()
409 $this->addIfExecute();
411 $args = func_get_args();
412 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
413 $this->addIfCondition(!$condition);
418 * BaseActions::parse_elseifnot()
419 * Parses <%elseifnot%> statements
424 public function parse_elseifnot()
426 if ( sizeof($this->if_conditions) == 0 )
431 array_pop($this->if_conditions);
433 if ( $this->if_currentlevel )
436 $this->updateIfExecute(1);
437 $this->addIfCondition(0);
439 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
442 $this->addIfCondition(0);
447 $args = func_get_args();
448 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
449 $this->addIfCondition(!$condition);
455 * BaseActions::parse_endif()
456 * Ends a conditional if-block
457 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
462 public function parse_endif()
464 // we can only close what has been opened
465 if ( sizeof($this->if_conditions) == 0 )
470 if ( $this->if_currentlevel )
479 array_pop($this->if_conditions);
480 array_pop($this->if_execute);
482 $this->updateTopIfCondition();