OSDN Git Service

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