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(
71 * BaseActions::__construct()
72 * Constructor for a new BaseAction object
74 protected function __construct()
79 $this->if_conditions = array(); // array on which condition values are pushed/popped
80 $this->if_execute = array(); // array on which condition values are pushed/popped
81 $this->if_currentlevel = 1; // 1 = current level is displayed; 0 = current level not displayed
84 $this->strHighlight = ''; // full highlight
85 $this->aHighlight = array(); // parsed highlight
90 * BaseActions::getDefinedActions()
96 static public function getDefinedActions()
98 return self::$defined_actions;
102 * BaseActions::parse_include()
103 * include file (no parsing of php)
105 * ToDo: function returns nothing and refering to the cross reference it
106 * isn't called from anywhere
108 * @param string $filename filename to be included
111 public function parse_include($filename)
113 @readfile($this->getIncludeFileName($filename));
118 * BaseActions::parse_phpinclude()
121 * @param string $filename filename to be included
124 public function parse_phpinclude($filename)
126 includephp($this->getIncludeFileName($filename));
132 * BaseActions::parse_parsedinclude()
135 * @param string $filename filename to be included
138 public function parse_parsedinclude($filename)
140 // check current level
141 if ( $this->level > 3 )
143 // max. depth reached (avoid endless loop)
147 $file = $this->getIncludeFileName($filename);
149 if ( !file_exists($file) )
154 $contents = file_get_contents($file);
156 if ( empty($contents) )
161 $this->level = $this->level + 1;
162 // parse file contents
163 $this->parser->parse($contents);
165 $this->level = $this->level - 1;
170 * BaseActions::getIncludeFileName()
171 * Returns the correct location of the file to be included, according to
174 * IF IncludeMode = 'skindir' => use skindir
176 * @param string $filename name of file to be inclluded
177 * @return string name of file with relative path
179 private function getIncludeFileName($filename)
181 // leave absolute filenames and http urls as they are
183 (i18n::substr($filename,0,1) == '/')
184 || (i18n::substr($filename,0,7) == 'http://')
185 || (i18n::substr($filename,0,6) == 'ftp://')
191 $filename = Parser::getProperty('IncludePrefix') . $filename;
193 if ( Parser::getProperty('IncludeMode') == 'skindir' )
196 return $DIR_SKINS . $filename;
202 * BaseActions::parse_skinfile()
203 * Inserts an url relative to the skindir (useful when doing import/export)
205 * e.g. <skinfile(default/myfile.sth)>
207 * @param string $filename name of file to be inclluded
210 public function parse_skinfile($filename)
213 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;
218 * BaseActions::parse_set()
219 * Sets a property for the parser
221 * @param string $property name of property
222 * @param string $value value of property
225 public function parse_set($property, $value)
227 Parser::setProperty($property, $value);
232 * BaseActions::addIfCondition()
233 * Helper function: add if condition
235 * @param string $condition condition for if context
238 private function addIfCondition($condition)
240 array_push($this->if_conditions,$condition);
241 $this->updateTopIfCondition();
247 * BaseActions::updateTopIfCondition()
248 * Helper function: update the Top of the If Conditions Array
253 private function updateTopIfCondition()
255 if ( sizeof($this->if_conditions) == 0 )
257 $this->if_currentlevel = 1;
261 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
267 * BaseActions::addIfExecute()
268 * Helper function for elseif / elseifnot
273 private function addIfExecute()
275 array_push($this->if_execute, 0);
280 * BaseActions::updateIfExecute()
281 * Helper function for elseif / elseifnot
283 * @param string $condition condition to be fullfilled
286 private function updateIfExecute($condition)
288 $index = sizeof($this->if_execute) - 1;
289 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
294 * BaseActions::getTopIfCondition()()
295 * returns the currently top if condition
298 * @return string level
300 public function getTopIfCondition()
302 return $this->if_currentlevel;
306 * BaseActions::setHighlight(()
307 * Sets the search terms to be highlighted
309 * @param string $highlight A series of search terms
312 public function setHighlight($highlight)
314 $this->strHighlight = $highlight;
317 $this->aHighlight = parseHighlight($highlight);
323 * BaseActions::highlight()
324 * Applies the highlight to the given piece of text
327 * @param string $data Data that needs to be highlighted
328 * @return string hilighted data
330 public function highlight($data)
332 if ( $this->aHighlight )
334 $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
340 * BaseActions::parse_if()
341 * Parses <%if%> statements
346 public function parse_if()
348 $this->addIfExecute();
349 $args = func_get_args();
350 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
351 $this->addIfCondition($condition);
356 * BaseActions::parse_else()
357 * Parses <%else%> statements
362 public function parse_else()
364 if ( sizeof($this->if_conditions) == 0 )
369 array_pop($this->if_conditions);
371 if ( $this->if_currentlevel )
374 $this->updateIfExecute(1);
375 $this->addIfCondition(0);
377 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
380 $this->addIfCondition(0);
385 $this->addIfCondition(1);
391 * BaseActions::parse_elseif()
392 * Parses <%elseif%> statements
397 public function parse_elseif()
399 if ( sizeof($this->if_conditions) == 0 )
404 array_pop($this->if_conditions);
406 if ( $this->if_currentlevel )
409 $this->updateIfExecute(1);
410 $this->addIfCondition(0);
412 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
415 $this->addIfCondition(0);
420 $args = func_get_args();
421 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
422 $this->addIfCondition($condition);
428 * BaseActions::parse_ifnot()
429 * Parses <%ifnot%> statements
434 public function parse_ifnot()
436 $this->addIfExecute();
438 $args = func_get_args();
439 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
440 $this->addIfCondition(!$condition);
445 * BaseActions::parse_elseifnot()
446 * Parses <%elseifnot%> statements
451 public function parse_elseifnot()
453 if ( sizeof($this->if_conditions) == 0 )
458 array_pop($this->if_conditions);
460 if ( $this->if_currentlevel )
463 $this->updateIfExecute(1);
464 $this->addIfCondition(0);
466 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
469 $this->addIfCondition(0);
474 $args = func_get_args();
475 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
476 $this->addIfCondition(!$condition);
482 * BaseActions::parse_endif()
483 * Ends a conditional if-block
484 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
489 public function parse_endif()
491 // we can only close what has been opened
492 if ( sizeof($this->if_conditions) == 0 )
497 if ( $this->if_currentlevel )
506 array_pop($this->if_conditions);
507 array_pop($this->if_execute);
509 $this->updateTopIfCondition();