PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/json/Services_JSON.php

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