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)
55 /* NOTE: defined actions for this base class */
56 static private $defined_actions = array(
72 * BaseActions::__construct()
73 * Constructor for a new BaseAction object
75 protected function __construct()
80 $this->if_conditions = array(); // array on which condition values are pushed/popped
81 $this->if_execute = array(); // array on which condition values are pushed/popped
82 $this->if_currentlevel = 1; // 1 = current level is displayed; 0 = current level not displayed
85 $this->strHighlight = ''; // full highlight
86 $this->aHighlight = array(); // parsed highlight
91 * BaseActions::getAvailableActions()
96 public function getAvailableActions()
98 return self::$defined_actions;
102 * BaseActions::setParser()
105 * @param object $parser an instance of Parser class
108 public function setParser(&$parser)
110 $this->parser =& $parser;
115 * BaseActions::parse_include()
116 * include file (no parsing of php)
118 * ToDo: function returns nothing and refering to the cross reference it
119 * isn't called from anywhere
121 * @param string $filename filename to be included
124 public function parse_include($filename)
126 @readfile($this->getIncludeFileName($filename));
131 * BaseActions::parse_phpinclude()
134 * @param string $filename filename to be included
137 public function parse_phpinclude($filename)
139 includephp($this->getIncludeFileName($filename));
145 * BaseActions::parse_parsedinclude()
148 * @param string $filename filename to be included
151 public function parse_parsedinclude($filename)
153 // check current level
154 if ( $this->level > 3 )
156 // max. depth reached (avoid endless loop)
160 $file = $this->getIncludeFileName($filename);
162 if ( !file_exists($file) )
167 $contents = file_get_contents($file);
169 if ( empty($contents) )
174 $this->level = $this->level + 1;
175 // parse file contents
176 $this->parser->parse($contents);
178 $this->level = $this->level - 1;
183 * BaseActions::getIncludeFileName()
184 * Returns the correct location of the file to be included, according to
187 * IF IncludeMode = 'skindir' => use skindir
189 * @param string $filename name of file to be inclluded
190 * @return string name of file with relative path
192 private function getIncludeFileName($filename)
194 // leave absolute filenames and http urls as they are
196 (i18n::substr($filename,0,1) == '/')
197 || (i18n::substr($filename,0,7) == 'http://')
198 || (i18n::substr($filename,0,6) == 'ftp://')
204 $filename = Parser::getProperty('IncludePrefix') . $filename;
206 if ( Parser::getProperty('IncludeMode') == 'skindir' )
209 return $DIR_SKINS . $filename;
215 * BaseActions::parse_skinfile()
216 * Inserts an url relative to the skindir (useful when doing import/export)
218 * e.g. <skinfile(default/myfile.sth)>
220 * @param string $filename name of file to be inclluded
223 public function parse_skinfile($filename)
226 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;
231 * BaseActions::parse_set()
232 * Sets a property for the parser
234 * @param string $property name of property
235 * @param string $value value of property
238 public function parse_set($property, $value)
240 Parser::setProperty($property, $value);
245 * BaseActions::parse_text()
248 * @param string $constant named constant
251 public function parse_text($constant)
253 if ( !defined($constant) )
255 echo Entity::hsc($constant);
259 echo Entity::hsc(constant($constant));
265 * BaseActions::addIfCondition()
266 * Helper function: add if condition
268 * @param string $condition condition for if context
271 private function addIfCondition($condition)
273 array_push($this->if_conditions,$condition);
274 $this->updateTopIfCondition();
280 * BaseActions::updateTopIfCondition()
281 * Helper function: update the Top of the If Conditions Array
286 private function updateTopIfCondition()
288 if ( sizeof($this->if_conditions) == 0 )
290 $this->if_currentlevel = 1;
294 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
300 * BaseActions::addIfExecute()
301 * Helper function for elseif / elseifnot
306 private function addIfExecute()
308 array_push($this->if_execute, 0);
313 * BaseActions::updateIfExecute()
314 * Helper function for elseif / elseifnot
316 * @param string $condition condition to be fullfilled
319 private function updateIfExecute($condition)
321 $index = sizeof($this->if_execute) - 1;
322 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
327 * BaseActions::getTopIfCondition()()
328 * returns the currently top if condition
331 * @return string level
333 public function getTopIfCondition()
335 return $this->if_currentlevel;
339 * BaseActions::setHighlight(()
340 * Sets the search terms to be highlighted
342 * @param string $highlight A series of search terms
345 public function setHighlight($highlight)
347 $this->strHighlight = $highlight;
350 $this->aHighlight = parseHighlight($highlight);
356 * BaseActions::highlight()
357 * Applies the highlight to the given piece of text
360 * @param string $data Data that needs to be highlighted
361 * @return string hilighted data
363 public function highlight($data)
365 if ( $this->aHighlight )
367 $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
373 * BaseActions::parse_if()
374 * Parses <%if%> statements
379 public function parse_if()
381 $this->addIfExecute();
382 $args = func_get_args();
383 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
384 $this->addIfCondition($condition);
389 * BaseActions::parse_else()
390 * Parses <%else%> statements
395 public function parse_else()
397 if ( sizeof($this->if_conditions) == 0 )
402 array_pop($this->if_conditions);
404 if ( $this->if_currentlevel )
407 $this->updateIfExecute(1);
408 $this->addIfCondition(0);
410 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
413 $this->addIfCondition(0);
418 $this->addIfCondition(1);
424 * BaseActions::parse_elseif()
425 * Parses <%elseif%> statements
430 public function parse_elseif()
432 if ( sizeof($this->if_conditions) == 0 )
437 array_pop($this->if_conditions);
439 if ( $this->if_currentlevel )
442 $this->updateIfExecute(1);
443 $this->addIfCondition(0);
445 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
448 $this->addIfCondition(0);
453 $args = func_get_args();
454 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
455 $this->addIfCondition($condition);
461 * BaseActions::parse_ifnot()
462 * Parses <%ifnot%> statements
467 public function parse_ifnot()
469 $this->addIfExecute();
471 $args = func_get_args();
472 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
473 $this->addIfCondition(!$condition);
478 * BaseActions::parse_elseifnot()
479 * Parses <%elseifnot%> statements
484 public function parse_elseifnot()
486 if ( sizeof($this->if_conditions) == 0 )
491 array_pop($this->if_conditions);
493 if ( $this->if_currentlevel )
496 $this->updateIfExecute(1);
497 $this->addIfCondition(0);
499 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
502 $this->addIfCondition(0);
507 $args = func_get_args();
508 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
509 $this->addIfCondition(!$condition);
515 * BaseActions::parse_endif()
516 * Ends a conditional if-block
517 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
522 public function parse_endif()
524 // we can only close what has been opened
525 if ( sizeof($this->if_conditions) == 0 )
530 if ( $this->if_currentlevel )
539 array_pop($this->if_conditions);
540 array_pop($this->if_execute);
542 $this->updateTopIfCondition();