3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2011 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-2011 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
78 if ($this->level > 3) return; // max. depth reached (avoid endless loop)
80 $skin = new SKIN($skinid);
81 $file = $this->getIncludeFileName($filename);
82 if (!$skin->isValid && !file_exists($file)) {
85 $contents = $skin->getContent($filename);
87 if (!file_exists($file)) return;
88 $contents = file_get_contents($file);
89 if (empty($contents)) return;
91 $this->level = $this->level + 1;
92 // parse file contents
93 $this->parser->parse($contents);
95 $this->level = $this->level - 1;
99 * Returns the correct location of the file to be included, according to
102 * IF IncludeMode = 'skindir' => use skindir
104 function getIncludeFileName($filename) {
105 // leave absolute filenames and http urls as they are
107 (substr($filename,0,1) == '/')
108 || (substr($filename,0,7) == 'http://')
109 || (substr($filename,0,6) == 'ftp://')
113 $filename = PARSER::getProperty('IncludePrefix') . $filename;
114 if (PARSER::getProperty('IncludeMode') == 'skindir') {
116 return $DIR_SKINS . $filename;
123 * Inserts an url relative to the skindir (useful when doing import/export)
125 * e.g. <skinfile(default/myfile.sth)>
127 function parse_skinfile($filename) {
130 echo $CONF['SkinsURL'] . PARSER::getProperty('IncludePrefix') . $filename;
134 * Sets a property for the parser
136 function parse_set($property, $value) {
137 PARSER::setProperty($property, $value);
141 * Helper function: add if condition
143 function _addIfCondition($condition) {
145 array_push($this->if_conditions,$condition);
147 $this->_updateTopIfCondition();
152 function _updateTopIfCondition() {
153 if (sizeof($this->if_conditions) == 0)
154 $this->if_currentlevel = 1;
156 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
160 * Helper function for elseif / elseifnot
162 function _addIfExecute() {
163 array_push($this->if_execute, 0);
167 * Helper function for elseif / elseifnot
168 * @param string condition to be fullfilled
170 function _updateIfExecute($condition) {
171 $index = sizeof($this->if_execute) - 1;
172 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
176 * returns the currently top if condition
178 function _getTopIfCondition() {
179 return $this->if_currentlevel;
183 * Sets the search terms to be highlighted
186 * A series of search terms
188 function setHighlight($highlight) {
189 $this->strHighlight = $highlight;
191 $this->aHighlight = parseHighlight($highlight);
196 * Applies the highlight to the given piece of text
199 * Data that needs to be highlighted
202 function highlight(&$data) {
203 if ($this->aHighlight)
204 return highlight($data,$this->aHighlight,$this->template['SEARCH_HIGHLIGHT']);
210 * Parses <%if%> statements
212 function parse_if() {
213 $this->_addIfExecute();
215 $args = func_get_args();
216 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
217 $this->_addIfCondition($condition);
221 * Parses <%else%> statements
223 function parse_else() {
224 if (sizeof($this->if_conditions) == 0) return;
225 array_pop($this->if_conditions);
226 if ($this->if_currentlevel) {
228 $this->_updateIfExecute(1);
229 $this->_addIfCondition(0);
230 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
232 $this->_addIfCondition(0);
235 $this->_addIfCondition(1);
240 * Parses <%elseif%> statements
242 function parse_elseif() {
243 if (sizeof($this->if_conditions) == 0) return;
244 array_pop($this->if_conditions);
245 if ($this->if_currentlevel) {
247 $this->_updateIfExecute(1);
248 $this->_addIfCondition(0);
249 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
251 $this->_addIfCondition(0);
254 $args = func_get_args();
255 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
256 $this->_addIfCondition($condition);
261 * Parses <%ifnot%> statements
263 function parse_ifnot() {
264 $this->_addIfExecute();
266 $args = func_get_args();
267 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
268 $this->_addIfCondition(!$condition);
272 * Parses <%elseifnot%> statements
274 function parse_elseifnot() {
275 if (sizeof($this->if_conditions) == 0) return;
276 array_pop($this->if_conditions);
277 if ($this->if_currentlevel) {
279 $this->_updateIfExecute(1);
280 $this->_addIfCondition(0);
281 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
283 $this->_addIfCondition(0);
286 $args = func_get_args();
287 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
288 $this->_addIfCondition(!$condition);
293 * Ends a conditional if-block
294 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
296 function parse_endif() {
297 // we can only close what has been opened
298 if (sizeof($this->if_conditions) == 0) return;
300 if ($this->if_currentlevel) {
305 array_pop($this->if_conditions);
306 array_pop($this->if_execute);
308 $this->_updateTopIfCondition();