3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2012 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)
12 * This class contains parse actions that are available in all ACTION classes
13 * e.g. include, phpinclude, parsedinclude, skinfile, ...
15 * It should never be used on it's own
20 // depth level for includes (max. level is 3)
23 // array of evaluated conditions (true/false). The element at the end is the one for the most nested
27 // in the "elseif" / "elseifnot" sequences, if one of the conditions become "true" remained conditions should not
28 // be tested. this variable (actually a stack) holds this information.
31 // at all times, can be evaluated to either true if the current block needs to be displayed. This
32 // variable is used to decide to skip skinvars in parts that will never be outputted.
35 // contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight
38 // array of keywords that need to be highlighted in search results (see the highlight()
39 // and parseHighlight() methods)
42 // reference to the parser object that is using this object as actions-handler
45 function BaseActions() {
49 $this->if_conditions = array(); // array on which condition values are pushed/popped
50 $this->if_execute = array(); // array on which condition values are pushed/popped
51 $this->if_currentlevel = 1; // 1 = current level is displayed; 0 = current level not displayed
54 $this->strHighlight = ''; // full highlight
55 $this->aHighlight = array(); // parsed highlight
59 // include file (no parsing of php)
60 function parse_include($filename) {
61 @readfile($this->getIncludeFileName($filename));
65 function parse_phpinclude($filename) {
66 includephp($this->getIncludeFileName($filename));
70 function parse_parsedinclude($filename) {
71 // check current level
74 return; // max. depth reached (avoid endless loop)
77 $skin = new SKIN($skinid);
78 $file = $this->getIncludeFileName($filename);
79 if (!$skin->isValid && !file_exists($file))
83 $contents = $skin->getContent($filename);
86 if (!file_exists($file))
90 $contents = file_get_contents($file);
96 $this->level = $this->level + 1;
97 // parse file contents
98 $this->parser->parse($contents);
100 $this->level = $this->level - 1;
104 * Returns the correct location of the file to be included, according to
107 * IF IncludeMode = 'skindir' => use skindir
109 function getIncludeFileName($filename) {
110 // leave absolute filenames and http urls as they are
112 (substr($filename,0,1) == '/')
113 || (substr($filename,0,7) == 'http://')
114 || (substr($filename,0,6) == 'ftp://')
118 $filename = PARSER::getProperty('IncludePrefix') . $filename;
119 if (PARSER::getProperty('IncludeMode') == 'skindir') {
121 return $DIR_SKINS . $filename;
128 * Inserts an url relative to the skindir (useful when doing import/export)
130 * e.g. <skinfile(default/myfile.sth)>
132 function parse_skinfile($filename) {
135 echo $CONF['SkinsURL'] . PARSER::getProperty('IncludePrefix') . $filename;
139 * Sets a property for the parser
141 function parse_set($property, $value) {
142 PARSER::setProperty($property, $value);
146 * Helper function: add if condition
148 function _addIfCondition($condition) {
150 array_push($this->if_conditions,$condition);
152 $this->_updateTopIfCondition();
157 function _updateTopIfCondition() {
158 if (sizeof($this->if_conditions) == 0)
159 $this->if_currentlevel = 1;
161 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
165 * Helper function for elseif / elseifnot
167 function _addIfExecute() {
168 array_push($this->if_execute, 0);
172 * Helper function for elseif / elseifnot
173 * @param string condition to be fullfilled
175 function _updateIfExecute($condition) {
176 $index = sizeof($this->if_execute) - 1;
177 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
181 * returns the currently top if condition
183 function _getTopIfCondition() {
184 return $this->if_currentlevel;
188 * Sets the search terms to be highlighted
191 * A series of search terms
193 function setHighlight($highlight) {
194 $this->strHighlight = $highlight;
196 $this->aHighlight = parseHighlight($highlight);
201 * Applies the highlight to the given piece of text
204 * Data that needs to be highlighted
207 function highlight(&$data) {
208 if ($this->aHighlight)
209 return highlight($data,$this->aHighlight,$this->template['SEARCH_HIGHLIGHT']);
215 * Parses <%if%> statements
217 function parse_if() {
218 $this->_addIfExecute();
220 $args = func_get_args();
221 $condition = call_user_func_array(array($this,'checkCondition'), $args);
222 $this->_addIfCondition($condition);
226 * Parses <%else%> statements
228 function parse_else() {
229 if (sizeof($this->if_conditions) == 0) return;
230 array_pop($this->if_conditions);
231 if ($this->if_currentlevel) {
233 $this->_updateIfExecute(1);
234 $this->_addIfCondition(0);
235 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
237 $this->_addIfCondition(0);
240 $this->_addIfCondition(1);
245 * Parses <%elseif%> statements
247 function parse_elseif() {
248 if (sizeof($this->if_conditions) == 0) return;
249 array_pop($this->if_conditions);
250 if ($this->if_currentlevel) {
252 $this->_updateIfExecute(1);
253 $this->_addIfCondition(0);
254 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
256 $this->_addIfCondition(0);
259 $args = func_get_args();
260 $condition = call_user_func_array(array($this,'checkCondition'), $args);
261 $this->_addIfCondition($condition);
266 * Parses <%ifnot%> statements
268 function parse_ifnot() {
269 $this->_addIfExecute();
271 $args = func_get_args();
272 $condition = call_user_func_array(array($this,'checkCondition'), $args);
273 $this->_addIfCondition(!$condition);
277 * Parses <%elseifnot%> statements
279 function parse_elseifnot() {
280 if (sizeof($this->if_conditions) == 0) return;
281 array_pop($this->if_conditions);
282 if ($this->if_currentlevel) {
284 $this->_updateIfExecute(1);
285 $this->_addIfCondition(0);
286 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
288 $this->_addIfCondition(0);
291 $args = func_get_args();
292 $condition = call_user_func_array(array($this,'checkCondition'), $args);
293 $this->_addIfCondition(!$condition);
298 * Ends a conditional if-block
299 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
301 function parse_endif() {
302 // we can only close what has been opened
303 if (sizeof($this->if_conditions) == 0) return;
305 if ($this->if_currentlevel) {
310 array_pop($this->if_conditions);
311 array_pop($this->if_execute);
313 $this->_updateTopIfCondition();