PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/api/ApiFormatJson_json.php

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