3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2009 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
13 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
14 * @copyright Copyright (C) 2002-2009 The Nucleus Group
\r
15 * @version $Id: PARSER.php 1529 2011-06-21 11:07:14Z sakamocchi $
\r
18 if ( !function_exists('requestVar') ) exit;
\r
19 require_once dirname(__FILE__) . '/BaseActions.php';
\r
22 * This is the parser class of Nucleus. It is used for various things (skin parsing,
\r
23 * form generation, ...)
\r
27 // array with the names of all allowed actions
\r
30 // reference to actions handler
\r
33 // delimiters that can be used for skin/templatevars
\r
36 // parameter delimiter (to separate skinvar params)
\r
39 // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions
\r
40 var $norestrictions;
\r
43 * Creates a new parser object with the given allowed actions
\r
44 * and the given handler
\r
46 * @param $allowedActions array
\r
47 * @param $handler class object with functions for each action (reference)
\r
48 * @param $delim optional delimiter
\r
49 * @param $paramdelim optional parameterdelimiter
\r
51 function PARSER($allowedActions, &$handler, $delim = '(<%|%>)', $pdelim = ',') {
\r
52 $this->actions = $allowedActions;
\r
53 $this->handler =& $handler;
\r
54 $this->delim = $delim;
\r
55 $this->pdelim = $pdelim;
\r
56 $this->norestrictions = 0; // set this to 1 to disable checking for allowedActions
\r
60 * Parses the given contents and outputs it
\r
62 function parse(&$contents) {
\r
64 $pieces = preg_split('/'.$this->delim.'/',$contents);
\r
66 $maxidx = sizeof($pieces);
\r
67 for ($idx = 0; $idx < $maxidx; $idx++) {
\r
70 if ($idx < $maxidx) {
\r
71 $this->doAction($pieces[$idx]);
\r
78 * Called from the parser to handle an action
\r
80 * @param $action name of the action (e.g. blog, image ...)
\r
82 function doAction($action) {
\r
83 global $manager, $CONF;
\r
85 if (!$action) return;
\r
87 // split into action name + arguments
\r
88 if (strstr($action,'(')) {
\r
89 $paramStartPos = i18n::strpos($action, '(');
\r
90 $params = i18n::substr($action, $paramStartPos + 1, i18n::strlen($action) - $paramStartPos - 2);
\r
91 $action = i18n::substr($action, 0, $paramStartPos);
\r
92 $params = preg_split ('#' . $this->pdelim . '#', $params);
\r
95 // for PHP versions lower than 4.0.6:
\r
96 // - add // before '$params = ...'
\r
97 // - remove // before 'foreach'
\r
98 $params = array_map('trim',$params);
\r
99 // foreach ($params as $key => $value) { $params[$key] = trim($value); }
\r
105 $actionlc = strtolower($action);
\r
107 // skip execution of skinvars while inside an if condition which hides this part of the page
\r
108 if (!$this->handler->if_currentlevel && ($actionlc != 'else') && ($actionlc != 'elseif') && ($actionlc != 'endif') && ($actionlc != 'ifnot') && ($actionlc != 'elseifnot') && (i18n::substr($actionlc,0,2) != 'if'))
\r
111 if (in_array($actionlc, $this->actions) || $this->norestrictions ) {
\r
112 // when using PHP versions lower than 4.0.5, uncomment the line before
\r
113 // and comment the call_user_func_array call
\r
114 //$this->call_using_array($action, $this->handler, $params);
\r
115 call_user_func_array(array(&$this->handler,'parse_' . $actionlc), $params);
\r
117 // redirect to plugin action if possible
\r
118 if (in_array('plugin', $this->actions) && $manager->pluginInstalled('NP_'.$action)) {
\r
119 $this->doAction('plugin('.$action.$this->pdelim.implode($this->pdelim,$params).')');
\r
121 if ($CONF['DebugVars']==true) {
\r
122 echo '<%' , $action , '(', implode($this->pdelim, $params), ')%>';
\r
131 * Calls a method using an array of parameters (for use with PHP versions lower than 4.0.5)
\r
132 * ( = call_user_func_array() function )
\r
134 function call_using_array($methodname, &$handler, $paramarray) {
\r
136 $methodname = 'parse_' . $methodname;
\r
138 if (!method_exists($handler, $methodname)) {
\r
142 $command = 'call_user_func(array(&$handler,$methodname)';
\r
143 for ($i = 0; $i<count($paramarray); $i++)
\r
144 $command .= ',$paramarray[' . $i . ']';
\r
146 eval($command); // execute the correct method
\r
150 * Set a property of the parser in the manager
\r
152 * @param $property additional parser property (e.g. include prefix of the skin)
\r
153 * @param $value new value
\r
155 function setProperty($property, $value) {
\r
157 $manager->setParserProperty($property, $value);
\r
161 * Get a property of the parser from the manager
\r
163 * @param $name name of the property
\r
165 function getProperty($name) {
\r
167 return $manager->getParserProperty($name);
\r