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 1527 2011-06-21 10:43:44Z sakamocchi $
25 // depth level for includes (max. level is 3)
28 // array of evaluated conditions (true/false). The element at the end is the one for the most nested
32 // in the "elseif" / "elseifnot" sequences, if one of the conditions become "true" remained conditions should not
33 // be tested. this variable (actually a stack) holds this information.
36 // at all times, can be evaluated to either true if the current block needs to be displayed. This
37 // variable is used to decide to skip skinvars in parts that will never be outputted.
40 // contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight
43 // array of keywords that need to be highlighted in search results (see the highlight()
44 // and parseHighlight() methods)
47 // reference to the parser object that is using this object as actions-handler
51 * Constructor for a new BaseAction object
53 function BaseActions() {
57 $this->if_conditions = array(); // array on which condition values are pushed/popped
58 $this->if_execute = array(); // array on which condition values are pushed/popped
59 $this->if_currentlevel = 1; // 1 = current level is displayed; 0 = current level not displayed
62 $this->strHighlight = ''; // full highlight
63 $this->aHighlight = array(); // parsed highlight
68 * include file (no parsing of php)
70 * ToDo: function returns nothing and refering to the cross reference it
71 * isn't called from anywhere
75 function parse_include($filename) {
76 @readfile($this->getIncludeFileName($filename));
84 function parse_phpinclude($filename) {
85 includephp($this->getIncludeFileName($filename));
94 function parse_parsedinclude($filename) {
95 // check current level
96 if ($this->level > 3) return; // max. depth reached (avoid endless loop)
97 $file = $this->getIncludeFileName($filename);
98 if (!file_exists($file)) return;
99 $contents = file_get_contents($file);
100 if (empty($contents)) return;
102 $this->level = $this->level + 1;
103 // parse file contents
104 $this->parser->parse($contents);
106 $this->level = $this->level - 1;
110 * Returns the correct location of the file to be included, according to
113 * IF IncludeMode = 'skindir' => use skindir
117 function getIncludeFileName($filename) {
118 // leave absolute filenames and http urls as they are
120 (i18n::substr($filename,0,1) == '/')
121 || (i18n::substr($filename,0,7) == 'http://')
122 || (i18n::substr($filename,0,6) == 'ftp://')
126 $filename = PARSER::getProperty('IncludePrefix') . $filename;
127 if (PARSER::getProperty('IncludeMode') == 'skindir') {
129 return $DIR_SKINS . $filename;
136 * Inserts an url relative to the skindir (useful when doing import/export)
138 * e.g. <skinfile(default/myfile.sth)>
140 function parse_skinfile($filename) {
143 echo $CONF['SkinsURL'] . PARSER::getProperty('IncludePrefix') . $filename;
147 * Sets a property for the parser
149 function parse_set($property, $value) {
150 PARSER::setProperty($property, $value);
154 * Helper function: add if condition
156 function _addIfCondition($condition) {
158 array_push($this->if_conditions,$condition);
160 $this->_updateTopIfCondition();
166 * Helper function: update the Top of the If Conditions Array
168 function _updateTopIfCondition() {
169 if (sizeof($this->if_conditions) == 0) {
170 $this->if_currentlevel = 1;
173 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
178 * Helper function for elseif / elseifnot
180 function _addIfExecute() {
181 array_push($this->if_execute, 0);
185 * Helper function for elseif / elseifnot
186 * @param string condition to be fullfilled
188 function _updateIfExecute($condition) {
189 $index = sizeof($this->if_execute) - 1;
190 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
194 * returns the currently top if condition
196 function _getTopIfCondition() {
197 return $this->if_currentlevel;
201 * Sets the search terms to be highlighted
204 * A series of search terms
206 function setHighlight($highlight) {
207 $this->strHighlight = $highlight;
209 $this->aHighlight = parseHighlight($highlight);
214 * Applies the highlight to the given piece of text
217 * @param String $data Data that needs to be highlighted
218 * @return String hilighted data
220 function highlight($data)
222 if ( $this->aHighlight )
224 $data = ENTITY::highlight($data, $this->aHighlight, $this->template['SEARCH_HIGHLIGHT']);
230 * Parses <%if%> statements
232 function parse_if() {
233 $this->_addIfExecute();
235 $args = func_get_args();
236 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
237 $this->_addIfCondition($condition);
241 * Parses <%else%> statements
243 function parse_else() {
244 if (sizeof($this->if_conditions) == 0) return;
245 array_pop($this->if_conditions);
246 if ($this->if_currentlevel) {
248 $this->_updateIfExecute(1);
249 $this->_addIfCondition(0);
250 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
252 $this->_addIfCondition(0);
255 $this->_addIfCondition(1);
260 * Parses <%elseif%> statements
262 function parse_elseif() {
263 if (sizeof($this->if_conditions) == 0) return;
264 array_pop($this->if_conditions);
265 if ($this->if_currentlevel) {
267 $this->_updateIfExecute(1);
268 $this->_addIfCondition(0);
269 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
271 $this->_addIfCondition(0);
274 $args = func_get_args();
275 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
276 $this->_addIfCondition($condition);
281 * Parses <%ifnot%> statements
283 function parse_ifnot() {
284 $this->_addIfExecute();
286 $args = func_get_args();
287 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
288 $this->_addIfCondition(!$condition);
292 * Parses <%elseifnot%> statements
294 function parse_elseifnot() {
295 if (sizeof($this->if_conditions) == 0) return;
296 array_pop($this->if_conditions);
297 if ($this->if_currentlevel) {
299 $this->_updateIfExecute(1);
300 $this->_addIfCondition(0);
301 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
303 $this->_addIfCondition(0);
306 $args = func_get_args();
307 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
308 $this->_addIfCondition(!$condition);
313 * Ends a conditional if-block
314 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
316 function parse_endif() {
317 // we can only close what has been opened
318 if (sizeof($this->if_conditions) == 0) return;
320 if ($this->if_currentlevel) {
325 array_pop($this->if_conditions);
326 array_pop($this->if_execute);
328 $this->_updateTopIfCondition();