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