1 | <?php |
---|
2 | /* |
---|
3 | File: xajaxArgumentManager.inc.php |
---|
4 | |
---|
5 | Contains the xajaxArgumentManager class |
---|
6 | |
---|
7 | Title: xajaxArgumentManager class |
---|
8 | |
---|
9 | Please see <copyright.inc.php> for a detailed description, copyright |
---|
10 | and license information. |
---|
11 | */ |
---|
12 | |
---|
13 | /* |
---|
14 | @package xajax |
---|
15 | @version $Id: xajaxArgumentManager.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $ |
---|
16 | @copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson |
---|
17 | @license http://www.xajaxproject.org/bsd_license.txt BSD License |
---|
18 | */ |
---|
19 | |
---|
20 | if (!defined('XAJAX_METHOD_UNKNOWN')) define('XAJAX_METHOD_UNKNOWN', 0); |
---|
21 | if (!defined('XAJAX_METHOD_GET')) define('XAJAX_METHOD_GET', 1); |
---|
22 | if (!defined('XAJAX_METHOD_POST')) define('XAJAX_METHOD_POST', 2); |
---|
23 | |
---|
24 | /* |
---|
25 | Class: xajaxArgumentManager |
---|
26 | |
---|
27 | This class processes the input arguments from the GET or POST data of |
---|
28 | the request. If this is a request for the initial page load, no arguments |
---|
29 | will be processed. During a xajax request, any arguments found in the |
---|
30 | GET or POST will be converted to a PHP array. |
---|
31 | */ |
---|
32 | class xajaxArgumentManager |
---|
33 | { |
---|
34 | /* |
---|
35 | Array: aArgs |
---|
36 | |
---|
37 | An array of arguments received via the GET or POST parameter |
---|
38 | xjxargs. |
---|
39 | */ |
---|
40 | var $aArgs; |
---|
41 | |
---|
42 | /* |
---|
43 | Boolean: bDecodeUTF8Input |
---|
44 | |
---|
45 | A configuration option used to indicate whether input data should be |
---|
46 | UTF8 decoded automatically. |
---|
47 | */ |
---|
48 | var $bDecodeUTF8Input; |
---|
49 | |
---|
50 | /* |
---|
51 | String: sCharacterEncoding |
---|
52 | |
---|
53 | The character encoding in which the input data will be received. |
---|
54 | */ |
---|
55 | var $sCharacterEncoding; |
---|
56 | |
---|
57 | /* |
---|
58 | Integer: nMethod |
---|
59 | |
---|
60 | Stores the method that was used to send the arguments from the client. Will |
---|
61 | be one of: XAJAX_METHOD_UNKNOWN, XAJAX_METHOD_GET, XAJAX_METHOD_POST |
---|
62 | */ |
---|
63 | var $nMethod; |
---|
64 | |
---|
65 | /* |
---|
66 | Array: aSequence |
---|
67 | |
---|
68 | Stores the decoding sequence table. |
---|
69 | */ |
---|
70 | var $aSequence; |
---|
71 | |
---|
72 | function argumentStripSlashes(&$sArg) |
---|
73 | { |
---|
74 | if (false == is_string($sArg)) |
---|
75 | return; |
---|
76 | |
---|
77 | $sArg = stripslashes($sArg); |
---|
78 | } |
---|
79 | |
---|
80 | function argumentDecodeXML(&$sArg) |
---|
81 | { |
---|
82 | if (false == is_string($sArg)) |
---|
83 | return; |
---|
84 | |
---|
85 | if (0 == strlen($sArg)) |
---|
86 | return; |
---|
87 | |
---|
88 | $nStackDepth = 0; |
---|
89 | $aStack = array(); |
---|
90 | $aArg = array(); |
---|
91 | |
---|
92 | $nCurrent = 0; |
---|
93 | $nLast = 0; |
---|
94 | // $sText = ''; |
---|
95 | $aExpecting = array(); |
---|
96 | $nFound = 0; |
---|
97 | list($aExpecting, $nFound) = $this->aSequence['start']; |
---|
98 | |
---|
99 | $nLength = strlen($sArg); |
---|
100 | |
---|
101 | $sKey = ''; |
---|
102 | $mValue = ''; |
---|
103 | |
---|
104 | while ($nCurrent < $nLength) |
---|
105 | { |
---|
106 | $bFound = false; |
---|
107 | |
---|
108 | foreach ($aExpecting as $sExpecting => $nExpectedLength) |
---|
109 | { |
---|
110 | if ($sArg[$nCurrent] == $sExpecting[0]) |
---|
111 | if ($sExpecting == substr($sArg, $nCurrent, $nExpectedLength)) |
---|
112 | { |
---|
113 | list($aExpecting, $nFound) = $this->aSequence[$sExpecting]; |
---|
114 | |
---|
115 | switch ($nFound) |
---|
116 | { |
---|
117 | case 3: // v |
---|
118 | $sKey = ''; |
---|
119 | break; |
---|
120 | case 4: // /v |
---|
121 | $sKey = str_replace( |
---|
122 | array('<'.'![CDATA[', ']]>'), |
---|
123 | '', |
---|
124 | // $sText |
---|
125 | substr($sArg, $nLast, $nCurrent - $nLast) |
---|
126 | ); |
---|
127 | break; |
---|
128 | case 5: // k |
---|
129 | $mValue = ''; |
---|
130 | break; |
---|
131 | case 6: // /k |
---|
132 | if ($nLast < $nCurrent) |
---|
133 | { |
---|
134 | $mValue = str_replace( |
---|
135 | array('<'.'![CDATA[', ']]>'), |
---|
136 | '', |
---|
137 | // $sText |
---|
138 | substr($sArg, $nLast, $nCurrent - $nLast) |
---|
139 | ); |
---|
140 | } |
---|
141 | break; |
---|
142 | case 7: // /e |
---|
143 | $aArg[$sKey] = $mValue; |
---|
144 | break; |
---|
145 | case 1: // xjxobj |
---|
146 | ++$nStackDepth; |
---|
147 | array_push($aStack, $aArg); |
---|
148 | $aArg = array(); |
---|
149 | array_push($aStack, $sKey); |
---|
150 | $sKey = ''; |
---|
151 | break; |
---|
152 | case 8: // /xjxobj |
---|
153 | if (1 < $nStackDepth) { |
---|
154 | $mValue = $aArg; |
---|
155 | $sKey = array_pop($aStack); |
---|
156 | $aArg = array_pop($aStack); |
---|
157 | --$nStackDepth; |
---|
158 | } else { |
---|
159 | $sArg = $aArg; |
---|
160 | return; |
---|
161 | } |
---|
162 | break; |
---|
163 | } |
---|
164 | $nCurrent += $nExpectedLength; |
---|
165 | $nLast = $nCurrent; |
---|
166 | // $sText = ''; |
---|
167 | $bFound = true; |
---|
168 | break; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | if (false == $bFound) |
---|
173 | { |
---|
174 | if (0 == $nCurrent) |
---|
175 | { |
---|
176 | $sArg = str_replace( |
---|
177 | array('<'.'![CDATA[', ']]>'), |
---|
178 | '', |
---|
179 | $sArg |
---|
180 | ); |
---|
181 | |
---|
182 | return; |
---|
183 | } |
---|
184 | |
---|
185 | // for larger arg data, performance may suffer using concatenation |
---|
186 | // $sText .= $sArg[$nCurrent]; |
---|
187 | $nCurrent++; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | $objLanguageManager =& xajaxLanguageManager::getInstance(); |
---|
192 | |
---|
193 | trigger_error( |
---|
194 | $objLanguageManager->getText('ARGMGR:ERR:01') |
---|
195 | . $sExpected |
---|
196 | . $objLanguageManager->getText('ARGMGR:ERR:02') |
---|
197 | . $sChunk |
---|
198 | , E_USER_ERROR |
---|
199 | ); |
---|
200 | } |
---|
201 | |
---|
202 | function argumentDecodeUTF8_iconv(&$mArg) |
---|
203 | { |
---|
204 | if (is_array($mArg)) |
---|
205 | { |
---|
206 | foreach (array_keys($mArg) as $sKey) |
---|
207 | { |
---|
208 | $sNewKey = $sKey; |
---|
209 | $this->argumentDecodeUTF8_iconv($sNewKey); |
---|
210 | |
---|
211 | if ($sNewKey != $sKey) |
---|
212 | { |
---|
213 | $mArg[$sNewKey] = $mArg[$sKey]; |
---|
214 | unset($mArg[$sKey]); |
---|
215 | $sKey = $sNewKey; |
---|
216 | } |
---|
217 | |
---|
218 | $this->argumentDecodeUTF8_iconv($mArg[$sKey]); |
---|
219 | } |
---|
220 | } |
---|
221 | else if (is_string($mArg)) |
---|
222 | $mArg = iconv("UTF-8", $this->sCharacterEncoding.'//TRANSLIT', $mArg); |
---|
223 | } |
---|
224 | |
---|
225 | function argumentDecodeUTF8_mb_convert_encoding(&$mArg) |
---|
226 | { |
---|
227 | if (is_array($mArg)) |
---|
228 | { |
---|
229 | foreach (array_keys($mArg) as $sKey) |
---|
230 | { |
---|
231 | $sNewKey = $sKey; |
---|
232 | $this->argumentDecodeUTF8_mb_convert_encoding($sNewKey); |
---|
233 | |
---|
234 | if ($sNewKey != $sKey) |
---|
235 | { |
---|
236 | $mArg[$sNewKey] = $mArg[$sKey]; |
---|
237 | unset($mArg[$sKey]); |
---|
238 | $sKey = $sNewKey; |
---|
239 | } |
---|
240 | |
---|
241 | $this->argumentDecodeUTF8_mb_convert_encoding($mArg[$sKey]); |
---|
242 | } |
---|
243 | } |
---|
244 | else if (is_string($mArg)) |
---|
245 | $mArg = mb_convert_encoding($mArg, $this->sCharacterEncoding, "UTF-8"); |
---|
246 | } |
---|
247 | |
---|
248 | function argumentDecodeUTF8_utf8_decode(&$mArg) |
---|
249 | { |
---|
250 | if (is_array($mArg)) |
---|
251 | { |
---|
252 | foreach (array_keys($mArg) as $sKey) |
---|
253 | { |
---|
254 | $sNewKey = $sKey; |
---|
255 | $this->argumentDecodeUTF8_utf8_decode($sNewKey); |
---|
256 | |
---|
257 | if ($sNewKey != $sKey) |
---|
258 | { |
---|
259 | $mArg[$sNewKey] = $mArg[$sKey]; |
---|
260 | unset($mArg[$sKey]); |
---|
261 | $sKey = $sNewKey; |
---|
262 | } |
---|
263 | |
---|
264 | $this->argumentDecodeUTF8_utf8_decode($mArg[$sKey]); |
---|
265 | } |
---|
266 | } |
---|
267 | else if (is_string($mArg)) |
---|
268 | $mArg = utf8_decode($mArg); |
---|
269 | } |
---|
270 | |
---|
271 | /* |
---|
272 | Constructor: xajaxArgumentManager |
---|
273 | |
---|
274 | Initializes configuration settings to their default values and reads |
---|
275 | the argument data from the GET or POST data. |
---|
276 | */ |
---|
277 | function xajaxArgumentManager() |
---|
278 | { |
---|
279 | $this->aArgs = array(); |
---|
280 | $this->bDecodeUTF8Input = false; |
---|
281 | $this->sCharacterEncoding = 'UTF-8'; |
---|
282 | $this->nMethod = XAJAX_METHOD_UNKNOWN; |
---|
283 | |
---|
284 | $this->aSequence = array( |
---|
285 | '<'.'k'.'>' => array(array( |
---|
286 | '<'.'/k'.'>' => 4 |
---|
287 | ), 3), |
---|
288 | '<'.'/k'.'>' => array(array( |
---|
289 | '<'.'v'.'>' => 3, |
---|
290 | '<'.'/e'.'>' => 4 |
---|
291 | ), 4), |
---|
292 | '<'.'v'.'>' => array(array( |
---|
293 | '<'.'xjxobj'.'>' => 8, |
---|
294 | '<'.'/v'.'>' => 4 |
---|
295 | ), 5), |
---|
296 | '<'.'/v'.'>' => array(array( |
---|
297 | '<'.'/e'.'>' => 4, |
---|
298 | '<'.'k'.'>' => 3 |
---|
299 | ), 6), |
---|
300 | '<'.'e'.'>' => array(array( |
---|
301 | '<'.'k'.'>' => 3, |
---|
302 | '<'.'v'.'>' => 3, |
---|
303 | '<'.'/e'.'>' => 4 |
---|
304 | ), 2), |
---|
305 | '<'.'/e'.'>' => array(array( |
---|
306 | '<'.'e'.'>' => 3, |
---|
307 | '<'.'/xjxobj'.'>' => 9 |
---|
308 | ), 7), |
---|
309 | '<'.'xjxobj'.'>' => array(array( |
---|
310 | '<'.'e'.'>' => 3, |
---|
311 | '<'.'/xjxobj'.'>' => 9 |
---|
312 | ), 1), |
---|
313 | '<'.'/xjxobj'.'>' => array(array( |
---|
314 | '<'.'/v'.'>' => 4 |
---|
315 | ), 8), |
---|
316 | 'start' => array(array( |
---|
317 | '<'.'xjxobj'.'>' => 8 |
---|
318 | ), 9) |
---|
319 | ); |
---|
320 | |
---|
321 | if (isset($_POST['xjxargs'])) { |
---|
322 | $this->nMethod = XAJAX_METHOD_POST; |
---|
323 | $this->aArgs = $_POST['xjxargs']; |
---|
324 | } else if (isset($_GET['xjxargs'])) { |
---|
325 | $this->nMethod = XAJAX_METHOD_GET; |
---|
326 | $this->aArgs = $_GET['xjxargs']; |
---|
327 | } |
---|
328 | |
---|
329 | if (1 == get_magic_quotes_gpc()) |
---|
330 | array_walk($this->aArgs, array(&$this, 'argumentStripSlashes')); |
---|
331 | |
---|
332 | array_walk($this->aArgs, array(&$this, 'argumentDecodeXML')); |
---|
333 | } |
---|
334 | |
---|
335 | /* |
---|
336 | Function: getInstance |
---|
337 | |
---|
338 | Returns: |
---|
339 | |
---|
340 | object - A reference to an instance of this class. This function is |
---|
341 | used to implement the singleton pattern. |
---|
342 | */ |
---|
343 | function &getInstance() |
---|
344 | { |
---|
345 | static $obj; |
---|
346 | if (!$obj) { |
---|
347 | $obj = new xajaxArgumentManager(); |
---|
348 | } |
---|
349 | return $obj; |
---|
350 | } |
---|
351 | |
---|
352 | /* |
---|
353 | Function: configure |
---|
354 | |
---|
355 | Accepts configuration settings from the main <xajax> object. |
---|
356 | |
---|
357 | The <xajaxArgumentManager> tracks the following configuration settings: |
---|
358 | <decodeUTF8Input> - (boolean): See <xajaxArgumentManager->bDecodeUTF8Input> |
---|
359 | <characterEncoding> - (string): See <xajaxArgumentManager->sCharacterEncoding> |
---|
360 | */ |
---|
361 | function configure($sName, $mValue) |
---|
362 | { |
---|
363 | if ('decodeUTF8Input' == $sName) { |
---|
364 | if (true === $mValue || false === $mValue) |
---|
365 | $this->bDecodeUTF8Input = $mValue; |
---|
366 | } else if ('characterEncoding' == $sName) { |
---|
367 | $this->sCharacterEncoding = $mValue; |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | /* |
---|
372 | Function: getRequestMethod |
---|
373 | |
---|
374 | Returns the method that was used to send the arguments from the client. |
---|
375 | */ |
---|
376 | function getRequestMethod() |
---|
377 | { |
---|
378 | return $this->nMethod; |
---|
379 | } |
---|
380 | |
---|
381 | /* |
---|
382 | Function: process |
---|
383 | |
---|
384 | Returns the array of arguments that were extracted and parsed from |
---|
385 | the GET or POST data. |
---|
386 | */ |
---|
387 | function process() |
---|
388 | { |
---|
389 | if ($this->bDecodeUTF8Input) |
---|
390 | { |
---|
391 | $sFunction = ''; |
---|
392 | |
---|
393 | if (function_exists('iconv')) |
---|
394 | $sFunction = "iconv"; |
---|
395 | else if (function_exists('mb_convert_encoding')) |
---|
396 | $sFunction = "mb_convert_encoding"; |
---|
397 | else if ($this->sCharacterEncoding == "ISO-8859-1") |
---|
398 | $sFunction = "utf8_decode"; |
---|
399 | else { |
---|
400 | $objLanguageManager =& xajaxLanguageManager::getInstance(); |
---|
401 | trigger_error( |
---|
402 | $objLanguageManager->getText('ARGMGR:ERR:03') |
---|
403 | , E_USER_NOTICE |
---|
404 | ); |
---|
405 | } |
---|
406 | |
---|
407 | $mFunction = array(&$this, 'argumentDecodeUTF8_' . $sFunction); |
---|
408 | |
---|
409 | array_walk($this->aArgs, $mFunction); |
---|
410 | |
---|
411 | $this->bDecodeUTF8Input = false; |
---|
412 | } |
---|
413 | |
---|
414 | return $this->aArgs; |
---|
415 | } |
---|
416 | } |
---|