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