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)
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-2012 The Nucleus Group
21 * @version $NucleusJP: BaseActions.php,v 1.2 2006/07/20 08:01:52 kimitake Exp $
26 // depth level for includes (max. level is 3)
29 // array of evaluated conditions (true/false). The element at the end is the one for the most nested
33 // in the "elseif" / "elseifnot" sequences, if one of the conditions become "true" remained conditions should not
34 // be tested. this variable (actually a stack) holds this information.
37 // at all times, can be evaluated to either true if the current block needs to be displayed. This
38 // variable is used to decide to skip skinvars in parts that will never be outputted.
41 // contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight
44 // array of keywords that need to be highlighted in search results (see the highlight()
45 // and parseHighlight() methods)
48 // reference to the parser object that is using this object as actions-handler
51 function BaseActions() {
55 $this->if_conditions = array(); // array on which condition values are pushed/popped
56 $this->if_execute = array(); // array on which condition values are pushed/popped
57 $this->if_currentlevel = 1; // 1 = current level is displayed; 0 = current level not displayed
60 $this->strHighlight = ''; // full highlight
61 $this->aHighlight = array(); // parsed highlight
65 // include file (no parsing of php)
66 function parse_include($filename) {
67 @readfile($this->getIncludeFileName($filename));
71 function parse_phpinclude($filename) {
72 includephp($this->getIncludeFileName($filename));
76 function parse_parsedinclude($filename) {
77 // check current level
80 return; // max. depth reached (avoid endless loop)
83 $skin = new SKIN($skinid);
84 $file = $this->getIncludeFileName($filename);
85 if (!$skin->isValid && !file_exists($file))
89 $contents = $skin->getContent($filename);
92 if (!file_exists($file))
96 $contents = file_get_contents($file);
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
115 function getIncludeFileName($filename) {
116 // leave absolute filenames and http urls as they are
118 (substr($filename,0,1) == '/')
119 || (substr($filename,0,7) == 'http://')
120 || (substr($filename,0,6) == 'ftp://')
124 $filename = PARSER::getProperty('IncludePrefix') . $filename;
125 if (PARSER::getProperty('IncludeMode') == 'skindir') {
127 return $DIR_SKINS . $filename;
134 * Inserts an url relative to the skindir (useful when doing import/export)
136 * e.g. <skinfile(default/myfile.sth)>
138 function parse_skinfile($filename) {
141 echo $CONF['SkinsURL'] . PARSER::getProperty('IncludePrefix') . $filename;
145 * Sets a property for the parser
147 function parse_set($property, $value) {
148 PARSER::setProperty($property, $value);
152 * Helper function: add if condition
154 function _addIfCondition($condition) {
156 array_push($this->if_conditions,$condition);
158 $this->_updateTopIfCondition();
163 function _updateTopIfCondition() {
164 if (sizeof($this->if_conditions) == 0)
165 $this->if_currentlevel = 1;
167 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
171 * Helper function for elseif / elseifnot
173 function _addIfExecute() {
174 array_push($this->if_execute, 0);
178 * Helper function for elseif / elseifnot
179 * @param string condition to be fullfilled
181 function _updateIfExecute($condition) {
182 $index = sizeof($this->if_execute) - 1;
183 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
187 * returns the currently top if condition
189 function _getTopIfCondition() {
190 return $this->if_currentlevel;
194 * Sets the search terms to be highlighted
197 * A series of search terms
199 function setHighlight($highlight) {
200 $this->strHighlight = $highlight;
202 $this->aHighlight = parseHighlight($highlight);
207 * Applies the highlight to the given piece of text
210 * Data that needs to be highlighted
213 function highlight(&$data) {
214 if ($this->aHighlight)
215 return highlight($data,$this->aHighlight,$this->template['SEARCH_HIGHLIGHT']);
221 * Parses <%if%> statements
223 function parse_if() {
224 $this->_addIfExecute();
226 $args = func_get_args();
227 $condition = call_user_func_array(array($this,'checkCondition'), $args);
228 $this->_addIfCondition($condition);
232 * Parses <%else%> statements
234 function parse_else() {
235 if (sizeof($this->if_conditions) == 0) return;
236 array_pop($this->if_conditions);
237 if ($this->if_currentlevel) {
239 $this->_updateIfExecute(1);
240 $this->_addIfCondition(0);
241 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
243 $this->_addIfCondition(0);
246 $this->_addIfCondition(1);
251 * Parses <%elseif%> statements
253 function parse_elseif() {
254 if (sizeof($this->if_conditions) == 0) return;
255 array_pop($this->if_conditions);
256 if ($this->if_currentlevel) {
258 $this->_updateIfExecute(1);
259 $this->_addIfCondition(0);
260 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
262 $this->_addIfCondition(0);
265 $args = func_get_args();
266 $condition = call_user_func_array(array($this,'checkCondition'), $args);
267 $this->_addIfCondition($condition);
272 * Parses <%ifnot%> statements
274 function parse_ifnot() {
275 $this->_addIfExecute();
277 $args = func_get_args();
278 $condition = call_user_func_array(array($this,'checkCondition'), $args);
279 $this->_addIfCondition(!$condition);
283 * Parses <%elseifnot%> statements
285 function parse_elseifnot() {
286 if (sizeof($this->if_conditions) == 0) return;
287 array_pop($this->if_conditions);
288 if ($this->if_currentlevel) {
290 $this->_updateIfExecute(1);
291 $this->_addIfCondition(0);
292 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
294 $this->_addIfCondition(0);
297 $args = func_get_args();
298 $condition = call_user_func_array(array($this,'checkCondition'), $args);
299 $this->_addIfCondition(!$condition);
304 * Ends a conditional if-block
305 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
307 function parse_endif() {
308 // we can only close what has been opened
309 if (sizeof($this->if_conditions) == 0) return;
311 if ($this->if_currentlevel) {
316 array_pop($this->if_conditions);
317 array_pop($this->if_execute);
319 $this->_updateTopIfCondition();