Index: branches/version-2_13_0/data/module/Services/JSON.php
===================================================================
--- branches/version-2_13_0/data/module/Services/JSON.php	(revision 23126)
+++ branches/version-2_13_0/data/module/Services/JSON.php	(revision 23143)
@@ -1,4 +1,5 @@
 <?php
 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
 /**
  * Converts to and from JSON format.
@@ -92,9 +93,4 @@
 
 /**
- * Behavior switch for Services_JSON::decode()
- */
-define('SERVICES_JSON_USE_TO_JSON', 64);
-
-/**
  * Converts to and from JSON format.
  *
@@ -134,22 +130,10 @@
     *                                   bubble up with an error, so all return values
     *                                   from encode() should be checked with isError()
-    *                           - SERVICES_JSON_USE_TO_JSON:  call toJSON when serializing objects
-    *                                   It serializes the return value from the toJSON call rather 
-    *                                   than the object it'self,  toJSON can return associative arrays, 
-    *                                   strings or numbers, if you return an object, make sure it does
-    *                                   not have a toJSON method, otherwise an error will occur.
     */
     function Services_JSON($use = 0)
     {
         $this->use = $use;
-        $this->_mb_strlen            = function_exists('mb_strlen');
-        $this->_mb_convert_encoding  = function_exists('mb_convert_encoding');
-        $this->_mb_substr            = function_exists('mb_substr');
-    }
-    // private - cache the mbstring lookup results..
-    var $_mb_strlen = false;
-    var $_mb_substr = false;
-    var $_mb_convert_encoding = false;
-    
+    }
+
    /**
     * convert a string from one UTF-16 char to one UTF-8 char
@@ -166,5 +150,5 @@
     {
         // oh please oh please oh please oh please oh please
-        if($this->_mb_convert_encoding) {
+        if(function_exists('mb_convert_encoding')) {
             return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
         }
@@ -210,9 +194,9 @@
     {
         // oh please oh please oh please oh please oh please
-        if($this->_mb_convert_encoding) {
+        if(function_exists('mb_convert_encoding')) {
             return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
         }
 
-        switch($this->strlen8($utf8)) {
+        switch(strlen($utf8)) {
             case 1:
                 // this case should never be reached, because we are in ASCII range
@@ -241,5 +225,5 @@
 
    /**
-    * encodes an arbitrary variable into JSON format (and sends JSON Header)
+    * encodes an arbitrary variable into JSON format
     *
     * @param    mixed   $var    any number, boolean, string, array, or object to be encoded.
@@ -253,42 +237,4 @@
     function encode($var)
     {
-        header('Content-type: application/json');
-        return $this->encodeUnsafe($var);
-    }
-    /**
-    * encodes an arbitrary variable into JSON format without JSON Header - warning - may allow XSS!!!!)
-    *
-    * @param    mixed   $var    any number, boolean, string, array, or object to be encoded.
-    *                           see argument 1 to Services_JSON() above for array-parsing behavior.
-    *                           if var is a strng, note that encode() always expects it
-    *                           to be in ASCII or UTF-8 format!
-    *
-    * @return   mixed   JSON string representation of input var or an error if a problem occurs
-    * @access   public
-    */
-    function encodeUnsafe($var)
-    {
-        // see bug #16908 - regarding numeric locale printing
-        $lc = setlocale(LC_NUMERIC, 0);
-        setlocale(LC_NUMERIC, 'C');
-        $ret = $this->_encode($var);
-        setlocale(LC_NUMERIC, $lc);
-        return $ret;
-        
-    }
-    /**
-    * PRIVATE CODE that does the work of encodes an arbitrary variable into JSON format 
-    *
-    * @param    mixed   $var    any number, boolean, string, array, or object to be encoded.
-    *                           see argument 1 to Services_JSON() above for array-parsing behavior.
-    *                           if var is a strng, note that encode() always expects it
-    *                           to be in ASCII or UTF-8 format!
-    *
-    * @return   mixed   JSON string representation of input var or an error if a problem occurs
-    * @access   public
-    */
-    function _encode($var) 
-    {
-         
         switch (gettype($var)) {
             case 'boolean':
@@ -303,10 +249,10 @@
             case 'double':
             case 'float':
-                return  (float) $var;
+                return (float) $var;
 
             case 'string':
                 // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT
                 $ascii = '';
-                $strlen_var = $this->strlen8($var);
+                $strlen_var = strlen($var);
 
                /*
@@ -350,10 +296,4 @@
                             // characters U-00000080 - U-000007FF, mask 110XXXXX
                             // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
-                            if ($c+1 >= $strlen_var) {
-                                $c += 1;
-                                $ascii .= '?';
-                                break;
-                            }
-                            
                             $char = pack('C*', $ord_var_c, ord($var{$c + 1}));
                             $c += 1;
@@ -363,14 +303,9 @@
 
                         case (($ord_var_c & 0xF0) == 0xE0):
-                            if ($c+2 >= $strlen_var) {
-                                $c += 2;
-                                $ascii .= '?';
-                                break;
-                            }
                             // characters U-00000800 - U-0000FFFF, mask 1110XXXX
                             // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
                             $char = pack('C*', $ord_var_c,
-                                         @ord($var{$c + 1}),
-                                         @ord($var{$c + 2}));
+                                         ord($var{$c + 1}),
+                                         ord($var{$c + 2}));
                             $c += 2;
                             $utf16 = $this->utf82utf16($char);
@@ -379,9 +314,4 @@
 
                         case (($ord_var_c & 0xF8) == 0xF0):
-                            if ($c+3 >= $strlen_var) {
-                                $c += 3;
-                                $ascii .= '?';
-                                break;
-                            }
                             // characters U-00010000 - U-001FFFFF, mask 11110XXX
                             // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
@@ -398,9 +328,4 @@
                             // characters U-00200000 - U-03FFFFFF, mask 111110XX
                             // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
-                            if ($c+4 >= $strlen_var) {
-                                $c += 4;
-                                $ascii .= '?';
-                                break;
-                            }
                             $char = pack('C*', $ord_var_c,
                                          ord($var{$c + 1}),
@@ -414,9 +339,4 @@
 
                         case (($ord_var_c & 0xFE) == 0xFC):
-                        if ($c+5 >= $strlen_var) {
-                                $c += 5;
-                                $ascii .= '?';
-                                break;
-                            }
                             // characters U-04000000 - U-7FFFFFFF, mask 1111110X
                             // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
@@ -433,5 +353,6 @@
                     }
                 }
-                return  '"'.$ascii.'"';
+
+                return '"'.$ascii.'"';
 
             case 'array':
@@ -470,5 +391,5 @@
 
                 // treat it like a regular array
-                $elements = array_map(array($this, '_encode'), $var);
+                $elements = array_map(array($this, 'encode'), $var);
 
                 foreach($elements as $element) {
@@ -481,25 +402,6 @@
 
             case 'object':
-            
-                // support toJSON methods.
-                if (($this->use & SERVICES_JSON_USE_TO_JSON) && method_exists($var, 'toJSON')) {
-                    // this may end up allowing unlimited recursion
-                    // so we check the return value to make sure it's not got the same method.
-                    $recode = $var->toJSON();
-                    
-                    if (method_exists($recode, 'toJSON')) {
-                        
-                        return ($this->use & SERVICES_JSON_SUPPRESS_ERRORS)
-                        ? 'null'
-                        : new Services_JSON_Error(class_name($var).
-                            " toJSON returned an object with a toJSON method.");
-                            
-                    }
-                    
-                    return $this->_encode( $recode );
-                } 
-                
                 $vars = get_object_vars($var);
-                
+
                 $properties = array_map(array($this, 'name_value'),
                                         array_keys($vars),
@@ -532,5 +434,5 @@
     function name_value($name, $value)
     {
-        $encoded_value = $this->_encode($value);
+        $encoded_value = $this->encode($value);
 
         if(Services_JSON::isError($encoded_value)) {
@@ -538,5 +440,5 @@
         }
 
-        return $this->_encode(strval($name)) . ':' . $encoded_value;
+        return $this->encode(strval($name)) . ':' . $encoded_value;
     }
 
@@ -611,12 +513,12 @@
                 } elseif (preg_match('/^("|\').*(\1)$/s', $str, $m) && $m[1] == $m[2]) {
                     // STRINGS RETURNED IN UTF-8 FORMAT
-                    $delim = $this->substr8($str, 0, 1);
-                    $chrs = $this->substr8($str, 1, -1);
+                    $delim = substr($str, 0, 1);
+                    $chrs = substr($str, 1, -1);
                     $utf8 = '';
-                    $strlen_chrs = $this->strlen8($chrs);
+                    $strlen_chrs = strlen($chrs);
 
                     for ($c = 0; $c < $strlen_chrs; ++$c) {
 
-                        $substr_chrs_c_2 = $this->substr8($chrs, $c, 2);
+                        $substr_chrs_c_2 = substr($chrs, $c, 2);
                         $ord_chrs_c = ord($chrs{$c});
 
@@ -653,8 +555,8 @@
                                 break;
 
-                            case preg_match('/\\\u[0-9A-F]{4}/i', $this->substr8($chrs, $c, 6)):
+                            case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $c, 6)):
                                 // single, escaped unicode character
-                                $utf16 = chr(hexdec($this->substr8($chrs, ($c + 2), 2)))
-                                       . chr(hexdec($this->substr8($chrs, ($c + 4), 2)));
+                                $utf16 = chr(hexdec(substr($chrs, ($c + 2), 2)))
+                                       . chr(hexdec(substr($chrs, ($c + 4), 2)));
                                 $utf8 .= $this->utf162utf8($utf16);
                                 $c += 5;
@@ -668,5 +570,5 @@
                                 // characters U-00000080 - U-000007FF, mask 110XXXXX
                                 //see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
-                                $utf8 .= $this->substr8($chrs, $c, 2);
+                                $utf8 .= substr($chrs, $c, 2);
                                 ++$c;
                                 break;
@@ -675,5 +577,5 @@
                                 // characters U-00000800 - U-0000FFFF, mask 1110XXXX
                                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
-                                $utf8 .= $this->substr8($chrs, $c, 3);
+                                $utf8 .= substr($chrs, $c, 3);
                                 $c += 2;
                                 break;
@@ -682,5 +584,5 @@
                                 // characters U-00010000 - U-001FFFFF, mask 11110XXX
                                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
-                                $utf8 .= $this->substr8($chrs, $c, 4);
+                                $utf8 .= substr($chrs, $c, 4);
                                 $c += 3;
                                 break;
@@ -689,5 +591,5 @@
                                 // characters U-00200000 - U-03FFFFFF, mask 111110XX
                                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
-                                $utf8 .= $this->substr8($chrs, $c, 5);
+                                $utf8 .= substr($chrs, $c, 5);
                                 $c += 4;
                                 break;
@@ -696,5 +598,5 @@
                                 // characters U-04000000 - U-7FFFFFFF, mask 1111110X
                                 // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
-                                $utf8 .= $this->substr8($chrs, $c, 6);
+                                $utf8 .= substr($chrs, $c, 6);
                                 $c += 5;
                                 break;
@@ -726,5 +628,5 @@
                                            'delim' => false));
 
-                    $chrs = $this->substr8($str, 1, -1);
+                    $chrs = substr($str, 1, -1);
                     $chrs = $this->reduce_string($chrs);
 
@@ -741,17 +643,17 @@
                     //print("\nparsing {$chrs}\n");
 
-                    $strlen_chrs = $this->strlen8($chrs);
+                    $strlen_chrs = strlen($chrs);
 
                     for ($c = 0; $c <= $strlen_chrs; ++$c) {
 
                         $top = end($stk);
-                        $substr_chrs_c_2 = $this->substr8($chrs, $c, 2);
+                        $substr_chrs_c_2 = substr($chrs, $c, 2);
 
                         if (($c == $strlen_chrs) || (($chrs{$c} == ',') && ($top['what'] == SERVICES_JSON_SLICE))) {
                             // found a comma that is not inside a string, array, etc.,
                             // OR we've reached the end of the character list
-                            $slice = $this->substr8($chrs, $top['where'], ($c - $top['where']));
+                            $slice = substr($chrs, $top['where'], ($c - $top['where']));
                             array_push($stk, array('what' => SERVICES_JSON_SLICE, 'where' => ($c + 1), 'delim' => false));
-                            //print("Found split at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
+                            //print("Found split at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
 
                             if (reset($stk) == SERVICES_JSON_IN_ARR) {
@@ -766,8 +668,9 @@
                                 $parts = array();
                                 
-                               if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:/Uis', $slice, $parts)) {
- 	                              // "name":value pair
+                                if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
+                                    // "name":value pair
                                     $key = $this->decode($parts[1]);
-                                    $val = $this->decode(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));
+                                    $val = $this->decode($parts[2]);
+
                                     if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
                                         $obj[$key] = $val;
@@ -775,8 +678,8 @@
                                         $obj->$key = $val;
                                     }
-                                } elseif (preg_match('/^\s*(\w+)\s*:/Uis', $slice, $parts)) {
+                                } elseif (preg_match('/^\s*(\w+)\s*:\s*(\S.*),?$/Uis', $slice, $parts)) {
                                     // name:value pair, where name is unquoted
                                     $key = $parts[1];
-                                    $val = $this->decode(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));
+                                    $val = $this->decode($parts[2]);
 
                                     if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
@@ -796,10 +699,10 @@
                         } elseif (($chrs{$c} == $top['delim']) &&
                                  ($top['what'] == SERVICES_JSON_IN_STR) &&
-                                 (($this->strlen8($this->substr8($chrs, 0, $c)) - $this->strlen8(rtrim($this->substr8($chrs, 0, $c), '\\'))) % 2 != 1)) {
+                                 ((strlen(substr($chrs, 0, $c)) - strlen(rtrim(substr($chrs, 0, $c), '\\'))) % 2 != 1)) {
                             // found a quote, we're in a string, and it's not escaped
                             // we know that it's not escaped becase there is _not_ an
                             // odd number of backslashes at the end of the string so far
                             array_pop($stk);
-                            //print("Found end of string at {$c}: ".$this->substr8($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
+                            //print("Found end of string at {$c}: ".substr($chrs, $top['where'], (1 + 1 + $c - $top['where']))."\n");
 
                         } elseif (($chrs{$c} == '[') &&
@@ -812,5 +715,5 @@
                             // found a right-bracket, and we're in an array
                             array_pop($stk);
-                            //print("Found end of array at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
+                            //print("Found end of array at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
 
                         } elseif (($chrs{$c} == '{') &&
@@ -823,5 +726,5 @@
                             // found a right-brace, and we're in an object
                             array_pop($stk);
-                            //print("Found end of object at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
+                            //print("Found end of object at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
 
                         } elseif (($substr_chrs_c_2 == '/*') &&
@@ -840,5 +743,5 @@
                                 $chrs = substr_replace($chrs, ' ', $i, 1);
 
-                            //print("Found end of comment at {$c}: ".$this->substr8($chrs, $top['where'], (1 + $c - $top['where']))."\n");
+                            //print("Found end of comment at {$c}: ".substr($chrs, $top['where'], (1 + $c - $top['where']))."\n");
 
                         }
@@ -872,36 +775,4 @@
         return false;
     }
-    
-    /**
-    * Calculates length of string in bytes
-    * @param string 
-    * @return integer length
-    */
-    function strlen8( $str ) 
-    {
-        if ( $this->_mb_strlen ) {
-            return mb_strlen( $str, "8bit" );
-        }
-        return strlen( $str );
-    }
-    
-    /**
-    * Returns part of a string, interpreting $start and $length as number of bytes.
-    * @param string 
-    * @param integer start 
-    * @param integer length 
-    * @return integer length
-    */
-    function substr8( $string, $start, $length=false ) 
-    {
-        if ( $length === false ) {
-            $length = $this->strlen8( $string ) - $start;
-        }
-        if ( $this->_mb_substr ) {
-            return mb_substr( $string, $start, $length, "8bit" );
-        }
-        return substr( $string, $start, $length );
-    }
-
 }
 
@@ -930,4 +801,6 @@
         }
     }
+
+}
     
-}
+?>
