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(
74 * BaseActions::__construct()
75 * Constructor for a new BaseAction object
77 protected function __construct()
82 $this->if_conditions = array(); // array on which condition values are pushed/popped
83 $this->if_execute = array(); // array on which condition values are pushed/popped
84 $this->if_currentlevel = 1; // 1 = current level is displayed; 0 = current level not displayed
87 $this->strHighlight = ''; // full highlight
88 $this->aHighlight = array(); // parsed highlight
93 * BaseActions::getAvailableActions()
98 public function getAvailableActions()
100 return self::$defined_actions;
104 * BaseActions::setParser()
107 * @param object $parser an instance of Parser class
110 public function setParser(&$parser)
112 $this->parser =& $parser;
117 * BaseActions::parse_charset()
118 * Parse charset to appropriate character set name registered to IANA
123 public function parse_charset()
127 if ( i18n::get_forced_charset() !== '' )
129 echo i18n::get_forced_charset();
133 echo i18n::get_current_charset();
140 * BaseActions::parse_locale()
141 * Parse locale to language-script-region according to RFC 4646
146 public function parse_locale()
148 echo preg_replace('#_#', '-', i18n::get_current_locale());
153 * BaseActions::parse_include()
154 * include file (no parsing of php)
156 * ToDo: function returns nothing and refering to the cross reference it
157 * isn't called from anywhere
159 * @param string $filename filename to be included
162 public function parse_include($filename)
164 @readfile($this->getIncludeFileName($filename));
169 * BaseActions::parse_phpinclude()
172 * @param string $filename filename to be included
175 public function parse_phpinclude($filename)
177 includephp($this->getIncludeFileName($filename));
183 * BaseActions::parse_parsedinclude()
186 * @param string $filename filename to be included
189 public function parse_parsedinclude($filename)
191 // check current level
192 if ( $this->level > 3 )
194 // max. depth reached (avoid endless loop)
198 $file = $this->getIncludeFileName($filename);
200 if ( !file_exists($file) )
205 $contents = file_get_contents($file);
207 if ( empty($contents) )
212 $this->level = $this->level + 1;
213 // parse file contents
214 $this->parser->parse($contents);
216 $this->level = $this->level - 1;
221 * BaseActions::getIncludeFileName()
222 * Returns the correct location of the file to be included, according to
225 * IF IncludeMode = 'skindir' => use skindir
227 * @param string $filename name of file to be inclluded
228 * @return string name of file with relative path
230 private function getIncludeFileName($filename)
232 // leave absolute filenames and http urls as they are
234 (i18n::substr($filename,0,1) == '/')
235 || (i18n::substr($filename,0,7) == 'http://')
236 || (i18n::substr($filename,0,6) == 'ftp://')
242 $filename = Parser::getProperty('IncludePrefix') . $filename;
244 if ( Parser::getProperty('IncludeMode') == 'skindir' )
247 return $DIR_SKINS . $filename;
253 * BaseActions::parse_skinfile()
254 * Inserts an url relative to the skindir (useful when doing import/export)
256 * e.g. <skinfile(default/myfile.sth)>
258 * @param string $filename name of file to be inclluded
261 public function parse_skinfile($filename)
264 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;
269 * BaseActions::parse_set()
270 * Sets a property for the parser
272 * @param string $property name of property
273 * @param string $value value of property
276 public function parse_set($property, $value)
278 Parser::setProperty($property, $value);
283 * BaseActions::parse_text()
286 * @param string $constant named constant
289 public function parse_text($constant)
291 if ( !defined($constant) )
293 echo Entity::hsc($constant);
297 echo Entity::hsc(constant($constant));
303 * BaseActions::addIfCondition()
304 * Helper function: add if condition
306 * @param string $condition condition for if context
309 private function addIfCondition($condition)
311 array_push($this->if_conditions,$condition);
312 $this->updateTopIfCondition();
318 * BaseActions::updateTopIfCondition()
319 * Helper function: update the Top of the If Conditions Array
324 private function updateTopIfCondition()
326 if ( sizeof($this->if_conditions) == 0 )
328 $this->if_currentlevel = 1;
332 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
338 * BaseActions::addIfExecute()
339 * Helper function for elseif / elseifnot
344 private function addIfExecute()
346 array_push($this->if_execute, 0);
351 * BaseActions::updateIfExecute()
352 * Helper function for elseif / elseifnot
354 * @param string $condition condition to be fullfilled
357 private function updateIfExecute($condition)
359 $index = sizeof($this->if_execute) - 1;
360 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
365 * BaseActions::getTopIfCondition()()
366 * returns the currently top if condition
369 * @return string level
371 public function getTopIfCondition()
373 return $this->if_currentlevel;
377 * BaseActions::setHighlight(()
378 * Sets the search terms to be highlighted
380 * @param string $highlight A series of search terms
383 public function setHighlight($highlight)
385 $this->strHighlight = $highlight;
388 $this->aHighlight = parseHighlight($highlight);
394 * BaseActions::highlight()
395 * Applies the highlight to the given piece of text
398 * @param string $data Data that needs to be highlighted
399 * @return string hilighted data
401 public function highlight($data)
403 if ( $this->aHighlight )
405 $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
411 * BaseActions::parse_if()
412 * Parses <%if%> statements
417 public function parse_if()
419 $this->addIfExecute();
420 $args = func_get_args();
421 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
422 $this->addIfCondition($condition);
427 * BaseActions::parse_else()
428 * Parses <%else%> statements
433 public function parse_else()
435 if ( sizeof($this->if_conditions) == 0 )
440 array_pop($this->if_conditions);
442 if ( $this->if_currentlevel )
445 $this->updateIfExecute(1);
446 $this->addIfCondition(0);
448 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
451 $this->addIfCondition(0);
456 $this->addIfCondition(1);
462 * BaseActions::parse_elseif()
463 * Parses <%elseif%> statements
468 public function parse_elseif()
470 if ( sizeof($this->if_conditions) == 0 )
475 array_pop($this->if_conditions);
477 if ( $this->if_currentlevel )
480 $this->updateIfExecute(1);
481 $this->addIfCondition(0);
483 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
486 $this->addIfCondition(0);
491 $args = func_get_args();
492 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
493 $this->addIfCondition($condition);
499 * BaseActions::parse_ifnot()
500 * Parses <%ifnot%> statements
505 public function parse_ifnot()
507 $this->addIfExecute();
509 $args = func_get_args();
510 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
511 $this->addIfCondition(!$condition);
516 * BaseActions::parse_elseifnot()
517 * Parses <%elseifnot%> statements
522 public function parse_elseifnot()
524 if ( sizeof($this->if_conditions) == 0 )
529 array_pop($this->if_conditions);
531 if ( $this->if_currentlevel )
534 $this->updateIfExecute(1);
535 $this->addIfCondition(0);
537 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
540 $this->addIfCondition(0);
545 $args = func_get_args();
546 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
547 $this->addIfCondition(!$condition);
553 * BaseActions::parse_endif()
554 * Ends a conditional if-block
555 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
560 public function parse_endif()
562 // we can only close what has been opened
563 if ( sizeof($this->if_conditions) == 0 )
568 if ( $this->if_currentlevel )
577 array_pop($this->if_conditions);
578 array_pop($this->if_execute);
580 $this->updateTopIfCondition();