3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2004 The Nucleus Group
\r
6 * This program is free software; you can redistribute it and/or
\r
7 * modify it under the terms of the GNU General Public License
\r
8 * as published by the Free Software Foundation; either version 2
\r
9 * of the License, or (at your option) any later version.
\r
10 * (see nucleus/documentation/index.html#license for more info)
\r
15 * This is the parser class of Nucleus. It is used for various things (skin parsing,
\r
16 * form generation, ...)
\r
20 // array with the names of all allowed actions
\r
23 // reference to actions handler
\r
26 // delimiters that can be used for skin/templatevars
\r
29 // parameter delimiter (to separate skinvar params)
\r
32 // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions
\r
33 var $norestrictions;
\r
36 * Creates a new parser object with the given allowed actions
\r
37 * and the given handler
\r
39 * @param $allowedActions array
\r
40 * @param $handler class object with functions for each action (reference)
\r
41 * @param $delim optional delimiter
\r
42 * @param $paramdelim optional parameterdelimiter
\r
44 function PARSER($allowedActions, &$handler, $delim = '(<%|%>)', $pdelim = ',') {
\r
45 $this->actions = $allowedActions;
\r
46 $this->handler =& $handler;
\r
47 $this->delim = $delim;
\r
48 $this->pdelim = $pdelim;
\r
49 $this->norestrictions = 0; // set this to 1 to disable checking for allowedActions
\r
53 * Parses the given contents and outputs it
\r
55 function parse(&$contents) {
\r
57 $pieces = preg_split('/'.$this->delim.'/',$contents);
\r
59 $maxidx = sizeof($pieces);
\r
60 for ($idx = 0;$idx<$maxidx;$idx++) {
\r
61 echo $pieces[$idx];
\r
63 $this->doAction($pieces[$idx]);
\r
71 function doAction($action) {
\r
74 if (!$action) return;
\r
76 // split into action name + arguments
\r
77 if (strstr($action,'(')) {
\r
78 $paramStartPos = strpos($action, '(');
\r
79 $params = substr($action, $paramStartPos + 1, strlen($action) - $paramStartPos - 2);
\r
80 $action = substr($action, 0, $paramStartPos);
\r
81 $params = explode ($this->pdelim, $params);
\r
84 // for PHP versions lower than 4.0.6:
\r
85 // - add // before '$params = ...'
\r
86 // - remove // before 'foreach'
\r
87 $params = array_map('trim',$params);
\r
88 // foreach ($params as $key => $value) { $params[$key] = trim($value); }
\r
94 $actionlc = strtolower($action);
\r
96 // skip execution of skinvars while inside an if condition which hides this part of the page
\r
97 if (!$this->handler->if_currentlevel && ($actionlc != 'else') && ($actionlc != 'endif') && (substr($actionlc,0,2) != 'if'))
\r
100 if (in_array($actionlc, $this->actions) || $this->norestrictions ) {
\r
101 // when using PHP versions lower than 4.0.5, uncomment the line before
\r
102 // and comment the call_user_func_array call
\r
103 //$this->call_using_array($action, $this->handler, $params);
\r
104 call_user_func_array(array(&$this->handler,'parse_' . $actionlc), $params);
\r
106 // redirect to plugin action if possible
\r
107 if (in_array('plugin', $this->actions) && $manager->pluginInstalled('NP_'.$action))
\r
108 $this->doAction('plugin('.$action.$this->pdelim.implode($this->pdelim,$params).')');
\r
110 echo '<b>DISALLOWED (' , $action , ')</b>';
\r
116 * Calls a method using an array of parameters (for use with PHP versions lower than 4.0.5)
\r
117 * ( = call_user_func_array() function )
\r
119 function call_using_array($methodname, &$handler, $paramarray) {
\r
121 $methodname = 'parse_' . $methodname;
\r
123 if (!method_exists($handler, $methodname)) {
\r
127 $command = 'call_user_func(array(&$handler,$methodname)';
\r
128 for ($i = 0; $i<count($paramarray); $i++)
\r
129 $command .= ',$paramarray[' . $i . ']';
\r
131 eval($command); // execute the correct method
\r
134 function setProperty($property, $value) {
\r
136 $manager->setParserProperty($property, $value);
\r
139 function getProperty($name) {
\r
141 return $manager->getParserProperty($name);
\r
148 * This class contains parse actions that are available in all ACTION classes
\r
149 * e.g. include, phpinclude, parsedinclude, skinfile, ...
\r
151 * It should never be used on it's own
\r
153 class BaseActions {
\r
155 // depth level for includes (max. level is 3)
\r
158 // array of evaluated conditions (true/false). The element at the end is the one for the most nested
\r
160 var $if_conditions;
\r
162 // at all times, can be evaluated to either true if the current block needs to be displayed. This
\r
163 // variable is used to decide to skip skinvars in parts that will never be outputted.
\r
164 var $if_currentlevel;
\r
166 // contains a search string with keywords that need to be highlighted. These get parsed into $aHighlight
\r
169 // array of keywords that need to be highlighted in search results (see the highlight()
\r
170 // and parseHighlight() methods)
\r
174 // reference to the parser object that is using this object as actions-handler
\r
179 function BaseActions() {
\r
182 // if nesting level
\r
183 $this->if_conditions = array(); // array on which condition values are pushed/popped
\r
184 $this->if_currentlevel = 1; // 1 = current level is displayed; 0 = current level not displayed
\r
187 $this->strHighlight = ''; // full highlight
\r
188 $this->aHighlight = array(); // parsed highlight
\r
192 // include file (no parsing of php)
\r
193 function parse_include($filename) {
\r
194 @readfile($this->getIncludeFileName($filename));
\r
197 // php-include file
\r
198 function parse_phpinclude($filename) {
\r
199 includephp($this->getIncludeFileName($filename));
\r
202 function parse_parsedinclude($filename) {
\r
203 // check current level
\r
204 if ($this->level > 3) return; // max. depth reached (avoid endless loop)
\r
205 $filename = $this->getIncludeFileName($filename);
\r
206 if (!file_exists($filename)) return '';
\r
208 $fsize = filesize($filename);
\r
210 // nothing to include
\r
214 $this->level = $this->level + 1;
\r
217 $fd = fopen ($filename, 'r');
\r
218 $contents = fread ($fd, $fsize);
\r
221 // parse file contents
\r
222 $this->parser->parse($contents);
\r
224 $this->level = $this->level - 1;
\r
228 * Returns the correct location of the file to be included, according to
\r
229 * parser properties
\r
231 * IF IncludeMode = 'skindir' => use skindir
\r
233 function getIncludeFileName($filename) {
\r
234 // leave absolute filenames and http urls as they are
\r
236 (substr($filename,0,1) == '/')
\r
237 || (substr($filename,0,7) == 'http://')
\r
238 || (substr($filename,0,6) == 'ftp://')
\r
242 $filename = PARSER::getProperty('IncludePrefix') . $filename;
\r
243 if (PARSER::getProperty('IncludeMode') == 'skindir') {
\r
245 return $DIR_SKINS . $filename;
\r
252 * Inserts an url relative to the skindir (useful when doing import/export)
\r
254 * e.g. <skinfile(default/myfile.sth)>
\r
256 function parse_skinfile($filename) {
\r
259 echo $CONF['SkinsURL'] . PARSER::getProperty('IncludePrefix') . $filename;
\r
263 * Sets a property for the parser
\r
265 function parse_set($property, $value) {
\r
266 PARSER::setProperty($property, $value);
\r
270 * Helper function: add if condition
\r
272 function _addIfCondition($condition) {
\r
274 array_push($this->if_conditions,$condition);
\r
276 $this->_updateTopIfCondition();
\r
281 function _updateTopIfCondition() {
\r
282 if (sizeof($this->if_conditions) == 0)
\r
283 $this->if_currentlevel = 1;
\r
285 $this->if_currentlevel = $this->if_conditions[sizeof($this->if_conditions) - 1];
\r
289 * returns the currently top if condition
\r
291 function _getTopIfCondition() {
\r
292 return $this->if_currentlevel;
\r
298 function parse_else() {
\r
299 if (sizeof($this->if_conditions) == 0) return;
\r
300 $old = $this->if_currentlevel;
\r
301 if (array_pop($this->if_conditions)) {
\r
303 $this->_addIfCondition(0);
\r
306 $this->_addIfCondition(1);
\r
311 * Ends a conditional if-block
\r
312 * see e.g. ifcat (BLOG), ifblogsetting (PAGEFACTORY)
\r
314 function parse_endif() {
\r
315 // we can only close what has been opened
\r
316 if (sizeof($this->if_conditions) == 0) return;
\r
318 if (array_pop($this->if_conditions)) {
\r
324 $this->_updateTopIfCondition();
\r
329 * Sets the search terms to be highlighted
\r
331 * @param $highlight
\r
332 * A series of search terms
\r
334 function setHighlight($highlight) {
\r
335 $this->strHighlight = $highlight;
\r
337 $this->aHighlight = parseHighlight($highlight);
\r
342 * Applies the highlight to the given piece of text
\r
345 * Data that needs to be highlighted
\r
346 * @see setHighlight
\r
348 function highlight(&$data) {
\r
349 if ($this->aHighlight)
\r
350 return highlight($data,$this->aHighlight,$this->template['SEARCH_HIGHLIGHT']);
\r