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
21 * @version $Id: BaseActions.php 1848 2012-05-16 12:17:00Z sakamocchi $
23 * @version $Id: BaseActions.php 1882 2012-06-17 07:52:43Z sakamocchi $
24 >>>>>>> skinnable-master
30 * BaseActions::$parser
31 * Skin class calls parser with action classes succeeded to this class
36 // depth level for includes (max. level is 3)
39 // array of evaluated conditions (true/false). The element at the end is the one for the most nested
41 private $if_conditions;
43 // in the "elseif" / "elseifnot" sequences, if one of the conditions become "true" remained conditions should not
44 // be tested. this variable (actually a stack) holds this information.
47 // at all times, can be evaluated to either true if the current block needs to be displayed. This
48 // variable is used to decide to skip skinvars in parts that will never be outputted.
49 private $if_currentlevel;
52 // contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight
53 protected $strHighlight;
55 // array of keywords that need to be highlighted in search results (see the highlight()
56 // and parseHighlight() methods)
59 /* NOTE: defined actions for this base class */
60 static private $defined_actions = array(
79 * BaseActions::__construct()
80 * Constructor for a new BaseAction object
82 protected function __construct()
87 $this->if_conditions = array(); // array on which condition values are pushed/popped
88 $this->if_execute = array(); // array on which condition values are pushed/popped
89 $this->if_currentlevel = 1; // 1 = current level is displayed; 0 = current level not displayed
92 $this->strHighlight = ''; // full highlight
93 $this->aHighlight = array(); // parsed highlight
98 * BaseActions::getAvailableActions()
103 public function getAvailableActions()
105 return self::$defined_actions;
109 * BaseActions::setParser()
112 * @param object $parser an instance of Parser class
115 public function setParser(&$parser)
117 $this->parser =& $parser;
122 * BaseActions::parse_charset()
123 * Parse charset to appropriate character set name registered to IANA
128 public function parse_charset()
132 if ( i18n::get_forced_charset() !== '' )
134 echo i18n::get_forced_charset();
138 echo i18n::get_current_charset();
145 * BaseActions::parse_locale()
147 * Parse locale to language-script-region according to RFC 4646
149 * Parse locale to language-region according to RFC 4646
150 >>>>>>> skinnable-master
155 public function parse_locale()
158 echo preg_replace('#_#', '-', i18n::get_current_locale());
160 echo preg_replace('#(.+)_(.+)_(.+)#', '$1-$3', i18n::get_current_locale());
161 >>>>>>> skinnable-master
166 * BaseActions::parse_include()
167 * include file (no parsing of php)
169 * ToDo: function returns nothing and refering to the cross reference it
170 * isn't called from anywhere
172 * @param string $filename filename to be included
175 public function parse_include($filename)
177 @readfile($this->getIncludeFileName($filename));
182 * BaseActions::parse_phpinclude()
185 * @param string $filename filename to be included
188 public function parse_phpinclude($filename)
190 includephp($this->getIncludeFileName($filename));
196 * BaseActions::parse_parsedinclude()
199 * @param string $name filename to be included
202 public function parse_parsedinclude($name)
204 // check current level
205 if ( $this->level > 3 )
207 // max. depth reached (avoid endless loop)
211 $file = $this->getIncludeFileName($name);
212 if ( !file_exists($file) && $this->parser->skin != NULL)
214 $contents = $this->parser->skin->getContentFromDB($name);
218 $contents = file_get_contents($file);
221 if ( empty($contents) )
226 $this->level = $this->level + 1;
227 // parse file contents
228 $this->parser->parse($contents);
230 $this->level = $this->level - 1;
235 * BaseActions::getIncludeFileName()
236 * Returns the correct location of the file to be included, according to
239 * IF IncludeMode = 'skindir' => use skindir
241 * @param string $filename name of file to be inclluded
242 * @return string name of file with relative path
244 private function getIncludeFileName($filename)
246 // leave absolute filenames and http urls as they are
248 (i18n::substr($filename,0,1) == '/')
249 || (i18n::substr($filename,0,7) == 'http://')
250 || (i18n::substr($filename,0,6) == 'ftp://')
256 $filename = Parser::getProperty('IncludePrefix') . $filename;
258 if ( Parser::getProperty('IncludeMode') == 'skindir' )
261 return $DIR_SKINS . $filename;
267 * BaseActions::parse_skinfile()
268 * Inserts an url relative to the skindir (useful when doing import/export)
270 * e.g. <skinfile(default/myfile.sth)>
272 * @param string $filename name of file to be inclluded
275 public function parse_skinfile($filename)
278 echo $CONF['SkinsURL'] . Parser::getProperty('IncludePrefix') . $filename;
283 * BaseActions::parse_set()
284 * Sets a property for the parser
286 * @param string $property name of property
287 * @param string $value value of property
290 public function parse_set($property, $value)
292 Parser::setProperty($property, $value);
297 * BaseActions::parse_text()
300 * @param string $constant named constant
303 public function parse_text($constant)
305 if ( !defined($constant) )
311 echo constant($constant);
317 * BaseActions::addIfCondition()
318 * Helper function: add if condition
320 * @param string $condition condition for if context
323 private function addIfCondition($condition)
325 array_push($this->if_conditions,$condition);
326 $this->updateTopIfCondition();
332 * BaseActions::updateTopIfCondition()
333 * Helper function: update the Top of the If Conditions Array
338 private function updateTopIfCondition()
340 if ( sizeof($this->if_conditions) == 0 )
342 $this->if_currentlevel = 1;
346 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
352 * BaseActions::addIfExecute()
353 * Helper function for elseif / elseifnot
358 private function addIfExecute()
360 array_push($this->if_execute, 0);
365 * BaseActions::updateIfExecute()
366 * Helper function for elseif / elseifnot
368 * @param string $condition condition to be fullfilled
371 private function updateIfExecute($condition)
373 $index = sizeof($this->if_execute) - 1;
374 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
379 * BaseActions::getTopIfCondition()()
380 * returns the currently top if condition
383 * @return string level
385 public function getTopIfCondition()
387 return $this->if_currentlevel;
391 * BaseActions::setHighlight(()
392 * Sets the search terms to be highlighted
394 * @param string $highlight A series of search terms
397 public function setHighlight($highlight)
399 $this->strHighlight = $highlight;
402 $this->aHighlight = parseHighlight($highlight);
408 * BaseActions::highlight()
409 * Applies the highlight to the given piece of text
412 * @param string $data Data that needs to be highlighted
413 * @return string hilighted data
415 public function highlight($data)
417 if ( $this->aHighlight )
419 $data = Entity::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
425 * BaseActions::parse_benchmark()
430 public function parse_benchmark()
433 $loadtime = microtime(TRUE) - $StartTime;
434 printf("%.3F sec/%d queries", $loadtime, DB::getExecCount());
439 * BaseActions::parse_if()
440 * Parses <%if%> statements
445 public function parse_if()
447 $this->addIfExecute();
448 $args = func_get_args();
450 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
452 $condition = call_user_func_array(array($this,'checkCondition'), $args);
453 >>>>>>> skinnable-master
454 $this->addIfCondition($condition);
459 * BaseActions::parse_else()
460 * Parses <%else%> statements
465 public function parse_else()
467 if ( sizeof($this->if_conditions) == 0 )
472 array_pop($this->if_conditions);
474 if ( $this->if_currentlevel )
477 $this->updateIfExecute(1);
478 $this->addIfCondition(0);
480 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
483 $this->addIfCondition(0);
488 $this->addIfCondition(1);
494 * BaseActions::parse_elseif()
495 * Parses <%elseif%> statements
500 public function parse_elseif()
502 if ( sizeof($this->if_conditions) == 0 )
507 array_pop($this->if_conditions);
509 if ( $this->if_currentlevel )
512 $this->updateIfExecute(1);
513 $this->addIfCondition(0);
515 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
518 $this->addIfCondition(0);
523 $args = func_get_args();
525 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
527 $condition = call_user_func_array(array($this,'checkCondition'), $args);
528 >>>>>>> skinnable-master
529 $this->addIfCondition($condition);
535 * BaseActions::parse_ifnot()
536 * Parses <%ifnot%> statements
541 public function parse_ifnot()
543 $this->addIfExecute();
545 $args = func_get_args();
547 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
549 $condition = call_user_func_array(array($this,'checkCondition'), $args);
550 >>>>>>> skinnable-master
551 $this->addIfCondition(!$condition);
556 * BaseActions::parse_elseifnot()
557 * Parses <%elseifnot%> statements
562 public function parse_elseifnot()
564 if ( sizeof($this->if_conditions) == 0 )
569 array_pop($this->if_conditions);
571 if ( $this->if_currentlevel )
574 $this->updateIfExecute(1);
575 $this->addIfCondition(0);
577 elseif ( $this->if_execute[sizeof($this->if_execute) - 1] )
580 $this->addIfCondition(0);
585 $args = func_get_args();
587 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
589 $condition = call_user_func_array(array($this,'checkCondition'), $args);
590 >>>>>>> skinnable-master
591 $this->addIfCondition(!$condition);
597 * BaseActions::parse_endif()
598 * Ends a conditional if-block
599 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
604 public function parse_endif()
606 // we can only close what has been opened
607 if ( sizeof($this->if_conditions) == 0 )
612 if ( $this->if_currentlevel )
621 array_pop($this->if_conditions);
622 array_pop($this->if_execute);
624 $this->updateTopIfCondition();