Changeset 23141 for branches/version-2_13-dev/data/module/Services/JSON.php
- Timestamp:
- 2013/08/28 13:26:44 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_13-dev/data/module/Services/JSON.php
r23125 r23141 1 1 <?php 2 2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 3 4 /** 4 5 * Converts to and from JSON format. … … 51 52 * @author Brett Stimmerman <brettstimmerman[at]gmail[dot]com> 52 53 * @copyright 2005 Michal Migurski 53 * @version CVS: $Id : JSON.php 305040 2010-11-02 23:19:03Z alan_k$54 * @version CVS: $Id$ 54 55 * @license http://www.opensource.org/licenses/bsd-license.php 55 56 * @link http://pear.php.net/pepr/pepr-proposal-show.php?id=198 … … 90 91 */ 91 92 define('SERVICES_JSON_SUPPRESS_ERRORS', 32); 92 93 /**94 * Behavior switch for Services_JSON::decode()95 */96 define('SERVICES_JSON_USE_TO_JSON', 64);97 93 98 94 /** … … 134 130 * bubble up with an error, so all return values 135 131 * from encode() should be checked with isError() 136 * - SERVICES_JSON_USE_TO_JSON: call toJSON when serializing objects137 * It serializes the return value from the toJSON call rather138 * than the object it'self, toJSON can return associative arrays,139 * strings or numbers, if you return an object, make sure it does140 * not have a toJSON method, otherwise an error will occur.141 132 */ 142 133 function Services_JSON($use = 0) 143 134 { 144 135 $this->use = $use; 145 $this->_mb_strlen = function_exists('mb_strlen'); 146 $this->_mb_convert_encoding = function_exists('mb_convert_encoding'); 147 $this->_mb_substr = function_exists('mb_substr'); 148 } 149 // private - cache the mbstring lookup results.. 150 var $_mb_strlen = false; 151 var $_mb_substr = false; 152 var $_mb_convert_encoding = false; 153 136 } 137 154 138 /** 155 139 * convert a string from one UTF-16 char to one UTF-8 char … … 166 150 { 167 151 // oh please oh please oh please oh please oh please 168 if( $this->_mb_convert_encoding) {152 if(function_exists('mb_convert_encoding')) { 169 153 return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16'); 170 154 } … … 210 194 { 211 195 // oh please oh please oh please oh please oh please 212 if( $this->_mb_convert_encoding) {196 if(function_exists('mb_convert_encoding')) { 213 197 return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8'); 214 198 } 215 199 216 switch( $this->strlen8($utf8)) {200 switch(strlen($utf8)) { 217 201 case 1: 218 202 // this case should never be reached, because we are in ASCII range … … 241 225 242 226 /** 243 * encodes an arbitrary variable into JSON format (and sends JSON Header)227 * encodes an arbitrary variable into JSON format 244 228 * 245 229 * @param mixed $var any number, boolean, string, array, or object to be encoded. … … 253 237 function encode($var) 254 238 { 255 header('Content-type: application/json');256 return $this->encodeUnsafe($var);257 }258 /**259 * encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!)260 *261 * @param mixed $var any number, boolean, string, array, or object to be encoded.262 * see argument 1 to Services_JSON() above for array-parsing behavior.263 * if var is a strng, note that encode() always expects it264 * to be in ASCII or UTF-8 format!265 *266 * @return mixed JSON string representation of input var or an error if a problem occurs267 * @access public268 */269 function encodeUnsafe($var)270 {271 // see bug #16908 - regarding numeric locale printing272 $lc = setlocale(LC_NUMERIC, 0);273 setlocale(LC_NUMERIC, 'C');274 $ret = $this->_encode($var);275 setlocale(LC_NUMERIC, $lc);276 return $ret;277 278 }279 /**280 * PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format281 *282 * @param mixed $var any number, boolean, string, array, or object to be encoded.283 * see argument 1 to Services_JSON() above for array-parsing behavior.284 * if var is a strng, note that encode() always expects it285 * to be in ASCII or UTF-8 format!286 *287 * @return mixed JSON string representation of input var or an error if a problem occurs288 * @access public289 */290 function _encode($var)291 {292 293 239 switch (gettype($var)) { 294 240 case 'boolean': … … 303 249 case 'double': 304 250 case 'float': 305 return 251 return (float) $var; 306 252 307 253 case 'string': 308 254 // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT 309 255 $ascii = ''; 310 $strlen_var = $this->strlen8($var);256 $strlen_var = strlen($var); 311 257 312 258 /* … … 350 296 // characters U-00000080 - U-000007FF, mask 110XXXXX 351 297 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 352 if ($c+1 >= $strlen_var) {353 $c += 1;354 $ascii .= '?';355 break;356 }357 358 298 $char = pack('C*', $ord_var_c, ord($var{$c + 1})); 359 299 $c += 1; … … 363 303 364 304 case (($ord_var_c & 0xF0) == 0xE0): 365 if ($c+2 >= $strlen_var) {366 $c += 2;367 $ascii .= '?';368 break;369 }370 305 // characters U-00000800 - U-0000FFFF, mask 1110XXXX 371 306 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 372 307 $char = pack('C*', $ord_var_c, 373 @ord($var{$c + 1}),374 @ord($var{$c + 2}));308 ord($var{$c + 1}), 309 ord($var{$c + 2})); 375 310 $c += 2; 376 311 $utf16 = $this->utf82utf16($char); … … 379 314 380 315 case (($ord_var_c & 0xF8) == 0xF0): 381 if ($c+3 >= $strlen_var) {382 $c += 3;383 $ascii .= '?';384 break;385 }386 316 // characters U-00010000 - U-001FFFFF, mask 11110XXX 387 317 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 … … 398 328 // characters U-00200000 - U-03FFFFFF, mask 111110XX 399 329 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 400 if ($c+4 >= $strlen_var) {401 $c += 4;402 $ascii .= '?';403 break;404 }405 330 $char = pack('C*', $ord_var_c, 406 331 ord($var{$c + 1}), … … 414 339 415 340 case (($ord_var_c & 0xFE) == 0xFC): 416 if ($c+5 >= $strlen_var) {417 $c += 5;418 $ascii .= '?';419 break;420 }421 341 // characters U-04000000 - U-7FFFFFFF, mask 1111110X 422 342 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 … … 433 353 } 434 354 } 435 return '"'.$ascii.'"'; 355 356 return '"'.$ascii.'"'; 436 357 437 358 case 'array': … … 470 391 471 392 // treat it like a regular array 472 $elements = array_map(array($this, ' _encode'), $var);393 $elements = array_map(array($this, 'encode'), $var); 473 394 474 395 foreach($elements as $element) { … … 481 402 482 403 case 'object': 483 484 // support toJSON methods.485 if (($this->use & SERVICES_JSON_USE_TO_JSON) && method_exists($var, 'toJSON')) {486 // this may end up allowing unlimited recursion487 // so we check the return value to make sure it's not got the same method.488 $recode = $var->toJSON();489 490 if (method_exists($recode, 'toJSON')) {491 492 return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)493 ? 'null'494 : new Services_JSON_Error(class_name($var).495 " toJSON returned an object with a toJSON method.");496 497 }498 499 return $this->_encode( $recode );500 }501 502 404 $vars = get_object_vars($var); 503 405 504 406 $properties = array_map(array($this, 'name_value'), 505 407 array_keys($vars), … … 532 434 function name_value($name, $value) 533 435 { 534 $encoded_value = $this-> _encode($value);436 $encoded_value = $this->encode($value); 535 437 536 438 if(Services_JSON::isError($encoded_value)) { … … 538 440 } 539 441 540 return $this-> _encode(strval($name)) . ':' . $encoded_value;442 return $this->encode(strval($name)) . ':' . $encoded_value; 541 443 } 542 444 … … 611 513 } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) { 612 514 // STRINGS RETURNED IN UTF-8 FORMAT 613 $delim = $this->substr8($str, 0, 1);614 $chrs = $this->substr8($str, 1, -1);515 $delim = substr($str, 0, 1); 516 $chrs = substr($str, 1, -1); 615 517 $utf8 = ''; 616 $strlen_chrs = $this->strlen8($chrs);518 $strlen_chrs = strlen($chrs); 617 519 618 520 for ($c = 0; $c < $strlen_chrs; ++$c) { 619 521 620 $substr_chrs_c_2 = $this->substr8($chrs, $c, 2);522 $substr_chrs_c_2 = substr($chrs, $c, 2); 621 523 $ord_chrs_c = ord($chrs{$c}); 622 524 … … 653 555 break; 654 556 655 case preg_match('/\\\u[0-9A-F]{4}/i', $this->substr8($chrs, $c, 6)):557 case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)): 656 558 // single, escaped unicode character 657 $utf16 = chr(hexdec( $this->substr8($chrs, ($c + 2), 2)))658 . chr(hexdec( $this->substr8($chrs, ($c + 4), 2)));559 $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2))) 560 . chr(hexdec(substr($chrs, ($c + 4), 2))); 659 561 $utf8 .= $this->utf162utf8($utf16); 660 562 $c += 5; … … 668 570 // characters U-00000080 - U-000007FF, mask 110XXXXX 669 571 //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 670 $utf8 .= $this->substr8($chrs, $c, 2);572 $utf8 .= substr($chrs, $c, 2); 671 573 ++$c; 672 574 break; … … 675 577 // characters U-00000800 - U-0000FFFF, mask 1110XXXX 676 578 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 677 $utf8 .= $this->substr8($chrs, $c, 3);579 $utf8 .= substr($chrs, $c, 3); 678 580 $c += 2; 679 581 break; … … 682 584 // characters U-00010000 - U-001FFFFF, mask 11110XXX 683 585 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 684 $utf8 .= $this->substr8($chrs, $c, 4);586 $utf8 .= substr($chrs, $c, 4); 685 587 $c += 3; 686 588 break; … … 689 591 // characters U-00200000 - U-03FFFFFF, mask 111110XX 690 592 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 691 $utf8 .= $this->substr8($chrs, $c, 5);593 $utf8 .= substr($chrs, $c, 5); 692 594 $c += 4; 693 595 break; … … 696 598 // characters U-04000000 - U-7FFFFFFF, mask 1111110X 697 599 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 698 $utf8 .= $this->substr8($chrs, $c, 6);600 $utf8 .= substr($chrs, $c, 6); 699 601 $c += 5; 700 602 break; … … 726 628 'delim' => false)); 727 629 728 $chrs = $this->substr8($str, 1, -1);630 $chrs = substr($str, 1, -1); 729 631 $chrs = $this->reduce_string($chrs); 730 632 … … 741 643 //print("\nparsing {$chrs}\n"); 742 644 743 $strlen_chrs = $this->strlen8($chrs);645 $strlen_chrs = strlen($chrs); 744 646 745 647 for ($c = 0; $c <= $strlen_chrs; ++$c) { 746 648 747 649 $top = end($stk); 748 $substr_chrs_c_2 = $this->substr8($chrs, $c, 2);650 $substr_chrs_c_2 = substr($chrs, $c, 2); 749 651 750 652 if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) { 751 653 // found a comma that is not inside a string, array, etc., 752 654 // OR we've reached the end of the character list 753 $slice = $this->substr8($chrs, $top['where'], ($c - $top['where']));655 $slice = substr($chrs, $top['where'], ($c - $top['where'])); 754 656 array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false)); 755 //print("Found split at {$c}: ". $this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");657 //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); 756 658 757 659 if (reset($stk) == SERVICES_JSON_IN_ARR) { … … 766 668 $parts = array(); 767 669 768 if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:/Uis', $slice, $parts)) {769 670 if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { 671 // "name":value pair 770 672 $key = $this->decode($parts[1]); 771 $val = $this->decode(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B")); 673 $val = $this->decode($parts[2]); 674 772 675 if ($this->use & SERVICES_JSON_LOOSE_TYPE) { 773 676 $obj[$key] = $val; … … 775 678 $obj->$key = $val; 776 679 } 777 } elseif (preg_match('/^\s*(\w+)\s*: /Uis', $slice, $parts)) {680 } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) { 778 681 // name:value pair, where name is unquoted 779 682 $key = $parts[1]; 780 $val = $this->decode( trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));683 $val = $this->decode($parts[2]); 781 684 782 685 if ($this->use & SERVICES_JSON_LOOSE_TYPE) { … … 796 699 } elseif (($chrs{$c} == $top['delim']) && 797 700 ($top['what'] == SERVICES_JSON_IN_STR) && 798 (( $this->strlen8($this->substr8($chrs, 0, $c)) - $this->strlen8(rtrim($this->substr8($chrs, 0, $c), '\\'))) % 2 != 1)) {701 ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) { 799 702 // found a quote, we're in a string, and it's not escaped 800 703 // we know that it's not escaped becase there is _not_ an 801 704 // odd number of backslashes at the end of the string so far 802 705 array_pop($stk); 803 //print("Found end of string at {$c}: ". $this->substr8($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");706 //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n"); 804 707 805 708 } elseif (($chrs{$c} == '[') && … … 812 715 // found a right-bracket, and we're in an array 813 716 array_pop($stk); 814 //print("Found end of array at {$c}: ". $this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");717 //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); 815 718 816 719 } elseif (($chrs{$c} == '{') && … … 823 726 // found a right-brace, and we're in an object 824 727 array_pop($stk); 825 //print("Found end of object at {$c}: ". $this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");728 //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); 826 729 827 730 } elseif (($substr_chrs_c_2 == '/*') && … … 840 743 $chrs = substr_replace($chrs, ' ', $i, 1); 841 744 842 //print("Found end of comment at {$c}: ". $this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");745 //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n"); 843 746 844 747 } … … 872 775 return false; 873 776 } 874 875 /**876 * Calculates length of string in bytes877 * @param string878 * @return integer length879 */880 function strlen8( $str )881 {882 if ( $this->_mb_strlen ) {883 return mb_strlen( $str, "8bit" );884 }885 return strlen( $str );886 }887 888 /**889 * Returns part of a string, interpreting $start and $length as number of bytes.890 * @param string891 * @param integer start892 * @param integer length893 * @return integer length894 */895 function substr8( $string, $start, $length=false )896 {897 if ( $length === false ) {898 $length = $this->strlen8( $string ) - $start;899 }900 if ( $this->_mb_substr ) {901 return mb_substr( $string, $start, $length, "8bit" );902 }903 return substr( $string, $start, $length );904 }905 906 777 } 907 778 … … 930 801 } 931 802 } 803 804 } 932 805 933 } 806 ?>
Note: See TracChangeset
for help on using the changeset viewer.