b7c101a1c02b72d38bbfdf6fabb2475f50be04ca
[lhc/web/wiklou.git] / includes / json / Services_JSON.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 /**
5 * Converts to and from JSON format.
6 *
7 * JSON (JavaScript Object Notation) is a lightweight data-interchange
8 * format. It is easy for humans to read and write. It is easy for machines
9 * to parse and generate. It is based on a subset of the JavaScript
10 * Programming Language, Standard ECMA-262 3rd Edition - December 1999.
11 * This feature can also be found in Python. JSON is a text format that is
12 * completely language independent but uses conventions that are familiar
13 * to programmers of the C-family of languages, including C, C++, C#, Java,
14 * JavaScript, Perl, TCL, and many others. These properties make JSON an
15 * ideal data-interchange language.
16 *
17 * This package provides a simple encoder and decoder for JSON notation. It
18 * is intended for use with client-side Javascript applications that make
19 * use of HTTPRequest to perform server communication functions - data can
20 * be encoded into JSON notation for use in a client-side javascript, or
21 * decoded from incoming Javascript requests. JSON format is native to
22 * Javascript, and can be directly eval()'ed with no further parsing
23 * overhead
24 *
25 * All strings should be in ASCII or UTF-8 format!
26 *
27 * LICENSE: Redistribution and use in source and binary forms, with or
28 * without modification, are permitted provided that the following
29 * conditions are met: Redistributions of source code must retain the
30 * above copyright notice, this list of conditions and the following
31 * disclaimer. Redistributions in binary form must reproduce the above
32 * copyright notice, this list of conditions and the following disclaimer
33 * in the documentation and/or other materials provided with the
34 * distribution.
35 *
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
38 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
39 * NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
41 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
42 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
44 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
45 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
46 * DAMAGE.
47 *
48 * @file
49 * @ingroup API
50 * @author Michal Migurski <mike-json@teczno.com>
51 * @author Matt Knapp <mdknapp[at]gmail[dot]com>
52 * @author Brett Stimmerman <brettstimmerman[at]gmail[dot]com>
53 * @copyright 2005 Michal Migurski
54 * @version CVS: $Id$
55 * @license http://www.opensource.org/licenses/bsd-license.php
56 * @see http://pear.php.net/pepr/pepr-proposal-show.php?id=198
57 */
58
59 /**
60 * Marker constant for Services_JSON::decode(), used to flag stack state
61 */
62 define('SERVICES_JSON_SLICE', 1);
63
64 /**
65 * Marker constant for Services_JSON::decode(), used to flag stack state
66 */
67 define('SERVICES_JSON_IN_STR', 2);
68
69 /**
70 * Marker constant for Services_JSON::decode(), used to flag stack state
71 */
72 define('SERVICES_JSON_IN_ARR', 3);
73
74 /**
75 * Marker constant for Services_JSON::decode(), used to flag stack state
76 */
77 define('SERVICES_JSON_IN_OBJ', 4);
78
79 /**
80 * Marker constant for Services_JSON::decode(), used to flag stack state
81 */
82 define('SERVICES_JSON_IN_CMT', 5);
83
84 /**
85 * Behavior switch for Services_JSON::decode()
86 */
87 define('SERVICES_JSON_LOOSE_TYPE', 16);
88
89 /**
90 * Behavior switch for Services_JSON::decode()
91 */
92 define('SERVICES_JSON_SUPPRESS_ERRORS', 32);
93
94 /**
95 * Converts to and from JSON format.
96 *
97 * Brief example of use:
98 *
99 * <code>
100 * // create a new instance of Services_JSON
101 * $json = new Services_JSON();
102 *
103 * // convert a complex value to JSON notation, and send it to the browser
104 * $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4)));
105 * $output = $json->encode($value);
106 *
107 * print($output);
108 * // prints: ["foo","bar",[1,2,"baz"],[3,[4]]]
109 *
110 * // accept incoming POST data, assumed to be in JSON notation
111 * $input = file_get_contents('php://input', 1000000);
112 * $value = $json->decode($input);
113 * </code>
114 *
115 * @ingroup API
116 */
117 class Services_JSON
118 {
119 /**
120 * constructs a new JSON instance
121 *
122 * @param $use Integer: object behavior flags; combine with boolean-OR
123 *
124 * possible values:
125 * - SERVICES_JSON_LOOSE_TYPE: loose typing.
126 * "{...}" syntax creates associative arrays
127 * instead of objects in decode().
128 * - SERVICES_JSON_SUPPRESS_ERRORS: error suppression.
129 * Values which can't be encoded (e.g. resources)
130 * appear as NULL instead of throwing errors.
131 * By default, a deeply-nested resource will
132 * bubble up with an error, so all return values
133 * from encode() should be checked with isError()
134 */
135 function __construct($use = 0)
136 {
137 $this->use = $use;
138 }
139
140 private static $mHavePear = null;
141 /**
142 * Returns cached result of class_exists('pear'), to avoid calling AutoLoader numerous times
143 * in cases when PEAR is not present.
144 * @return boolean
145 */
146 private static function pearInstalled() {
147 if ( self::$mHavePear === null ) {
148 self::$mHavePear = class_exists( 'pear' );
149 }
150 return self::$mHavePear;
151 }
152
153 /**
154 * convert a string from one UTF-16 char to one UTF-8 char
155 *
156 * Normally should be handled by mb_convert_encoding, but
157 * provides a slower PHP-only method for installations
158 * that lack the multibyte string extension.
159 *
160 * @param string $utf16 UTF-16 character
161 * @return String: UTF-8 character
162 * @access private
163 */
164 function utf162utf8($utf16)
165 {
166 // oh please oh please oh please oh please oh please
167 if(function_exists('mb_convert_encoding')) {
168 return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
169 }
170
171 $bytes = (ord($utf16[0]) << 8) | ord($utf16[1]);
172
173 switch(true) {
174 case ((0x7F & $bytes) == $bytes):
175 // this case should never be reached, because we are in ASCII range
176 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
177 return chr(0x7F & $bytes);
178
179 case (0x07FF & $bytes) == $bytes:
180 // return a 2-byte UTF-8 character
181 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
182 return chr(0xC0 | (($bytes >> 6) & 0x1F))
183 . chr(0x80 | ($bytes & 0x3F));
184
185 case (0xFC00 & $bytes) == 0xD800 && strlen($utf16) >= 4 && (0xFC & ord($utf16[2])) == 0xDC:
186 // return a 4-byte UTF-8 character
187 $char = ((($bytes & 0x03FF) << 10)
188 | ((ord($utf16[2]) & 0x03) << 8)
189 | ord($utf16[3]));
190 $char += 0x10000;
191 return chr(0xF0 | (($char >> 18) & 0x07))
192 . chr(0x80 | (($char >> 12) & 0x3F))
193 . chr(0x80 | (($char >> 6) & 0x3F))
194 . chr(0x80 | ($char & 0x3F));
195
196 case (0xFFFF & $bytes) == $bytes:
197 // return a 3-byte UTF-8 character
198 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
199 return chr(0xE0 | (($bytes >> 12) & 0x0F))
200 . chr(0x80 | (($bytes >> 6) & 0x3F))
201 . chr(0x80 | ($bytes & 0x3F));
202 }
203
204 // ignoring UTF-32 for now, sorry
205 return '';
206 }
207
208 /**
209 * convert a string from one UTF-8 char to one UTF-16 char
210 *
211 * Normally should be handled by mb_convert_encoding, but
212 * provides a slower PHP-only method for installations
213 * that lack the multibyte string extension.
214 *
215 * @param string $utf8 UTF-8 character
216 * @return String: UTF-16 character
217 * @access private
218 */
219 function utf82utf16($utf8)
220 {
221 // oh please oh please oh please oh please oh please
222 if(function_exists('mb_convert_encoding')) {
223 return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
224 }
225
226 switch(strlen($utf8)) {
227 case 1:
228 // this case should never be reached, because we are in ASCII range
229 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
230 return $utf8;
231
232 case 2:
233 // return a UTF-16 character from a 2-byte UTF-8 char
234 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
235 return chr(0x07 & (ord($utf8[0]) >> 2))
236 . chr((0xC0 & (ord($utf8[0]) << 6))
237 | (0x3F & ord($utf8[1])));
238
239 case 3:
240 // return a UTF-16 character from a 3-byte UTF-8 char
241 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
242 return chr((0xF0 & (ord($utf8[0]) << 4))
243 | (0x0F & (ord($utf8[1]) >> 2)))
244 . chr((0xC0 & (ord($utf8[1]) << 6))
245 | (0x7F & ord($utf8[2])));
246
247 case 4:
248 // return a UTF-16 surrogate pair from a 4-byte UTF-8 char
249 if(ord($utf8[0]) > 0xF4) return ''; # invalid
250 $char = ((0x1C0000 & (ord($utf8[0]) << 18))
251 | (0x03F000 & (ord($utf8[1]) << 12))
252 | (0x000FC0 & (ord($utf8[2]) << 6))
253 | (0x00003F & ord($utf8[3])));
254 if($char > 0x10FFFF) return ''; # invalid
255 $char -= 0x10000;
256 return chr(0xD8 | (($char >> 18) & 0x03))
257 . chr(($char >> 10) & 0xFF)
258 . chr(0xDC | (($char >> 8) & 0x03))
259 . chr($char & 0xFF);
260 }
261
262 // ignoring UTF-32 for now, sorry
263 return '';
264 }
265
266 /**
267 * encodes an arbitrary variable into JSON format
268 *
269 * @param $var Mixed: any number, boolean, string, array, or object to be encoded.
270 * see argument 1 to Services_JSON() above for array-parsing behavior.
271 * if var is a string, note that encode() always expects it
272 * to be in ASCII or UTF-8 format!
273 * @param $pretty Boolean: pretty-print output with indents and newlines
274 *
275 * @return mixed JSON string representation of input var or an error if a problem occurs
276 * @access public
277 */
278 function encode($var, $pretty=false)
279 {
280 $this->indent = 0;
281 $this->pretty = $pretty;
282 $this->nameValSeparator = $pretty ? ': ' : ':';
283 return $this->encode2($var);
284 }
285
286 /**
287 * encodes an arbitrary variable into JSON format
288 *
289 * @param $var Mixed: any number, boolean, string, array, or object to be encoded.
290 * see argument 1 to Services_JSON() above for array-parsing behavior.
291 * if var is a string, note that encode() always expects it
292 * to be in ASCII or UTF-8 format!
293 *
294 * @return mixed JSON string representation of input var or an error if a problem occurs
295 * @access private
296 */
297 function encode2($var)
298 {
299 if ($this->pretty) {
300 $close = "\n" . str_repeat("\t", $this->indent);
301 $open = $close . "\t";
302 $mid = ',' . $open;
303 }
304 else {
305 $open = $close = '';
306 $mid = ',';
307 }
308
309 switch (gettype($var)) {
310 case 'boolean':
311 return $var ? 'true' : 'false';
312
313 case 'NULL':
314 return 'null';
315
316 case 'integer':
317 return (int) $var;
318
319 case 'double':
320 case 'float':
321 return (float) $var;
322
323 case 'string':
324 // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
325 $ascii = '';
326 $strlen_var = strlen($var);
327
328 /*
329 * Iterate over every character in the string,
330 * escaping with a slash or encoding to UTF-8 where necessary
331 */
332 for ($c = 0; $c < $strlen_var; ++$c) {
333
334 $ord_var_c = ord($var[$c]);
335
336 switch (true) {
337 case $ord_var_c == 0x08:
338 $ascii .= '\b';
339 break;
340 case $ord_var_c == 0x09:
341 $ascii .= '\t';
342 break;
343 case $ord_var_c == 0x0A:
344 $ascii .= '\n';
345 break;
346 case $ord_var_c == 0x0C:
347 $ascii .= '\f';
348 break;
349 case $ord_var_c == 0x0D:
350 $ascii .= '\r';
351 break;
352
353 case $ord_var_c == 0x22:
354 case $ord_var_c == 0x2F:
355 case $ord_var_c == 0x5C:
356 // double quote, slash, slosh
357 $ascii .= '\\'.$var[$c];
358 break;
359
360 case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
361 // characters U-00000000 - U-0000007F (same as ASCII)
362 $ascii .= $var[$c];
363 break;
364
365 case (($ord_var_c & 0xE0) == 0xC0):
366 // characters U-00000080 - U-000007FF, mask 110XXXXX
367 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
368 $char = pack('C*', $ord_var_c, ord($var[$c + 1]));
369 $c += 1;
370 $utf16 = $this->utf82utf16($char);
371 $ascii .= sprintf('\u%04s', bin2hex($utf16));
372 break;
373
374 case (($ord_var_c & 0xF0) == 0xE0):
375 // characters U-00000800 - U-0000FFFF, mask 1110XXXX
376 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
377 $char = pack('C*', $ord_var_c,
378 ord($var[$c + 1]),
379 ord($var[$c + 2]));
380 $c += 2;
381 $utf16 = $this->utf82utf16($char);
382 $ascii .= sprintf('\u%04s', bin2hex($utf16));
383 break;
384
385 case (($ord_var_c & 0xF8) == 0xF0):
386 // characters U-00010000 - U-001FFFFF, mask 11110XXX
387 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
388 // These will always return a surrogate pair
389 $char = pack('C*', $ord_var_c,
390 ord($var[$c + 1]),
391 ord($var[$c + 2]),
392 ord($var[$c + 3]));
393 $c += 3;
394 $utf16 = $this->utf82utf16($char);
395 if($utf16 == '') {
396 $ascii .= '\ufffd';
397 } else {
398 $utf16 = str_split($utf16, 2);
399 $ascii .= sprintf('\u%04s\u%04s', bin2hex($utf16[0]), bin2hex($utf16[1]));
400 }
401 break;
402 }
403 }
404
405 return '"'.$ascii.'"';
406
407 case 'array':
408 /*
409 * As per JSON spec if any array key is not an integer
410 * we must treat the the whole array as an object. We
411 * also try to catch a sparsely populated associative
412 * array with numeric keys here because some JS engines
413 * will create an array with empty indexes up to
414 * max_index which can cause memory issues and because
415 * the keys, which may be relevant, will be remapped
416 * otherwise.
417 *
418 * As per the ECMA and JSON specification an object may
419 * have any string as a property. Unfortunately due to
420 * a hole in the ECMA specification if the key is a
421 * ECMA reserved word or starts with a digit the
422 * parameter is only accessible using ECMAScript's
423 * bracket notation.
424 */
425
426 // treat as a JSON object
427 if (is_array($var) && count($var) && (array_keys($var) !== range(0, count($var) - 1))) {
428 $this->indent++;
429 $properties = array_map(array($this, 'name_value'),
430 array_keys($var),
431 array_values($var));
432 $this->indent--;
433
434 foreach($properties as $property) {
435 if($this->isError($property)) {
436 return $property;
437 }
438 }
439
440 return '{' . $open . join($mid, $properties) . $close . '}';
441 }
442
443 // treat it like a regular array
444 $this->indent++;
445 $elements = array_map(array($this, 'encode2'), $var);
446 $this->indent--;
447
448 foreach($elements as $element) {
449 if($this->isError($element)) {
450 return $element;
451 }
452 }
453
454 return '[' . $open . join($mid, $elements) . $close . ']';
455
456 case 'object':
457 $vars = get_object_vars($var);
458
459 $this->indent++;
460 $properties = array_map(array($this, 'name_value'),
461 array_keys($vars),
462 array_values($vars));
463 $this->indent--;
464
465 foreach($properties as $property) {
466 if($this->isError($property)) {
467 return $property;
468 }
469 }
470
471 return '{' . $open . join($mid, $properties) . $close . '}';
472
473 default:
474 return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
475 ? 'null'
476 : new Services_JSON_Error(gettype($var)." can not be encoded as JSON string");
477 }
478 }
479
480 /**
481 * array-walking function for use in generating JSON-formatted name-value pairs
482 *
483 * @param string $name name of key to use
484 * @param $value Mixed: reference to an array element to be encoded
485 *
486 * @return String: JSON-formatted name-value pair, like '"name":value'
487 * @access private
488 */
489 function name_value($name, $value)
490 {
491 $encoded_value = $this->encode2($value);
492
493 if($this->isError($encoded_value)) {
494 return $encoded_value;
495 }
496
497 return $this->encode2(strval($name)) . $this->nameValSeparator . $encoded_value;
498 }
499
500 /**
501 * reduce a string by removing leading and trailing comments and whitespace
502 *
503 * @param string $str string value to strip of comments and whitespace
504 *
505 * @return String: string value stripped of comments and whitespace
506 * @access private
507 */
508 function reduce_string($str)
509 {
510 $str = preg_replace(array(
511
512 // eliminate single line comments in '// ...' form
513 '#^\s*//(.+)$#m',
514
515 // eliminate multi-line comments in '/* ... */' form, at start of string
516 '#^\s*/\*(.+)\*/#Us',
517
518 // eliminate multi-line comments in '/* ... */' form, at end of string
519 '#/\*(.+)\*/\s*$#Us'
520
521 ), '', $str);
522
523 // eliminate extraneous space
524 return trim($str);
525 }
526
527 /**
528 * decodes a JSON string into appropriate variable
529 *
530 * @param string $str JSON-formatted string
531 *
532 * @return mixed number, boolean, string, array, or object
533 * corresponding to given JSON input string.
534 * See argument 1 to Services_JSON() above for object-output behavior.
535 * Note that decode() always returns strings
536 * in ASCII or UTF-8 format!
537 * @access public
538 */
539 function decode($str)
540 {
541 $str = $this->reduce_string($str);
542
543 switch (strtolower($str)) {
544 case 'true':
545 return true;
546
547 case 'false':
548 return false;
549
550 case 'null':
551 return null;
552
553 default:
554 $m = array();
555
556 if (is_numeric($str)) {
557 // Lookie-loo, it's a number
558
559 // This would work on its own, but I'm trying to be
560 // good about returning integers where appropriate:
561 // return (float)$str;
562
563 // Return float or int, as appropriate
564 return ((float)$str == (integer)$str)
565 ? (integer)$str
566 : (float)$str;
567
568 } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) {
569 // STRINGS RETURNED IN UTF-8 FORMAT
570 $delim = substr($str, 0, 1);
571 $chrs = substr($str, 1, -1);
572 $utf8 = '';
573 $strlen_chrs = strlen($chrs);
574
575 for ($c = 0; $c < $strlen_chrs; ++$c) {
576
577 $substr_chrs_c_2 = substr($chrs, $c, 2);
578 $ord_chrs_c = ord($chrs[$c]);
579
580 switch (true) {
581 case $substr_chrs_c_2 == '\b':
582 $utf8 .= chr(0x08);
583 ++$c;
584 break;
585 case $substr_chrs_c_2 == '\t':
586 $utf8 .= chr(0x09);
587 ++$c;
588 break;
589 case $substr_chrs_c_2 == '\n':
590 $utf8 .= chr(0x0A);
591 ++$c;
592 break;
593 case $substr_chrs_c_2 == '\f':
594 $utf8 .= chr(0x0C);
595 ++$c;
596 break;
597 case $substr_chrs_c_2 == '\r':
598 $utf8 .= chr(0x0D);
599 ++$c;
600 break;
601
602 case $substr_chrs_c_2 == '\\"':
603 case $substr_chrs_c_2 == '\\\'':
604 case $substr_chrs_c_2 == '\\\\':
605 case $substr_chrs_c_2 == '\\/':
606 if (($delim == '"' && $substr_chrs_c_2 != '\\\'') ||
607 ($delim == "'" && $substr_chrs_c_2 != '\\"')) {
608 $utf8 .= $chrs[++$c];
609 }
610 break;
611
612 case preg_match('/\\\uD[89AB][0-9A-F]{2}\\\uD[C-F][0-9A-F]{2}/i', substr($chrs, $c, 12)):
613 // escaped unicode surrogate pair
614 $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2)))
615 . chr(hexdec(substr($chrs, ($c + 4), 2)))
616 . chr(hexdec(substr($chrs, ($c + 8), 2)))
617 . chr(hexdec(substr($chrs, ($c + 10), 2)));
618 $utf8 .= $this->utf162utf8($utf16);
619 $c += 11;
620 break;
621
622 case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)):
623 // single, escaped unicode character
624 $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2)))
625 . chr(hexdec(substr($chrs, ($c + 4), 2)));
626 $utf8 .= $this->utf162utf8($utf16);
627 $c += 5;
628 break;
629
630 case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F):
631 $utf8 .= $chrs[$c];
632 break;
633
634 case ($ord_chrs_c & 0xE0) == 0xC0:
635 // characters U-00000080 - U-000007FF, mask 110XXXXX
636 //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
637 $utf8 .= substr($chrs, $c, 2);
638 ++$c;
639 break;
640
641 case ($ord_chrs_c & 0xF0) == 0xE0:
642 // characters U-00000800 - U-0000FFFF, mask 1110XXXX
643 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
644 $utf8 .= substr($chrs, $c, 3);
645 $c += 2;
646 break;
647
648 case ($ord_chrs_c & 0xF8) == 0xF0:
649 // characters U-00010000 - U-001FFFFF, mask 11110XXX
650 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
651 $utf8 .= substr($chrs, $c, 4);
652 $c += 3;
653 break;
654
655 case ($ord_chrs_c & 0xFC) == 0xF8:
656 // characters U-00200000 - U-03FFFFFF, mask 111110XX
657 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
658 $utf8 .= substr($chrs, $c, 5);
659 $c += 4;
660 break;
661
662 case ($ord_chrs_c & 0xFE) == 0xFC:
663 // characters U-04000000 - U-7FFFFFFF, mask 1111110X
664 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
665 $utf8 .= substr($chrs, $c, 6);
666 $c += 5;
667 break;
668
669 }
670
671 }
672
673 return $utf8;
674
675 } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) {
676 // array, or object notation
677
678 if ($str[0] == '[') {
679 $stk = array(SERVICES_JSON_IN_ARR);
680 $arr = array();
681 } else {
682 if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
683 $stk = array(SERVICES_JSON_IN_OBJ);
684 $obj = array();
685 } else {
686 $stk = array(SERVICES_JSON_IN_OBJ);
687 $obj = new stdClass();
688 }
689 }
690
691 array_push($stk, array( 'what' => SERVICES_JSON_SLICE,
692 'where' => 0,
693 'delim' => false));
694
695 $chrs = substr($str, 1, -1);
696 $chrs = $this->reduce_string($chrs);
697
698 if ($chrs == '') {
699 if (reset($stk) == SERVICES_JSON_IN_ARR) {
700 return $arr;
701
702 } else {
703 return $obj;
704
705 }
706 }
707
708 //print("\nparsing {$chrs}\n");
709
710 $strlen_chrs = strlen($chrs);
711
712 for ($c = 0; $c <= $strlen_chrs; ++$c) {
713
714 $top = end($stk);
715 $substr_chrs_c_2 = substr($chrs, $c, 2);
716
717 if (($c == $strlen_chrs) || (($chrs[$c] == ',') && ($top['what'] == SERVICES_JSON_SLICE))) {
718 // found a comma that is not inside a string, array, etc.,
719 // OR we've reached the end of the character list
720 $slice = substr($chrs, $top['where'], ($c - $top['where']));
721 array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
722 //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
723
724 if (reset($stk) == SERVICES_JSON_IN_ARR) {
725 // we are in an array, so just push an element onto the stack
726 array_push($arr, $this->decode($slice));
727
728 } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
729 // we are in an object, so figure
730 // out the property name and set an
731 // element in an associative array,
732 // for now
733 $parts = array();
734
735 if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
736 // "name":value pair
737 $key = $this->decode($parts[1]);
738 $val = $this->decode($parts[2]);
739
740 if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
741 $obj[$key] = $val;
742 } else {
743 $obj->$key = $val;
744 }
745 } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
746 // name:value pair, where name is unquoted
747 $key = $parts[1];
748 $val = $this->decode($parts[2]);
749
750 if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
751 $obj[$key] = $val;
752 } else {
753 $obj->$key = $val;
754 }
755 }
756
757 }
758
759 } elseif ((($chrs[$c] == '"') || ($chrs[$c] == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) {
760 // found a quote, and we are not inside a string
761 array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs[$c]));
762 //print("Found start of string at {$c}\n");
763
764 } elseif (($chrs[$c] == $top['delim']) &&
765 ($top['what'] == SERVICES_JSON_IN_STR) &&
766 (($chrs[$c - 1] != '\\') ||
767 ($chrs[$c - 1] == '\\' && $chrs[$c - 2] == '\\'))) {
768 // found a quote, we're in a string, and it's not escaped
769 array_pop($stk);
770 //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
771
772 } elseif (($chrs[$c] == '[') &&
773 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
774 // found a left-bracket, and we are in an array, object, or slice
775 array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false));
776 //print("Found start of array at {$c}\n");
777
778 } elseif (($chrs[$c] == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) {
779 // found a right-bracket, and we're in an array
780 array_pop($stk);
781 //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
782
783 } elseif (($chrs[$c] == '{') &&
784 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
785 // found a left-brace, and we are in an array, object, or slice
786 array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false));
787 //print("Found start of object at {$c}\n");
788
789 } elseif (($chrs[$c] == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) {
790 // found a right-brace, and we're in an object
791 array_pop($stk);
792 //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
793
794 } elseif (($substr_chrs_c_2 == '/*') &&
795 in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
796 // found a comment start, and we are in an array, object, or slice
797 array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false));
798 $c++;
799 //print("Found start of comment at {$c}\n");
800
801 } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) {
802 // found a comment end, and we're in one now
803 array_pop($stk);
804 $c++;
805
806 for ($i = $top['where']; $i <= $c; ++$i)
807 $chrs = substr_replace($chrs, ' ', $i, 1);
808
809 //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
810
811 }
812
813 }
814
815 if (reset($stk) == SERVICES_JSON_IN_ARR) {
816 return $arr;
817
818 } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
819 return $obj;
820
821 }
822
823 }
824 }
825 }
826
827 /**
828 * @todo Ultimately, this should just call PEAR::isError()
829 * @return bool
830 */
831 function isError($data, $code = null)
832 {
833 if ( self::pearInstalled() ) {
834 //avoid some strict warnings on PEAR isError check (looks like http://pear.php.net/bugs/bug.php?id=9950 has been around for some time)
835 return @PEAR::isError($data, $code);
836 } elseif (is_object($data) && (get_class($data) == 'services_json_error' ||
837 is_subclass_of($data, 'services_json_error'))) {
838 return true;
839 }
840
841 return false;
842 }
843 }
844
845
846 // Hide the PEAR_Error variant from Doxygen
847 /// @cond
848 if (class_exists('PEAR_Error')) {
849
850 /**
851 * @ingroup API
852 */
853 class Services_JSON_Error extends PEAR_Error
854 {
855 function Services_JSON_Error($message = 'unknown error', $code = null,
856 $mode = null, $options = null, $userinfo = null)
857 {
858 parent::PEAR_Error($message, $code, $mode, $options, $userinfo);
859 }
860 }
861
862 } else {
863 /// @endcond
864
865 /**
866 * @todo Ultimately, this class shall be descended from PEAR_Error
867 * @ingroup API
868 */
869 class Services_JSON_Error
870 {
871 function Services_JSON_Error($message = 'unknown error', $code = null,
872 $mode = null, $options = null, $userinfo = null)
873 {
874 $this->message = $message;
875 }
876
877 function __toString()
878 {
879 return $this->message;
880 }
881 }
882 }