PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/public/ctl/mce/plugins/filemanager/classes/Utils/JSON.php

https://github.com/magwai/zkernel
PHP | 595 lines | 517 code | 63 blank | 15 comment | 32 complexity | cf584c960e39ecac32f718fe53b29538 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * $Id: JSON.php 149 2007-11-06 11:24:58Z spocke $
  4. *
  5. * @package MCManager.utils
  6. * @author Moxiecode
  7. * @copyright Copyright � 2007, Moxiecode Systems AB, All rights reserved.
  8. */
  9. define('JSON_BOOL', 1);
  10. define('JSON_INT', 2);
  11. define('JSON_STR', 3);
  12. define('JSON_FLOAT', 4);
  13. define('JSON_NULL', 5);
  14. define('JSON_START_OBJ', 6);
  15. define('JSON_END_OBJ', 7);
  16. define('JSON_START_ARRAY', 8);
  17. define('JSON_END_ARRAY', 9);
  18. define('JSON_KEY', 10);
  19. define('JSON_SKIP', 11);
  20. define('JSON_IN_ARRAY', 30);
  21. define('JSON_IN_OBJECT', 40);
  22. define('JSON_IN_BETWEEN', 50);
  23. class Moxiecode_JSONReader {
  24. var $_data, $_len, $_pos;
  25. var $_value, $_token;
  26. var $_location, $_lastLocations;
  27. var $_needProp;
  28. function Moxiecode_JSONReader($data) {
  29. $this->_data = $data;
  30. $this->_len = strlen($data);
  31. $this->_pos = -1;
  32. $this->_location = JSON_IN_BETWEEN;
  33. $this->_lastLocations = array();
  34. $this->_needProp = false;
  35. }
  36. function getToken() {
  37. return $this->_token;
  38. }
  39. function getLocation() {
  40. return $this->_location;
  41. }
  42. function getTokenName() {
  43. switch ($this->_token) {
  44. case JSON_BOOL:
  45. return 'JSON_BOOL';
  46. case JSON_INT:
  47. return 'JSON_INT';
  48. case JSON_STR:
  49. return 'JSON_STR';
  50. case JSON_FLOAT:
  51. return 'JSON_FLOAT';
  52. case JSON_NULL:
  53. return 'JSON_NULL';
  54. case JSON_START_OBJ:
  55. return 'JSON_START_OBJ';
  56. case JSON_END_OBJ:
  57. return 'JSON_END_OBJ';
  58. case JSON_START_ARRAY:
  59. return 'JSON_START_ARRAY';
  60. case JSON_END_ARRAY:
  61. return 'JSON_END_ARRAY';
  62. case JSON_KEY:
  63. return 'JSON_KEY';
  64. }
  65. return 'UNKNOWN';
  66. }
  67. function getValue() {
  68. return $this->_value;
  69. }
  70. function readToken() {
  71. $chr = $this->read();
  72. if ($chr != null) {
  73. switch ($chr) {
  74. case '[':
  75. $this->_lastLocation[] = $this->_location;
  76. $this->_location = JSON_IN_ARRAY;
  77. $this->_token = JSON_START_ARRAY;
  78. $this->_value = null;
  79. $this->readAway();
  80. return true;
  81. case ']':
  82. $this->_location = array_pop($this->_lastLocation);
  83. $this->_token = JSON_END_ARRAY;
  84. $this->_value = null;
  85. $this->readAway();
  86. if ($this->_location == JSON_IN_OBJECT)
  87. $this->_needProp = true;
  88. return true;
  89. case '{':
  90. $this->_lastLocation[] = $this->_location;
  91. $this->_location = JSON_IN_OBJECT;
  92. $this->_needProp = true;
  93. $this->_token = JSON_START_OBJ;
  94. $this->_value = null;
  95. $this->readAway();
  96. return true;
  97. case '}':
  98. $this->_location = array_pop($this->_lastLocation);
  99. $this->_token = JSON_END_OBJ;
  100. $this->_value = null;
  101. $this->readAway();
  102. if ($this->_location == JSON_IN_OBJECT)
  103. $this->_needProp = true;
  104. return true;
  105. // String
  106. case '"':
  107. case '\'':
  108. return $this->_readString($chr);
  109. // Null
  110. case 'n':
  111. return $this->_readNull();
  112. // Bool
  113. case 't':
  114. case 'f':
  115. return $this->_readBool($chr);
  116. default:
  117. // Is number
  118. if (is_numeric($chr) || $chr == '-' || $chr == '.')
  119. return $this->_readNumber($chr);
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. function _readBool($chr) {
  126. $this->_token = JSON_BOOL;
  127. $this->_value = $chr == 't';
  128. if ($chr == 't')
  129. $this->skip(3); // rue
  130. else
  131. $this->skip(4); // alse
  132. $this->readAway();
  133. if ($this->_location == JSON_IN_OBJECT && !$this->_needProp)
  134. $this->_needProp = true;
  135. return true;
  136. }
  137. function _readNull() {
  138. $this->_token = JSON_NULL;
  139. $this->_value = null;
  140. $this->skip(3); // ull
  141. $this->readAway();
  142. if ($this->_location == JSON_IN_OBJECT && !$this->_needProp)
  143. $this->_needProp = true;
  144. return true;
  145. }
  146. function _readString($quote) {
  147. $output = "";
  148. $this->_token = JSON_STR;
  149. $endString = false;
  150. while (($chr = $this->peek()) != -1) {
  151. switch ($chr) {
  152. case '\\':
  153. // Read away slash
  154. $this->read();
  155. // Read escape code
  156. $chr = $this->read();
  157. switch ($chr) {
  158. case 't':
  159. $output .= "\t";
  160. break;
  161. /*case 'b':
  162. $output .= "\b";
  163. break;*/
  164. case 'f':
  165. $output .= "\f";
  166. break;
  167. case 'r':
  168. $output .= "\r";
  169. break;
  170. case 'n':
  171. $output .= "\n";
  172. break;
  173. case 'u':
  174. $output .= $this->_int2utf8(hexdec($this->read(4)));
  175. break;
  176. default:
  177. $output .= $chr;
  178. break;
  179. }
  180. break;
  181. case '\'':
  182. case '"':
  183. if ($chr == $quote)
  184. $endString = true;
  185. $chr = $this->read();
  186. if ($chr != -1 && $chr != $quote)
  187. $output .= $chr;
  188. break;
  189. default:
  190. $output .= $this->read();
  191. }
  192. // String terminated
  193. if ($endString)
  194. break;
  195. }
  196. $this->readAway();
  197. $this->_value = $output;
  198. // Needed a property
  199. if ($this->_needProp) {
  200. $this->_token = JSON_KEY;
  201. $this->_needProp = false;
  202. return true;
  203. }
  204. if ($this->_location == JSON_IN_OBJECT && !$this->_needProp)
  205. $this->_needProp = true;
  206. return true;
  207. }
  208. function _int2utf8($int) {
  209. $int = intval($int);
  210. switch ($int) {
  211. case 0:
  212. return chr(0);
  213. case ($int & 0x7F):
  214. return chr($int);
  215. case ($int & 0x7FF):
  216. return chr(0xC0 | (($int >> 6) & 0x1F)) . chr(0x80 | ($int & 0x3F));
  217. case ($int & 0xFFFF):
  218. return chr(0xE0 | (($int >> 12) & 0x0F)) . chr(0x80 | (($int >> 6) & 0x3F)) . chr (0x80 | ($int & 0x3F));
  219. case ($int & 0x1FFFFF):
  220. return chr(0xF0 | ($int >> 18)) . chr(0x80 | (($int >> 12) & 0x3F)) . chr(0x80 | (($int >> 6) & 0x3F)) . chr(0x80 | ($int & 0x3F));
  221. }
  222. }
  223. function _readNumber($start) {
  224. $value = "";
  225. $isFloat = false;
  226. $this->_token = JSON_INT;
  227. $value .= $start;
  228. while (($chr = $this->peek()) != -1) {
  229. if (is_numeric($chr) || $chr == '-' || $chr == '.') {
  230. if ($chr == '.')
  231. $isFloat = true;
  232. $value .= $this->read();
  233. } else
  234. break;
  235. }
  236. $this->readAway();
  237. if ($isFloat) {
  238. $this->_token = JSON_FLOAT;
  239. $this->_value = floatval($value);
  240. } else
  241. $this->_value = intval($value);
  242. if ($this->_location == JSON_IN_OBJECT && !$this->_needProp)
  243. $this->_needProp = true;
  244. return true;
  245. }
  246. function readAway() {
  247. while (($chr = $this->peek()) != null) {
  248. if ($chr != ':' && $chr != ',' && $chr != ' ')
  249. return;
  250. $this->read();
  251. }
  252. }
  253. function read($len = 1) {
  254. if ($this->_pos < $this->_len) {
  255. if ($len > 1) {
  256. $str = substr($this->_data, $this->_pos + 1, $len);
  257. $this->_pos += $len;
  258. return $str;
  259. } else
  260. return $this->_data[++$this->_pos];
  261. }
  262. return null;
  263. }
  264. function skip($len) {
  265. $this->_pos += $len;
  266. }
  267. function peek() {
  268. if ($this->_pos < $this->_len)
  269. return $this->_data[$this->_pos + 1];
  270. return null;
  271. }
  272. }
  273. /**
  274. * This class handles JSON stuff.
  275. *
  276. * @package MCManager.utils
  277. */
  278. class Moxiecode_JSON {
  279. function Moxiecode_JSON() {
  280. }
  281. function decode($input) {
  282. $reader = new Moxiecode_JSONReader($input);
  283. return $this->readValue($reader);
  284. }
  285. function readValue(&$reader) {
  286. $this->data = array();
  287. $this->parents = array();
  288. $this->cur =& $this->data;
  289. $key = null;
  290. $loc = JSON_IN_ARRAY;
  291. while ($reader->readToken()) {
  292. switch ($reader->getToken()) {
  293. case JSON_STR:
  294. case JSON_INT:
  295. case JSON_BOOL:
  296. case JSON_FLOAT:
  297. case JSON_NULL:
  298. switch ($reader->getLocation()) {
  299. case JSON_IN_OBJECT:
  300. $this->cur[$key] = $reader->getValue();
  301. break;
  302. case JSON_IN_ARRAY:
  303. $this->cur[] = $reader->getValue();
  304. break;
  305. default:
  306. return $reader->getValue();
  307. }
  308. break;
  309. case JSON_KEY:
  310. $key = $reader->getValue();
  311. break;
  312. case JSON_START_OBJ:
  313. case JSON_START_ARRAY:
  314. if ($loc == JSON_IN_OBJECT)
  315. $this->addArray($key);
  316. else
  317. $this->addArray(null);
  318. $obj = isset($obj) ? $obj : null;
  319. $cur =& $obj;
  320. $loc = $reader->getLocation();
  321. break;
  322. case JSON_END_OBJ:
  323. case JSON_END_ARRAY:
  324. $loc = $reader->getLocation();
  325. if (count($this->parents) > 0) {
  326. $this->cur =& $this->parents[count($this->parents) - 1];
  327. array_pop($this->parents);
  328. }
  329. break;
  330. }
  331. }
  332. return $this->data[0];
  333. }
  334. // This method was needed since PHP is crapy and doesn't have pointers/references
  335. function addArray($key) {
  336. $this->parents[] =& $this->cur;
  337. $ar = array();
  338. if ($key)
  339. $this->cur[$key] =& $ar;
  340. else
  341. $this->cur[] =& $ar;
  342. $this->cur =& $ar;
  343. }
  344. function getDelim($index, &$reader) {
  345. switch ($reader->getLocation()) {
  346. case JSON_IN_ARRAY:
  347. case JSON_IN_OBJECT:
  348. if ($index > 0)
  349. return ",";
  350. break;
  351. }
  352. return "";
  353. }
  354. function encode($input) {
  355. switch (gettype($input)) {
  356. case 'boolean':
  357. return $input ? 'true' : 'false';
  358. case 'integer':
  359. return (int) $input;
  360. case 'float':
  361. case 'double':
  362. return (float) $input;
  363. case 'NULL':
  364. return 'null';
  365. case 'string':
  366. return $this->encodeString($input);
  367. case 'array':
  368. return $this->_encodeArray($input);
  369. case 'object':
  370. return $this->_encodeArray(get_object_vars($input));
  371. }
  372. return '';
  373. }
  374. function encodeString($input) {
  375. // Needs to be escaped
  376. if (preg_match('/[^a-zA-Z0-9]/', $input)) {
  377. $output = '';
  378. for ($i=0; $i<strlen($input); $i++) {
  379. switch ($input[$i]) {
  380. /*case "\b":
  381. $output .= "\\b";
  382. break;*/
  383. case "\t":
  384. $output .= "\\t";
  385. break;
  386. case "\f":
  387. $output .= "\\f";
  388. break;
  389. case "\r":
  390. $output .= "\\r";
  391. break;
  392. case "\n":
  393. $output .= "\\n";
  394. break;
  395. case '\\':
  396. $output .= "\\\\";
  397. break;
  398. case '\'':
  399. $output .= "\\'";
  400. break;
  401. case '"':
  402. $output .= '\"';
  403. break;
  404. default:
  405. $byte = ord($input[$i]);
  406. if (($byte & 0xE0) == 0xC0) {
  407. $char = pack('C*', $byte, ord($input[$i + 1]));
  408. $i += 1;
  409. $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
  410. } if (($byte & 0xF0) == 0xE0) {
  411. $char = pack('C*', $byte, ord($input[$i + 1]), ord($input[$i + 2]));
  412. $i += 2;
  413. $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
  414. } if (($byte & 0xF8) == 0xF0) {
  415. $char = pack('C*', $byte, ord($input[$i + 1]), ord($input[$i + 2], ord($input[$i + 3])));
  416. $i += 3;
  417. $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
  418. } if (($byte & 0xFC) == 0xF8) {
  419. $char = pack('C*', $byte, ord($input[$i + 1]), ord($input[$i + 2], ord($input[$i + 3]), ord($input[$i + 4])));
  420. $i += 4;
  421. $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
  422. } if (($byte & 0xFE) == 0xFC) {
  423. $char = pack('C*', $byte, ord($input[$i + 1]), ord($input[$i + 2], ord($input[$i + 3]), ord($input[$i + 4]), ord($input[$i + 5])));
  424. $i += 5;
  425. $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
  426. } else if ($byte < 128)
  427. $output .= $input[$i];
  428. }
  429. }
  430. return '"' . $output . '"';
  431. }
  432. return '"' . $input . '"';
  433. }
  434. function _utf82utf16($utf8) {
  435. if (function_exists('mb_convert_encoding'))
  436. return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
  437. switch (strlen($utf8)) {
  438. case 1:
  439. return $utf8;
  440. case 2:
  441. return chr(0x07 & (ord($utf8[0]) >> 2)) . chr((0xC0 & (ord($utf8[0]) << 6)) | (0x3F & ord($utf8[1])));
  442. case 3:
  443. return chr((0xF0 & (ord($utf8[0]) << 4)) | (0x0F & (ord($utf8[1]) >> 2))) . chr((0xC0 & (ord($utf8[1]) << 6)) | (0x7F & ord($utf8[2])));
  444. }
  445. return '';
  446. }
  447. function _encodeArray($input) {
  448. $output = '';
  449. $isIndexed = true;
  450. $keys = array_keys($input);
  451. for ($i=0; $i<count($keys); $i++) {
  452. if (!is_int($keys[$i])) {
  453. $output .= $this->encodeString($keys[$i]) . ':' . $this->encode($input[$keys[$i]]);
  454. $isIndexed = false;
  455. } else
  456. $output .= $this->encode($input[$keys[$i]]);
  457. if ($i != count($keys) - 1)
  458. $output .= ',';
  459. }
  460. return $isIndexed ? '[' . $output . ']' : '{' . $output . '}';
  461. }
  462. }
  463. ?>