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)
81 global $manager, $blogid;
84 $blogid = $CONF['DefaultBlog'];
86 $blog = &$manager->getBlog($blogid);
87 $skinid = $blog->getDefaultSkin();
89 $skin = new SKIN($skinid);
90 $file = $this->getIncludeFileName($filename);
91 if (!$skin->isValid && !file_exists($file)) {
94 $parts = explode('|', $filename, 2);
95 if ($skin->getContent($parts[0])) {
96 $contents = $skin->getContent($parts[0]);
98 $filename = $this->getIncludeFileName($filename);
99 if (!file_exists($filename)) return '';
101 $fsize = filesize($filename);
103 // nothing to include
104 if ($fsize <= 0) return;
106 $this->level = $this->level + 1;
109 $fd = fopen ($filename, 'r');
110 $contents = fread ($fd, $fsize);
114 // parse file contents
115 $this->parser->parse($contents);
117 $this->level = $this->level - 1;
121 * Returns the correct location of the file to be included, according to
124 * IF IncludeMode = 'skindir' => use skindir
126 function getIncludeFileName($filename) {
127 // leave absolute filenames and http urls as they are
129 (substr($filename,0,1) == '/')
130 || (substr($filename,0,7) == 'http://')
131 || (substr($filename,0,6) == 'ftp://')
135 $filename = PARSER::getProperty('IncludePrefix') . $filename;
136 if (PARSER::getProperty('IncludeMode') == 'skindir') {
138 return $DIR_SKINS . $filename;
145 * Inserts an url relative to the skindir (useful when doing import/export)
147 * e.g. <skinfile(default/myfile.sth)>
149 function parse_skinfile($filename) {
152 echo $CONF['SkinsURL'] . PARSER::getProperty('IncludePrefix') . $filename;
156 * Sets a property for the parser
158 function parse_set($property, $value) {
159 PARSER::setProperty($property, $value);
163 * Helper function: add if condition
165 function _addIfCondition($condition) {
167 array_push($this->if_conditions,$condition);
169 $this->_updateTopIfCondition();
174 function _updateTopIfCondition() {
175 if (sizeof($this->if_conditions) == 0)
176 $this->if_currentlevel = 1;
178 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
182 * Helper function for elseif / elseifnot
184 function _addIfExecute() {
185 array_push($this->if_execute, 0);
189 * Helper function for elseif / elseifnot
190 * @param string condition to be fullfilled
192 function _updateIfExecute($condition) {
193 $index = sizeof($this->if_execute) - 1;
194 $this->if_execute[$index] = $this->if_execute[$index] || $condition;
198 * returns the currently top if condition
200 function _getTopIfCondition() {
201 return $this->if_currentlevel;
205 * Sets the search terms to be highlighted
208 * A series of search terms
210 function setHighlight($highlight) {
211 $this->strHighlight = $highlight;
213 $this->aHighlight = parseHighlight($highlight);
218 * Applies the highlight to the given piece of text
221 * Data that needs to be highlighted
224 function highlight(&$data) {
225 if ($this->aHighlight)
226 return highlight($data,$this->aHighlight,$this->template['SEARCH_HIGHLIGHT']);
232 * Parses <%if%> statements
234 function parse_if() {
235 $this->_addIfExecute();
237 $args = func_get_args();
238 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
239 $this->_addIfCondition($condition);
243 * Parses <%else%> statements
245 function parse_else() {
246 if (sizeof($this->if_conditions) == 0) return;
247 array_pop($this->if_conditions);
248 if ($this->if_currentlevel) {
250 $this->_updateIfExecute(1);
251 $this->_addIfCondition(0);
252 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
254 $this->_addIfCondition(0);
257 $this->_addIfCondition(1);
262 * Parses <%elseif%> statements
264 function parse_elseif() {
265 if (sizeof($this->if_conditions) == 0) return;
266 array_pop($this->if_conditions);
267 if ($this->if_currentlevel) {
269 $this->_updateIfExecute(1);
270 $this->_addIfCondition(0);
271 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
273 $this->_addIfCondition(0);
276 $args = func_get_args();
277 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
278 $this->_addIfCondition($condition);
283 * Parses <%ifnot%> statements
285 function parse_ifnot() {
286 $this->_addIfExecute();
288 $args = func_get_args();
289 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
290 $this->_addIfCondition(!$condition);
294 * Parses <%elseifnot%> statements
296 function parse_elseifnot() {
297 if (sizeof($this->if_conditions) == 0) return;
298 array_pop($this->if_conditions);
299 if ($this->if_currentlevel) {
301 $this->_updateIfExecute(1);
302 $this->_addIfCondition(0);
303 } elseif ($this->if_execute[sizeof($this->if_execute) - 1]) {
305 $this->_addIfCondition(0);
308 $args = func_get_args();
309 $condition = call_user_func_array(array(&$this,'checkCondition'), $args);
310 $this->_addIfCondition(!$condition);
315 * Ends a conditional if-block
316 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
318 function parse_endif() {
319 // we can only close what has been opened
320 if (sizeof($this->if_conditions) == 0) return;
322 if ($this->if_currentlevel) {
327 array_pop($this->if_conditions);
328 array_pop($this->if_execute);
330 $this->_updateTopIfCondition();