3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2006 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 * @license http://nucleuscms.org/license.txt GNU General Public License
14 * @copyright Copyright (C) 2002-2006 The Nucleus Group
15 * @version $Id: PARSER.php,v 1.5 2006-07-12 07:11:47 kimitake Exp $
16 * $NucleusJP: PARSER.php,v 1.4 2005/08/13 07:33:02 kimitake Exp $
19 // temporary: dirt way to separe class BaseActions from PARSER
20 require_once $DIR_LIBS . 'BaseActions.php';
22 * This is the parser class of Nucleus. It is used for various things (skin parsing,
23 * form generation, ...)
27 // array with the names of all allowed actions
30 // reference to actions handler
33 // delimiters that can be used for skin/templatevars
36 // parameter delimiter (to separate skinvar params)
39 // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions
43 * Creates a new parser object with the given allowed actions
44 * and the given handler
46 * @param $allowedActions array
47 * @param $handler class object with functions for each action (reference)
48 * @param $delim optional delimiter
49 * @param $paramdelim optional parameterdelimiter
51 function PARSER($allowedActions, &$handler, $delim = '(<%|%>)', $pdelim = ',') {
52 $this->actions = $allowedActions;
53 $this->handler =& $handler;
54 $this->delim = $delim;
55 $this->pdelim = $pdelim;
56 $this->norestrictions = 0; // set this to 1 to disable checking for allowedActions
60 * Parses the given contents and outputs it
62 function parse(&$contents) {
64 $pieces = preg_split('/'.$this->delim.'/',$contents);
66 $maxidx = sizeof($pieces);
67 for ($idx = 0; $idx < $maxidx; $idx++) {
71 $this->doAction($pieces[$idx]);
80 function doAction($action) {
85 // split into action name + arguments
86 if (strstr($action,'(')) {
87 $paramStartPos = strpos($action, '(');
88 $params = substr($action, $paramStartPos + 1, strlen($action) - $paramStartPos - 2);
89 $action = substr($action, 0, $paramStartPos);
90 $params = explode ($this->pdelim, $params);
93 // for PHP versions lower than 4.0.6:
94 // - add // before '$params = ...'
95 // - remove // before 'foreach'
96 $params = array_map('trim',$params);
97 // foreach ($params as $key => $value) { $params[$key] = trim($value); }
103 $actionlc = strtolower($action);
105 // skip execution of skinvars while inside an if condition which hides this part of the page
106 if (!$this->handler->if_currentlevel && ($actionlc != 'else') && ($actionlc != 'elseif') && ($actionlc != 'endif') && ($actionlc != 'ifnot') && ($actionlc != 'elseifnot') && (substr($actionlc,0,2) != 'if'))
109 if (in_array($actionlc, $this->actions) || $this->norestrictions ) {
110 // when using PHP versions lower than 4.0.5, uncomment the line before
111 // and comment the call_user_func_array call
112 //$this->call_using_array($action, $this->handler, $params);
113 call_user_func_array(array(&$this->handler,'parse_' . $actionlc), $params);
115 // redirect to plugin action if possible
116 if (in_array('plugin', $this->actions) && $manager->pluginInstalled('NP_'.$action))
117 $this->doAction('plugin('.$action.$this->pdelim.implode($this->pdelim,$params).')');
119 echo '<%' , $action , '(', implode($this->pdelim, $params), ')%>';
125 * Calls a method using an array of parameters (for use with PHP versions lower than 4.0.5)
126 * ( = call_user_func_array() function )
128 function call_using_array($methodname, &$handler, $paramarray) {
130 $methodname = 'parse_' . $methodname;
132 if (!method_exists($handler, $methodname)) {
136 $command = 'call_user_func(array(&$handler,$methodname)';
137 for ($i = 0; $i<count($paramarray); $i++)
138 $command .= ',$paramarray[' . $i . ']';
140 eval($command); // execute the correct method
143 function setProperty($property, $value) {
145 $manager->setParserProperty($property, $value);
148 function getProperty($name) {
150 return $manager->getParserProperty($name);