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::setParser()
106 * @param object $parser an instance of Parser class
109 public function setParser(&$parser)
111 $this->parser =& $parser;
116 * BaseActions::parse_include()
117 * include file (no parsing of php)
119 * ToDo: function returns nothing and refering to the cross reference it
120 * isn't called from anywhere
122 * @param string $filename filename to be included
125 public function parse_include($filename)
127 @readfile($this->getIncludeFileName($filename));
132 * BaseActions::parse_phpinclude()
135 * @param string $filename filename to be included
138 public function parse_phpinclude($filename)
140 includephp($this->getIncludeFileName($filename));
146 * BaseActions::parse_parsedinclude()
149 * @param string $filename filename to be included
152 public function parse_parsedinclude($filename)
154 // check current level
155 if ( $this->level > 3 )
157 // max. depth reached (avoid endless loop)
161 $file = $this->getIncludeFileName($filename);
163 if ( !file_exists($file) )
168 $contents = file_get_contents($file);
170 if ( empty($contents) )
175 $this->level = $this->level + 1;
176 // parse file contents
177 $this->parser->parse($contents);
179 $this->level = $this->level - 1;
184 * BaseActions::getIncludeFileName()
185 * Returns the correct location of the file to be included, according to
188 * IF IncludeMode = 'skindir' => use skindir
190 * @param string $filename name of file to be inclluded
191 * @return string name of file with relative path
193 private function getIncludeFileName($filename)
195 // leave absolute filenames and http urls as they are
197 (i18n::substr($filename,0,1) == '/')
198 || (i18n::substr($filename,0,7) == 'http://')
199 || (i18n::substr($filename,0,6) == 'ftp://')
205 $filename = Parser::getProperty('IncludePrefix') . $filename;
207 if ( Parser::getProperty('IncludeMode') == 'skindir' )
210 return $DIR_SKINS . $filename;
216 * BaseActions::parse_skinfile()
217 * Inserts an url relative to the skindir (useful when doing import/export)
219 * e.g. <skinfile(default/myfile.sth)>
221 * @param string $filename name of file to be inclluded
224 public function parse_skinfile($filename)
227 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;
232 * BaseActions::parse_set()
233 * Sets a property for the parser
235 * @param string $property name of property
236 * @param string $value value of property
239 public function parse_set($property, $value)
241 Parser::setProperty($property, $value);
246 * BaseActions::parse_text()
249 * @param string $constant named constant
252 public function parse_text($constant)
254 if ( !defined($constant) )
256 echo Entity::hsc($constant);
260 echo Entity::hsc(constant($constant));
266 * BaseActions::addIfCondition()
267 * Helper function: add if condition
269 * @param string $condition condition for if context
272 private function addIfCondition($condition)
274 array_push($this->if_conditions,$condition);
275 $this->updateTopIfCondition();
281 * BaseActions::updateTopIfCondition()
282 * Helper function: update the Top of the If Conditions Array
287 private function updateTopIfCondition()
289 if ( sizeof($this->if_conditions) == 0 )
291 $this->if_currentlevel = 1;
295 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
301 * BaseActions::addIfExecute()
302 * Helper function for elseif / elseifnot
307 private function addIfExecute()
309 array_push($this->if_execute, 0);
314 * BaseActions::updateIfExecute()
315 * Helper function for elseif / elseifnot
317 * @param string $condition condition to be fullfilled
320 private function updateIfExecute($condition)
322 $index = sizeof($this->if_execute) - 1;
323 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
328 * BaseActions::getTopIfCondition()()
329 * returns the currently top if condition
332 * @return string level
334 public function getTopIfCondition()
336 return $this->if_currentlevel;
340 * BaseActions::setHighlight(()
341 * Sets the search terms to be highlighted
343 * @param string $highlight A series of search terms
346 public function setHighlight($highlight)
348 $this->strHighlight = $highlight;
351 $this->aHighlight = parseHighlight($highlight);
357 * BaseActions::highlight()
358 * Applies the highlight to the given piece of text
361 * @param string $data Data that needs to be highlighted
362 * @return string hilighted data
364 public function highlight($data)
366 if ( $this->aHighlight )
368 $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
374 * BaseActions::parse_if()
375 * Parses <%if%> statements
380 public function parse_if()
382 $this->addIfExecute();
383 $args = func_get_args();
384 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
385 $this->addIfCondition($condition);
390 * BaseActions::parse_else()
391 * Parses <%else%> statements
396 public function parse_else()
398 if ( sizeof($this->if_conditions) == 0 )
403 array_pop($this->if_conditions);
405 if ( $this->if_currentlevel )
408 $this->updateIfExecute(1);
409 $this->addIfCondition(0);
411 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
414 $this->addIfCondition(0);
419 $this->addIfCondition(1);
425 * BaseActions::parse_elseif()
426 * Parses <%elseif%> statements
431 public function parse_elseif()
433 if ( sizeof($this->if_conditions) == 0 )
438 array_pop($this->if_conditions);
440 if ( $this->if_currentlevel )
443 $this->updateIfExecute(1);
444 $this->addIfCondition(0);
446 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
449 $this->addIfCondition(0);
454 $args = func_get_args();
455 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
456 $this->addIfCondition($condition);
462 * BaseActions::parse_ifnot()
463 * Parses <%ifnot%> statements
468 public function parse_ifnot()
470 $this->addIfExecute();
472 $args = func_get_args();
473 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
474 $this->addIfCondition(!$condition);
479 * BaseActions::parse_elseifnot()
480 * Parses <%elseifnot%> statements
485 public function parse_elseifnot()
487 if ( sizeof($this->if_conditions) == 0 )
492 array_pop($this->if_conditions);
494 if ( $this->if_currentlevel )
497 $this->updateIfExecute(1);
498 $this->addIfCondition(0);
500 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
503 $this->addIfCondition(0);
508 $args = func_get_args();
509 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
510 $this->addIfCondition(!$condition);
516 * BaseActions::parse_endif()
517 * Ends a conditional if-block
518 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
523 public function parse_endif()
525 // we can only close what has been opened
526 if ( sizeof($this->if_conditions) == 0 )
531 if ( $this->if_currentlevel )
540 array_pop($this->if_conditions);
541 array_pop($this->if_execute);
543 $this->updateTopIfCondition();