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 * Data that needs to be highlighted
220 function highlight(&$data) {
221 if ($this->aHighlight)
222 return highlight($data,$this->aHighlight,$this->template['SEARCH_HIGHLIGHT']);
228 * Parses <%if%> statements
230 function parse_if() {
231 $this->_addIfExecute();
233 $args = func_get_args();
234 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
235 $this->_addIfCondition($condition);
239 * Parses <%else%> statements
241 function parse_else() {
242 if (sizeof($this->if_conditions) == 0) return;
243 array_pop($this->if_conditions);
244 if ($this->if_currentlevel) {
246 $this->_updateIfExecute(1);
247 $this->_addIfCondition(0);
248 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
250 $this->_addIfCondition(0);
253 $this->_addIfCondition(1);
258 * Parses <%elseif%> statements
260 function parse_elseif() {
261 if (sizeof($this->if_conditions) == 0) return;
262 array_pop($this->if_conditions);
263 if ($this->if_currentlevel) {
265 $this->_updateIfExecute(1);
266 $this->_addIfCondition(0);
267 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
269 $this->_addIfCondition(0);
272 $args = func_get_args();
273 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
274 $this->_addIfCondition($condition);
279 * Parses <%ifnot%> statements
281 function parse_ifnot() {
282 $this->_addIfExecute();
284 $args = func_get_args();
285 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
286 $this->_addIfCondition(!$condition);
290 * Parses <%elseifnot%> statements
292 function parse_elseifnot() {
293 if (sizeof($this->if_conditions) == 0) return;
294 array_pop($this->if_conditions);
295 if ($this->if_currentlevel) {
297 $this->_updateIfExecute(1);
298 $this->_addIfCondition(0);
299 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
301 $this->_addIfCondition(0);
304 $args = func_get_args();
305 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
306 $this->_addIfCondition(!$condition);
311 * Ends a conditional if-block
312 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
314 function parse_endif() {
315 // we can only close what has been opened
316 if (sizeof($this->if_conditions) == 0) return;
318 if ($this->if_currentlevel) {
323 array_pop($this->if_conditions);
324 array_pop($this->if_execute);
326 $this->_updateTopIfCondition();