OSDN Git Service

ADD: 不足していたアクセス修飾子を追加
[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         // delimiters that can be used for skin/templatevars\r
37         public $delim;\r
38         \r
39         // parameter delimiter (to separate skinvar params)\r
40         public $pdelim;\r
41         \r
42         // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions\r
43         public $norestrictions;\r
44         \r
45         /**\r
46          * Parset::__construct()\r
47          * Creates a new parser object with the given allowed actions\r
48          * and the given handler\r
49          *\r
50          * @param $handler class object with functions for each action (reference)\r
51          * @param $delim optional delimiter\r
52          * @param $paramdelim optional parameterdelimiter\r
53          */\r
54         public function __construct( &$handler, $delim = '(<%|%>)', $pdelim = ',')\r
55         {\r
56                 $this->handler  = &$handler;\r
57                 $this->actions  =  $handler->getAvailableActions();\r
58                 $this->delim    =  $delim;\r
59                 $this->pdelim   =  $pdelim;\r
60                 $this->norestrictions = 0;      // set this to 1 to disable checking for allowedActions\r
61                 \r
62                 $handler->setParser($this);\r
63                 \r
64                 return;\r
65         }\r
66         \r
67         /**\r
68          * Parses the given contents and outputs it\r
69          */\r
70         public function parse(&$contents)\r
71         {\r
72                 /* escaping only pcre delimiter */\r
73                 $pcre = preg_replace('#\##', '#', $this->delim);\r
74                 \r
75                 $pieces = preg_split("#{$pcre}#", $contents);\r
76                 \r
77                 $maxidx = sizeof($pieces);\r
78                 for ( $idx = 0; $idx < $maxidx; $idx++ )\r
79                 {\r
80                         echo $pieces[$idx];\r
81                         $idx++;\r
82                         if ( $idx < $maxidx )\r
83                         {\r
84                                 $this->doAction($pieces[$idx]);\r
85                         }\r
86                 }\r
87                 return;\r
88         }\r
89 \r
90 \r
91         /**\r
92          * Parset::doAction()\r
93          * Called from the parser to handle an action\r
94          * \r
95          * @param       string  $action name of the action (e.g. blog, image ...)\r
96          * @return      void\r
97          */\r
98         public function doAction($action)\r
99         {\r
100                 global $manager, $CONF;\r
101 \r
102                 if ( !$action )\r
103                 {\r
104                         return;\r
105                 }\r
106                 \r
107                 // split into action name + arguments\r
108                 if ( i18n::strpos($action, '(') != FALSE )\r
109                 {\r
110                         $paramStartPos  = i18n::strpos($action, '(');\r
111                         $params                 = i18n::substr($action, $paramStartPos + 1, i18n::strlen($action) - $paramStartPos - 2);\r
112                         $action                 = i18n::substr($action, 0, $paramStartPos);\r
113                         $params                 = preg_split ('#' . preg_quote($this->pdelim, '#') . '#', $params);\r
114                         $params                 = array_map('trim', $params);\r
115                 }\r
116                 else\r
117                 {\r
118                         // no parameters\r
119                         $params = array();\r
120                 }\r
121                 \r
122                 $actionlc = strtolower($action);\r
123                 // skip execution of skinvars while inside an if condition which hides this part of the page\r
124                 $if_tags = array('else', 'elseif', 'endif', 'ifnot', 'elseifnot');\r
125                 if ( !$this->handler->getTopIfCondition()\r
126                   && !in_array($actionlc, $if_tags)\r
127                   && (i18n::substr($actionlc, 0, 2) != 'if') )\r
128                 {\r
129                         return;\r
130                 }\r
131                 \r
132                 if ( in_array($actionlc, $this->actions) || $this->norestrictions )\r
133                 {\r
134                         call_user_func_array(array(&$this->handler, "parse_{$actionlc}"), $params);\r
135                 }\r
136                 else\r
137                 {\r
138                         // redirect to plugin action if possible\r
139                         if ( in_array('plugin', $this->actions) && $manager->pluginInstalled("NP_{$action}") )\r
140                         {\r
141                                 $this->doAction('plugin(' . $action . $this->pdelim . implode($this->pdelim,$params) . ')');\r
142                         }\r
143                         else\r
144                         {\r
145                                 if ( $CONF['DebugVars']==true )\r
146                                 {\r
147                                         echo '&lt;%' , $action , '(', implode($this->pdelim, $params), ')%&gt;';\r
148                                 }\r
149                         }\r
150                 }\r
151                 return;\r
152         }\r
153         \r
154         /**\r
155          * Parser::setProperty()\r
156          * Set a property of the parser in the manager\r
157          * \r
158          * @static\r
159          * @param       string  $property       additional parser property (e.g. include prefix of the skin)\r
160          * @param       string  $value          new value\r
161          * @return      void\r
162          */\r
163         static public function setProperty($property, $value)\r
164         {\r
165                 global $manager;\r
166                 $manager->setParserProperty($property, $value);\r
167                 return;\r
168         }\r
169 \r
170         /**\r
171          * Parser::getProperty()\r
172          * Get a property of the parser from the manager\r
173          * \r
174          * @static\r
175          * @param       string  $name   name of the property\r
176          * @return      string  value of the property\r
177          */\r
178         static public function getProperty($name)\r
179         {\r
180                 global $manager;\r
181                 return $manager->getParserProperty($name);\r
182         }\r
183 }\r