OSDN Git Service

MERGE: リビジョン1721をマージ。主要なクラス名をUpperCamelCaseに統一。
[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 1529 2011-06-21 11:07:14Z sakamocchi $\r
16  */\r
17 \r
18 if ( !function_exists('requestVar') ) exit;\r
19 require_once dirname(__FILE__) . '/BaseActions.php';\r
20 \r
21 /**\r
22  * This is the parser class of Nucleus. It is used for various things (skin parsing,\r
23  * form generation, ...)\r
24  */\r
25 class Parser\r
26 {\r
27         // array with the names of all allowed actions\r
28         var $actions;\r
29 \r
30         // reference to actions handler\r
31         var $handler;\r
32 \r
33         // delimiters that can be used for skin/templatevars\r
34         var $delim;\r
35 \r
36         // parameter delimiter (to separate skinvar params)\r
37         var $pdelim;\r
38 \r
39         // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions\r
40         var $norestrictions;\r
41 \r
42         /**\r
43          * Creates a new parser object with the given allowed actions\r
44          * and the given handler\r
45          *\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
50          */\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
57         }\r
58 \r
59         /**\r
60          * Parses the given contents and outputs it\r
61          */\r
62         function parse(&$contents) {\r
63 \r
64                 $pieces = preg_split('/'.$this->delim.'/',$contents);\r
65 \r
66                 $maxidx = sizeof($pieces);\r
67                 for ($idx = 0; $idx < $maxidx; $idx++) {\r
68                         echo $pieces[$idx];\r
69                         $idx++;\r
70                         if ($idx < $maxidx) {\r
71                                 $this->doAction($pieces[$idx]);\r
72                         }\r
73                 }\r
74         }\r
75 \r
76 \r
77         /**\r
78           * Called from the parser to handle an action\r
79           * \r
80           * @param $action name of the action (e.g. blog, image ...)\r
81           */\r
82         function doAction($action) {\r
83                 global $manager, $CONF;\r
84 \r
85                 if (!$action) return;\r
86 \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
93 \r
94                         // trim parameters\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
100                 } else {\r
101                         // no parameters\r
102                         $params = array();\r
103                 }\r
104 \r
105                 $actionlc = strtolower($action);\r
106 \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
109                         return;\r
110 \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
116                 } else {\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
120                         } else {\r
121                                 if ($CONF['DebugVars']==true) {\r
122                                 echo '&lt;%' , $action , '(', implode($this->pdelim, $params), ')%&gt;';\r
123                 }\r
124                         }\r
125                         \r
126                 }\r
127 \r
128         }\r
129 \r
130         /**\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
133           */\r
134         function call_using_array($methodname, &$handler, $paramarray) {\r
135 \r
136                 $methodname = 'parse_' . $methodname;\r
137 \r
138                 if (!method_exists($handler, $methodname)) {\r
139                         return;\r
140                 }\r
141 \r
142                 $command = 'call_user_func(array(&$handler,$methodname)';\r
143                 for ($i = 0; $i<count($paramarray); $i++)\r
144                         $command .= ',$paramarray[' . $i . ']';\r
145                 $command .= ');';\r
146                 eval($command); // execute the correct method\r
147         }\r
148 \r
149         /**\r
150          * Set a property of the parser in the manager\r
151          * \r
152          * @param $property additional parser property (e.g. include prefix of the skin)\r
153          * @param $value new value\r
154          */\r
155         function setProperty($property, $value) {\r
156                 global $manager;\r
157                 $manager->setParserProperty($property, $value);\r
158         }\r
159 \r
160         /**\r
161          * Get a property of the parser from the manager\r
162          * \r
163          * @param $name name of the property\r
164          */\r
165         function getProperty($name) {\r
166                 global $manager;\r
167                 return $manager->getParserProperty($name);\r
168         }\r
169 \r
170 \r
171 }\r
172 \r
173 ?>\r