-
+ FE081B86E03F39F2413BB72FA5A013ABD7065AF40B619D91FE16486F2BDBB8A9B69D803D9D6017A5D97A7A78967ACC8B7026CD28F50E7D3B2371836CBD7C359D
mp-wp/wp-includes/js/tinymce/plugins/spellchecker/classes/utils/JSON.php
(0 . 0)(1 . 594)
122946 <?php
122947 /**
122948 * $Id: JSON.php 40 2007-06-18 11:43:15Z spocke $
122949 *
122950 * @package MCManager.utils
122951 * @author Moxiecode
122952 */
122953
122954 define('JSON_BOOL', 1);
122955 define('JSON_INT', 2);
122956 define('JSON_STR', 3);
122957 define('JSON_FLOAT', 4);
122958 define('JSON_NULL', 5);
122959 define('JSON_START_OBJ', 6);
122960 define('JSON_END_OBJ', 7);
122961 define('JSON_START_ARRAY', 8);
122962 define('JSON_END_ARRAY', 9);
122963 define('JSON_KEY', 10);
122964 define('JSON_SKIP', 11);
122965
122966 define('JSON_IN_ARRAY', 30);
122967 define('JSON_IN_OBJECT', 40);
122968 define('JSON_IN_BETWEEN', 50);
122969
122970 class Moxiecode_JSONReader {
122971 var $_data, $_len, $_pos;
122972 var $_value, $_token;
122973 var $_location, $_lastLocations;
122974 var $_needProp;
122975
122976 function Moxiecode_JSONReader($data) {
122977 $this->_data = $data;
122978 $this->_len = strlen($data);
122979 $this->_pos = -1;
122980 $this->_location = JSON_IN_BETWEEN;
122981 $this->_lastLocations = array();
122982 $this->_needProp = false;
122983 }
122984
122985 function getToken() {
122986 return $this->_token;
122987 }
122988
122989 function getLocation() {
122990 return $this->_location;
122991 }
122992
122993 function getTokenName() {
122994 switch ($this->_token) {
122995 case JSON_BOOL:
122996 return 'JSON_BOOL';
122997
122998 case JSON_INT:
122999 return 'JSON_INT';
123000
123001 case JSON_STR:
123002 return 'JSON_STR';
123003
123004 case JSON_FLOAT:
123005 return 'JSON_FLOAT';
123006
123007 case JSON_NULL:
123008 return 'JSON_NULL';
123009
123010 case JSON_START_OBJ:
123011 return 'JSON_START_OBJ';
123012
123013 case JSON_END_OBJ:
123014 return 'JSON_END_OBJ';
123015
123016 case JSON_START_ARRAY:
123017 return 'JSON_START_ARRAY';
123018
123019 case JSON_END_ARRAY:
123020 return 'JSON_END_ARRAY';
123021
123022 case JSON_KEY:
123023 return 'JSON_KEY';
123024 }
123025
123026 return 'UNKNOWN';
123027 }
123028
123029 function getValue() {
123030 return $this->_value;
123031 }
123032
123033 function readToken() {
123034 $chr = $this->read();
123035
123036 if ($chr != null) {
123037 switch ($chr) {
123038 case '[':
123039 $this->_lastLocation[] = $this->_location;
123040 $this->_location = JSON_IN_ARRAY;
123041 $this->_token = JSON_START_ARRAY;
123042 $this->_value = null;
123043 $this->readAway();
123044 return true;
123045
123046 case ']':
123047 $this->_location = array_pop($this->_lastLocation);
123048 $this->_token = JSON_END_ARRAY;
123049 $this->_value = null;
123050 $this->readAway();
123051
123052 if ($this->_location == JSON_IN_OBJECT)
123053 $this->_needProp = true;
123054
123055 return true;
123056
123057 case '{':
123058 $this->_lastLocation[] = $this->_location;
123059 $this->_location = JSON_IN_OBJECT;
123060 $this->_needProp = true;
123061 $this->_token = JSON_START_OBJ;
123062 $this->_value = null;
123063 $this->readAway();
123064 return true;
123065
123066 case '}':
123067 $this->_location = array_pop($this->_lastLocation);
123068 $this->_token = JSON_END_OBJ;
123069 $this->_value = null;
123070 $this->readAway();
123071
123072 if ($this->_location == JSON_IN_OBJECT)
123073 $this->_needProp = true;
123074
123075 return true;
123076
123077 // String
123078 case '"':
123079 case '\'':
123080 return $this->_readString($chr);
123081
123082 // Null
123083 case 'n':
123084 return $this->_readNull();
123085
123086 // Bool
123087 case 't':
123088 case 'f':
123089 return $this->_readBool($chr);
123090
123091 default:
123092 // Is number
123093 if (is_numeric($chr) || $chr == '-' || $chr == '.')
123094 return $this->_readNumber($chr);
123095
123096 return true;
123097 }
123098 }
123099
123100 return false;
123101 }
123102
123103 function _readBool($chr) {
123104 $this->_token = JSON_BOOL;
123105 $this->_value = $chr == 't';
123106
123107 if ($chr == 't')
123108 $this->skip(3); // rue
123109 else
123110 $this->skip(4); // alse
123111
123112 $this->readAway();
123113
123114 if ($this->_location == JSON_IN_OBJECT && !$this->_needProp)
123115 $this->_needProp = true;
123116
123117 return true;
123118 }
123119
123120 function _readNull() {
123121 $this->_token = JSON_NULL;
123122 $this->_value = null;
123123
123124 $this->skip(3); // ull
123125 $this->readAway();
123126
123127 if ($this->_location == JSON_IN_OBJECT && !$this->_needProp)
123128 $this->_needProp = true;
123129
123130 return true;
123131 }
123132
123133 function _readString($quote) {
123134 $output = "";
123135 $this->_token = JSON_STR;
123136 $endString = false;
123137
123138 while (($chr = $this->peek()) != -1) {
123139 switch ($chr) {
123140 case '\\':
123141 // Read away slash
123142 $this->read();
123143
123144 // Read escape code
123145 $chr = $this->read();
123146 switch ($chr) {
123147 case 't':
123148 $output .= "\t";
123149 break;
123150
123151 case 'b':
123152 $output .= "\b";
123153 break;
123154
123155 case 'f':
123156 $output .= "\f";
123157 break;
123158
123159 case 'r':
123160 $output .= "\r";
123161 break;
123162
123163 case 'n':
123164 $output .= "\n";
123165 break;
123166
123167 case 'u':
123168 $output .= $this->_int2utf8(hexdec($this->read(4)));
123169 break;
123170
123171 default:
123172 $output .= $chr;
123173 break;
123174 }
123175
123176 break;
123177
123178 case '\'':
123179 case '"':
123180 if ($chr == $quote)
123181 $endString = true;
123182
123183 $chr = $this->read();
123184 if ($chr != -1 && $chr != $quote)
123185 $output .= $chr;
123186
123187 break;
123188
123189 default:
123190 $output .= $this->read();
123191 }
123192
123193 // String terminated
123194 if ($endString)
123195 break;
123196 }
123197
123198 $this->readAway();
123199 $this->_value = $output;
123200
123201 // Needed a property
123202 if ($this->_needProp) {
123203 $this->_token = JSON_KEY;
123204 $this->_needProp = false;
123205 return true;
123206 }
123207
123208 if ($this->_location == JSON_IN_OBJECT && !$this->_needProp)
123209 $this->_needProp = true;
123210
123211 return true;
123212 }
123213
123214 function _int2utf8($int) {
123215 $int = intval($int);
123216
123217 switch ($int) {
123218 case 0:
123219 return chr(0);
123220
123221 case ($int & 0x7F):
123222 return chr($int);
123223
123224 case ($int & 0x7FF):
123225 return chr(0xC0 | (($int >> 6) & 0x1F)) . chr(0x80 | ($int & 0x3F));
123226
123227 case ($int & 0xFFFF):
123228 return chr(0xE0 | (($int >> 12) & 0x0F)) . chr(0x80 | (($int >> 6) & 0x3F)) . chr (0x80 | ($int & 0x3F));
123229
123230 case ($int & 0x1FFFFF):
123231 return chr(0xF0 | ($int >> 18)) . chr(0x80 | (($int >> 12) & 0x3F)) . chr(0x80 | (($int >> 6) & 0x3F)) . chr(0x80 | ($int & 0x3F));
123232 }
123233 }
123234
123235 function _readNumber($start) {
123236 $value = "";
123237 $isFloat = false;
123238
123239 $this->_token = JSON_INT;
123240 $value .= $start;
123241
123242 while (($chr = $this->peek()) != -1) {
123243 if (is_numeric($chr) || $chr == '-' || $chr == '.') {
123244 if ($chr == '.')
123245 $isFloat = true;
123246
123247 $value .= $this->read();
123248 } else
123249 break;
123250 }
123251
123252 $this->readAway();
123253
123254 if ($isFloat) {
123255 $this->_token = JSON_FLOAT;
123256 $this->_value = floatval($value);
123257 } else
123258 $this->_value = intval($value);
123259
123260 if ($this->_location == JSON_IN_OBJECT && !$this->_needProp)
123261 $this->_needProp = true;
123262
123263 return true;
123264 }
123265
123266 function readAway() {
123267 while (($chr = $this->peek()) != null) {
123268 if ($chr != ':' && $chr != ',' && $chr != ' ')
123269 return;
123270
123271 $this->read();
123272 }
123273 }
123274
123275 function read($len = 1) {
123276 if ($this->_pos < $this->_len) {
123277 if ($len > 1) {
123278 $str = substr($this->_data, $this->_pos + 1, $len);
123279 $this->_pos += $len;
123280
123281 return $str;
123282 } else
123283 return $this->_data[++$this->_pos];
123284 }
123285
123286 return null;
123287 }
123288
123289 function skip($len) {
123290 $this->_pos += $len;
123291 }
123292
123293 function peek() {
123294 if ($this->_pos < $this->_len)
123295 return $this->_data[$this->_pos + 1];
123296
123297 return null;
123298 }
123299 }
123300
123301 /**
123302 * This class handles JSON stuff.
123303 *
123304 * @package MCManager.utils
123305 */
123306 class Moxiecode_JSON {
123307 function Moxiecode_JSON() {
123308 }
123309
123310 function decode($input) {
123311 $reader = new Moxiecode_JSONReader($input);
123312
123313 return $this->readValue($reader);
123314 }
123315
123316 function readValue(&$reader) {
123317 $this->data = array();
123318 $this->parents = array();
123319 $this->cur =& $this->data;
123320 $key = null;
123321 $loc = JSON_IN_ARRAY;
123322
123323 while ($reader->readToken()) {
123324 switch ($reader->getToken()) {
123325 case JSON_STR:
123326 case JSON_INT:
123327 case JSON_BOOL:
123328 case JSON_FLOAT:
123329 case JSON_NULL:
123330 switch ($reader->getLocation()) {
123331 case JSON_IN_OBJECT:
123332 $this->cur[$key] = $reader->getValue();
123333 break;
123334
123335 case JSON_IN_ARRAY:
123336 $this->cur[] = $reader->getValue();
123337 break;
123338
123339 default:
123340 return $reader->getValue();
123341 }
123342 break;
123343
123344 case JSON_KEY:
123345 $key = $reader->getValue();
123346 break;
123347
123348 case JSON_START_OBJ:
123349 case JSON_START_ARRAY:
123350 if ($loc == JSON_IN_OBJECT)
123351 $this->addArray($key);
123352 else
123353 $this->addArray(null);
123354
123355 $cur =& $obj;
123356
123357 $loc = $reader->getLocation();
123358 break;
123359
123360 case JSON_END_OBJ:
123361 case JSON_END_ARRAY:
123362 $loc = $reader->getLocation();
123363
123364 if (count($this->parents) > 0) {
123365 $this->cur =& $this->parents[count($this->parents) - 1];
123366 array_pop($this->parents);
123367 }
123368 break;
123369 }
123370 }
123371
123372 return $this->data[0];
123373 }
123374
123375 // This method was needed since PHP is crapy and doesn't have pointers/references
123376 function addArray($key) {
123377 $this->parents[] =& $this->cur;
123378 $ar = array();
123379
123380 if ($key)
123381 $this->cur[$key] =& $ar;
123382 else
123383 $this->cur[] =& $ar;
123384
123385 $this->cur =& $ar;
123386 }
123387
123388 function getDelim($index, &$reader) {
123389 switch ($reader->getLocation()) {
123390 case JSON_IN_ARRAY:
123391 case JSON_IN_OBJECT:
123392 if ($index > 0)
123393 return ",";
123394 break;
123395 }
123396
123397 return "";
123398 }
123399
123400 function encode($input) {
123401 switch (gettype($input)) {
123402 case 'boolean':
123403 return $input ? 'true' : 'false';
123404
123405 case 'integer':
123406 return (int) $input;
123407
123408 case 'float':
123409 case 'double':
123410 return (float) $input;
123411
123412 case 'NULL':
123413 return 'null';
123414
123415 case 'string':
123416 return $this->encodeString($input);
123417
123418 case 'array':
123419 return $this->_encodeArray($input);
123420
123421 case 'object':
123422 return $this->_encodeArray(get_object_vars($input));
123423 }
123424
123425 return '';
123426 }
123427
123428 function encodeString($input) {
123429 // Needs to be escaped
123430 if (preg_match('/[^a-zA-Z0-9]/', $input)) {
123431 $output = '';
123432
123433 for ($i=0; $i<strlen($input); $i++) {
123434 switch ($input[$i]) {
123435 case "\b":
123436 $output .= "\\b";
123437 break;
123438
123439 case "\t":
123440 $output .= "\\t";
123441 break;
123442
123443 case "\f":
123444 $output .= "\\f";
123445 break;
123446
123447 case "\r":
123448 $output .= "\\r";
123449 break;
123450
123451 case "\n":
123452 $output .= "\\n";
123453 break;
123454
123455 case '\\':
123456 $output .= "\\\\";
123457 break;
123458
123459 case '\'':
123460 $output .= "\\'";
123461 break;
123462
123463 case '"':
123464 $output .= '\"';
123465 break;
123466
123467 default:
123468 $byte = ord($input[$i]);
123469
123470 if (($byte & 0xE0) == 0xC0) {
123471 $char = pack('C*', $byte, ord($input[$i + 1]));
123472 $i += 1;
123473 $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
123474 } if (($byte & 0xF0) == 0xE0) {
123475 $char = pack('C*', $byte, ord($input[$i + 1]), ord($input[$i + 2]));
123476 $i += 2;
123477 $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
123478 } if (($byte & 0xF8) == 0xF0) {
123479 $char = pack('C*', $byte, ord($input[$i + 1]), ord($input[$i + 2], ord($input[$i + 3])));
123480 $i += 3;
123481 $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
123482 } if (($byte & 0xFC) == 0xF8) {
123483 $char = pack('C*', $byte, ord($input[$i + 1]), ord($input[$i + 2], ord($input[$i + 3]), ord($input[$i + 4])));
123484 $i += 4;
123485 $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
123486 } if (($byte & 0xFE) == 0xFC) {
123487 $char = pack('C*', $byte, ord($input[$i + 1]), ord($input[$i + 2], ord($input[$i + 3]), ord($input[$i + 4]), ord($input[$i + 5])));
123488 $i += 5;
123489 $output .= sprintf('\u%04s', bin2hex($this->_utf82utf16($char)));
123490 } else if ($byte < 128)
123491 $output .= $input[$i];
123492 }
123493 }
123494
123495 return '"' . $output . '"';
123496 }
123497
123498 return '"' . $input . '"';
123499 }
123500
123501 function _utf82utf16($utf8) {
123502 if (function_exists('mb_convert_encoding'))
123503 return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
123504
123505 switch (strlen($utf8)) {
123506 case 1:
123507 return $utf8;
123508
123509 case 2:
123510 return chr(0x07 & (ord($utf8[0]) >> 2)) . chr((0xC0 & (ord($utf8[0]) << 6)) | (0x3F & ord($utf8[1])));
123511
123512 case 3:
123513 return chr((0xF0 & (ord($utf8[0]) << 4)) | (0x0F & (ord($utf8[1]) >> 2))) . chr((0xC0 & (ord($utf8[1]) << 6)) | (0x7F & ord($utf8[2])));
123514 }
123515
123516 return '';
123517 }
123518
123519 function _encodeArray($input) {
123520 $output = '';
123521 $isIndexed = true;
123522
123523 $keys = array_keys($input);
123524 for ($i=0; $i<count($keys); $i++) {
123525 if (!is_int($keys[$i])) {
123526 $output .= $this->encodeString($keys[$i]) . ':' . $this->encode($input[$keys[$i]]);
123527 $isIndexed = false;
123528 } else
123529 $output .= $this->encode($input[$keys[$i]]);
123530
123531 if ($i != count($keys) - 1)
123532 $output .= ',';
123533 }
123534
123535 return $isIndexed ? '[' . $output . ']' : '{' . $output . '}';
123536 }
123537 }
123538
123539 ?>