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 1848 2012-05-16 12:17:00Z 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(
75 * BaseActions::__construct()
76 * Constructor for a new BaseAction object
78 protected function __construct()
83 $this->if_conditions = array(); // array on which condition values are pushed/popped
84 $this->if_execute = array(); // array on which condition values are pushed/popped
85 $this->if_currentlevel = 1; // 1 = current level is displayed; 0 = current level not displayed
88 $this->strHighlight = ''; // full highlight
89 $this->aHighlight = array(); // parsed highlight
94 * BaseActions::getAvailableActions()
99 public function getAvailableActions()
101 return self::$defined_actions;
105 * BaseActions::setParser()
108 * @param object $parser an instance of Parser class
111 public function setParser(&$parser)
113 $this->parser =& $parser;
118 * BaseActions::parse_charset()
119 * Parse charset to appropriate character set name registered to IANA
124 public function parse_charset()
128 if ( i18n::get_forced_charset() !== '' )
130 echo i18n::get_forced_charset();
134 echo i18n::get_current_charset();
141 * BaseActions::parse_locale()
142 * Parse locale to language-region according to RFC 4646
147 public function parse_locale()
149 echo preg_replace('#(.+)_(.+)_(.+)#', '$1-$3', i18n::get_current_locale());
154 * BaseActions::parse_include()
155 * include file (no parsing of php)
157 * ToDo: function returns nothing and refering to the cross reference it
158 * isn't called from anywhere
160 * @param string $filename filename to be included
163 public function parse_include($filename)
165 @readfile($this->getIncludeFileName($filename));
170 * BaseActions::parse_phpinclude()
173 * @param string $filename filename to be included
176 public function parse_phpinclude($filename)
178 includephp($this->getIncludeFileName($filename));
184 * BaseActions::parse_parsedinclude()
187 * @param string $name filename to be included
190 public function parse_parsedinclude($name)
192 // check current level
193 if ( $this->level > 3 )
195 // max. depth reached (avoid endless loop)
199 $file = $this->getIncludeFileName($name);
200 if ( !file_exists($file) && $this->parser->skin != NULL)
202 $contents = $this->parser->skin->getContentFromDB($name);
206 $contents = file_get_contents($file);
209 if ( empty($contents) )
214 $this->level = $this->level + 1;
215 // parse file contents
216 $this->parser->parse($contents);
218 $this->level = $this->level - 1;
223 * BaseActions::getIncludeFileName()
224 * Returns the correct location of the file to be included, according to
227 * IF IncludeMode = 'skindir' => use skindir
229 * @param string $filename name of file to be inclluded
230 * @return string name of file with relative path
232 private function getIncludeFileName($filename)
234 // leave absolute filenames and http urls as they are
236 (i18n::substr($filename,0,1) == '/')
237 || (i18n::substr($filename,0,7) == 'http://')
238 || (i18n::substr($filename,0,6) == 'ftp://')
244 $filename = Parser::getProperty('IncludePrefix') . $filename;
246 if ( Parser::getProperty('IncludeMode') == 'skindir' )
249 return $DIR_SKINS . $filename;
255 * BaseActions::parse_skinfile()
256 * Inserts an url relative to the skindir (useful when doing import/export)
258 * e.g. <skinfile(default/myfile.sth)>
260 * @param string $filename name of file to be inclluded
263 public function parse_skinfile($filename)
266 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;
271 * BaseActions::parse_set()
272 * Sets a property for the parser
274 * @param string $property name of property
275 * @param string $value value of property
278 public function parse_set($property, $value)
280 Parser::setProperty($property, $value);
285 * BaseActions::parse_text()
288 * @param string $constant named constant
291 public function parse_text($constant)
293 if ( !defined($constant) )
299 echo constant($constant);
305 * BaseActions::addIfCondition()
306 * Helper function: add if condition
308 * @param string $condition condition for if context
311 private function addIfCondition($condition)
313 array_push($this->if_conditions,$condition);
314 $this->updateTopIfCondition();
320 * BaseActions::updateTopIfCondition()
321 * Helper function: update the Top of the If Conditions Array
326 private function updateTopIfCondition()
328 if ( sizeof($this->if_conditions) == 0 )
330 $this->if_currentlevel = 1;
334 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
340 * BaseActions::addIfExecute()
341 * Helper function for elseif / elseifnot
346 private function addIfExecute()
348 array_push($this->if_execute, 0);
353 * BaseActions::updateIfExecute()
354 * Helper function for elseif / elseifnot
356 * @param string $condition condition to be fullfilled
359 private function updateIfExecute($condition)
361 $index = sizeof($this->if_execute) - 1;
362 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
367 * BaseActions::getTopIfCondition()()
368 * returns the currently top if condition
371 * @return string level
373 public function getTopIfCondition()
375 return $this->if_currentlevel;
379 * BaseActions::setHighlight(()
380 * Sets the search terms to be highlighted
382 * @param string $highlight A series of search terms
385 public function setHighlight($highlight)
387 $this->strHighlight = $highlight;
390 $this->aHighlight = parseHighlight($highlight);
396 * BaseActions::highlight()
397 * Applies the highlight to the given piece of text
400 * @param string $data Data that needs to be highlighted
401 * @return string hilighted data
403 public function highlight($data)
405 if ( $this->aHighlight )
407 $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
413 * BaseActions::parse_benchmark()
418 public function parse_benchmark()
421 $loadtime = microtime(TRUE) - $StartTime;
422 printf("%.3F sec/%d queries", $loadtime, DB::getExecCount());
427 * BaseActions::parse_if()
428 * Parses <%if%> statements
433 public function parse_if()
435 $this->addIfExecute();
436 $args = func_get_args();
437 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
438 $this->addIfCondition($condition);
443 * BaseActions::parse_else()
444 * Parses <%else%> statements
449 public function parse_else()
451 if ( sizeof($this->if_conditions) == 0 )
456 array_pop($this->if_conditions);
458 if ( $this->if_currentlevel )
461 $this->updateIfExecute(1);
462 $this->addIfCondition(0);
464 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
467 $this->addIfCondition(0);
472 $this->addIfCondition(1);
478 * BaseActions::parse_elseif()
479 * Parses <%elseif%> statements
484 public function parse_elseif()
486 if ( sizeof($this->if_conditions) == 0 )
491 array_pop($this->if_conditions);
493 if ( $this->if_currentlevel )
496 $this->updateIfExecute(1);
497 $this->addIfCondition(0);
499 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
502 $this->addIfCondition(0);
507 $args = func_get_args();
508 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
509 $this->addIfCondition($condition);
515 * BaseActions::parse_ifnot()
516 * Parses <%ifnot%> statements
521 public function parse_ifnot()
523 $this->addIfExecute();
525 $args = func_get_args();
526 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
527 $this->addIfCondition(!$condition);
532 * BaseActions::parse_elseifnot()
533 * Parses <%elseifnot%> statements
538 public function parse_elseifnot()
540 if ( sizeof($this->if_conditions) == 0 )
545 array_pop($this->if_conditions);
547 if ( $this->if_currentlevel )
550 $this->updateIfExecute(1);
551 $this->addIfCondition(0);
553 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
556 $this->addIfCondition(0);
561 $args = func_get_args();
562 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
563 $this->addIfCondition(!$condition);
569 * BaseActions::parse_endif()
570 * Ends a conditional if-block
571 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
576 public function parse_endif()
578 // we can only close what has been opened
579 if ( sizeof($this->if_conditions) == 0 )
584 if ( $this->if_currentlevel )
593 array_pop($this->if_conditions);
594 array_pop($this->if_execute);
596 $this->updateTopIfCondition();