You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

961 lines
40 KiB

  1. <?php
  2. if ( ! class_exists( 'Services_JSON' ) ) :
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  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. * @category
  49. * @package Services_JSON
  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: JSON.php 305040 2010-11-02 23:19:03Z alan_k $
  55. * @license http://www.opensource.org/licenses/bsd-license.php
  56. * @link http://pear.php.net/pepr/pepr-proposal-show.php?id=198
  57. */
  58. /**
  59. * Marker constant for Services_JSON::decode(), used to flag stack state
  60. */
  61. define('SERVICES_JSON_SLICE', 1);
  62. /**
  63. * Marker constant for Services_JSON::decode(), used to flag stack state
  64. */
  65. define('SERVICES_JSON_IN_STR', 2);
  66. /**
  67. * Marker constant for Services_JSON::decode(), used to flag stack state
  68. */
  69. define('SERVICES_JSON_IN_ARR', 3);
  70. /**
  71. * Marker constant for Services_JSON::decode(), used to flag stack state
  72. */
  73. define('SERVICES_JSON_IN_OBJ', 4);
  74. /**
  75. * Marker constant for Services_JSON::decode(), used to flag stack state
  76. */
  77. define('SERVICES_JSON_IN_CMT', 5);
  78. /**
  79. * Behavior switch for Services_JSON::decode()
  80. */
  81. define('SERVICES_JSON_LOOSE_TYPE', 16);
  82. /**
  83. * Behavior switch for Services_JSON::decode()
  84. */
  85. define('SERVICES_JSON_SUPPRESS_ERRORS', 32);
  86. /**
  87. * Behavior switch for Services_JSON::decode()
  88. */
  89. define('SERVICES_JSON_USE_TO_JSON', 64);
  90. /**
  91. * Converts to and from JSON format.
  92. *
  93. * Brief example of use:
  94. *
  95. * <code>
  96. * // create a new instance of Services_JSON
  97. * $json = new Services_JSON();
  98. *
  99. * // convert a complexe value to JSON notation, and send it to the browser
  100. * $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4)));
  101. * $output = $json->encode($value);
  102. *
  103. * print($output);
  104. * // prints: ["foo","bar",[1,2,"baz"],[3,[4]]]
  105. *
  106. * // accept incoming POST data, assumed to be in JSON notation
  107. * $input = file_get_contents('php://input', 1000000);
  108. * $value = $json->decode($input);
  109. * </code>
  110. */
  111. class Services_JSON
  112. {
  113. /**
  114. * constructs a new JSON instance
  115. *
  116. * @param int $use object behavior flags; combine with boolean-OR
  117. *
  118. * possible values:
  119. * - SERVICES_JSON_LOOSE_TYPE: loose typing.
  120. * "{...}" syntax creates associative arrays
  121. * instead of objects in decode().
  122. * - SERVICES_JSON_SUPPRESS_ERRORS: error suppression.
  123. * Values which can't be encoded (e.g. resources)
  124. * appear as NULL instead of throwing errors.
  125. * By default, a deeply-nested resource will
  126. * bubble up with an error, so all return values
  127. * from encode() should be checked with isError()
  128. * - SERVICES_JSON_USE_TO_JSON: call toJSON when serializing objects
  129. * It serializes the return value from the toJSON call rather
  130. * than the object itself, toJSON can return associative arrays,
  131. * strings or numbers, if you return an object, make sure it does
  132. * not have a toJSON method, otherwise an error will occur.
  133. */
  134. function __construct( $use = 0 )
  135. {
  136. $this->use = $use;
  137. $this->_mb_strlen = function_exists('mb_strlen');
  138. $this->_mb_convert_encoding = function_exists('mb_convert_encoding');
  139. $this->_mb_substr = function_exists('mb_substr');
  140. }
  141. /**
  142. * PHP4 constructor.
  143. */
  144. public function Services_JSON( $use = 0 ) {
  145. self::__construct( $use );
  146. }
  147. // private - cache the mbstring lookup results..
  148. var $_mb_strlen = false;
  149. var $_mb_substr = false;
  150. var $_mb_convert_encoding = false;
  151. /**
  152. * convert a string from one UTF-16 char to one UTF-8 char
  153. *
  154. * Normally should be handled by mb_convert_encoding, but
  155. * provides a slower PHP-only method for installations
  156. * that lack the multibye string extension.
  157. *
  158. * @param string $utf16 UTF-16 character
  159. * @return string UTF-8 character
  160. * @access private
  161. */
  162. function utf162utf8($utf16)
  163. {
  164. // oh please oh please oh please oh please oh please
  165. if($this->_mb_convert_encoding) {
  166. return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
  167. }
  168. $bytes = (ord($utf16{0}) << 8) | ord($utf16{1});
  169. switch(true) {
  170. case ((0x7F & $bytes) == $bytes):
  171. // this case should never be reached, because we are in ASCII range
  172. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  173. return chr(0x7F & $bytes);
  174. case (0x07FF & $bytes) == $bytes:
  175. // return a 2-byte UTF-8 character
  176. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  177. return chr(0xC0 | (($bytes >> 6) & 0x1F))
  178. . chr(0x80 | ($bytes & 0x3F));
  179. case (0xFFFF & $bytes) == $bytes:
  180. // return a 3-byte UTF-8 character
  181. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  182. return chr(0xE0 | (($bytes >> 12) & 0x0F))
  183. . chr(0x80 | (($bytes >> 6) & 0x3F))
  184. . chr(0x80 | ($bytes & 0x3F));
  185. }
  186. // ignoring UTF-32 for now, sorry
  187. return '';
  188. }
  189. /**
  190. * convert a string from one UTF-8 char to one UTF-16 char
  191. *
  192. * Normally should be handled by mb_convert_encoding, but
  193. * provides a slower PHP-only method for installations
  194. * that lack the multibye string extension.
  195. *
  196. * @param string $utf8 UTF-8 character
  197. * @return string UTF-16 character
  198. * @access private
  199. */
  200. function utf82utf16($utf8)
  201. {
  202. // oh please oh please oh please oh please oh please
  203. if($this->_mb_convert_encoding) {
  204. return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
  205. }
  206. switch($this->strlen8($utf8)) {
  207. case 1:
  208. // this case should never be reached, because we are in ASCII range
  209. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  210. return $utf8;
  211. case 2:
  212. // return a UTF-16 character from a 2-byte UTF-8 char
  213. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  214. return chr(0x07 & (ord($utf8{0}) >> 2))
  215. . chr((0xC0 & (ord($utf8{0}) << 6))
  216. | (0x3F & ord($utf8{1})));
  217. case 3:
  218. // return a UTF-16 character from a 3-byte UTF-8 char
  219. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  220. return chr((0xF0 & (ord($utf8{0}) << 4))
  221. | (0x0F & (ord($utf8{1}) >> 2)))
  222. . chr((0xC0 & (ord($utf8{1}) << 6))
  223. | (0x7F & ord($utf8{2})));
  224. }
  225. // ignoring UTF-32 for now, sorry
  226. return '';
  227. }
  228. /**
  229. * encodes an arbitrary variable into JSON format (and sends JSON Header)
  230. *
  231. * @param mixed $var any number, boolean, string, array, or object to be encoded.
  232. * see argument 1 to Services_JSON() above for array-parsing behavior.
  233. * if var is a strng, note that encode() always expects it
  234. * to be in ASCII or UTF-8 format!
  235. *
  236. * @return mixed JSON string representation of input var or an error if a problem occurs
  237. * @access public
  238. */
  239. function encode($var)
  240. {
  241. header('Content-type: application/json');
  242. return $this->encodeUnsafe($var);
  243. }
  244. /**
  245. * encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!)
  246. *
  247. * @param mixed $var any number, boolean, string, array, or object to be encoded.
  248. * see argument 1 to Services_JSON() above for array-parsing behavior.
  249. * if var is a strng, note that encode() always expects it
  250. * to be in ASCII or UTF-8 format!
  251. *
  252. * @return mixed JSON string representation of input var or an error if a problem occurs
  253. * @access public
  254. */
  255. function encodeUnsafe($var)
  256. {
  257. // see bug #16908 - regarding numeric locale printing
  258. $lc = setlocale(LC_NUMERIC, 0);
  259. setlocale(LC_NUMERIC, 'C');
  260. $ret = $this->_encode($var);
  261. setlocale(LC_NUMERIC, $lc);
  262. return $ret;
  263. }
  264. /**
  265. * PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format
  266. *
  267. * @param mixed $var any number, boolean, string, array, or object to be encoded.
  268. * see argument 1 to Services_JSON() above for array-parsing behavior.
  269. * if var is a strng, note that encode() always expects it
  270. * to be in ASCII or UTF-8 format!
  271. *
  272. * @return mixed JSON string representation of input var or an error if a problem occurs
  273. * @access public
  274. */
  275. function _encode($var)
  276. {
  277. switch (gettype($var)) {
  278. case 'boolean':
  279. return $var ? 'true' : 'false';
  280. case 'NULL':
  281. return 'null';
  282. case 'integer':
  283. return (int) $var;
  284. case 'double':
  285. case 'float':
  286. return (float) $var;
  287. case 'string':
  288. // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
  289. $ascii = '';
  290. $strlen_var = $this->strlen8($var);
  291. /*
  292. * Iterate over every character in the string,
  293. * escaping with a slash or encoding to UTF-8 where necessary
  294. */
  295. for ($c = 0; $c < $strlen_var; ++$c) {
  296. $ord_var_c = ord($var{$c});
  297. switch (true) {
  298. case $ord_var_c == 0x08:
  299. $ascii .= '\b';
  300. break;
  301. case $ord_var_c == 0x09:
  302. $ascii .= '\t';
  303. break;
  304. case $ord_var_c == 0x0A:
  305. $ascii .= '\n';
  306. break;
  307. case $ord_var_c == 0x0C:
  308. $ascii .= '\f';
  309. break;
  310. case $ord_var_c == 0x0D:
  311. $ascii .= '\r';
  312. break;
  313. case $ord_var_c == 0x22:
  314. case $ord_var_c == 0x2F:
  315. case $ord_var_c == 0x5C:
  316. // double quote, slash, slosh
  317. $ascii .= '\\'.$var{$c};
  318. break;
  319. case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
  320. // characters U-00000000 - U-0000007F (same as ASCII)
  321. $ascii .= $var{$c};
  322. break;
  323. case (($ord_var_c & 0xE0) == 0xC0):
  324. // characters U-00000080 - U-000007FF, mask 110XXXXX
  325. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  326. if ($c+1 >= $strlen_var) {
  327. $c += 1;
  328. $ascii .= '?';
  329. break;
  330. }
  331. $char = pack('C*', $ord_var_c, ord($var{$c + 1}));
  332. $c += 1;
  333. $utf16 = $this->utf82utf16($char);
  334. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  335. break;
  336. case (($ord_var_c & 0xF0) == 0xE0):
  337. if ($c+2 >= $strlen_var) {
  338. $c += 2;
  339. $ascii .= '?';
  340. break;
  341. }
  342. // characters U-00000800 - U-0000FFFF, mask 1110XXXX
  343. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  344. $char = pack('C*', $ord_var_c,
  345. @ord($var{$c + 1}),
  346. @ord($var{$c + 2}));
  347. $c += 2;
  348. $utf16 = $this->utf82utf16($char);
  349. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  350. break;
  351. case (($ord_var_c & 0xF8) == 0xF0):
  352. if ($c+3 >= $strlen_var) {
  353. $c += 3;
  354. $ascii .= '?';
  355. break;
  356. }
  357. // characters U-00010000 - U-001FFFFF, mask 11110XXX
  358. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  359. $char = pack('C*', $ord_var_c,
  360. ord($var{$c + 1}),
  361. ord($var{$c + 2}),
  362. ord($var{$c + 3}));
  363. $c += 3;
  364. $utf16 = $this->utf82utf16($char);
  365. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  366. break;
  367. case (($ord_var_c & 0xFC) == 0xF8):
  368. // characters U-00200000 - U-03FFFFFF, mask 111110XX
  369. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  370. if ($c+4 >= $strlen_var) {
  371. $c += 4;
  372. $ascii .= '?';
  373. break;
  374. }
  375. $char = pack('C*', $ord_var_c,
  376. ord($var{$c + 1}),
  377. ord($var{$c + 2}),
  378. ord($var{$c + 3}),
  379. ord($var{$c + 4}));
  380. $c += 4;
  381. $utf16 = $this->utf82utf16($char);
  382. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  383. break;
  384. case (($ord_var_c & 0xFE) == 0xFC):
  385. if ($c+5 >= $strlen_var) {
  386. $c += 5;
  387. $ascii .= '?';
  388. break;
  389. }
  390. // characters U-04000000 - U-7FFFFFFF, mask 1111110X
  391. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  392. $char = pack('C*', $ord_var_c,
  393. ord($var{$c + 1}),
  394. ord($var{$c + 2}),
  395. ord($var{$c + 3}),
  396. ord($var{$c + 4}),
  397. ord($var{$c + 5}));
  398. $c += 5;
  399. $utf16 = $this->utf82utf16($char);
  400. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  401. break;
  402. }
  403. }
  404. return '"'.$ascii.'"';
  405. case 'array':
  406. /*
  407. * As per JSON spec if any array key is not an integer
  408. * we must treat the whole array as an object. We
  409. * also try to catch a sparsely populated associative
  410. * array with numeric keys here because some JS engines
  411. * will create an array with empty indexes up to
  412. * max_index which can cause memory issues and because
  413. * the keys, which may be relevant, will be remapped
  414. * otherwise.
  415. *
  416. * As per the ECMA and JSON specification an object may
  417. * have any string as a property. Unfortunately due to
  418. * a hole in the ECMA specification if the key is a
  419. * ECMA reserved word or starts with a digit the
  420. * parameter is only accessible using ECMAScript's
  421. * bracket notation.
  422. */
  423. // treat as a JSON object
  424. if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
  425. $properties = array_map(array($this, 'name_value'),
  426. array_keys($var),
  427. array_values($var));
  428. foreach($properties as $property) {
  429. if(Services_JSON::isError($property)) {
  430. return $property;
  431. }
  432. }
  433. return '{' . join(',', $properties) . '}';
  434. }
  435. // treat it like a regular array
  436. $elements = array_map(array($this, '_encode'), $var);
  437. foreach($elements as $element) {
  438. if(Services_JSON::isError($element)) {
  439. return $element;
  440. }
  441. }
  442. return '[' . join(',', $elements) . ']';
  443. case 'object':
  444. // support toJSON methods.
  445. if (($this->use & SERVICES_JSON_USE_TO_JSON) && method_exists($var, 'toJSON')) {
  446. // this may end up allowing unlimited recursion
  447. // so we check the return value to make sure it's not got the same method.
  448. $recode = $var->toJSON();
  449. if (method_exists($recode, 'toJSON')) {
  450. return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
  451. ? 'null'
  452. : new Services_JSON_Error(get_class($var).
  453. " toJSON returned an object with a toJSON method.");
  454. }
  455. return $this->_encode( $recode );
  456. }
  457. $vars = get_object_vars($var);
  458. $properties = array_map(array($this, 'name_value'),
  459. array_keys($vars),
  460. array_values($vars));
  461. foreach($properties as $property) {
  462. if(Services_JSON::isError($property)) {
  463. return $property;
  464. }
  465. }
  466. return '{' . join(',', $properties) . '}';
  467. default:
  468. return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
  469. ? 'null'
  470. : new Services_JSON_Error(gettype($var)." can not be encoded as JSON string");
  471. }
  472. }
  473. /**
  474. * array-walking function for use in generating JSON-formatted name-value pairs
  475. *
  476. * @param string $name name of key to use
  477. * @param mixed $value reference to an array element to be encoded
  478. *
  479. * @return string JSON-formatted name-value pair, like '"name":value'
  480. * @access private
  481. */
  482. function name_value($name, $value)
  483. {
  484. $encoded_value = $this->_encode($value);
  485. if(Services_JSON::isError($encoded_value)) {
  486. return $encoded_value;
  487. }
  488. return $this->_encode(strval($name)) . ':' . $encoded_value;
  489. }
  490. /**
  491. * reduce a string by removing leading and trailing comments and whitespace
  492. *
  493. * @param $str string string value to strip of comments and whitespace
  494. *
  495. * @return string string value stripped of comments and whitespace
  496. * @access private
  497. */
  498. function reduce_string($str)
  499. {
  500. $str = preg_replace(array(
  501. // eliminate single line comments in '// ...' form
  502. '#^\s*//(.+)$#m',
  503. // eliminate multi-line comments in '/* ... */' form, at start of string
  504. '#^\s*/\*(.+)\*/#Us',
  505. // eliminate multi-line comments in '/* ... */' form, at end of string
  506. '#/\*(.+)\*/\s*$#Us'
  507. ), '', $str);
  508. // eliminate extraneous space
  509. return trim($str);
  510. }
  511. /**
  512. * decodes a JSON string into appropriate variable
  513. *
  514. * @param string $str JSON-formatted string
  515. *
  516. * @return mixed number, boolean, string, array, or object
  517. * corresponding to given JSON input string.
  518. * See argument 1 to Services_JSON() above for object-output behavior.
  519. * Note that decode() always returns strings
  520. * in ASCII or UTF-8 format!
  521. * @access public
  522. */
  523. function decode($str)
  524. {
  525. $str = $this->reduce_string($str);
  526. switch (strtolower($str)) {
  527. case 'true':
  528. return true;
  529. case 'false':
  530. return false;
  531. case 'null':
  532. return null;
  533. default:
  534. $m = array();
  535. if (is_numeric($str)) {
  536. // Lookie-loo, it's a number
  537. // This would work on its own, but I'm trying to be
  538. // good about returning integers where appropriate:
  539. // return (float)$str;
  540. // Return float or int, as appropriate
  541. return ((float)$str == (integer)$str)
  542. ? (integer)$str
  543. : (float)$str;
  544. } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) {
  545. // STRINGS RETURNED IN UTF-8 FORMAT
  546. $delim = $this->substr8($str, 0, 1);
  547. $chrs = $this->substr8($str, 1, -1);
  548. $utf8 = '';
  549. $strlen_chrs = $this->strlen8($chrs);
  550. for ($c = 0; $c < $strlen_chrs; ++$c) {
  551. $substr_chrs_c_2 = $this->substr8($chrs, $c, 2);
  552. $ord_chrs_c = ord($chrs{$c});
  553. switch (true) {
  554. case $substr_chrs_c_2 == '\b':
  555. $utf8 .= chr(0x08);
  556. ++$c;
  557. break;
  558. case $substr_chrs_c_2 == '\t':
  559. $utf8 .= chr(0x09);
  560. ++$c;
  561. break;
  562. case $substr_chrs_c_2 == '\n':
  563. $utf8 .= chr(0x0A);
  564. ++$c;
  565. break;
  566. case $substr_chrs_c_2 == '\f':
  567. $utf8 .= chr(0x0C);
  568. ++$c;
  569. break;
  570. case $substr_chrs_c_2 == '\r':
  571. $utf8 .= chr(0x0D);
  572. ++$c;
  573. break;
  574. case $substr_chrs_c_2 == '\\"':
  575. case $substr_chrs_c_2 == '\\\'':
  576. case $substr_chrs_c_2 == '\\\\':
  577. case $substr_chrs_c_2 == '\\/':
  578. if (($delim == '"' && $substr_chrs_c_2 != '\\\'') ||
  579. ($delim == "'" && $substr_chrs_c_2 != '\\"')) {
  580. $utf8 .= $chrs{++$c};
  581. }
  582. break;
  583. case preg_match('/\\\u[0-9A-F]{4}/i', $this->substr8($chrs, $c, 6)):
  584. // single, escaped unicode character
  585. $utf16 = chr(hexdec($this->substr8($chrs, ($c + 2), 2)))
  586. . chr(hexdec($this->substr8($chrs, ($c + 4), 2)));
  587. $utf8 .= $this->utf162utf8($utf16);
  588. $c += 5;
  589. break;
  590. case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F):
  591. $utf8 .= $chrs{$c};
  592. break;
  593. case ($ord_chrs_c & 0xE0) == 0xC0:
  594. // characters U-00000080 - U-000007FF, mask 110XXXXX
  595. //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  596. $utf8 .= $this->substr8($chrs, $c, 2);
  597. ++$c;
  598. break;
  599. case ($ord_chrs_c & 0xF0) == 0xE0:
  600. // characters U-00000800 - U-0000FFFF, mask 1110XXXX
  601. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  602. $utf8 .= $this->substr8($chrs, $c, 3);
  603. $c += 2;
  604. break;
  605. case ($ord_chrs_c & 0xF8) == 0xF0:
  606. // characters U-00010000 - U-001FFFFF, mask 11110XXX
  607. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  608. $utf8 .= $this->substr8($chrs, $c, 4);
  609. $c += 3;
  610. break;
  611. case ($ord_chrs_c & 0xFC) == 0xF8:
  612. // characters U-00200000 - U-03FFFFFF, mask 111110XX
  613. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  614. $utf8 .= $this->substr8($chrs, $c, 5);
  615. $c += 4;
  616. break;
  617. case ($ord_chrs_c & 0xFE) == 0xFC:
  618. // characters U-04000000 - U-7FFFFFFF, mask 1111110X
  619. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  620. $utf8 .= $this->substr8($chrs, $c, 6);
  621. $c += 5;
  622. break;
  623. }
  624. }
  625. return $utf8;
  626. } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) {
  627. // array, or object notation
  628. if ($str{0} == '[') {
  629. $stk = array(SERVICES_JSON_IN_ARR);
  630. $arr = array();
  631. } else {
  632. if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
  633. $stk = array(SERVICES_JSON_IN_OBJ);
  634. $obj = array();
  635. } else {
  636. $stk = array(SERVICES_JSON_IN_OBJ);
  637. $obj = new stdClass();
  638. }
  639. }
  640. array_push($stk, array('what' => SERVICES_JSON_SLICE,
  641. 'where' => 0,
  642. 'delim' => false));
  643. $chrs = $this->substr8($str, 1, -1);
  644. $chrs = $this->reduce_string($chrs);
  645. if ($chrs == '') {
  646. if (reset($stk) == SERVICES_JSON_IN_ARR) {
  647. return $arr;
  648. } else {
  649. return $obj;
  650. }
  651. }
  652. //print("\nparsing {$chrs}\n");
  653. $strlen_chrs = $this->strlen8($chrs);
  654. for ($c = 0; $c <= $strlen_chrs; ++$c) {
  655. $top = end($stk);
  656. $substr_chrs_c_2 = $this->substr8($chrs, $c, 2);
  657. if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) {
  658. // found a comma that is not inside a string, array, etc.,
  659. // OR we've reached the end of the character list
  660. $slice = $this->substr8($chrs, $top['where'], ($c - $top['where']));
  661. array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
  662. //print("Found split at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  663. if (reset($stk) == SERVICES_JSON_IN_ARR) {
  664. // we are in an array, so just push an element onto the stack
  665. array_push($arr, $this->decode($slice));
  666. } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
  667. // we are in an object, so figure
  668. // out the property name and set an
  669. // element in an associative array,
  670. // for now
  671. $parts = array();
  672. if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:/Uis', $slice, $parts)) {
  673. // "name":value pair
  674. $key = $this->decode($parts[1]);
  675. $val = $this->decode(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));
  676. if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
  677. $obj[$key] = $val;
  678. } else {
  679. $obj->$key = $val;
  680. }
  681. } elseif (preg_match('/^\s*(\w+)\s*:/Uis', $slice, $parts)) {
  682. // name:value pair, where name is unquoted
  683. $key = $parts[1];
  684. $val = $this->decode(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));
  685. if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
  686. $obj[$key] = $val;
  687. } else {
  688. $obj->$key = $val;
  689. }
  690. }
  691. }
  692. } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) {
  693. // found a quote, and we are not inside a string
  694. array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c}));
  695. //print("Found start of string at {$c}\n");
  696. } elseif (($chrs{$c} == $top['delim']) &&
  697. ($top['what'] == SERVICES_JSON_IN_STR) &&
  698. (($this->strlen8($this->substr8($chrs, 0, $c)) - $this->strlen8(rtrim($this->substr8($chrs, 0, $c), '\\'))) % 2 != 1)) {
  699. // found a quote, we're in a string, and it's not escaped
  700. // we know that it's not escaped becase there is _not_ an
  701. // odd number of backslashes at the end of the string so far
  702. array_pop($stk);
  703. //print("Found end of string at {$c}: ".$this->substr8($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
  704. } elseif (($chrs{$c} == '[') &&
  705. in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
  706. // found a left-bracket, and we are in an array, object, or slice
  707. array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false));
  708. //print("Found start of array at {$c}\n");
  709. } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) {
  710. // found a right-bracket, and we're in an array
  711. array_pop($stk);
  712. //print("Found end of array at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  713. } elseif (($chrs{$c} == '{') &&
  714. in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
  715. // found a left-brace, and we are in an array, object, or slice
  716. array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false));
  717. //print("Found start of object at {$c}\n");
  718. } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) {
  719. // found a right-brace, and we're in an object
  720. array_pop($stk);
  721. //print("Found end of object at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  722. } elseif (($substr_chrs_c_2 == '/*') &&
  723. in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
  724. // found a comment start, and we are in an array, object, or slice
  725. array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false));
  726. $c++;
  727. //print("Found start of comment at {$c}\n");
  728. } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) {
  729. // found a comment end, and we're in one now
  730. array_pop($stk);
  731. $c++;
  732. for ($i = $top['where']; $i <= $c; ++$i)
  733. $chrs = substr_replace($chrs, ' ', $i, 1);
  734. //print("Found end of comment at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  735. }
  736. }
  737. if (reset($stk) == SERVICES_JSON_IN_ARR) {
  738. return $arr;
  739. } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
  740. return $obj;
  741. }
  742. }
  743. }
  744. }
  745. /**
  746. * @todo Ultimately, this should just call PEAR::isError()
  747. */
  748. function isError($data, $code = null)
  749. {
  750. if (class_exists('pear')) {
  751. return PEAR::isError($data, $code);
  752. } elseif (is_object($data) && (get_class($data) == 'services_json_error' ||
  753. is_subclass_of($data, 'services_json_error'))) {
  754. return true;
  755. }
  756. return false;
  757. }
  758. /**
  759. * Calculates length of string in bytes
  760. * @param string
  761. * @return integer length
  762. */
  763. function strlen8( $str )
  764. {
  765. if ( $this->_mb_strlen ) {
  766. return mb_strlen( $str, "8bit" );
  767. }
  768. return strlen( $str );
  769. }
  770. /**
  771. * Returns part of a string, interpreting $start and $length as number of bytes.
  772. * @param string
  773. * @param integer start
  774. * @param integer length
  775. * @return integer length
  776. */
  777. function substr8( $string, $start, $length=false )
  778. {
  779. if ( $length === false ) {
  780. $length = $this->strlen8( $string ) - $start;
  781. }
  782. if ( $this->_mb_substr ) {
  783. return mb_substr( $string, $start, $length, "8bit" );
  784. }
  785. return substr( $string, $start, $length );
  786. }
  787. }
  788. if (class_exists('PEAR_Error')) {
  789. class Services_JSON_Error extends PEAR_Error
  790. {
  791. function __construct($message = 'unknown error', $code = null,
  792. $mode = null, $options = null, $userinfo = null)
  793. {
  794. parent::PEAR_Error($message, $code, $mode, $options, $userinfo);
  795. }
  796. public function Services_JSON_Error($message = 'unknown error', $code = null,
  797. $mode = null, $options = null, $userinfo = null) {
  798. self::__construct($message = 'unknown error', $code = null,
  799. $mode = null, $options = null, $userinfo = null);
  800. }
  801. }
  802. } else {
  803. /**
  804. * @todo Ultimately, this class shall be descended from PEAR_Error
  805. */
  806. class Services_JSON_Error
  807. {
  808. /**
  809. * PHP5 constructor.
  810. */
  811. function __construct( $message = 'unknown error', $code = null,
  812. $mode = null, $options = null, $userinfo = null )
  813. {
  814. }
  815. /**
  816. * PHP4 constructor.
  817. */
  818. public function Services_JSON_Error( $message = 'unknown error', $code = null,
  819. $mode = null, $options = null, $userinfo = null ) {
  820. self::__construct( $message, $code, $mode, $options, $userinfo );
  821. }
  822. }
  823. }
  824. endif;