1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: 14: 15: 16: 17: 18: 19:
20:
21: if (!defined('XAJAX_METHOD_UNKNOWN')) define('XAJAX_METHOD_UNKNOWN', 0);
22: if (!defined('XAJAX_METHOD_GET')) define('XAJAX_METHOD_GET', 1);
23: if (!defined('XAJAX_METHOD_POST')) define('XAJAX_METHOD_POST', 2);
24:
25: 26: 27: 28: 29: 30: 31: 32:
33: final class xajaxArgumentManager
34: {
35: 36: 37: 38: 39: 40:
41: private $aArgs;
42:
43: 44: 45: 46: 47: 48:
49: private $bDecodeUTF8Input;
50:
51: 52: 53: 54: 55:
56: private $sCharacterEncoding;
57:
58: 59: 60: 61: 62: 63:
64: private $nMethod;
65:
66: 67: 68: 69: 70:
71: private $aSequence;
72:
73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84:
85:
86: private function __convertStringToBool($sValue)
87: {
88: if (0 == strcasecmp($sValue, 'true'))
89: return true;
90: if (0 == strcasecmp($sValue, 'false'))
91: return false;
92: if (is_numeric($sValue))
93: {
94: if (0 == $sValue)
95: return false;
96: return true;
97: }
98: return false;
99: }
100:
101: private function __argumentStripSlashes($sArg)
102: {
103: if (false == is_string($sArg))
104: return;
105:
106: $sArg = stripslashes($sArg);
107: }
108:
109: private function __convertValue($value)
110: {
111: $cType = substr($value, 0, 1);
112: $sValue = substr($value, 1);
113: switch ($cType) {
114: case 'S': $value = false === $sValue ? '' : $sValue; break;
115: case 'B': $value = $this->__convertStringToBool($sValue); break;
116: case 'N': $value = $sValue == floor($sValue) ? (int)$sValue : (float)$sValue; break;
117: case '*': $value = null; break;
118: }
119: return $value;
120: }
121:
122: private function __decodeXML($xml)
123: {
124: $return = array();
125: $nodes = $xml->e;
126: foreach ($nodes as $node) {
127: $key = (string) $node->k;
128: if (isset($node->v->xjxobj)) {
129: $value = $this->__decodeXML($node->v->xjxobj);
130: } else {
131: $value = $this->__convertValue( (string) $node->v );
132: }
133: $return[$key] = $value;
134: }
135:
136: return $return;
137: }
138:
139:
140: private function __argumentDecode( &$sArg )
141: {
142:
143: if ('' == $sArg ) return;
144:
145: $data = json_decode( $sArg , true );
146: if ( null !== $data ) {
147: $sArg = $data;
148: } else {
149: $sArg = $this->__convertValue( $sArg );
150: }
151: }
152:
153: private function __argumentDecodeUTF8_iconv( &$mArg )
154: {
155:
156:
157: if ( is_array( $mArg ) )
158: {
159: foreach ( array_keys( $mArg ) as $sKey )
160: {
161: $sNewKey = $sKey;
162: $this->__argumentDecodeUTF8_iconv($sNewKey);
163:
164: if ($sNewKey != $sKey)
165: {
166: $mArg[$sNewKey] = $mArg[$sKey];
167: unset($mArg[$sKey]);
168: $sKey = $sNewKey;
169: }
170:
171: $this->__argumentDecodeUTF8_iconv($mArg[$sKey]);
172: }
173: }
174: else if (is_string($mArg))
175: $mArg = iconv("UTF-8", $this->sCharacterEncoding.'//TRANSLIT', $mArg);
176: }
177:
178: private function __argumentDecodeUTF8_mb_convert_encoding(&$mArg)
179: {
180: if (is_array($mArg))
181: {
182: foreach (array_keys($mArg) as $sKey)
183: {
184: $sNewKey = $sKey;
185: $this->__argumentDecodeUTF8_mb_convert_encoding($sNewKey);
186:
187: if ($sNewKey != $sKey)
188: {
189: $mArg[$sNewKey] = $mArg[$sKey];
190: unset($mArg[$sKey]);
191: $sKey = $sNewKey;
192: }
193:
194: $this->__argumentDecodeUTF8_mb_convert_encoding($mArg[$sKey]);
195: }
196: }
197: else if (is_string($mArg))
198: $mArg = mb_convert_encoding($mArg, $this->sCharacterEncoding, "UTF-8");
199: }
200:
201: private function __argumentDecodeUTF8_utf8_decode(&$mArg)
202: {
203: if (is_array($mArg))
204: {
205: foreach (array_keys($mArg) as $sKey)
206: {
207: $sNewKey = $sKey;
208: $this->__argumentDecodeUTF8_utf8_decode($sNewKey);
209:
210: if ($sNewKey != $sKey)
211: {
212: $mArg[$sNewKey] = $mArg[$sKey];
213: unset($mArg[$sKey]);
214: $sKey = $sNewKey;
215: }
216:
217: $this->__argumentDecodeUTF8_utf8_decode($mArg[$sKey]);
218: }
219: }
220: else if (is_string($mArg))
221: $mArg = utf8_decode($mArg);
222: }
223:
224: 225: 226: 227: 228: 229:
230: private function __construct()
231: {
232: $this->aArgs = array();
233: $this->bDecodeUTF8Input = false;
234: $this->sCharacterEncoding = 'UTF-8';
235: $this->nMethod = XAJAX_METHOD_UNKNOWN;
236:
237: if (isset($_POST['xjxargs'])) {
238: $this->nMethod = XAJAX_METHOD_POST;
239: $this->aArgs = $_POST['xjxargs'];
240: } else if (isset($_GET['xjxargs'])) {
241: $this->nMethod = XAJAX_METHOD_GET;
242: $this->aArgs = $_GET['xjxargs'];
243: }
244: if (1 == get_magic_quotes_gpc())
245: array_walk($this->aArgs, array(&$this, '__argumentStripSlashes'));
246:
247: array_walk($this->aArgs, array(&$this, '__argumentDecode'));
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257:
258: public static function &getInstance()
259: {
260: static $obj;
261: if (!$obj) {
262: $obj = new xajaxArgumentManager();
263: }
264: return $obj;
265: }
266:
267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279:
280: public function configure($sName, $mValue)
281: {
282: if ('decodeUTF8Input' == $sName) {
283: if (true === $mValue || false === $mValue)
284: $this->bDecodeUTF8Input = $mValue;
285: } else if ('characterEncoding' == $sName) {
286: $this->sCharacterEncoding = $mValue;
287: }
288: }
289:
290: 291: 292: 293: 294:
295: public function getRequestMethod()
296: {
297: return $this->nMethod;
298: }
299:
300: 301: 302: 303: 304: 305:
306: public function process()
307: {
308: if ($this->bDecodeUTF8Input)
309: {
310:
311: $sFunction = '';
312:
313: if (function_exists('iconv'))
314: $sFunction = "iconv";
315: else if (function_exists('mb_convert_encoding'))
316: $sFunction = "mb_convert_encoding";
317: else if ($this->sCharacterEncoding == "ISO-8859-1")
318: $sFunction = "utf8_decode";
319: else {
320: $objLanguageManager = xajaxLanguageManager::getInstance();
321: trigger_error(
322: $objLanguageManager->getText('ARGMGR:ERR:03')
323: , E_USER_NOTICE
324: );
325: }
326:
327: $mFunction = array(&$this, '__argumentDecodeUTF8_' . $sFunction);
328:
329: array_walk($this->aArgs, $mFunction);
330: $this->bDecodeUTF8Input = false;
331: }
332:
333: return $this->aArgs;
334: }
335: }
336: