PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/www/wp-includes/class-json.php

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