OSDN Git Service

merged from v3.31sp1
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / PARSER.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2007 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-2007 The Nucleus Group
15  * @version $Id: PARSER.php,v 1.8 2008-02-08 09:31:22 kimitake Exp $
16  * $NucleusJP: PARSER.php,v 1.7.2.1 2007/09/05 07:35:59 kimitake Exp $
17  */
18
19 if ( !function_exists('requestVar') ) exit;
20 require_once dirname(__FILE__) . '/BaseActions.php';
21
22 /**
23  * This is the parser class of Nucleus. It is used for various things (skin parsing,
24  * form generation, ...)
25  */
26 class PARSER {
27
28         // array with the names of all allowed actions
29         var $actions;
30
31         // reference to actions handler
32         var $handler;
33
34         // delimiters that can be used for skin/templatevars
35         var $delim;
36
37         // parameter delimiter (to separate skinvar params)
38         var $pdelim;
39
40         // usually set to 0. When set to 1, all skinvars are allowed regardless of $actions
41         var $norestrictions;
42
43         /**
44          * Creates a new parser object with the given allowed actions
45          * and the given handler
46          *
47          * @param $allowedActions array
48          * @param $handler class object with functions for each action (reference)
49          * @param $delim optional delimiter
50          * @param $paramdelim optional parameterdelimiter
51          */
52         function PARSER($allowedActions, &$handler, $delim = '(<%|%>)', $pdelim = ',') {
53                 $this->actions = $allowedActions;
54                 $this->handler =& $handler;
55                 $this->delim = $delim;
56                 $this->pdelim = $pdelim;
57                 $this->norestrictions = 0;      // set this to 1 to disable checking for allowedActions
58         }
59
60         /**
61          * Parses the given contents and outputs it
62          */
63         function parse(&$contents) {
64
65                 $pieces = preg_split('/'.$this->delim.'/',$contents);
66
67                 $maxidx = sizeof($pieces);
68                 for ($idx = 0; $idx < $maxidx; $idx++) {
69                         echo $pieces[$idx];
70                         $idx++;
71                         if ($idx < $maxidx) {
72                                 $this->doAction($pieces[$idx]);
73                         }
74                 }
75         }
76
77
78         /**
79           * handle an action
80           */
81         function doAction($action) {
82                 global $manager;
83
84                 if (!$action) return;
85
86                 // split into action name + arguments
87                 if (strstr($action,'(')) {
88                         $paramStartPos = strpos($action, '(');
89                         $params = substr($action, $paramStartPos + 1, strlen($action) - $paramStartPos - 2);
90                         $action = substr($action, 0, $paramStartPos);
91                         $params = explode ($this->pdelim, $params);
92
93                         // trim parameters
94                         // for PHP versions lower than 4.0.6:
95                         //   - add // before '$params = ...'
96                         //   - remove // before 'foreach'
97                         $params = array_map('trim',$params);
98                         // foreach ($params as $key => $value) { $params[$key] = trim($value); }
99                 } else {
100                         // no parameters
101                         $params = array();
102                 }
103
104                 $actionlc = strtolower($action);
105
106                 // skip execution of skinvars while inside an if condition which hides this part of the page
107                 if (!$this->handler->if_currentlevel && ($actionlc != 'else') && ($actionlc != 'elseif') && ($actionlc != 'endif') && ($actionlc != 'ifnot') && ($actionlc != 'elseifnot') && (substr($actionlc,0,2) != 'if'))
108                         return;
109
110                 if (in_array($actionlc, $this->actions) || $this->norestrictions ) {
111                         // when using PHP versions lower than 4.0.5, uncomment the line before
112                         // and comment the call_user_func_array call
113                         //$this->call_using_array($action, $this->handler, $params);
114                         call_user_func_array(array(&$this->handler,'parse_' . $actionlc), $params);
115                 } else {
116                         // redirect to plugin action if possible
117                         if (in_array('plugin', $this->actions) && $manager->pluginInstalled('NP_'.$action))
118                                 $this->doAction('plugin('.$action.$this->pdelim.implode($this->pdelim,$params).')');
119                         else
120                                 echo '&lt;%' , $action , '(', implode($this->pdelim, $params), ')%&gt;';
121                 }
122
123         }
124
125         /**
126           * Calls a method using an array of parameters (for use with PHP versions lower than 4.0.5)
127           * ( = call_user_func_array() function )
128           */
129         function call_using_array($methodname, &$handler, $paramarray) {
130
131                 $methodname = 'parse_' . $methodname;
132
133                 if (!method_exists($handler, $methodname)) {
134                         return;
135                 }
136
137                 $command = 'call_user_func(array(&$handler,$methodname)';
138                 for ($i = 0; $i<count($paramarray); $i++)
139                         $command .= ',$paramarray[' . $i . ']';
140                 $command .= ');';
141                 eval($command); // execute the correct method
142         }
143
144         function setProperty($property, $value) {
145                 global $manager;
146                 $manager->setParserProperty($property, $value);
147         }
148
149         function getProperty($name) {
150                 global $manager;
151                 return $manager->getParserProperty($name);
152         }
153
154
155 }
156
157 ?>