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 $name filename to be included
189 public function parse_parsedinclude($name)
191 // check current level
192 if ( $this->level > 3 )
194 // max. depth reached (avoid endless loop)
198 $file = $this->getIncludeFileName($name);
199 if ( !file_exists($file) && $this->parser->skin != NULL)
201 $contents = $this->parser->skin->getContentFromDB($name);
205 $contents = file_get_contents($file);
208 if ( empty($contents) )
213 $this->level = $this->level + 1;
214 // parse file contents
215 $this->parser->parse($contents);
217 $this->level = $this->level - 1;
222 * BaseActions::getIncludeFileName()
223 * Returns the correct location of the file to be included, according to
226 * IF IncludeMode = 'skindir' => use skindir
228 * @param string $filename name of file to be inclluded
229 * @return string name of file with relative path
231 private function getIncludeFileName($filename)
233 // leave absolute filenames and http urls as they are
235 (i18n::substr($filename,0,1) == '/')
236 || (i18n::substr($filename,0,7) == 'http://')
237 || (i18n::substr($filename,0,6) == 'ftp://')
243 $filename = Parser::getProperty('IncludePrefix') . $filename;
245 if ( Parser::getProperty('IncludeMode') == 'skindir' )
248 return $DIR_SKINS . $filename;
254 * BaseActions::parse_skinfile()
255 * Inserts an url relative to the skindir (useful when doing import/export)
257 * e.g. <skinfile(default/myfile.sth)>
259 * @param string $filename name of file to be inclluded
262 public function parse_skinfile($filename)
265 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;
270 * BaseActions::parse_set()
271 * Sets a property for the parser
273 * @param string $property name of property
274 * @param string $value value of property
277 public function parse_set($property, $value)
279 Parser::setProperty($property, $value);
284 * BaseActions::parse_text()
287 * @param string $constant named constant
290 public function parse_text($constant)
292 if ( !defined($constant) )
298 echo constant($constant);
304 * BaseActions::addIfCondition()
305 * Helper function: add if condition
307 * @param string $condition condition for if context
310 private function addIfCondition($condition)
312 array_push($this->if_conditions,$condition);
313 $this->updateTopIfCondition();
319 * BaseActions::updateTopIfCondition()
320 * Helper function: update the Top of the If Conditions Array
325 private function updateTopIfCondition()
327 if ( sizeof($this->if_conditions) == 0 )
329 $this->if_currentlevel = 1;
333 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
339 * BaseActions::addIfExecute()
340 * Helper function for elseif / elseifnot
345 private function addIfExecute()
347 array_push($this->if_execute, 0);
352 * BaseActions::updateIfExecute()
353 * Helper function for elseif / elseifnot
355 * @param string $condition condition to be fullfilled
358 private function updateIfExecute($condition)
360 $index = sizeof($this->if_execute) - 1;
361 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
366 * BaseActions::getTopIfCondition()()
367 * returns the currently top if condition
370 * @return string level
372 public function getTopIfCondition()
374 return $this->if_currentlevel;
378 * BaseActions::setHighlight(()
379 * Sets the search terms to be highlighted
381 * @param string $highlight A series of search terms
384 public function setHighlight($highlight)
386 $this->strHighlight = $highlight;
389 $this->aHighlight = parseHighlight($highlight);
395 * BaseActions::highlight()
396 * Applies the highlight to the given piece of text
399 * @param string $data Data that needs to be highlighted
400 * @return string hilighted data
402 public function highlight($data)
404 if ( $this->aHighlight )
406 $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
412 * BaseActions::parse_if()
413 * Parses <%if%> statements
418 public function parse_if()
420 $this->addIfExecute();
421 $args = func_get_args();
422 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
423 $this->addIfCondition($condition);
428 * BaseActions::parse_else()
429 * Parses <%else%> statements
434 public function parse_else()
436 if ( sizeof($this->if_conditions) == 0 )
441 array_pop($this->if_conditions);
443 if ( $this->if_currentlevel )
446 $this->updateIfExecute(1);
447 $this->addIfCondition(0);
449 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
452 $this->addIfCondition(0);
457 $this->addIfCondition(1);
463 * BaseActions::parse_elseif()
464 * Parses <%elseif%> statements
469 public function parse_elseif()
471 if ( sizeof($this->if_conditions) == 0 )
476 array_pop($this->if_conditions);
478 if ( $this->if_currentlevel )
481 $this->updateIfExecute(1);
482 $this->addIfCondition(0);
484 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
487 $this->addIfCondition(0);
492 $args = func_get_args();
493 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
494 $this->addIfCondition($condition);
500 * BaseActions::parse_ifnot()
501 * Parses <%ifnot%> statements
506 public function parse_ifnot()
508 $this->addIfExecute();
510 $args = func_get_args();
511 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
512 $this->addIfCondition(!$condition);
517 * BaseActions::parse_elseifnot()
518 * Parses <%elseifnot%> statements
523 public function parse_elseifnot()
525 if ( sizeof($this->if_conditions) == 0 )
530 array_pop($this->if_conditions);
532 if ( $this->if_currentlevel )
535 $this->updateIfExecute(1);
536 $this->addIfCondition(0);
538 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
541 $this->addIfCondition(0);
546 $args = func_get_args();
547 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
548 $this->addIfCondition(!$condition);
554 * BaseActions::parse_endif()
555 * Ends a conditional if-block
556 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
561 public function parse_endif()
563 // we can only close what has been opened
564 if ( sizeof($this->if_conditions) == 0 )
569 if ( $this->if_currentlevel )
578 array_pop($this->if_conditions);
579 array_pop($this->if_execute);
581 $this->updateTopIfCondition();