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

/htdocs/core/lib/json.lib.php

https://bitbucket.org/speedealing/speedealing
PHP | 356 lines | 295 code | 10 blank | 51 comment | 20 complexity | fc2ed00ed4c7ad887d9c06a052684079 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2011-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2011-2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. * or see http://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/lib/json.lib.php
  21. * \brief Functions to emulate json function for PHP < 5.3 compatibility
  22. * \ingroup core
  23. */
  24. if (! function_exists('json_encode'))
  25. {
  26. /**
  27. * Implement json_encode for PHP that does not support it
  28. *
  29. * @param mixed $elements PHP Object to json encode
  30. * @return string Json encoded string
  31. */
  32. function json_encode($elements)
  33. {
  34. return 'ttt';
  35. //return dol_json_encode($elements);
  36. }
  37. }
  38. /**
  39. * Implement json_encode for PHP that does not support it
  40. *
  41. * @param mixed $elements PHP Object to json encode
  42. * @return string Json encoded string
  43. */
  44. function dol_json_encode($elements)
  45. {
  46. $num = count($elements);
  47. // determine type
  48. if (is_numeric(key($elements)))
  49. {
  50. // indexed (list)
  51. $output = '[';
  52. for ($i = 0, $last = ($num - 1); isset($elements[$i]); ++$i)
  53. {
  54. if (is_array($elements[$i])) $output.= json_encode($elements[$i]);
  55. else $output .= _val($elements[$i]);
  56. if($i !== $last) $output.= ',';
  57. }
  58. $output.= ']';
  59. }
  60. else
  61. {
  62. // associative (object)
  63. $output = '{';
  64. $last = $num - 1;
  65. $i = 0;
  66. foreach($elements as $key => $value)
  67. {
  68. $output .= '"'.$key.'":';
  69. if (is_array($value)) $output.= json_encode($value);
  70. else $output .= _val($value);
  71. if ($i !== $last) $output.= ',';
  72. ++$i;
  73. }
  74. $output.= '}';
  75. }
  76. // return
  77. return $output;
  78. }
  79. /**
  80. * Return text according to type
  81. *
  82. * @param mixed $val Value to show
  83. * @return string Formated value
  84. */
  85. function _val($val)
  86. {
  87. if (is_string($val))
  88. {
  89. // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
  90. $ascii = '';
  91. $strlen_var = strlen($val);
  92. /*
  93. * Iterate over every character in the string,
  94. * escaping with a slash or encoding to UTF-8 where necessary
  95. */
  96. for ($c = 0; $c < $strlen_var; ++$c) {
  97. $ord_var_c = ord($val{$c});
  98. switch (true) {
  99. case $ord_var_c == 0x08:
  100. $ascii .= '\b';
  101. break;
  102. case $ord_var_c == 0x09:
  103. $ascii .= '\t';
  104. break;
  105. case $ord_var_c == 0x0A:
  106. $ascii .= '\n';
  107. break;
  108. case $ord_var_c == 0x0C:
  109. $ascii .= '\f';
  110. break;
  111. case $ord_var_c == 0x0D:
  112. $ascii .= '\r';
  113. break;
  114. case $ord_var_c == 0x22:
  115. case $ord_var_c == 0x2F:
  116. case $ord_var_c == 0x5C:
  117. // double quote, slash, slosh
  118. $ascii .= '\\'.$val{$c};
  119. break;
  120. case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
  121. // characters U-00000000 - U-0000007F (same as ASCII)
  122. $ascii .= $val{$c};
  123. break;
  124. case (($ord_var_c & 0xE0) == 0xC0):
  125. // characters U-00000080 - U-000007FF, mask 110XXXXX
  126. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  127. $char = pack('C*', $ord_var_c, ord($val{$c + 1}));
  128. $c += 1;
  129. $utf16 = utf82utf16($char);
  130. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  131. break;
  132. case (($ord_var_c & 0xF0) == 0xE0):
  133. // characters U-00000800 - U-0000FFFF, mask 1110XXXX
  134. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  135. $char = pack('C*', $ord_var_c, ord($val{$c + 1}), ord($val{$c + 2}));
  136. $c += 2;
  137. $utf16 = utf82utf16($char);
  138. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  139. break;
  140. case (($ord_var_c & 0xF8) == 0xF0):
  141. // characters U-00010000 - U-001FFFFF, mask 11110XXX
  142. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  143. $char = pack('C*', $ord_var_c, ord($val{$c + 1}), ord($val{$c + 2}), ord($val{$c + 3}));
  144. $c += 3;
  145. $utf16 = utf82utf16($char);
  146. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  147. break;
  148. case (($ord_var_c & 0xFC) == 0xF8):
  149. // characters U-00200000 - U-03FFFFFF, mask 111110XX
  150. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  151. $char = pack('C*', $ord_var_c, ord($val{$c + 1}), ord($val{$c + 2}), ord($val{$c + 3}), ord($val{$c + 4}));
  152. $c += 4;
  153. $utf16 = utf82utf16($char);
  154. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  155. break;
  156. case (($ord_var_c & 0xFE) == 0xFC):
  157. // characters U-04000000 - U-7FFFFFFF, mask 1111110X
  158. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  159. $char = pack('C*', $ord_var_c, ord($val{$c + 1}), ord($val{$c + 2}), ord($val{$c + 3}), ord($val{$c + 4}), ord($val{$c + 5}));
  160. $c += 5;
  161. $utf16 = utf82utf16($char);
  162. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  163. break;
  164. }
  165. }
  166. return '"'.$ascii.'"';
  167. }
  168. elseif (is_int($val)) return sprintf('%d', $val);
  169. elseif (is_float($val)) return sprintf('%F', $val);
  170. elseif (is_bool($val)) return ($val ? 'true' : 'false');
  171. else return 'null';
  172. }
  173. if (! function_exists('json_decode'))
  174. {
  175. /**
  176. * Implement json_decode for PHP that does not support it
  177. *
  178. * @param string $json Json encoded to PHP Object or Array
  179. * @param bool $assoc False return an object, true return an array
  180. * @return mixed Object or Array
  181. */
  182. function json_decode($json, $assoc=false)
  183. {
  184. return dol_json_decode($json, $assoc);
  185. }
  186. }
  187. /**
  188. * Implement json_decode for PHP that does not support it
  189. *
  190. * @param string $json Json encoded to PHP Object or Array
  191. * @param bool $assoc False return an object, true return an array. Try to always use it with true !
  192. * @return mixed Object or Array
  193. */
  194. function dol_json_decode($json, $assoc=false)
  195. {
  196. $comment = false;
  197. $out='';
  198. $strLength = strlen($json); // Must stay strlen and not dol_strlen because we want technical length, not visible length
  199. for ($i=0; $i<$strLength; $i++)
  200. {
  201. if (! $comment)
  202. {
  203. if (($json[$i] == '{') || ($json[$i] == '[')) $out.= 'array(';
  204. else if (($json[$i] == '}') || ($json[$i] == ']')) $out.= ')';
  205. else if ($json[$i] == ':') $out.= ' => ';
  206. else $out.=$json[$i];
  207. }
  208. else $out.= $json[$i];
  209. if ($json[$i] == '"' && $json[($i-1)]!="\\") $comment = !$comment;
  210. }
  211. $out=_unval($out);
  212. // Return an array
  213. if ($out != '') eval('$array = '.$out.';');
  214. else $array=array();
  215. // Return an object
  216. if (! $assoc)
  217. {
  218. if (! empty($array))
  219. {
  220. $object = false;
  221. foreach ($array as $key => $value)
  222. {
  223. $object->{$key} = $value;
  224. }
  225. return $object;
  226. }
  227. return false;
  228. }
  229. return $array;
  230. }
  231. /**
  232. * Return text according to type
  233. *
  234. * @param mixed $val Value to decode
  235. * @return string Formated value
  236. */
  237. function _unval($val)
  238. {
  239. while (preg_match('/\\\u([0-9A-F]{2})([0-9A-F]{2})/i', $val, $reg))
  240. {
  241. // single, escaped unicode character
  242. $utf16 = chr(hexdec($reg[1])) . chr(hexdec($reg[2]));
  243. $utf8 = utf162utf8($utf16);
  244. $val=preg_replace('/\\\u'.$reg[1].$reg[2].'/i',$utf8,$val);
  245. }
  246. return $val;
  247. }
  248. /**
  249. * convert a string from one UTF-16 char to one UTF-8 char
  250. *
  251. * Normally should be handled by mb_convert_encoding, but
  252. * provides a slower PHP-only method for installations
  253. * that lack the multibye string extension.
  254. *
  255. * @param string $utf16 UTF-16 character
  256. * @return string UTF-8 character
  257. */
  258. function utf162utf8($utf16)
  259. {
  260. // oh please oh please oh please oh please oh please
  261. if(function_exists('mb_convert_encoding')) {
  262. return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
  263. }
  264. $bytes = (ord($utf16{0}) << 8) | ord($utf16{1});
  265. switch(true) {
  266. case ((0x7F & $bytes) == $bytes):
  267. // this case should never be reached, because we are in ASCII range
  268. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  269. return chr($bytes);
  270. case (0x07FF & $bytes) == $bytes:
  271. // return a 2-byte UTF-8 character
  272. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  273. return chr(0xC0 | (($bytes >> 6) & 0x1F))
  274. . chr(0x80 | ($bytes & 0x3F));
  275. case (0xFFFF & $bytes) == $bytes:
  276. // return a 3-byte UTF-8 character
  277. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  278. return chr(0xE0 | (($bytes >> 12) & 0x0F))
  279. . chr(0x80 | (($bytes >> 6) & 0x3F))
  280. . chr(0x80 | ($bytes & 0x3F));
  281. }
  282. // ignoring UTF-32 for now, sorry
  283. return '';
  284. }
  285. /**
  286. * convert a string from one UTF-8 char to one UTF-16 char
  287. *
  288. * Normally should be handled by mb_convert_encoding, but
  289. * provides a slower PHP-only method for installations
  290. * that lack the multibye string extension.
  291. *
  292. * @param string $utf8 UTF-8 character
  293. * @return string UTF-16 character
  294. */
  295. function utf82utf16($utf8)
  296. {
  297. // oh please oh please oh please oh please oh please
  298. if(function_exists('mb_convert_encoding')) {
  299. return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
  300. }
  301. switch(strlen($utf8)) {
  302. case 1:
  303. // this case should never be reached, because we are in ASCII range
  304. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  305. return $utf8;
  306. case 2:
  307. // return a UTF-16 character from a 2-byte UTF-8 char
  308. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  309. return chr(0x07 & (ord($utf8{0}) >> 2)) . chr((0xC0 & (ord($utf8{0}) << 6)) | (0x3F & ord($utf8{1})));
  310. case 3:
  311. // return a UTF-16 character from a 3-byte UTF-8 char
  312. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  313. return chr((0xF0 & (ord($utf8{0}) << 4)) | (0x0F & (ord($utf8{1}) >> 2))) . chr((0xC0 & (ord($utf8{1}) << 6)) | (0x7F & ord($utf8{2})));
  314. }
  315. // ignoring UTF-32 for now, sorry
  316. return '';
  317. }