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::getDefinedActions()
97 static public function getDefinedActions()
99 return self::$defined_actions;
103 * BaseActions::parse_include()
104 * include file (no parsing of php)
106 * ToDo: function returns nothing and refering to the cross reference it
107 * isn't called from anywhere
109 * @param string $filename filename to be included
112 public function parse_include($filename)
114 @readfile($this->getIncludeFileName($filename));
119 * BaseActions::parse_phpinclude()
122 * @param string $filename filename to be included
125 public function parse_phpinclude($filename)
127 includephp($this->getIncludeFileName($filename));
133 * BaseActions::parse_parsedinclude()
136 * @param string $filename filename to be included
139 public function parse_parsedinclude($filename)
141 // check current level
142 if ( $this->level > 3 )
144 // max. depth reached (avoid endless loop)
148 $file = $this->getIncludeFileName($filename);
150 if ( !file_exists($file) )
155 $contents = file_get_contents($file);
157 if ( empty($contents) )
162 $this->level = $this->level + 1;
163 // parse file contents
164 $this->parser->parse($contents);
166 $this->level = $this->level - 1;
171 * BaseActions::getIncludeFileName()
172 * Returns the correct location of the file to be included, according to
175 * IF IncludeMode = 'skindir' => use skindir
177 * @param string $filename name of file to be inclluded
178 * @return string name of file with relative path
180 private function getIncludeFileName($filename)
182 // leave absolute filenames and http urls as they are
184 (i18n::substr($filename,0,1) == '/')
185 || (i18n::substr($filename,0,7) == 'http://')
186 || (i18n::substr($filename,0,6) == 'ftp://')
192 $filename = Parser::getProperty('IncludePrefix') . $filename;
194 if ( Parser::getProperty('IncludeMode') == 'skindir' )
197 return $DIR_SKINS . $filename;
203 * BaseActions::parse_skinfile()
204 * Inserts an url relative to the skindir (useful when doing import/export)
206 * e.g. <skinfile(default/myfile.sth)>
208 * @param string $filename name of file to be inclluded
211 public function parse_skinfile($filename)
214 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;
219 * BaseActions::parse_set()
220 * Sets a property for the parser
222 * @param string $property name of property
223 * @param string $value value of property
226 public function parse_set($property, $value)
228 Parser::setProperty($property, $value);
233 * Actions::parse_text()
236 * @param string $constant named constant
239 public function parse_text($constant)
241 if ( !defined($constant) )
243 echo Entity::hsc($constant);
247 echo Entity::hsc(constant($constant));
253 * BaseActions::addIfCondition()
254 * Helper function: add if condition
256 * @param string $condition condition for if context
259 private function addIfCondition($condition)
261 array_push($this->if_conditions,$condition);
262 $this->updateTopIfCondition();
268 * BaseActions::updateTopIfCondition()
269 * Helper function: update the Top of the If Conditions Array
274 private function updateTopIfCondition()
276 if ( sizeof($this->if_conditions) == 0 )
278 $this->if_currentlevel = 1;
282 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
288 * BaseActions::addIfExecute()
289 * Helper function for elseif / elseifnot
294 private function addIfExecute()
296 array_push($this->if_execute, 0);
301 * BaseActions::updateIfExecute()
302 * Helper function for elseif / elseifnot
304 * @param string $condition condition to be fullfilled
307 private function updateIfExecute($condition)
309 $index = sizeof($this->if_execute) - 1;
310 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
315 * BaseActions::getTopIfCondition()()
316 * returns the currently top if condition
319 * @return string level
321 public function getTopIfCondition()
323 return $this->if_currentlevel;
327 * BaseActions::setHighlight(()
328 * Sets the search terms to be highlighted
330 * @param string $highlight A series of search terms
333 public function setHighlight($highlight)
335 $this->strHighlight = $highlight;
338 $this->aHighlight = parseHighlight($highlight);
344 * BaseActions::highlight()
345 * Applies the highlight to the given piece of text
348 * @param string $data Data that needs to be highlighted
349 * @return string hilighted data
351 public function highlight($data)
353 if ( $this->aHighlight )
355 $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
361 * BaseActions::parse_if()
362 * Parses <%if%> statements
367 public function parse_if()
369 $this->addIfExecute();
370 $args = func_get_args();
371 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
372 $this->addIfCondition($condition);
377 * BaseActions::parse_else()
378 * Parses <%else%> statements
383 public function parse_else()
385 if ( sizeof($this->if_conditions) == 0 )
390 array_pop($this->if_conditions);
392 if ( $this->if_currentlevel )
395 $this->updateIfExecute(1);
396 $this->addIfCondition(0);
398 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
401 $this->addIfCondition(0);
406 $this->addIfCondition(1);
412 * BaseActions::parse_elseif()
413 * Parses <%elseif%> statements
418 public function parse_elseif()
420 if ( sizeof($this->if_conditions) == 0 )
425 array_pop($this->if_conditions);
427 if ( $this->if_currentlevel )
430 $this->updateIfExecute(1);
431 $this->addIfCondition(0);
433 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
436 $this->addIfCondition(0);
441 $args = func_get_args();
442 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
443 $this->addIfCondition($condition);
449 * BaseActions::parse_ifnot()
450 * Parses <%ifnot%> statements
455 public function parse_ifnot()
457 $this->addIfExecute();
459 $args = func_get_args();
460 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
461 $this->addIfCondition(!$condition);
466 * BaseActions::parse_elseifnot()
467 * Parses <%elseifnot%> statements
472 public function parse_elseifnot()
474 if ( sizeof($this->if_conditions) == 0 )
479 array_pop($this->if_conditions);
481 if ( $this->if_currentlevel )
484 $this->updateIfExecute(1);
485 $this->addIfCondition(0);
487 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
490 $this->addIfCondition(0);
495 $args = func_get_args();
496 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
497 $this->addIfCondition(!$condition);
503 * BaseActions::parse_endif()
504 * Ends a conditional if-block
505 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
510 public function parse_endif()
512 // we can only close what has been opened
513 if ( sizeof($this->if_conditions) == 0 )
518 if ( $this->if_currentlevel )
527 array_pop($this->if_conditions);
528 array_pop($this->if_execute);
530 $this->updateTopIfCondition();