OSDN Git Service

Subversion由来のタグを削除
[nucleus-jp/nucleus-jp-ancient.git] / nucleus / libs / PARSER.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2012 The Nucleus Group
5  *
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)
11  */
12
13 if ( !function_exists('requestVar') ) exit;\r
14 require_once dirname(__FILE__) . '/BaseActions.php';\r
15 \r
16 /**\r
17  * This is the parser class of Nucleus. It is used for various things (skin parsing,\r
18  * form generation, ...)\r
19  */\r
20 class PARSER {\r
21 \r
22         // array with the names of all allowed actions\r
23         var $actions;\r
24 \r
25         // reference to actions handler\r
26         var $handler;\r
27 \r
28         // delimiters that can be used for skin/templatevars\r
29         var $delim;\r
30 \r
31         // parameter delimiter (to separate skinvar params)\r
32         var $pdelim;\r
33 \r
34         // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions\r
35         var $norestrictions;\r
36 \r
37         /**\r
38          * Creates a new parser object with the given allowed actions\r
39          * and the given handler\r
40          *\r
41          * @param $allowedActions array\r
42          * @param $handler class object with functions for each action (reference)\r
43          * @param $delim optional delimiter\r
44          * @param $paramdelim optional parameterdelimiter\r
45          */\r
46         function PARSER($allowedActions, &$handler, $delim = '(<%|%>)', $pdelim = ',') {\r
47                 $this->actions = $allowedActions;\r
48                 $this->handler =& $handler;\r
49                 $this->delim = $delim;\r
50                 $this->pdelim = $pdelim;\r
51                 $this->norestrictions = 0;      // set this to 1 to disable checking for allowedActions\r
52         }\r
53 \r
54         /**\r
55          * Parses the given contents and outputs it\r
56          */\r
57         function parse(&$contents) {\r
58 \r
59                 $pieces = preg_split('/'.$this->delim.'/',$contents);\r
60 \r
61                 $maxidx = sizeof($pieces);\r
62                 for ($idx = 0; $idx < $maxidx; $idx++) {\r
63                         echo $pieces[$idx];\r
64                         $idx++;\r
65                         if ($idx < $maxidx) {\r
66                                 $this->doAction($pieces[$idx]);\r
67                         }\r
68                 }\r
69         }\r
70 \r
71 \r
72         /**\r
73           * handle an action\r
74           */\r
75         function doAction($action) {\r
76                 global $manager, $CONF;\r
77 \r
78                 if (!$action) return;\r
79 \r
80                 // split into action name + arguments\r
81                 if (strstr($action,'(')) {\r
82                         $paramStartPos = strpos($action, '(');\r
83                         $params = substr($action, $paramStartPos + 1, strlen($action) - $paramStartPos - 2);\r
84                         $action = substr($action, 0, $paramStartPos);\r
85                         $params = explode ($this->pdelim, $params);\r
86 \r
87                         // trim parameters\r
88                         // for PHP versions lower than 4.0.6:\r
89                         //   - add // before '$params = ...'\r
90                         //   - remove // before 'foreach'\r
91                         $params = array_map('trim',$params);\r
92                         // foreach ($params as $key => $value) { $params[$key] = trim($value); }\r
93                 } else {\r
94                         // no parameters\r
95                         $params = array();\r
96                 }\r
97 \r
98                 $actionlc = strtolower($action);\r
99 \r
100                 // skip execution of skinvars while inside an if condition which hides this part of the page\r
101                 if (!$this->handler->if_currentlevel && ($actionlc != 'else') && ($actionlc != 'elseif') && ($actionlc != 'endif') && ($actionlc != 'ifnot') && ($actionlc != 'elseifnot') && (substr($actionlc,0,2) != 'if'))\r
102                         return;\r
103 \r
104                 if (in_array($actionlc, $this->actions) || $this->norestrictions ) {\r
105                         // when using PHP versions lower than 4.0.5, uncomment the line before\r
106                         // and comment the call_user_func_array call\r
107                         //$this->call_using_array($action, $this->handler, $params);\r
108                         call_user_func_array(array($this->handler, 'parse_' . $actionlc), $params);\r
109                 } else {\r
110                         // redirect to plugin action if possible\r
111                         if (in_array('plugin', $this->actions) && $manager->pluginInstalled('NP_' . $action)) {\r
112                                 $this->doAction('plugin('.$action.$this->pdelim.implode($this->pdelim,$params).')');\r
113                         } else {\r
114                                 if ($CONF['DebugVars']==true) {\r
115                                         echo '&lt;%' , $action , '(', implode($this->pdelim, $params), ')%&gt;';\r
116                                 }\r
117                         }\r
118 \r
119                 }\r
120 \r
121         }\r
122 \r
123         /**\r
124           * Calls a method using an array of parameters (for use with PHP versions lower than 4.0.5)\r
125           * ( = call_user_func_array() function )\r
126           */\r
127         function call_using_array($methodname, &$handler, $paramarray) {\r
128 \r
129                 $methodname = 'parse_' . $methodname;\r
130 \r
131                 if (!method_exists($handler, $methodname)) {\r
132                         return;\r
133                 }\r
134 \r
135                 $command = 'call_user_func(array($handler,$methodname)';\r
136                 for ($i = 0; $i<count($paramarray); $i++)\r
137                         $command .= ',$paramarray[' . $i . ']';\r
138                 $command .= ');';\r
139                 eval($command); // execute the correct method\r
140         }\r
141 \r
142         function setProperty($property, $value) {\r
143                 global $manager;\r
144                 $manager->setParserProperty($property, $value);\r
145         }\r
146 \r
147         function getProperty($name) {\r
148                 global $manager;\r
149                 return $manager->getParserProperty($name);\r
150         }\r
151 \r
152 \r
153 }\r
154 \r
155 ?>