PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/contrib/json.php

https://github.com/matthiask/swisdk2
PHP | 437 lines | 290 code | 57 blank | 90 comment | 60 complexity | df7ecee91ffe48b627ef957cd32944fd MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 4 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 2005 Michal Migurski |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 3.0 of the PHP license, |
  8. // | that is bundled with this package in the file LICENSE, and is |
  9. // | available through the world-wide-web at the following url: |
  10. // | http://www.php.net/license/3_0.txt. |
  11. // | If you did not receive a copy of the PHP license and are unable to |
  12. // | obtain it through the world-wide-web, please send a note to |
  13. // | license@php.net so we can mail you a copy immediately. |
  14. // +----------------------------------------------------------------------+
  15. // | Author: Michal Migurski, mike-json[at]teczno[dot]com |
  16. // | with contributions from: |
  17. // | Matt Knapp, mdknapp[at]gmail[dot]com |
  18. // | Brett Stimmerman, brettstimmerman[at]gmail[dot]com |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: HTML_AJAX_JSON.php,v 1.16 2005/06/19 00:46:05 migurski Exp $
  22. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  23. define('HTML_AJAX_JSON_SLICE', 1);
  24. define('HTML_AJAX_JSON_IN_STR', 2);
  25. define('HTML_AJAX_JSON_IN_ARR', 4);
  26. define('HTML_AJAX_JSON_IN_OBJ', 8);
  27. define('HTML_AJAX_JSON_IN_CMT', 16);
  28. define('HTML_AJAX_JSON_LOOSE_TYPE', 10);
  29. define('HTML_AJAX_JSON_STRICT_TYPE', 11);
  30. /** HTML_AJAX_JSON
  31. * Conversion to and from HTML_AJAX_JSON format.
  32. * See http://json.org for details.
  33. *
  34. * note all strings should be in ASCII or UTF-8 format!
  35. */
  36. class HTML_AJAX_JSON
  37. {
  38. /** function HTML_AJAX_JSON
  39. * constructor
  40. *
  41. * @param use int object behavior: when encoding or decoding,
  42. * be loose or strict about object/array usage
  43. *
  44. * possible values:
  45. * HTML_AJAX_JSON_STRICT_TYPE - strict typing, default
  46. * "{...}" syntax creates objects in decode
  47. * HTML_AJAX_JSON_LOOSE_TYPE - loose typing
  48. * "{...}" syntax creates associative arrays in decode
  49. */
  50. function HTML_AJAX_JSON($use=HTML_AJAX_JSON_STRICT_TYPE)
  51. {
  52. $this->use = $use;
  53. }
  54. /** function encode
  55. * encode an arbitrary variable into HTML_AJAX_JSON format
  56. *
  57. * @param var mixed any number, boolean, string, array, or object to be encoded.
  58. * see argument 1 to HTML_AJAX_JSON() above for array-parsing behavior.
  59. * if var is a strng, note that encode() always expects it
  60. * to be in ASCII or UTF-8 format!
  61. *
  62. * @return string HTML_AJAX_JSON string representation of input var
  63. */
  64. function encode($var)
  65. {
  66. switch(gettype($var)) {
  67. case 'boolean':
  68. return $var ? 'true' : 'false';
  69. case 'NULL':
  70. return 'null';
  71. case 'integer':
  72. return sprintf('%d', $var);
  73. case 'double':
  74. case 'float':
  75. return sprintf('%f', $var);
  76. case 'string': // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
  77. $ascii = '';
  78. $strlen_var = strlen($var);
  79. for($c = 0; $c < $strlen_var; $c++) {
  80. $ord_var_c = ord($var{$c});
  81. if($ord_var_c == 0x08) {
  82. $ascii .= '\b';
  83. } elseif($ord_var_c == 0x09) {
  84. $ascii .= '\t';
  85. } elseif($ord_var_c == 0x0A) {
  86. $ascii .= '\n';
  87. } elseif($ord_var_c == 0x0C) {
  88. $ascii .= '\f';
  89. } elseif($ord_var_c == 0x0D) {
  90. $ascii .= '\r';
  91. } elseif(($ord_var_c == 0x22) || ($ord_var_c == 0x2F) || ($ord_var_c == 0x5C)) {
  92. $ascii .= '\\'.$var{$c}; // double quote, slash, slosh
  93. } elseif(($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)) {
  94. // characters U-00000000 - U-0000007F (same as ASCII)
  95. $ascii .= $var{$c}; // most normal ASCII chars
  96. } elseif(($ord_var_c & 0xE0) == 0xC0) {
  97. // characters U-00000080 - U-000007FF, mask 110XXXXX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  98. $char = pack('C*', $ord_var_c, ord($var{$c+1})); $c+=1;
  99. $ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'UTF-16', 'UTF-8')));
  100. } elseif(($ord_var_c & 0xF0) == 0xE0) {
  101. // characters U-00000800 - U-0000FFFF, mask 1110XXXX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  102. $char = pack('C*', $ord_var_c, ord($var{$c+1}), ord($var{$c+2})); $c+=2;
  103. $ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'UTF-16', 'UTF-8')));
  104. } elseif(($ord_var_c & 0xF8) == 0xF0) {
  105. // characters U-00010000 - U-001FFFFF, mask 11110XXX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  106. $char = pack('C*', $ord_var_c, ord($var{$c+1}), ord($var{$c+2}), ord($var{$c+3})); $c+=3;
  107. $ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'UTF-16', 'UTF-8')));
  108. } elseif(($ord_var_c & 0xFC) == 0xF8) {
  109. // characters U-00200000 - U-03FFFFFF, mask 111110XX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  110. $char = pack('C*', $ord_var_c, ord($var{$c+1}), ord($var{$c+2}), ord($var{$c+3}), ord($var{$c+4})); $c+=4;
  111. $ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'UTF-16', 'UTF-8')));
  112. } elseif(($ord_var_c & 0xFE) == 0xFC) {
  113. // characters U-04000000 - U-7FFFFFFF, mask 1111110X, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  114. $char = pack('C*', $ord_var_c, ord($var{$c+1}), ord($var{$c+2}), ord($var{$c+3}), ord($var{$c+4}), ord($var{$c+5})); $c+=5;
  115. $ascii .= sprintf('\u%04s', bin2hex(mb_convert_encoding($char, 'UTF-16', 'UTF-8')));
  116. }
  117. }
  118. return sprintf('"%s"', $ascii);
  119. case 'array':
  120. // As per HTML_AJAX_JSON spec if any array key is not an integer we must treat the the whole array as an object.
  121. // We also try to catch a sparsely populated associative array with numeric keys here because some JS
  122. // engines will create an array with empty indexes up to max_index which can cause memory issues
  123. // and because the keys, which may be relevant, will be remapped otherwise.
  124. //
  125. // As per the ECMA and HTML_AJAX_JSON specification an object may have any string as a property. Unfortunately due to a
  126. // hole in the ECMA specification if the key is a ECMA reserved word or starts with a digit the parameter is only
  127. // accessible using ECMAScript's bracket notation.
  128. // treat as a HTML_AJAX_JSON object
  129. if(is_array($var) && (array_keys($var) !== range(0, sizeof($var) - 1)))
  130. return sprintf('{%s}', join(',', array_map(array($this, 'name_value'), array_keys($var), array_values($var))));
  131. // treat it like a regular array
  132. return sprintf('[%s]', join(',', array_map(array($this, 'encode'), $var)));
  133. case 'object':
  134. $vars = get_object_vars($var);
  135. return sprintf('{%s}', join(',', array_map(array($this, 'name_value'), array_keys($vars), array_values($vars))));
  136. default:
  137. return '';
  138. }
  139. }
  140. /** function enc
  141. * alias for encode()
  142. */
  143. function enc($var)
  144. {
  145. return $this->encode($var);
  146. }
  147. /** function name_value
  148. * array-walking function for use in generating HTML_AJAX_JSON-formatted name-value pairs
  149. *
  150. * @param name string name of key to use
  151. * @param value mixed reference to an array element to be encoded
  152. *
  153. * @return string HTML_AJAX_JSON-formatted name-value pair, like '"name":value'
  154. */
  155. function name_value($name, $value)
  156. {
  157. return (sprintf("%s:%s", $this->encode(strval($name)), $this->encode($value)));
  158. }
  159. /** function reduce_string
  160. * reduce a string by removing leading and trailing comments and whitespace
  161. *
  162. * @param str string string value to strip of comments and whitespace
  163. *
  164. * @return string string value stripped of comments and whitespace
  165. */
  166. function reduce_string($str)
  167. {
  168. $str = preg_replace('#^\s*//(.+)$#m', '', $str); // eliminate single line comments in '// ...' form
  169. $str = preg_replace('#^\s*/\*(.+)\*/#Us', '', $str); // eliminate multi-line comments in '/* ... */' form, at start of string
  170. $str = preg_replace('#/\*(.+)\*/\s*$#Us', '', $str); // eliminate multi-line comments in '/* ... */' form, at end of string
  171. $str = trim($str); // eliminate extraneous space
  172. return $str;
  173. }
  174. /** function decode
  175. * decode a HTML_AJAX_JSON string into appropriate variable
  176. *
  177. * @param str string HTML_AJAX_JSON-formatted string
  178. *
  179. * @return mixed number, boolean, string, array, or object
  180. * corresponding to given HTML_AJAX_JSON input string.
  181. * see argument 1 to HTML_AJAX_JSON() above for object-output behavior.
  182. * note that decode() always returns strings
  183. * in ASCII or UTF-8 format!
  184. */
  185. function decode($str)
  186. {
  187. $str = $this->reduce_string($str);
  188. switch(strtolower($str)) {
  189. case 'true':
  190. return true;
  191. case 'false':
  192. return false;
  193. case 'null':
  194. return null;
  195. default:
  196. if(is_numeric($str)) { // Lookie-loo, it's a number
  197. // return (float)$str; // This would work on its own, but I'm trying to be good about returning integers where appropriate
  198. return ((float)$str == (integer)$str)
  199. ? (integer)$str
  200. : (float)$str;
  201. } elseif(preg_match('/^".+"$/s', $str) || preg_match('/^\'.+\'$/s', $str)) { // STRINGS RETURNED IN UTF-8 FORMAT
  202. $delim = substr($str, 0, 1);
  203. $chrs = substr($str, 1, -1);
  204. $utf8 = '';
  205. $strlen_chrs = strlen($chrs);
  206. for($c = 0; $c < $strlen_chrs; $c++) {
  207. $substr_chrs_c_2 = substr($chrs, $c, 2);
  208. $ord_chrs_c = ord($chrs{$c});
  209. if($substr_chrs_c_2 == '\b') {
  210. $utf8 .= chr(0x08); $c+=1;
  211. } elseif($substr_chrs_c_2 == '\t') {
  212. $utf8 .= chr(0x09); $c+=1;
  213. } elseif($substr_chrs_c_2 == '\n') {
  214. $utf8 .= chr(0x0A); $c+=1;
  215. } elseif($substr_chrs_c_2 == '\f') {
  216. $utf8 .= chr(0x0C); $c+=1;
  217. } elseif($substr_chrs_c_2 == '\r') {
  218. $utf8 .= chr(0x0D); $c+=1;
  219. } elseif(($delim == '"') && (($substr_chrs_c_2 == '\\"') || ($substr_chrs_c_2 == '\\\\') || ($substr_chrs_c_2 == '\\/'))) {
  220. $utf8 .= $chrs{++$c};
  221. } elseif(($delim == "'") && (($substr_chrs_c_2 == '\\\'') || ($substr_chrs_c_2 == '\\\\') || ($substr_chrs_c_2 == '\\/'))) {
  222. $utf8 .= $chrs{++$c};
  223. } elseif(preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6))) { // single, escaped unicode character
  224. $utf16 = chr(hexdec(substr($chrs, ($c+2), 2))) . chr(hexdec(substr($chrs, ($c+4), 2)));
  225. $utf8 .= mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
  226. $c+=5;
  227. } elseif(($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F)) {
  228. $utf8 .= $chrs{$c};
  229. } elseif(($ord_chrs_c & 0xE0) == 0xC0) {
  230. // characters U-00000080 - U-000007FF, mask 110XXXXX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  231. $utf8 .= substr($chrs, $c, 2); $c += 1;
  232. } elseif(($ord_chrs_c & 0xF0) == 0xE0) {
  233. // characters U-00000800 - U-0000FFFF, mask 1110XXXX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  234. $utf8 .= substr($chrs, $c, 3); $c += 2;
  235. } elseif(($ord_chrs_c & 0xF8) == 0xF0) {
  236. // characters U-00010000 - U-001FFFFF, mask 11110XXX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  237. $utf8 .= substr($chrs, $c, 4); $c += 3;
  238. } elseif(($ord_chrs_c & 0xFC) == 0xF8) {
  239. // characters U-00200000 - U-03FFFFFF, mask 111110XX, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  240. $utf8 .= substr($chrs, $c, 5); $c += 4;
  241. } elseif(($ord_chrs_c & 0xFE) == 0xFC) {
  242. // characters U-04000000 - U-7FFFFFFF, mask 1111110X, see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  243. $utf8 .= substr($chrs, $c, 6); $c += 5;
  244. }
  245. }
  246. return $utf8;
  247. } elseif(preg_match('/^\[.*\]$/s', $str) || preg_match('/^{.*}$/s', $str)) { // array, or object notation
  248. if($str{0} == '[') {
  249. $stk = array(HTML_AJAX_JSON_IN_ARR);
  250. $arr = array();
  251. } else {
  252. if($this->use == HTML_AJAX_JSON_LOOSE_TYPE) {
  253. $stk = array(HTML_AJAX_JSON_IN_OBJ);
  254. $obj = array();
  255. } else {
  256. $stk = array(HTML_AJAX_JSON_IN_OBJ);
  257. $obj = new stdClass();
  258. }
  259. }
  260. array_push($stk, array('what' => HTML_AJAX_JSON_SLICE, 'where' => 0, 'delim' => false));
  261. $chrs = substr($str, 1, -1);
  262. $chrs = $this->reduce_string($chrs);
  263. if($chrs == '') {
  264. if(reset($stk) == HTML_AJAX_JSON_IN_ARR) {
  265. return $arr;
  266. } else {
  267. return $obj;
  268. }
  269. }
  270. //print("\nparsing {$chrs}\n");
  271. $strlen_chrs = strlen($chrs);
  272. for($c = 0; $c <= $strlen_chrs; $c++) {
  273. $top = end($stk);
  274. $substr_chrs_c_2 = substr($chrs, $c, 2);
  275. if(($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == HTML_AJAX_JSON_SLICE))) { // found a comma that is not inside a string, array, etc., OR we've reached the end of the character list
  276. $slice = substr($chrs, $top['where'], ($c - $top['where']));
  277. array_push($stk, array('what' => HTML_AJAX_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
  278. //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  279. if(reset($stk) == HTML_AJAX_JSON_IN_ARR) { // we are in an array, so just push an element onto the stack
  280. array_push($arr, $this->decode($slice));
  281. } elseif(reset($stk) == HTML_AJAX_JSON_IN_OBJ) { // we are in an object, so figure out the property name and set an element in an associative array, for now
  282. if(preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { // "name":value pair
  283. $key = $this->decode($parts[1]);
  284. $val = $this->decode($parts[2]);
  285. if($this->use == HTML_AJAX_JSON_LOOSE_TYPE) {
  286. $obj[$key] = $val;
  287. } else {
  288. $obj->$key = $val;
  289. }
  290. } elseif(preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { // name:value pair, where name is unquoted
  291. $key = $parts[1];
  292. $val = $this->decode($parts[2]);
  293. if($this->use == HTML_AJAX_JSON_LOOSE_TYPE) {
  294. $obj[$key] = $val;
  295. } else {
  296. $obj->$key = $val;
  297. }
  298. }
  299. }
  300. } elseif((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != HTML_AJAX_JSON_IN_STR)) { // found a quote, and we are not inside a string
  301. array_push($stk, array('what' => HTML_AJAX_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c}));
  302. //print("Found start of string at {$c}\n");
  303. } elseif(($chrs{$c} == $top['delim']) && ($top['what'] == HTML_AJAX_JSON_IN_STR) && (($chrs{$c - 1} != "\\") || ($chrs{$c - 1} == "\\" && $chrs{$c - 2} == "\\"))) { // found a quote, we're in a string, and it's not escaped
  304. array_pop($stk);
  305. //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
  306. } elseif(($chrs{$c} == '[') && in_array($top['what'], array(HTML_AJAX_JSON_SLICE, HTML_AJAX_JSON_IN_ARR, HTML_AJAX_JSON_IN_OBJ))) { // found a left-bracket, and we are in an array, object, or slice
  307. array_push($stk, array('what' => HTML_AJAX_JSON_IN_ARR, 'where' => $c, 'delim' => false));
  308. //print("Found start of array at {$c}\n");
  309. } elseif(($chrs{$c} == ']') && ($top['what'] == HTML_AJAX_JSON_IN_ARR)) { // found a right-bracket, and we're in an array
  310. array_pop($stk);
  311. //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  312. } elseif(($chrs{$c} == '{') && in_array($top['what'], array(HTML_AJAX_JSON_SLICE, HTML_AJAX_JSON_IN_ARR, HTML_AJAX_JSON_IN_OBJ))) { // found a left-brace, and we are in an array, object, or slice
  313. array_push($stk, array('what' => HTML_AJAX_JSON_IN_OBJ, 'where' => $c, 'delim' => false));
  314. //print("Found start of object at {$c}\n");
  315. } elseif(($chrs{$c} == '}') && ($top['what'] == HTML_AJAX_JSON_IN_OBJ)) { // found a right-brace, and we're in an object
  316. array_pop($stk);
  317. //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  318. } elseif(($substr_chrs_c_2 == '/*') && in_array($top['what'], array(HTML_AJAX_JSON_SLICE, HTML_AJAX_JSON_IN_ARR, HTML_AJAX_JSON_IN_OBJ))) { // found a comment start, and we are in an array, object, or slice
  319. array_push($stk, array('what' => HTML_AJAX_JSON_IN_CMT, 'where' => $c, 'delim' => false));
  320. $c++;
  321. //print("Found start of comment at {$c}\n");
  322. } elseif(($substr_chrs_c_2 == '*/') && ($top['what'] == HTML_AJAX_JSON_IN_CMT)) { // found a comment end, and we're in one now
  323. array_pop($stk);
  324. $c++;
  325. for($i = $top['where']; $i <= $c; $i++)
  326. $chrs = substr_replace($chrs, ' ', $i, 1);
  327. //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
  328. }
  329. }
  330. if(reset($stk) == HTML_AJAX_JSON_IN_ARR) {
  331. return $arr;
  332. } elseif(reset($stk) == HTML_AJAX_JSON_IN_OBJ) {
  333. return $obj;
  334. }
  335. }
  336. }
  337. }
  338. /** function dec
  339. * alias for decode()
  340. */
  341. function dec($var)
  342. {
  343. return $this->decode($var);
  344. }
  345. }
  346. ?>