OSDN Git Service

Merge branch 'skinnable-master' of sakamocchi@git.sourceforge.jp:/gitroot/nucleus...
[nucleus-jp/nucleus-next.git] / nucleus / libs / PARSER.php
1 <?php\r
2 /*\r
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
4  * Copyright (C) 2002-2009 The Nucleus Group\r
5  *\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
11  */\r
12 /**\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
16  */\r
17 \r
18 if ( !function_exists('requestVar') )\r
19 {\r
20         exit;\r
21 }\r
22 require_once dirname(__FILE__) . '/BaseActions.php';\r
23 \r
24 /**\r
25  * This is the parser class of Nucleus. It is used for various things\r
26  * (skin parsing, form generation, ...)\r
27  */\r
28 class Parser\r
29 {\r
30         // array with the names of all allowed actions\r
31         public $actions;\r
32         \r
33         // reference to actions handler\r
34         public $handler;\r
35         \r
36         // reference to an instance of Skin class\r
37         public $skin = NULL;\r
38         \r
39         // delimiters that can be used for skin/templatevars\r
40         public $delim;\r
41         \r
42         // parameter delimiter (to separate skinvar params)\r
43         public $pdelim;\r
44         \r
45         // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions\r
46         public $norestrictions;\r
47         \r
48         /**\r
49          * Parset::__construct()\r
50          * Creates a new parser object with the given allowed actions\r
51          * and the given handler\r
52          *\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
56          */\r
57         public function __construct( &$handler, $delim = '(<%|%>)', $pdelim = ',')\r
58         {\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
64                 \r
65                 $this->skin             = NULL;\r
66                 \r
67                 $handler->setParser($this);\r
68                 \r
69                 return;\r
70         }\r
71         \r
72         /**\r
73          * Parses the given contents and outputs it\r
74          */\r
75         public function parse(&$contents)\r
76         {\r
77                 /* escaping only pcre delimiter */\r
78                 $pcre = preg_replace('#\##', '#', $this->delim);\r
79                 \r
80                 $pieces = preg_split("#{$pcre}#", $contents);\r
81                 \r
82                 $maxidx = sizeof($pieces);\r
83                 for ( $idx = 0; $idx < $maxidx; $idx++ )\r
84                 {\r
85                         echo $pieces[$idx];\r
86                         $idx++;\r
87                         if ( $idx < $maxidx )\r
88                         {\r
89                                 $this->doAction($pieces[$idx]);\r
90                         }\r
91                 }\r
92                 return;\r
93         }\r
94 \r
95 \r
96         /**\r
97          * Parset::doAction()\r
98          * Called from the parser to handle an action\r
99          * \r
100          * @param       string  $action name of the action (e.g. blog, image ...)\r
101          * @return      void\r
102          */\r
103         public function doAction($action)\r
104         {\r
105                 global $manager, $CONF;\r
106 \r
107                 if ( !$action )\r
108                 {\r
109                         return;\r
110                 }\r
111                 \r
112                 // split into action name + arguments\r
113                 if ( i18n::strpos($action, '(') != FALSE )\r
114                 {\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
120                 }\r
121                 else\r
122                 {\r
123                         // no parameters\r
124                         $params = array();\r
125                 }\r
126                 \r
127                 $actionlc = strtolower($action);\r
128                 \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
134                 {\r
135                         return;\r
136                 }\r
137                 \r
138                 if ( in_array($actionlc, $this->actions) || $this->norestrictions )\r
139                 {\r
140                         call_user_func_array(array(&$this->handler, "parse_{$actionlc}"), $params);\r
141                 }\r
142                 else\r
143                 {\r
144                         // redirect to plugin action if possible\r
145                         if ( in_array('plugin', $this->actions) && $manager->pluginInstalled("NP_{$action}") )\r
146                         {\r
147                                 $this->doAction('plugin(' . $action . $this->pdelim . implode($this->pdelim,$params) . ')');\r
148                         }\r
149                         else\r
150                         {\r
151                                 if ( $CONF['DebugVars']==true )\r
152                                 {\r
153                                         echo '&lt;%' , $action , '(', implode($this->pdelim, $params), ')%&gt;';\r
154                                 }\r
155                         }\r
156                 }\r
157                 return;\r
158         }\r
159         \r
160         /**\r
161          * Parser::setSkin()\r
162          * Set the skin\r
163          * @param       object  $skin   an instance of Skin class\r
164          * @return      void\r
165          */\r
166         public function setSkin(&$skin)\r
167         {\r
168                 $this->skin = &$skin;\r
169                 return;\r
170         }\r
171         \r
172         /**\r
173          * Parser::setProperty()\r
174          * Set a property of the parser in the manager\r
175          * \r
176          * @static\r
177          * @param       string  $property       additional parser property (e.g. include prefix of the skin)\r
178          * @param       string  $value          new value\r
179          * @return      void\r
180          */\r
181         static public function setProperty($property, $value)\r
182         {\r
183                 global $manager;\r
184                 $manager->setParserProperty($property, $value);\r
185                 return;\r
186         }\r
187 \r
188         /**\r
189          * Parser::getProperty()\r
190          * Get a property of the parser from the manager\r
191          * \r
192          * @static\r
193          * @param       string  $name   name of the property\r
194          * @return      string  value of the property\r
195          */\r
196         static public function getProperty($name)\r
197         {\r
198                 global $manager;\r
199                 return $manager->getParserProperty($name);\r
200         }\r
201 }\r