OSDN Git Service

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