3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2007 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-2007 The Nucleus Group
15 * @version $Id: PARSER.php,v 1.8 2008-02-08 09:31:22 kimitake Exp $
16 * $NucleusJP: PARSER.php,v 1.7.2.1 2007/09/05 07:35:59 kimitake Exp $
19 if ( !function_exists('requestVar') ) exit;
20 require_once dirname(__FILE__) . '/BaseActions.php';
23 * This is the parser class of Nucleus. It is used for various things (skin parsing,
24 * form generation, ...)
28 // array with the names of all allowed actions
31 // reference to actions handler
34 // delimiters that can be used for skin/templatevars
37 // parameter delimiter (to separate skinvar params)
40 // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions
44 * Creates a new parser object with the given allowed actions
45 * and the given handler
47 * @param $allowedActions array
48 * @param $handler class object with functions for each action (reference)
49 * @param $delim optional delimiter
50 * @param $paramdelim optional parameterdelimiter
52 function PARSER($allowedActions, &$handler, $delim = '(<%|%>)', $pdelim = ',') {
53 $this->actions = $allowedActions;
54 $this->handler =& $handler;
55 $this->delim = $delim;
56 $this->pdelim = $pdelim;
57 $this->norestrictions = 0; // set this to 1 to disable checking for allowedActions
61 * Parses the given contents and outputs it
63 function parse(&$contents) {
65 $pieces = preg_split('/'.$this->delim.'/',$contents);
67 $maxidx = sizeof($pieces);
68 for ($idx = 0; $idx < $maxidx; $idx++) {
72 $this->doAction($pieces[$idx]);
81 function doAction($action) {
86 // split into action name + arguments
87 if (strstr($action,'(')) {
88 $paramStartPos = strpos($action, '(');
89 $params = substr($action, $paramStartPos + 1, strlen($action) - $paramStartPos - 2);
90 $action = substr($action, 0, $paramStartPos);
91 $params = explode ($this->pdelim, $params);
94 // for PHP versions lower than 4.0.6:
95 // - add // before '$params = ...'
96 // - remove // before 'foreach'
97 $params = array_map('trim',$params);
98 // foreach ($params as $key => $value) { $params[$key] = trim($value); }
104 $actionlc = strtolower($action);
106 // skip execution of skinvars while inside an if condition which hides this part of the page
107 if (!$this->handler->if_currentlevel && ($actionlc != 'else') && ($actionlc != 'elseif') && ($actionlc != 'endif') && ($actionlc != 'ifnot') && ($actionlc != 'elseifnot') && (substr($actionlc,0,2) != 'if'))
110 if (in_array($actionlc, $this->actions) || $this->norestrictions ) {
111 // when using PHP versions lower than 4.0.5, uncomment the line before
112 // and comment the call_user_func_array call
113 //$this->call_using_array($action, $this->handler, $params);
114 call_user_func_array(array(&$this->handler,'parse_' . $actionlc), $params);
116 // redirect to plugin action if possible
117 if (in_array('plugin', $this->actions) && $manager->pluginInstalled('NP_'.$action))
118 $this->doAction('plugin('.$action.$this->pdelim.implode($this->pdelim,$params).')');
120 echo '<%' , $action , '(', implode($this->pdelim, $params), ')%>';
126 * Calls a method using an array of parameters (for use with PHP versions lower than 4.0.5)
127 * ( = call_user_func_array() function )
129 function call_using_array($methodname, &$handler, $paramarray) {
131 $methodname = 'parse_' . $methodname;
133 if (!method_exists($handler, $methodname)) {
137 $command = 'call_user_func(array(&$handler,$methodname)';
138 for ($i = 0; $i<count($paramarray); $i++)
139 $command .= ',$paramarray[' . $i . ']';
141 eval($command); // execute the correct method
144 function setProperty($property, $value) {
146 $manager->setParserProperty($property, $value);
149 function getProperty($name) {
151 return $manager->getParserProperty($name);