wp/wp-includes/class-json.php
changeset 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
-1:000000000000 0:d970ebf37754
       
     1 <?php
       
     2 if ( ! class_exists( 'Services_JSON' ) ) :
       
     3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
       
     4 /**
       
     5  * Converts to and from JSON format.
       
     6  *
       
     7  * JSON (JavaScript Object Notation) is a lightweight data-interchange
       
     8  * format. It is easy for humans to read and write. It is easy for machines
       
     9  * to parse and generate. It is based on a subset of the JavaScript
       
    10  * Programming Language, Standard ECMA-262 3rd Edition - December 1999.
       
    11  * This feature can also be found in  Python. JSON is a text format that is
       
    12  * completely language independent but uses conventions that are familiar
       
    13  * to programmers of the C-family of languages, including C, C++, C#, Java,
       
    14  * JavaScript, Perl, TCL, and many others. These properties make JSON an
       
    15  * ideal data-interchange language.
       
    16  *
       
    17  * This package provides a simple encoder and decoder for JSON notation. It
       
    18  * is intended for use with client-side Javascript applications that make
       
    19  * use of HTTPRequest to perform server communication functions - data can
       
    20  * be encoded into JSON notation for use in a client-side javascript, or
       
    21  * decoded from incoming Javascript requests. JSON format is native to
       
    22  * Javascript, and can be directly eval()'ed with no further parsing
       
    23  * overhead
       
    24  *
       
    25  * All strings should be in ASCII or UTF-8 format!
       
    26  *
       
    27  * LICENSE: Redistribution and use in source and binary forms, with or
       
    28  * without modification, are permitted provided that the following
       
    29  * conditions are met: Redistributions of source code must retain the
       
    30  * above copyright notice, this list of conditions and the following
       
    31  * disclaimer. Redistributions in binary form must reproduce the above
       
    32  * copyright notice, this list of conditions and the following disclaimer
       
    33  * in the documentation and/or other materials provided with the
       
    34  * distribution.
       
    35  *
       
    36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
       
    37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
       
    38  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
       
    39  * NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
       
    40  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
       
    41  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
       
    42  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
       
    43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
       
    44  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
       
    45  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
       
    46  * DAMAGE.
       
    47  *
       
    48  * @category
       
    49  * @package     Services_JSON
       
    50  * @author      Michal Migurski <mike-json@teczno.com>
       
    51  * @author      Matt Knapp <mdknapp[at]gmail[dot]com>
       
    52  * @author      Brett Stimmerman <brettstimmerman[at]gmail[dot]com>
       
    53  * @copyright   2005 Michal Migurski
       
    54  * @version     CVS: $Id: JSON.php 305040 2010-11-02 23:19:03Z alan_k $
       
    55  * @license     http://www.opensource.org/licenses/bsd-license.php
       
    56  * @link        http://pear.php.net/pepr/pepr-proposal-show.php?id=198
       
    57  */
       
    58 
       
    59 /**
       
    60  * Marker constant for Services_JSON::decode(), used to flag stack state
       
    61  */
       
    62 define('SERVICES_JSON_SLICE',   1);
       
    63 
       
    64 /**
       
    65  * Marker constant for Services_JSON::decode(), used to flag stack state
       
    66  */
       
    67 define('SERVICES_JSON_IN_STR',  2);
       
    68 
       
    69 /**
       
    70  * Marker constant for Services_JSON::decode(), used to flag stack state
       
    71  */
       
    72 define('SERVICES_JSON_IN_ARR',  3);
       
    73 
       
    74 /**
       
    75  * Marker constant for Services_JSON::decode(), used to flag stack state
       
    76  */
       
    77 define('SERVICES_JSON_IN_OBJ',  4);
       
    78 
       
    79 /**
       
    80  * Marker constant for Services_JSON::decode(), used to flag stack state
       
    81  */
       
    82 define('SERVICES_JSON_IN_CMT', 5);
       
    83 
       
    84 /**
       
    85  * Behavior switch for Services_JSON::decode()
       
    86  */
       
    87 define('SERVICES_JSON_LOOSE_TYPE', 16);
       
    88 
       
    89 /**
       
    90  * Behavior switch for Services_JSON::decode()
       
    91  */
       
    92 define('SERVICES_JSON_SUPPRESS_ERRORS', 32);
       
    93 
       
    94 /**
       
    95  * Behavior switch for Services_JSON::decode()
       
    96  */
       
    97 define('SERVICES_JSON_USE_TO_JSON', 64);
       
    98 
       
    99 /**
       
   100  * Converts to and from JSON format.
       
   101  *
       
   102  * Brief example of use:
       
   103  *
       
   104  * <code>
       
   105  * // create a new instance of Services_JSON
       
   106  * $json = new Services_JSON();
       
   107  *
       
   108  * // convert a complexe value to JSON notation, and send it to the browser
       
   109  * $value = array('foo', 'bar', array(1, 2, 'baz'), array(3, array(4)));
       
   110  * $output = $json->encode($value);
       
   111  *
       
   112  * print($output);
       
   113  * // prints: ["foo","bar",[1,2,"baz"],[3,[4]]]
       
   114  *
       
   115  * // accept incoming POST data, assumed to be in JSON notation
       
   116  * $input = file_get_contents('php://input', 1000000);
       
   117  * $value = $json->decode($input);
       
   118  * </code>
       
   119  */
       
   120 class Services_JSON
       
   121 {
       
   122    /**
       
   123     * constructs a new JSON instance
       
   124     *
       
   125     * @param    int     $use    object behavior flags; combine with boolean-OR
       
   126     *
       
   127     *                           possible values:
       
   128     *                           - SERVICES_JSON_LOOSE_TYPE:  loose typing.
       
   129     *                                   "{...}" syntax creates associative arrays
       
   130     *                                   instead of objects in decode().
       
   131     *                           - SERVICES_JSON_SUPPRESS_ERRORS:  error suppression.
       
   132     *                                   Values which can't be encoded (e.g. resources)
       
   133     *                                   appear as NULL instead of throwing errors.
       
   134     *                                   By default, a deeply-nested resource will
       
   135     *                                   bubble up with an error, so all return values
       
   136     *                                   from encode() should be checked with isError()
       
   137     *                           - SERVICES_JSON_USE_TO_JSON:  call toJSON when serializing objects
       
   138     *                                   It serializes the return value from the toJSON call rather 
       
   139     *                                   than the object it'self,  toJSON can return associative arrays, 
       
   140     *                                   strings or numbers, if you return an object, make sure it does
       
   141     *                                   not have a toJSON method, otherwise an error will occur.
       
   142     */
       
   143     function Services_JSON($use = 0)
       
   144     {
       
   145         $this->use = $use;
       
   146         $this->_mb_strlen            = function_exists('mb_strlen');
       
   147         $this->_mb_convert_encoding  = function_exists('mb_convert_encoding');
       
   148         $this->_mb_substr            = function_exists('mb_substr');
       
   149     }
       
   150     // private - cache the mbstring lookup results..
       
   151     var $_mb_strlen = false;
       
   152     var $_mb_substr = false;
       
   153     var $_mb_convert_encoding = false;
       
   154     
       
   155    /**
       
   156     * convert a string from one UTF-16 char to one UTF-8 char
       
   157     *
       
   158     * Normally should be handled by mb_convert_encoding, but
       
   159     * provides a slower PHP-only method for installations
       
   160     * that lack the multibye string extension.
       
   161     *
       
   162     * @param    string  $utf16  UTF-16 character
       
   163     * @return   string  UTF-8 character
       
   164     * @access   private
       
   165     */
       
   166     function utf162utf8($utf16)
       
   167     {
       
   168         // oh please oh please oh please oh please oh please
       
   169         if($this->_mb_convert_encoding) {
       
   170             return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
       
   171         }
       
   172 
       
   173         $bytes = (ord($utf16{0}) << 8) | ord($utf16{1});
       
   174 
       
   175         switch(true) {
       
   176             case ((0x7F & $bytes) == $bytes):
       
   177                 // this case should never be reached, because we are in ASCII range
       
   178                 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   179                 return chr(0x7F & $bytes);
       
   180 
       
   181             case (0x07FF & $bytes) == $bytes:
       
   182                 // return a 2-byte UTF-8 character
       
   183                 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   184                 return chr(0xC0 | (($bytes >> 6) & 0x1F))
       
   185                      . chr(0x80 | ($bytes & 0x3F));
       
   186 
       
   187             case (0xFFFF & $bytes) == $bytes:
       
   188                 // return a 3-byte UTF-8 character
       
   189                 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   190                 return chr(0xE0 | (($bytes >> 12) & 0x0F))
       
   191                      . chr(0x80 | (($bytes >> 6) & 0x3F))
       
   192                      . chr(0x80 | ($bytes & 0x3F));
       
   193         }
       
   194 
       
   195         // ignoring UTF-32 for now, sorry
       
   196         return '';
       
   197     }
       
   198 
       
   199    /**
       
   200     * convert a string from one UTF-8 char to one UTF-16 char
       
   201     *
       
   202     * Normally should be handled by mb_convert_encoding, but
       
   203     * provides a slower PHP-only method for installations
       
   204     * that lack the multibye string extension.
       
   205     *
       
   206     * @param    string  $utf8   UTF-8 character
       
   207     * @return   string  UTF-16 character
       
   208     * @access   private
       
   209     */
       
   210     function utf82utf16($utf8)
       
   211     {
       
   212         // oh please oh please oh please oh please oh please
       
   213         if($this->_mb_convert_encoding) {
       
   214             return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
       
   215         }
       
   216 
       
   217         switch($this->strlen8($utf8)) {
       
   218             case 1:
       
   219                 // this case should never be reached, because we are in ASCII range
       
   220                 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   221                 return $utf8;
       
   222 
       
   223             case 2:
       
   224                 // return a UTF-16 character from a 2-byte UTF-8 char
       
   225                 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   226                 return chr(0x07 & (ord($utf8{0}) >> 2))
       
   227                      . chr((0xC0 & (ord($utf8{0}) << 6))
       
   228                          | (0x3F & ord($utf8{1})));
       
   229 
       
   230             case 3:
       
   231                 // return a UTF-16 character from a 3-byte UTF-8 char
       
   232                 // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   233                 return chr((0xF0 & (ord($utf8{0}) << 4))
       
   234                          | (0x0F & (ord($utf8{1}) >> 2)))
       
   235                      . chr((0xC0 & (ord($utf8{1}) << 6))
       
   236                          | (0x7F & ord($utf8{2})));
       
   237         }
       
   238 
       
   239         // ignoring UTF-32 for now, sorry
       
   240         return '';
       
   241     }
       
   242 
       
   243    /**
       
   244     * encodes an arbitrary variable into JSON format (and sends JSON Header)
       
   245     *
       
   246     * @param    mixed   $var    any number, boolean, string, array, or object to be encoded.
       
   247     *                           see argument 1 to Services_JSON() above for array-parsing behavior.
       
   248     *                           if var is a strng, note that encode() always expects it
       
   249     *                           to be in ASCII or UTF-8 format!
       
   250     *
       
   251     * @return   mixed   JSON string representation of input var or an error if a problem occurs
       
   252     * @access   public
       
   253     */
       
   254     function encode($var)
       
   255     {
       
   256         header('Content-type: application/json');
       
   257         return $this->encodeUnsafe($var);
       
   258     }
       
   259     /**
       
   260     * encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!)
       
   261     *
       
   262     * @param    mixed   $var    any number, boolean, string, array, or object to be encoded.
       
   263     *                           see argument 1 to Services_JSON() above for array-parsing behavior.
       
   264     *                           if var is a strng, note that encode() always expects it
       
   265     *                           to be in ASCII or UTF-8 format!
       
   266     *
       
   267     * @return   mixed   JSON string representation of input var or an error if a problem occurs
       
   268     * @access   public
       
   269     */
       
   270     function encodeUnsafe($var)
       
   271     {
       
   272         // see bug #16908 - regarding numeric locale printing
       
   273         $lc = setlocale(LC_NUMERIC, 0);
       
   274         setlocale(LC_NUMERIC, 'C');
       
   275         $ret = $this->_encode($var);
       
   276         setlocale(LC_NUMERIC, $lc);
       
   277         return $ret;
       
   278         
       
   279     }
       
   280     /**
       
   281     * PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format 
       
   282     *
       
   283     * @param    mixed   $var    any number, boolean, string, array, or object to be encoded.
       
   284     *                           see argument 1 to Services_JSON() above for array-parsing behavior.
       
   285     *                           if var is a strng, note that encode() always expects it
       
   286     *                           to be in ASCII or UTF-8 format!
       
   287     *
       
   288     * @return   mixed   JSON string representation of input var or an error if a problem occurs
       
   289     * @access   public
       
   290     */
       
   291     function _encode($var) 
       
   292     {
       
   293          
       
   294         switch (gettype($var)) {
       
   295             case 'boolean':
       
   296                 return $var ? 'true' : 'false';
       
   297 
       
   298             case 'NULL':
       
   299                 return 'null';
       
   300 
       
   301             case 'integer':
       
   302                 return (int) $var;
       
   303 
       
   304             case 'double':
       
   305             case 'float':
       
   306                 return  (float) $var;
       
   307 
       
   308             case 'string':
       
   309                 // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
       
   310                 $ascii = '';
       
   311                 $strlen_var = $this->strlen8($var);
       
   312 
       
   313                /*
       
   314                 * Iterate over every character in the string,
       
   315                 * escaping with a slash or encoding to UTF-8 where necessary
       
   316                 */
       
   317                 for ($c = 0; $c < $strlen_var; ++$c) {
       
   318 
       
   319                     $ord_var_c = ord($var{$c});
       
   320 
       
   321                     switch (true) {
       
   322                         case $ord_var_c == 0x08:
       
   323                             $ascii .= '\b';
       
   324                             break;
       
   325                         case $ord_var_c == 0x09:
       
   326                             $ascii .= '\t';
       
   327                             break;
       
   328                         case $ord_var_c == 0x0A:
       
   329                             $ascii .= '\n';
       
   330                             break;
       
   331                         case $ord_var_c == 0x0C:
       
   332                             $ascii .= '\f';
       
   333                             break;
       
   334                         case $ord_var_c == 0x0D:
       
   335                             $ascii .= '\r';
       
   336                             break;
       
   337 
       
   338                         case $ord_var_c == 0x22:
       
   339                         case $ord_var_c == 0x2F:
       
   340                         case $ord_var_c == 0x5C:
       
   341                             // double quote, slash, slosh
       
   342                             $ascii .= '\\'.$var{$c};
       
   343                             break;
       
   344 
       
   345                         case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
       
   346                             // characters U-00000000 - U-0000007F (same as ASCII)
       
   347                             $ascii .= $var{$c};
       
   348                             break;
       
   349 
       
   350                         case (($ord_var_c & 0xE0) == 0xC0):
       
   351                             // characters U-00000080 - U-000007FF, mask 110XXXXX
       
   352                             // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   353                             if ($c+1 >= $strlen_var) {
       
   354                                 $c += 1;
       
   355                                 $ascii .= '?';
       
   356                                 break;
       
   357                             }
       
   358                             
       
   359                             $char = pack('C*', $ord_var_c, ord($var{$c + 1}));
       
   360                             $c += 1;
       
   361                             $utf16 = $this->utf82utf16($char);
       
   362                             $ascii .= sprintf('\u%04s', bin2hex($utf16));
       
   363                             break;
       
   364 
       
   365                         case (($ord_var_c & 0xF0) == 0xE0):
       
   366                             if ($c+2 >= $strlen_var) {
       
   367                                 $c += 2;
       
   368                                 $ascii .= '?';
       
   369                                 break;
       
   370                             }
       
   371                             // characters U-00000800 - U-0000FFFF, mask 1110XXXX
       
   372                             // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   373                             $char = pack('C*', $ord_var_c,
       
   374                                          @ord($var{$c + 1}),
       
   375                                          @ord($var{$c + 2}));
       
   376                             $c += 2;
       
   377                             $utf16 = $this->utf82utf16($char);
       
   378                             $ascii .= sprintf('\u%04s', bin2hex($utf16));
       
   379                             break;
       
   380 
       
   381                         case (($ord_var_c & 0xF8) == 0xF0):
       
   382                             if ($c+3 >= $strlen_var) {
       
   383                                 $c += 3;
       
   384                                 $ascii .= '?';
       
   385                                 break;
       
   386                             }
       
   387                             // characters U-00010000 - U-001FFFFF, mask 11110XXX
       
   388                             // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   389                             $char = pack('C*', $ord_var_c,
       
   390                                          ord($var{$c + 1}),
       
   391                                          ord($var{$c + 2}),
       
   392                                          ord($var{$c + 3}));
       
   393                             $c += 3;
       
   394                             $utf16 = $this->utf82utf16($char);
       
   395                             $ascii .= sprintf('\u%04s', bin2hex($utf16));
       
   396                             break;
       
   397 
       
   398                         case (($ord_var_c & 0xFC) == 0xF8):
       
   399                             // characters U-00200000 - U-03FFFFFF, mask 111110XX
       
   400                             // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   401                             if ($c+4 >= $strlen_var) {
       
   402                                 $c += 4;
       
   403                                 $ascii .= '?';
       
   404                                 break;
       
   405                             }
       
   406                             $char = pack('C*', $ord_var_c,
       
   407                                          ord($var{$c + 1}),
       
   408                                          ord($var{$c + 2}),
       
   409                                          ord($var{$c + 3}),
       
   410                                          ord($var{$c + 4}));
       
   411                             $c += 4;
       
   412                             $utf16 = $this->utf82utf16($char);
       
   413                             $ascii .= sprintf('\u%04s', bin2hex($utf16));
       
   414                             break;
       
   415 
       
   416                         case (($ord_var_c & 0xFE) == 0xFC):
       
   417                         if ($c+5 >= $strlen_var) {
       
   418                                 $c += 5;
       
   419                                 $ascii .= '?';
       
   420                                 break;
       
   421                             }
       
   422                             // characters U-04000000 - U-7FFFFFFF, mask 1111110X
       
   423                             // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   424                             $char = pack('C*', $ord_var_c,
       
   425                                          ord($var{$c + 1}),
       
   426                                          ord($var{$c + 2}),
       
   427                                          ord($var{$c + 3}),
       
   428                                          ord($var{$c + 4}),
       
   429                                          ord($var{$c + 5}));
       
   430                             $c += 5;
       
   431                             $utf16 = $this->utf82utf16($char);
       
   432                             $ascii .= sprintf('\u%04s', bin2hex($utf16));
       
   433                             break;
       
   434                     }
       
   435                 }
       
   436                 return  '"'.$ascii.'"';
       
   437 
       
   438             case 'array':
       
   439                /*
       
   440                 * As per JSON spec if any array key is not an integer
       
   441                 * we must treat the the whole array as an object. We
       
   442                 * also try to catch a sparsely populated associative
       
   443                 * array with numeric keys here because some JS engines
       
   444                 * will create an array with empty indexes up to
       
   445                 * max_index which can cause memory issues and because
       
   446                 * the keys, which may be relevant, will be remapped
       
   447                 * otherwise.
       
   448                 *
       
   449                 * As per the ECMA and JSON specification an object may
       
   450                 * have any string as a property. Unfortunately due to
       
   451                 * a hole in the ECMA specification if the key is a
       
   452                 * ECMA reserved word or starts with a digit the
       
   453                 * parameter is only accessible using ECMAScript's
       
   454                 * bracket notation.
       
   455                 */
       
   456 
       
   457                 // treat as a JSON object
       
   458                 if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) {
       
   459                     $properties = array_map(array($this, 'name_value'),
       
   460                                             array_keys($var),
       
   461                                             array_values($var));
       
   462 
       
   463                     foreach($properties as $property) {
       
   464                         if(Services_JSON::isError($property)) {
       
   465                             return $property;
       
   466                         }
       
   467                     }
       
   468 
       
   469                     return '{' . join(',', $properties) . '}';
       
   470                 }
       
   471 
       
   472                 // treat it like a regular array
       
   473                 $elements = array_map(array($this, '_encode'), $var);
       
   474 
       
   475                 foreach($elements as $element) {
       
   476                     if(Services_JSON::isError($element)) {
       
   477                         return $element;
       
   478                     }
       
   479                 }
       
   480 
       
   481                 return '[' . join(',', $elements) . ']';
       
   482 
       
   483             case 'object':
       
   484             
       
   485                 // support toJSON methods.
       
   486                 if (($this->use & SERVICES_JSON_USE_TO_JSON) && method_exists($var, 'toJSON')) {
       
   487                     // this may end up allowing unlimited recursion
       
   488                     // so we check the return value to make sure it's not got the same method.
       
   489                     $recode = $var->toJSON();
       
   490                     
       
   491                     if (method_exists($recode, 'toJSON')) {
       
   492                         
       
   493                         return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
       
   494                         ? 'null'
       
   495                         : new Services_JSON_Error(get_class($var).
       
   496                             " toJSON returned an object with a toJSON method.");
       
   497                             
       
   498                     }
       
   499                     
       
   500                     return $this->_encode( $recode );
       
   501                 } 
       
   502                 
       
   503                 $vars = get_object_vars($var);
       
   504                 
       
   505                 $properties = array_map(array($this, 'name_value'),
       
   506                                         array_keys($vars),
       
   507                                         array_values($vars));
       
   508 
       
   509                 foreach($properties as $property) {
       
   510                     if(Services_JSON::isError($property)) {
       
   511                         return $property;
       
   512                     }
       
   513                 }
       
   514 
       
   515                 return '{' . join(',', $properties) . '}';
       
   516 
       
   517             default:
       
   518                 return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
       
   519                     ? 'null'
       
   520                     : new Services_JSON_Error(gettype($var)." can not be encoded as JSON string");
       
   521         }
       
   522     }
       
   523 
       
   524    /**
       
   525     * array-walking function for use in generating JSON-formatted name-value pairs
       
   526     *
       
   527     * @param    string  $name   name of key to use
       
   528     * @param    mixed   $value  reference to an array element to be encoded
       
   529     *
       
   530     * @return   string  JSON-formatted name-value pair, like '"name":value'
       
   531     * @access   private
       
   532     */
       
   533     function name_value($name, $value)
       
   534     {
       
   535         $encoded_value = $this->_encode($value);
       
   536 
       
   537         if(Services_JSON::isError($encoded_value)) {
       
   538             return $encoded_value;
       
   539         }
       
   540 
       
   541         return $this->_encode(strval($name)) . ':' . $encoded_value;
       
   542     }
       
   543 
       
   544    /**
       
   545     * reduce a string by removing leading and trailing comments and whitespace
       
   546     *
       
   547     * @param    $str    string      string value to strip of comments and whitespace
       
   548     *
       
   549     * @return   string  string value stripped of comments and whitespace
       
   550     * @access   private
       
   551     */
       
   552     function reduce_string($str)
       
   553     {
       
   554         $str = preg_replace(array(
       
   555 
       
   556                 // eliminate single line comments in '// ...' form
       
   557                 '#^\s*//(.+)$#m',
       
   558 
       
   559                 // eliminate multi-line comments in '/* ... */' form, at start of string
       
   560                 '#^\s*/\*(.+)\*/#Us',
       
   561 
       
   562                 // eliminate multi-line comments in '/* ... */' form, at end of string
       
   563                 '#/\*(.+)\*/\s*$#Us'
       
   564 
       
   565             ), '', $str);
       
   566 
       
   567         // eliminate extraneous space
       
   568         return trim($str);
       
   569     }
       
   570 
       
   571    /**
       
   572     * decodes a JSON string into appropriate variable
       
   573     *
       
   574     * @param    string  $str    JSON-formatted string
       
   575     *
       
   576     * @return   mixed   number, boolean, string, array, or object
       
   577     *                   corresponding to given JSON input string.
       
   578     *                   See argument 1 to Services_JSON() above for object-output behavior.
       
   579     *                   Note that decode() always returns strings
       
   580     *                   in ASCII or UTF-8 format!
       
   581     * @access   public
       
   582     */
       
   583     function decode($str)
       
   584     {
       
   585         $str = $this->reduce_string($str);
       
   586 
       
   587         switch (strtolower($str)) {
       
   588             case 'true':
       
   589                 return true;
       
   590 
       
   591             case 'false':
       
   592                 return false;
       
   593 
       
   594             case 'null':
       
   595                 return null;
       
   596 
       
   597             default:
       
   598                 $m = array();
       
   599 
       
   600                 if (is_numeric($str)) {
       
   601                     // Lookie-loo, it's a number
       
   602 
       
   603                     // This would work on its own, but I'm trying to be
       
   604                     // good about returning integers where appropriate:
       
   605                     // return (float)$str;
       
   606 
       
   607                     // Return float or int, as appropriate
       
   608                     return ((float)$str == (integer)$str)
       
   609                         ? (integer)$str
       
   610                         : (float)$str;
       
   611 
       
   612                 } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) {
       
   613                     // STRINGS RETURNED IN UTF-8 FORMAT
       
   614                     $delim = $this->substr8($str, 0, 1);
       
   615                     $chrs = $this->substr8($str, 1, -1);
       
   616                     $utf8 = '';
       
   617                     $strlen_chrs = $this->strlen8($chrs);
       
   618 
       
   619                     for ($c = 0; $c < $strlen_chrs; ++$c) {
       
   620 
       
   621                         $substr_chrs_c_2 = $this->substr8($chrs, $c, 2);
       
   622                         $ord_chrs_c = ord($chrs{$c});
       
   623 
       
   624                         switch (true) {
       
   625                             case $substr_chrs_c_2 == '\b':
       
   626                                 $utf8 .= chr(0x08);
       
   627                                 ++$c;
       
   628                                 break;
       
   629                             case $substr_chrs_c_2 == '\t':
       
   630                                 $utf8 .= chr(0x09);
       
   631                                 ++$c;
       
   632                                 break;
       
   633                             case $substr_chrs_c_2 == '\n':
       
   634                                 $utf8 .= chr(0x0A);
       
   635                                 ++$c;
       
   636                                 break;
       
   637                             case $substr_chrs_c_2 == '\f':
       
   638                                 $utf8 .= chr(0x0C);
       
   639                                 ++$c;
       
   640                                 break;
       
   641                             case $substr_chrs_c_2 == '\r':
       
   642                                 $utf8 .= chr(0x0D);
       
   643                                 ++$c;
       
   644                                 break;
       
   645 
       
   646                             case $substr_chrs_c_2 == '\\"':
       
   647                             case $substr_chrs_c_2 == '\\\'':
       
   648                             case $substr_chrs_c_2 == '\\\\':
       
   649                             case $substr_chrs_c_2 == '\\/':
       
   650                                 if (($delim == '"' && $substr_chrs_c_2 != '\\\'') ||
       
   651                                    ($delim == "'" && $substr_chrs_c_2 != '\\"')) {
       
   652                                     $utf8 .= $chrs{++$c};
       
   653                                 }
       
   654                                 break;
       
   655 
       
   656                             case preg_match('/\\\u[0-9A-F]{4}/i', $this->substr8($chrs, $c, 6)):
       
   657                                 // single, escaped unicode character
       
   658                                 $utf16 = chr(hexdec($this->substr8($chrs, ($c + 2), 2)))
       
   659                                        . chr(hexdec($this->substr8($chrs, ($c + 4), 2)));
       
   660                                 $utf8 .= $this->utf162utf8($utf16);
       
   661                                 $c += 5;
       
   662                                 break;
       
   663 
       
   664                             case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F):
       
   665                                 $utf8 .= $chrs{$c};
       
   666                                 break;
       
   667 
       
   668                             case ($ord_chrs_c & 0xE0) == 0xC0:
       
   669                                 // characters U-00000080 - U-000007FF, mask 110XXXXX
       
   670                                 //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   671                                 $utf8 .= $this->substr8($chrs, $c, 2);
       
   672                                 ++$c;
       
   673                                 break;
       
   674 
       
   675                             case ($ord_chrs_c & 0xF0) == 0xE0:
       
   676                                 // characters U-00000800 - U-0000FFFF, mask 1110XXXX
       
   677                                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   678                                 $utf8 .= $this->substr8($chrs, $c, 3);
       
   679                                 $c += 2;
       
   680                                 break;
       
   681 
       
   682                             case ($ord_chrs_c & 0xF8) == 0xF0:
       
   683                                 // characters U-00010000 - U-001FFFFF, mask 11110XXX
       
   684                                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   685                                 $utf8 .= $this->substr8($chrs, $c, 4);
       
   686                                 $c += 3;
       
   687                                 break;
       
   688 
       
   689                             case ($ord_chrs_c & 0xFC) == 0xF8:
       
   690                                 // characters U-00200000 - U-03FFFFFF, mask 111110XX
       
   691                                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   692                                 $utf8 .= $this->substr8($chrs, $c, 5);
       
   693                                 $c += 4;
       
   694                                 break;
       
   695 
       
   696                             case ($ord_chrs_c & 0xFE) == 0xFC:
       
   697                                 // characters U-04000000 - U-7FFFFFFF, mask 1111110X
       
   698                                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
       
   699                                 $utf8 .= $this->substr8($chrs, $c, 6);
       
   700                                 $c += 5;
       
   701                                 break;
       
   702 
       
   703                         }
       
   704 
       
   705                     }
       
   706 
       
   707                     return $utf8;
       
   708 
       
   709                 } elseif (preg_match('/^\[.*\]$/s', $str) || preg_match('/^\{.*\}$/s', $str)) {
       
   710                     // array, or object notation
       
   711 
       
   712                     if ($str{0} == '[') {
       
   713                         $stk = array(SERVICES_JSON_IN_ARR);
       
   714                         $arr = array();
       
   715                     } else {
       
   716                         if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
       
   717                             $stk = array(SERVICES_JSON_IN_OBJ);
       
   718                             $obj = array();
       
   719                         } else {
       
   720                             $stk = array(SERVICES_JSON_IN_OBJ);
       
   721                             $obj = new stdClass();
       
   722                         }
       
   723                     }
       
   724 
       
   725                     array_push($stk, array('what'  => SERVICES_JSON_SLICE,
       
   726                                            'where' => 0,
       
   727                                            'delim' => false));
       
   728 
       
   729                     $chrs = $this->substr8($str, 1, -1);
       
   730                     $chrs = $this->reduce_string($chrs);
       
   731 
       
   732                     if ($chrs == '') {
       
   733                         if (reset($stk) == SERVICES_JSON_IN_ARR) {
       
   734                             return $arr;
       
   735 
       
   736                         } else {
       
   737                             return $obj;
       
   738 
       
   739                         }
       
   740                     }
       
   741 
       
   742                     //print("\nparsing {$chrs}\n");
       
   743 
       
   744                     $strlen_chrs = $this->strlen8($chrs);
       
   745 
       
   746                     for ($c = 0; $c <= $strlen_chrs; ++$c) {
       
   747 
       
   748                         $top = end($stk);
       
   749                         $substr_chrs_c_2 = $this->substr8($chrs, $c, 2);
       
   750 
       
   751                         if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) {
       
   752                             // found a comma that is not inside a string, array, etc.,
       
   753                             // OR we've reached the end of the character list
       
   754                             $slice = $this->substr8($chrs, $top['where'], ($c - $top['where']));
       
   755                             array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
       
   756                             //print("Found split at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
       
   757 
       
   758                             if (reset($stk) == SERVICES_JSON_IN_ARR) {
       
   759                                 // we are in an array, so just push an element onto the stack
       
   760                                 array_push($arr, $this->decode($slice));
       
   761 
       
   762                             } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
       
   763                                 // we are in an object, so figure
       
   764                                 // out the property name and set an
       
   765                                 // element in an associative array,
       
   766                                 // for now
       
   767                                 $parts = array();
       
   768                                 
       
   769                                if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:/Uis', $slice, $parts)) {
       
   770  	                              // "name":value pair
       
   771                                     $key = $this->decode($parts[1]);
       
   772                                     $val = $this->decode(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));
       
   773                                     if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
       
   774                                         $obj[$key] = $val;
       
   775                                     } else {
       
   776                                         $obj->$key = $val;
       
   777                                     }
       
   778                                 } elseif (preg_match('/^\s*(\w+)\s*:/Uis', $slice, $parts)) {
       
   779                                     // name:value pair, where name is unquoted
       
   780                                     $key = $parts[1];
       
   781                                     $val = $this->decode(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));
       
   782 
       
   783                                     if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
       
   784                                         $obj[$key] = $val;
       
   785                                     } else {
       
   786                                         $obj->$key = $val;
       
   787                                     }
       
   788                                 }
       
   789 
       
   790                             }
       
   791 
       
   792                         } elseif ((($chrs{$c} == '"') || ($chrs{$c} == "'")) && ($top['what'] != SERVICES_JSON_IN_STR)) {
       
   793                             // found a quote, and we are not inside a string
       
   794                             array_push($stk, array('what' => SERVICES_JSON_IN_STR, 'where' => $c, 'delim' => $chrs{$c}));
       
   795                             //print("Found start of string at {$c}\n");
       
   796 
       
   797                         } elseif (($chrs{$c} == $top['delim']) &&
       
   798                                  ($top['what'] == SERVICES_JSON_IN_STR) &&
       
   799                                  (($this->strlen8($this->substr8($chrs, 0, $c)) - $this->strlen8(rtrim($this->substr8($chrs, 0, $c), '\\'))) % 2 != 1)) {
       
   800                             // found a quote, we're in a string, and it's not escaped
       
   801                             // we know that it's not escaped becase there is _not_ an
       
   802                             // odd number of backslashes at the end of the string so far
       
   803                             array_pop($stk);
       
   804                             //print("Found end of string at {$c}: ".$this->substr8($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
       
   805 
       
   806                         } elseif (($chrs{$c} == '[') &&
       
   807                                  in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
       
   808                             // found a left-bracket, and we are in an array, object, or slice
       
   809                             array_push($stk, array('what' => SERVICES_JSON_IN_ARR, 'where' => $c, 'delim' => false));
       
   810                             //print("Found start of array at {$c}\n");
       
   811 
       
   812                         } elseif (($chrs{$c} == ']') && ($top['what'] == SERVICES_JSON_IN_ARR)) {
       
   813                             // found a right-bracket, and we're in an array
       
   814                             array_pop($stk);
       
   815                             //print("Found end of array at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
       
   816 
       
   817                         } elseif (($chrs{$c} == '{') &&
       
   818                                  in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
       
   819                             // found a left-brace, and we are in an array, object, or slice
       
   820                             array_push($stk, array('what' => SERVICES_JSON_IN_OBJ, 'where' => $c, 'delim' => false));
       
   821                             //print("Found start of object at {$c}\n");
       
   822 
       
   823                         } elseif (($chrs{$c} == '}') && ($top['what'] == SERVICES_JSON_IN_OBJ)) {
       
   824                             // found a right-brace, and we're in an object
       
   825                             array_pop($stk);
       
   826                             //print("Found end of object at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
       
   827 
       
   828                         } elseif (($substr_chrs_c_2 == '/*') &&
       
   829                                  in_array($top['what'], array(SERVICES_JSON_SLICE, SERVICES_JSON_IN_ARR, SERVICES_JSON_IN_OBJ))) {
       
   830                             // found a comment start, and we are in an array, object, or slice
       
   831                             array_push($stk, array('what' => SERVICES_JSON_IN_CMT, 'where' => $c, 'delim' => false));
       
   832                             $c++;
       
   833                             //print("Found start of comment at {$c}\n");
       
   834 
       
   835                         } elseif (($substr_chrs_c_2 == '*/') && ($top['what'] == SERVICES_JSON_IN_CMT)) {
       
   836                             // found a comment end, and we're in one now
       
   837                             array_pop($stk);
       
   838                             $c++;
       
   839 
       
   840                             for ($i = $top['where']; $i <= $c; ++$i)
       
   841                                 $chrs = substr_replace($chrs, ' ', $i, 1);
       
   842 
       
   843                             //print("Found end of comment at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
       
   844 
       
   845                         }
       
   846 
       
   847                     }
       
   848 
       
   849                     if (reset($stk) == SERVICES_JSON_IN_ARR) {
       
   850                         return $arr;
       
   851 
       
   852                     } elseif (reset($stk) == SERVICES_JSON_IN_OBJ) {
       
   853                         return $obj;
       
   854 
       
   855                     }
       
   856 
       
   857                 }
       
   858         }
       
   859     }
       
   860 
       
   861     /**
       
   862      * @todo Ultimately, this should just call PEAR::isError()
       
   863      */
       
   864     function isError($data, $code = null)
       
   865     {
       
   866         if (class_exists('pear')) {
       
   867             return PEAR::isError($data, $code);
       
   868         } elseif (is_object($data) && (get_class($data) == 'services_json_error' ||
       
   869                                  is_subclass_of($data, 'services_json_error'))) {
       
   870             return true;
       
   871         }
       
   872 
       
   873         return false;
       
   874     }
       
   875     
       
   876     /**
       
   877     * Calculates length of string in bytes
       
   878     * @param string 
       
   879     * @return integer length
       
   880     */
       
   881     function strlen8( $str ) 
       
   882     {
       
   883         if ( $this->_mb_strlen ) {
       
   884             return mb_strlen( $str, "8bit" );
       
   885         }
       
   886         return strlen( $str );
       
   887     }
       
   888     
       
   889     /**
       
   890     * Returns part of a string, interpreting $start and $length as number of bytes.
       
   891     * @param string 
       
   892     * @param integer start 
       
   893     * @param integer length 
       
   894     * @return integer length
       
   895     */
       
   896     function substr8( $string, $start, $length=false ) 
       
   897     {
       
   898         if ( $length === false ) {
       
   899             $length = $this->strlen8( $string ) - $start;
       
   900         }
       
   901         if ( $this->_mb_substr ) {
       
   902             return mb_substr( $string, $start, $length, "8bit" );
       
   903         }
       
   904         return substr( $string, $start, $length );
       
   905     }
       
   906 
       
   907 }
       
   908 
       
   909 if (class_exists('PEAR_Error')) {
       
   910 
       
   911     class Services_JSON_Error extends PEAR_Error
       
   912     {
       
   913         function Services_JSON_Error($message = 'unknown error', $code = null,
       
   914                                      $mode = null, $options = null, $userinfo = null)
       
   915         {
       
   916             parent::PEAR_Error($message, $code, $mode, $options, $userinfo);
       
   917         }
       
   918     }
       
   919 
       
   920 } else {
       
   921 
       
   922     /**
       
   923      * @todo Ultimately, this class shall be descended from PEAR_Error
       
   924      */
       
   925     class Services_JSON_Error
       
   926     {
       
   927         function Services_JSON_Error($message = 'unknown error', $code = null,
       
   928                                      $mode = null, $options = null, $userinfo = null)
       
   929         {
       
   930 
       
   931         }
       
   932     }
       
   933     
       
   934 }
       
   935 
       
   936 endif;