OSDN Git Service

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