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 * @license http://nucleuscms.org/license.txt GNU General Public License
14 * @copyright Copyright (C) 2002-2009 The Nucleus Group
15 * @version $Id: PARSER.php 1879 2012-06-17 07:45:09Z sakamocchi $
18 if ( !function_exists('requestVar') )
22 require_once dirname(__FILE__) . '/BaseActions.php';
25 * This is the parser class of Nucleus. It is used for various things
26 * (skin parsing, form generation, ...)
30 // array with the names of all allowed actions
33 // reference to actions handler
36 // reference to an instance of Skin class
39 // delimiters that can be used for skin/templatevars
42 // parameter delimiter (to separate skinvar params)
45 // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions
46 public $norestrictions;
49 * Parset::__construct()
50 * Creates a new parser object with the given allowed actions
51 * and the given handler
53 * @param $handler class object with functions for each action (reference)
54 * @param $delim optional delimiter
55 * @param $paramdelim optional parameterdelimiter
57 public function __construct( &$handler, $delim = '(<%|%>)', $pdelim = ',')
59 $this->handler = &$handler;
60 $this->actions = $handler->getAvailableActions();
61 $this->delim = $delim;
62 $this->pdelim = $pdelim;
63 $this->norestrictions = 0; // set this to 1 to disable checking for allowedActions
65 $handler->setParser($this);
71 * Parses the given contents and outputs it
73 public function parse(&$contents)
75 /* escaping only pcre delimiter */
76 $pcre = preg_replace('#\##', '#', $this->delim);
78 $pieces = preg_split("#{$pcre}#", $contents);
80 $maxidx = sizeof($pieces);
81 for ( $idx = 0; $idx < $maxidx; $idx++ )
87 $this->doAction($pieces[$idx]);
96 * Called from the parser to handle an action
98 * @param string $action name of the action (e.g. blog, image ...)
101 public function doAction($action)
103 global $manager, $CONF;
110 // split into action name + arguments
111 if ( i18n::strpos($action, '(') != FALSE )
113 $paramStartPos = i18n::strpos($action, '(');
114 $params = i18n::substr($action, $paramStartPos + 1, i18n::strlen($action) - $paramStartPos - 2);
115 $action = i18n::substr($action, 0, $paramStartPos);
116 $params = preg_split ('#' . preg_quote($this->pdelim, '#') . '#', $params);
117 $params = array_map('trim', $params);
125 $actionlc = strtolower($action);
127 // skip execution of skinvars while inside an if condition which hides this part of the page
128 $if_tags = array('else', 'elseif', 'endif', 'ifnot', 'elseifnot');
129 if ( !$this->handler->getTopIfCondition()
130 && !in_array($actionlc, $if_tags)
131 && (i18n::substr($actionlc, 0, 2) != 'if') )
136 if ( in_array($actionlc, $this->actions) || $this->norestrictions )
138 call_user_func_array(array($this->handler, "parse_{$actionlc}"), $params);
142 // redirect to plugin action if possible
143 if ( in_array('plugin', $this->actions) && $manager->pluginInstalled("NP_{$action}") )
145 $this->doAction('plugin(' . $action . $this->pdelim . implode($this->pdelim,$params) . ')');
149 if ( $CONF['DebugVars']==true )
151 echo '<%' , $action , '(', implode($this->pdelim, $params), ')%>';
161 * @param object $skin an instance of Skin class
164 public function setSkin(&$skin)
166 $this->skin = &$skin;
171 * Parser::setProperty()
172 * Set a property of the parser in the manager
175 * @param string $property additional parser property (e.g. include prefix of the skin)
176 * @param string $value new value
179 static public function setProperty($property, $value)
182 $manager->setParserProperty($property, $value);
187 * Parser::getProperty()
188 * Get a property of the parser from the manager
191 * @param string $name name of the property
192 * @return string value of the property
194 static public function getProperty($name)
197 return $manager->getParserProperty($name);