OSDN Git Service

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