OSDN Git Service

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