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
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 $parts = explode('|', $filename, 2);
86 if ($skin->getContent($parts[0])) {
87 $contents = $skin->getContent($parts[0]);
89 $filename = $this->getIncludeFileName($filename);
90 if (!file_exists($filename)) return '';
92 $fsize = filesize($filename);
95 if ($fsize <= 0) return;
97 $this->level = $this->level + 1;
100 $fd = fopen ($filename, 'r');
101 $contents = fread ($fd, $fsize);
105 // parse file contents
106 $this->parser->parse($contents);
108 $this->level = $this->level - 1;
112 * Returns the correct location of the file to be included, according to
115 * IF IncludeMode = 'skindir' => use skindir
117 function getIncludeFileName($filename) {
118 // leave absolute filenames and http urls as they are
120 (substr($filename,0,1) == '/')
121 || (substr($filename,0,7) == 'http://')
122 || (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();
165 function _updateTopIfCondition() {
166 if (sizeof($this->if_conditions) == 0)
167 $this->if_currentlevel = 1;
169 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
173 * Helper function for elseif / elseifnot
175 function _addIfExecute() {
176 array_push($this->if_execute, 0);
180 * Helper function for elseif / elseifnot
181 * @param string condition to be fullfilled
183 function _updateIfExecute($condition) {
184 $index = sizeof($this->if_execute) - 1;
185 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
189 * returns the currently top if condition
191 function _getTopIfCondition() {
192 return $this->if_currentlevel;
196 * Sets the search terms to be highlighted
199 * A series of search terms
201 function setHighlight($highlight) {
202 $this->strHighlight = $highlight;
204 $this->aHighlight = parseHighlight($highlight);
209 * Applies the highlight to the given piece of text
212 * Data that needs to be highlighted
215 function highlight(&$data) {
216 if ($this->aHighlight)
217 return highlight($data,$this->aHighlight,$this->template['SEARCH_HIGHLIGHT']);
223 * Parses <%if%> statements
225 function parse_if() {
226 $this->_addIfExecute();
228 $args = func_get_args();
229 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
230 $this->_addIfCondition($condition);
234 * Parses <%else%> statements
236 function parse_else() {
237 if (sizeof($this->if_conditions) == 0) return;
238 array_pop($this->if_conditions);
239 if ($this->if_currentlevel) {
241 $this->_updateIfExecute(1);
242 $this->_addIfCondition(0);
243 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
245 $this->_addIfCondition(0);
248 $this->_addIfCondition(1);
253 * Parses <%elseif%> statements
255 function parse_elseif() {
256 if (sizeof($this->if_conditions) == 0) return;
257 array_pop($this->if_conditions);
258 if ($this->if_currentlevel) {
260 $this->_updateIfExecute(1);
261 $this->_addIfCondition(0);
262 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
264 $this->_addIfCondition(0);
267 $args = func_get_args();
268 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
269 $this->_addIfCondition($condition);
274 * Parses <%ifnot%> statements
276 function parse_ifnot() {
277 $this->_addIfExecute();
279 $args = func_get_args();
280 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
281 $this->_addIfCondition(!$condition);
285 * Parses <%elseifnot%> statements
287 function parse_elseifnot() {
288 if (sizeof($this->if_conditions) == 0) return;
289 array_pop($this->if_conditions);
290 if ($this->if_currentlevel) {
292 $this->_updateIfExecute(1);
293 $this->_addIfCondition(0);
294 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
296 $this->_addIfCondition(0);
299 $args = func_get_args();
300 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
301 $this->_addIfCondition(!$condition);
306 * Ends a conditional if-block
307 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
309 function parse_endif() {
310 // we can only close what has been opened
311 if (sizeof($this->if_conditions) == 0) return;
313 if ($this->if_currentlevel) {
318 array_pop($this->if_conditions);
319 array_pop($this->if_execute);
321 $this->_updateTopIfCondition();