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 1757 2012-04-15 09:02:32Z 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
67 $handler->setParser($this);
73 * Parses the given contents and outputs it
75 public function parse(&$contents)
77 /* escaping only pcre delimiter */
78 $pcre = preg_replace('#\##', '#', $this->delim);
80 $pieces = preg_split("#{$pcre}#", $contents);
82 $maxidx = sizeof($pieces);
83 for ( $idx = 0; $idx < $maxidx; $idx++ )
89 $this->doAction($pieces[$idx]);
98 * Called from the parser to handle an action
100 * @param string $action name of the action (e.g. blog, image ...)
103 public function doAction($action)
105 global $manager, $CONF;
112 // split into action name + arguments
113 if ( i18n::strpos($action, '(') != FALSE )
115 $paramStartPos = i18n::strpos($action, '(');
116 $params = i18n::substr($action, $paramStartPos + 1, i18n::strlen($action) - $paramStartPos - 2);
117 $action = i18n::substr($action, 0, $paramStartPos);
118 $params = preg_split ('#' . preg_quote($this->pdelim, '#') . '#', $params);
119 $params = array_map('trim', $params);
127 $actionlc = strtolower($action);
129 // skip execution of skinvars while inside an if condition which hides this part of the page
130 $if_tags = array('else', 'elseif', 'endif', 'ifnot', 'elseifnot');
131 if ( !$this->handler->getTopIfCondition()
132 && !in_array($actionlc, $if_tags)
133 && (i18n::substr($actionlc, 0, 2) != 'if') )
138 if ( in_array($actionlc, $this->actions) || $this->norestrictions )
140 call_user_func_array(array(&$this->handler, "parse_{$actionlc}"), $params);
144 // redirect to plugin action if possible
145 if ( in_array('plugin', $this->actions) && $manager->pluginInstalled("NP_{$action}") )
147 $this->doAction('plugin(' . $action . $this->pdelim . implode($this->pdelim,$params) . ')');
151 if ( $CONF['DebugVars']==true )
153 echo '<%' , $action , '(', implode($this->pdelim, $params), ')%>';
163 * @param object $skin an instance of Skin class
166 public function setSkin(&$skin)
168 $this->skin = &$skin;
173 * Parser::setProperty()
174 * Set a property of the parser in the manager
177 * @param string $property additional parser property (e.g. include prefix of the skin)
178 * @param string $value new value
181 static public function setProperty($property, $value)
184 $manager->setParserProperty($property, $value);
189 * Parser::getProperty()
190 * Get a property of the parser from the manager
193 * @param string $name name of the property
194 * @return string value of the property
196 static public function getProperty($name)
199 return $manager->getParserProperty($name);