OSDN Git Service

FIX:変数名の誤記を修正
[nucleus-jp/nucleus-next.git] / 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: PARSER.php 1757 2012-04-15 09:02:32Z sakamocchi $
16  */
17
18 if ( !function_exists('requestVar') )
19 {
20         exit;
21 }
22 require_once dirname(__FILE__) . '/BaseActions.php';
23
24 /**
25  * This is the parser class of Nucleus. It is used for various things
26  * (skin parsing, form generation, ...)
27  */
28 class Parser
29 {
30         // array with the names of all allowed actions
31         public $actions;
32         
33         // reference to actions handler
34         public $handler;
35         
36         // reference to an instance of Skin class
37         public $skin = NULL;
38         
39         // delimiters that can be used for skin/templatevars
40         public $delim;
41         
42         // parameter delimiter (to separate skinvar params)
43         public $pdelim;
44         
45         // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions
46         public $norestrictions;
47         
48         /**
49          * Parset::__construct()
50          * Creates a new parser object with the given allowed actions
51          * and the given handler
52          *
53          * @param $handler class object with functions for each action (reference)
54          * @param $delim optional delimiter
55          * @param $paramdelim optional parameterdelimiter
56          */
57         public function __construct( &$handler, $delim = '(<%|%>)', $pdelim = ',')
58         {
59                 $this->handler  = &$handler;
60                 $this->actions  =  $handler->getAvailableActions();
61                 $this->delim    =  $delim;
62                 $this->pdelim   =  $pdelim;
63                 $this->norestrictions = 0;      // set this to 1 to disable checking for allowedActions
64                 
65                 $this->skin             = NULL;
66                 
67                 $handler->setParser($this);
68                 
69                 return;
70         }
71         
72         /**
73          * Parses the given contents and outputs it
74          */
75         public function parse(&$contents)
76         {
77                 /* escaping only pcre delimiter */
78                 $pcre = preg_replace('#\##', '#', $this->delim);
79                 
80                 $pieces = preg_split("#{$pcre}#", $contents);
81                 
82                 $maxidx = sizeof($pieces);
83                 for ( $idx = 0; $idx < $maxidx; $idx++ )
84                 {
85                         echo $pieces[$idx];
86                         $idx++;
87                         if ( $idx < $maxidx )
88                         {
89                                 $this->doAction($pieces[$idx]);
90                         }
91                 }
92                 return;
93         }
94
95
96         /**
97          * Parset::doAction()
98          * Called from the parser to handle an action
99          * 
100          * @param       string  $action name of the action (e.g. blog, image ...)
101          * @return      void
102          */
103         public function doAction($action)
104         {
105                 global $manager, $CONF;
106
107                 if ( !$action )
108                 {
109                         return;
110                 }
111                 
112                 // split into action name + arguments
113                 if ( i18n::strpos($action, '(') != FALSE )
114                 {
115                         $paramStartPos  = i18n::strpos($action, '(');
116                         $params                 = i18n::substr($action, $paramStartPos + 1, i18n::strlen($action) - $paramStartPos - 2);
117                         $action                 = i18n::substr($action, 0, $paramStartPos);
118                         $params                 = preg_split ('#' . preg_quote($this->pdelim, '#') . '#', $params);
119                         $params                 = array_map('trim', $params);
120                 }
121                 else
122                 {
123                         // no parameters
124                         $params = array();
125                 }
126                 
127                 $actionlc = strtolower($action);
128                 
129                 // skip execution of skinvars while inside an if condition which hides this part of the page
130                 $if_tags = array('else', 'elseif', 'endif', 'ifnot', 'elseifnot');
131                 if ( !$this->handler->getTopIfCondition()
132                   && !in_array($actionlc, $if_tags)
133                   && (i18n::substr($actionlc, 0, 2) != 'if') )
134                 {
135                         return;
136                 }
137                 
138                 if ( in_array($actionlc, $this->actions) || $this->norestrictions )
139                 {
140                         call_user_func_array(array(&$this->handler, "parse_{$actionlc}"), $params);
141                 }
142                 else
143                 {
144                         // redirect to plugin action if possible
145                         if ( in_array('plugin', $this->actions) && $manager->pluginInstalled("NP_{$action}") )
146                         {
147                                 $this->doAction('plugin(' . $action . $this->pdelim . implode($this->pdelim,$params) . ')');
148                         }
149                         else
150                         {
151                                 if ( $CONF['DebugVars']==true )
152                                 {
153                                         echo '&lt;%' , $action , '(', implode($this->pdelim, $params), ')%&gt;';
154                                 }
155                         }
156                 }
157                 return;
158         }
159         
160         /**
161          * Parser::setSkin()
162          * Set the skin
163          * @param       object  $skin   an instance of Skin class
164          * @return      void
165          */
166         public function setSkin(&$skin)
167         {
168                 $this->skin = &$skin;
169                 return;
170         }
171         
172         /**
173          * Parser::setProperty()
174          * Set a property of the parser in the manager
175          * 
176          * @static
177          * @param       string  $property       additional parser property (e.g. include prefix of the skin)
178          * @param       string  $value          new value
179          * @return      void
180          */
181         static public function setProperty($property, $value)
182         {
183                 global $manager;
184                 $manager->setParserProperty($property, $value);
185                 return;
186         }
187
188         /**
189          * Parser::getProperty()
190          * Get a property of the parser from the manager
191          * 
192          * @static
193          * @param       string  $name   name of the property
194          * @return      string  value of the property
195          */
196         static public function getProperty($name)
197         {
198                 global $manager;
199                 return $manager->getParserProperty($name);
200         }
201 }