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 1757 2012-04-15 09:02:32Z sakamocchi $
\r
18 if ( !function_exists('requestVar') )
\r
22 require_once dirname(__FILE__) . '/BaseActions.php';
\r
25 * This is the parser class of Nucleus. It is used for various things
\r
26 * (skin parsing, form generation, ...)
\r
30 // array with the names of all allowed actions
\r
33 // reference to actions handler
\r
36 // reference to an instance of Skin class
\r
37 public $skin = NULL;
\r
39 // delimiters that can be used for skin/templatevars
\r
42 // parameter delimiter (to separate skinvar params)
\r
45 // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions
\r
46 public $norestrictions;
\r
49 * Parset::__construct()
\r
50 * Creates a new parser object with the given allowed actions
\r
51 * and the given handler
\r
53 * @param $handler class object with functions for each action (reference)
\r
54 * @param $delim optional delimiter
\r
55 * @param $paramdelim optional parameterdelimiter
\r
57 public function __construct( &$handler, $delim = '(<%|%>)', $pdelim = ',')
\r
59 $this->handler = &$handler;
\r
60 $this->actions = $handler->getAvailableActions();
\r
61 $this->delim = $delim;
\r
62 $this->pdelim = $pdelim;
\r
63 $this->norestrictions = 0; // set this to 1 to disable checking for allowedActions
\r
67 $handler->setParser($this);
\r
73 * Parses the given contents and outputs it
\r
75 public function parse(&$contents)
\r
77 /* escaping only pcre delimiter */
\r
78 $pcre = preg_replace('#\##', '#', $this->delim);
\r
80 $pieces = preg_split("#{$pcre}#", $contents);
\r
82 $maxidx = sizeof($pieces);
\r
83 for ( $idx = 0; $idx < $maxidx; $idx++ )
\r
87 if ( $idx < $maxidx )
\r
89 $this->doAction($pieces[$idx]);
\r
97 * Parset::doAction()
\r
98 * Called from the parser to handle an action
\r
100 * @param string $action name of the action (e.g. blog, image ...)
\r
103 public function doAction($action)
\r
105 global $manager, $CONF;
\r
112 // split into action name + arguments
\r
113 if ( i18n::strpos($action, '(') != FALSE )
\r
115 $paramStartPos = i18n::strpos($action, '(');
\r
116 $params = i18n::substr($action, $paramStartPos + 1, i18n::strlen($action) - $paramStartPos - 2);
\r
117 $action = i18n::substr($action, 0, $paramStartPos);
\r
118 $params = preg_split ('#' . preg_quote($this->pdelim, '#') . '#', $params);
\r
119 $params = array_map('trim', $params);
\r
127 $actionlc = strtolower($action);
\r
129 // skip execution of skinvars while inside an if condition which hides this part of the page
\r
130 $if_tags = array('else', 'elseif', 'endif', 'ifnot', 'elseifnot');
\r
131 if ( !$this->handler->getTopIfCondition()
\r
132 && !in_array($actionlc, $if_tags)
\r
133 && (i18n::substr($actionlc, 0, 2) != 'if') )
\r
138 if ( in_array($actionlc, $this->actions) || $this->norestrictions )
\r
140 call_user_func_array(array(&$this->handler, "parse_{$actionlc}"), $params);
\r
144 // redirect to plugin action if possible
\r
145 if ( in_array('plugin', $this->actions) && $manager->pluginInstalled("NP_{$action}") )
\r
147 $this->doAction('plugin(' . $action . $this->pdelim . implode($this->pdelim,$params) . ')');
\r
151 if ( $CONF['DebugVars']==true )
\r
153 echo '<%' , $action , '(', implode($this->pdelim, $params), ')%>';
\r
161 * Parser::setSkin()
\r
163 * @param object $skin an instance of Skin class
\r
166 public function setSkin(&$skin)
\r
168 $this->skin = &$skin;
\r
173 * Parser::setProperty()
\r
174 * Set a property of the parser in the manager
\r
177 * @param string $property additional parser property (e.g. include prefix of the skin)
\r
178 * @param string $value new value
\r
181 static public function setProperty($property, $value)
\r
184 $manager->setParserProperty($property, $value);
\r
189 * Parser::getProperty()
\r
190 * Get a property of the parser from the manager
\r
193 * @param string $name name of the property
\r
194 * @return string value of the property
\r
196 static public function getProperty($name)
\r
199 return $manager->getParserProperty($name);
\r