import v1.1.0_beta1 | 2009-08-21
This commit is contained in:
@ -23,12 +23,6 @@
|
||||
*/
|
||||
require_once 'Zend/Json.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Json_Exception
|
||||
*/
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
|
||||
|
||||
/**
|
||||
* Decode JSON encoded string to PHP variable constructs
|
||||
*
|
||||
@ -102,8 +96,8 @@ class Zend_Json_Decoder
|
||||
protected function __construct($source, $decodeType)
|
||||
{
|
||||
// Set defaults
|
||||
$this->_source = $source;
|
||||
$this->_sourceLength = strlen($source);
|
||||
$this->_source = self::decodeUnicodeString($source);
|
||||
$this->_sourceLength = strlen($this->_source);
|
||||
$this->_token = self::EOF;
|
||||
$this->_offset = 0;
|
||||
|
||||
@ -149,8 +143,10 @@ class Zend_Json_Decoder
|
||||
public static function decode($source = null, $objectDecodeType = Zend_Json::TYPE_ARRAY)
|
||||
{
|
||||
if (null === $source) {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception('Must specify JSON encoded source for decoding');
|
||||
} elseif (!is_string($source)) {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception('Can only decode JSON encoded strings');
|
||||
}
|
||||
|
||||
@ -189,7 +185,7 @@ class Zend_Json_Decoder
|
||||
* Decodes an object of the form:
|
||||
* { "attribute: value, "attribute2" : value,...}
|
||||
*
|
||||
* If Zend_Json_Encoder was used to encode the original object then
|
||||
* If Zend_Json_Encoder was used to encode the original object then
|
||||
* a special attribute called __className which specifies a class
|
||||
* name that should wrap the data contained within the encoded source.
|
||||
*
|
||||
@ -206,6 +202,7 @@ class Zend_Json_Decoder
|
||||
|
||||
while ($tok && $tok != self::RBRACE) {
|
||||
if ($tok != self::DATUM || ! is_string($this->_tokenValue)) {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception('Missing key in object encoding: ' . $this->_source);
|
||||
}
|
||||
|
||||
@ -213,6 +210,7 @@ class Zend_Json_Decoder
|
||||
$tok = $this->_getNextToken();
|
||||
|
||||
if ($tok != self::COLON) {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception('Missing ":" in object encoding: ' . $this->_source);
|
||||
}
|
||||
|
||||
@ -225,6 +223,7 @@ class Zend_Json_Decoder
|
||||
}
|
||||
|
||||
if ($tok != self::COMMA) {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception('Missing "," in object encoding: ' . $this->_source);
|
||||
}
|
||||
|
||||
@ -271,6 +270,7 @@ class Zend_Json_Decoder
|
||||
}
|
||||
|
||||
if ($tok != self::COMMA) {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception('Missing "," in array encoding: ' . $this->_source);
|
||||
}
|
||||
|
||||
@ -348,6 +348,7 @@ class Zend_Json_Decoder
|
||||
}
|
||||
|
||||
$chr = $str{$i};
|
||||
|
||||
if ($chr == '\\') {
|
||||
$i++;
|
||||
if ($i >= $str_length) {
|
||||
@ -383,10 +384,11 @@ class Zend_Json_Decoder
|
||||
$result .= '\'';
|
||||
break;
|
||||
default:
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception("Illegal escape "
|
||||
. "sequence '" . $chr . "'");
|
||||
}
|
||||
} elseif ($chr == '"') {
|
||||
}
|
||||
} elseif($chr == '"') {
|
||||
break;
|
||||
} else {
|
||||
$result .= $chr;
|
||||
@ -434,6 +436,7 @@ class Zend_Json_Decoder
|
||||
|
||||
if (is_numeric($datum)) {
|
||||
if (preg_match('/^0\d+$/', $datum)) {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception("Octal notation not supported by JSON (value: $datum)");
|
||||
} else {
|
||||
$val = intval($datum);
|
||||
@ -441,6 +444,7 @@ class Zend_Json_Decoder
|
||||
$this->_tokenValue = ($val == $fVal ? $val : $fVal);
|
||||
}
|
||||
} else {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception("Illegal number format: $datum");
|
||||
}
|
||||
|
||||
@ -448,10 +452,126 @@ class Zend_Json_Decoder
|
||||
$this->_offset = $start + strlen($datum);
|
||||
}
|
||||
} else {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception('Illegal Token');
|
||||
}
|
||||
|
||||
return($this->_token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode Unicode Characters from \u0000 ASCII syntax.
|
||||
*
|
||||
* This algorithm was originally developed for the
|
||||
* Solar Framework by Paul M. Jones
|
||||
*
|
||||
* @link http://solarphp.com/
|
||||
* @link http://svn.solarphp.com/core/trunk/Solar/Json.php
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public static function decodeUnicodeString($chrs)
|
||||
{
|
||||
$delim = substr($chrs, 0, 1);
|
||||
$utf8 = '';
|
||||
$strlen_chrs = strlen($chrs);
|
||||
|
||||
for($i = 0; $i < $strlen_chrs; $i++) {
|
||||
|
||||
$substr_chrs_c_2 = substr($chrs, $i, 2);
|
||||
$ord_chrs_c = ord($chrs[$i]);
|
||||
|
||||
switch (true) {
|
||||
case preg_match('/\\\u[0-9A-F]{4}/i', substr($chrs, $i, 6)):
|
||||
// single, escaped unicode character
|
||||
$utf16 = chr(hexdec(substr($chrs, ($i + 2), 2)))
|
||||
. chr(hexdec(substr($chrs, ($i + 4), 2)));
|
||||
$utf8 .= self::_utf162utf8($utf16);
|
||||
$i += 5;
|
||||
break;
|
||||
case ($ord_chrs_c >= 0x20) && ($ord_chrs_c <= 0x7F):
|
||||
$utf8 .= $chrs{$i};
|
||||
break;
|
||||
case ($ord_chrs_c & 0xE0) == 0xC0:
|
||||
// characters U-00000080 - U-000007FF, mask 110XXXXX
|
||||
//see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$utf8 .= substr($chrs, $i, 2);
|
||||
++$i;
|
||||
break;
|
||||
case ($ord_chrs_c & 0xF0) == 0xE0:
|
||||
// characters U-00000800 - U-0000FFFF, mask 1110XXXX
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$utf8 .= substr($chrs, $i, 3);
|
||||
$i += 2;
|
||||
break;
|
||||
case ($ord_chrs_c & 0xF8) == 0xF0:
|
||||
// characters U-00010000 - U-001FFFFF, mask 11110XXX
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$utf8 .= substr($chrs, $i, 4);
|
||||
$i += 3;
|
||||
break;
|
||||
case ($ord_chrs_c & 0xFC) == 0xF8:
|
||||
// characters U-00200000 - U-03FFFFFF, mask 111110XX
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$utf8 .= substr($chrs, $i, 5);
|
||||
$i += 4;
|
||||
break;
|
||||
case ($ord_chrs_c & 0xFE) == 0xFC:
|
||||
// characters U-04000000 - U-7FFFFFFF, mask 1111110X
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$utf8 .= substr($chrs, $i, 6);
|
||||
$i += 5;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $utf8;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a string from one UTF-16 char to one UTF-8 char.
|
||||
*
|
||||
* Normally should be handled by mb_convert_encoding, but
|
||||
* provides a slower PHP-only method for installations
|
||||
* that lack the multibye string extension.
|
||||
*
|
||||
* This method is from the Solar Framework by Paul M. Jones
|
||||
*
|
||||
* @link http://solarphp.com
|
||||
* @param string $utf16 UTF-16 character
|
||||
* @return string UTF-8 character
|
||||
*/
|
||||
protected static function _utf162utf8($utf16)
|
||||
{
|
||||
// Check for mb extension otherwise do by hand.
|
||||
if( function_exists('mb_convert_encoding') ) {
|
||||
return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
|
||||
}
|
||||
|
||||
$bytes = (ord($utf16{0}) << 8) | ord($utf16{1});
|
||||
|
||||
switch (true) {
|
||||
case ((0x7F & $bytes) == $bytes):
|
||||
// this case should never be reached, because we are in ASCII range
|
||||
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
return chr(0x7F & $bytes);
|
||||
|
||||
case (0x07FF & $bytes) == $bytes:
|
||||
// return a 2-byte UTF-8 character
|
||||
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
return chr(0xC0 | (($bytes >> 6) & 0x1F))
|
||||
. chr(0x80 | ($bytes & 0x3F));
|
||||
|
||||
case (0xFFFF & $bytes) == $bytes:
|
||||
// return a 3-byte UTF-8 character
|
||||
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
return chr(0xE0 | (($bytes >> 12) & 0x0F))
|
||||
. chr(0x80 | (($bytes >> 6) & 0x3F))
|
||||
. chr(0x80 | ($bytes & 0x3F));
|
||||
}
|
||||
|
||||
// ignoring UTF-32 for now, sorry
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,13 +18,6 @@
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Zend_Json_Exception
|
||||
*/
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
|
||||
|
||||
/**
|
||||
* Encode PHP constructs to JSON
|
||||
*
|
||||
@ -41,10 +34,10 @@ class Zend_Json_Encoder
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_cycleCheck;
|
||||
|
||||
|
||||
/**
|
||||
* Additional options used during encoding
|
||||
*
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_options = array();
|
||||
@ -122,13 +115,14 @@ class Zend_Json_Encoder
|
||||
{
|
||||
if ($this->_cycleCheck) {
|
||||
if ($this->_wasVisited($value)) {
|
||||
|
||||
|
||||
if (isset($this->_options['silenceCyclicalExceptions'])
|
||||
&& $this->_options['silenceCyclicalExceptions']===true) {
|
||||
|
||||
|
||||
return '"* RECURSION (' . get_class($value) . ') *"';
|
||||
|
||||
|
||||
} else {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception(
|
||||
'Cycles not supported in JSON encoding, cycle introduced by '
|
||||
. 'class "' . get_class($value) . '"'
|
||||
@ -140,13 +134,13 @@ class Zend_Json_Encoder
|
||||
}
|
||||
|
||||
$props = '';
|
||||
|
||||
|
||||
if ($value instanceof Iterator) {
|
||||
$propCollection = $value;
|
||||
$propCollection = $value;
|
||||
} else {
|
||||
$propCollection = get_object_vars($value);
|
||||
$propCollection = get_object_vars($value);
|
||||
}
|
||||
|
||||
|
||||
foreach ($propCollection as $name => $propValue) {
|
||||
if (isset($propValue)) {
|
||||
$props .= ','
|
||||
@ -236,6 +230,7 @@ class Zend_Json_Encoder
|
||||
|
||||
if (is_int($value) || is_float($value)) {
|
||||
$result = (string) $value;
|
||||
$result = str_replace(",", ".", $result);
|
||||
} elseif (is_string($value)) {
|
||||
$result = $this->_encodeString($value);
|
||||
} elseif (is_bool($value)) {
|
||||
@ -264,6 +259,7 @@ class Zend_Json_Encoder
|
||||
// 0x08 => \b
|
||||
// 0x0c => \f
|
||||
$string = str_replace(array(chr(0x08), chr(0x0C)), array('\b', '\f'), $string);
|
||||
$string = self::encodeUnicodeString($string);
|
||||
|
||||
return '"' . $string . '"';
|
||||
}
|
||||
@ -405,6 +401,7 @@ class Zend_Json_Encoder
|
||||
{
|
||||
$cls = new ReflectionClass($className);
|
||||
if (! $cls->isInstantiable()) {
|
||||
require_once 'Zend/Json/Exception.php';
|
||||
throw new Zend_Json_Exception("$className must be instantiable");
|
||||
}
|
||||
|
||||
@ -434,5 +431,143 @@ class Zend_Json_Encoder
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode Unicode Characters to \u0000 ASCII syntax.
|
||||
*
|
||||
* This algorithm was originally developed for the
|
||||
* Solar Framework by Paul M. Jones
|
||||
*
|
||||
* @link http://solarphp.com/
|
||||
* @link http://svn.solarphp.com/core/trunk/Solar/Json.php
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public static function encodeUnicodeString($value)
|
||||
{
|
||||
$strlen_var = strlen($value);
|
||||
$ascii = "";
|
||||
|
||||
/**
|
||||
* Iterate over every character in the string,
|
||||
* escaping with a slash or encoding to UTF-8 where necessary
|
||||
*/
|
||||
for($i = 0; $i < $strlen_var; $i++) {
|
||||
$ord_var_c = ord($value[$i]);
|
||||
|
||||
switch (true) {
|
||||
case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
|
||||
// characters U-00000000 - U-0000007F (same as ASCII)
|
||||
$ascii .= $value[$i];
|
||||
break;
|
||||
|
||||
case (($ord_var_c & 0xE0) == 0xC0):
|
||||
// characters U-00000080 - U-000007FF, mask 110XXXXX
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$char = pack('C*', $ord_var_c, ord($value[$i + 1]));
|
||||
$i += 1;
|
||||
$utf16 = self::_utf82utf16($char);
|
||||
$ascii .= sprintf('\u%04s', bin2hex($utf16));
|
||||
break;
|
||||
|
||||
case (($ord_var_c & 0xF0) == 0xE0):
|
||||
// characters U-00000800 - U-0000FFFF, mask 1110XXXX
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$char = pack('C*', $ord_var_c,
|
||||
ord($value[$i + 1]),
|
||||
ord($value[$i + 2]));
|
||||
$i += 2;
|
||||
$utf16 = self::_utf82utf16($char);
|
||||
$ascii .= sprintf('\u%04s', bin2hex($utf16));
|
||||
break;
|
||||
|
||||
case (($ord_var_c & 0xF8) == 0xF0):
|
||||
// characters U-00010000 - U-001FFFFF, mask 11110XXX
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$char = pack('C*', $ord_var_c,
|
||||
ord($value[$i + 1]),
|
||||
ord($value[$i + 2]),
|
||||
ord($value[$i + 3]));
|
||||
$i += 3;
|
||||
$utf16 = self::_utf82utf16($char);
|
||||
$ascii .= sprintf('\u%04s', bin2hex($utf16));
|
||||
break;
|
||||
|
||||
case (($ord_var_c & 0xFC) == 0xF8):
|
||||
// characters U-00200000 - U-03FFFFFF, mask 111110XX
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$char = pack('C*', $ord_var_c,
|
||||
ord($value[$i + 1]),
|
||||
ord($value[$i + 2]),
|
||||
ord($value[$i + 3]),
|
||||
ord($value[$i + 4]));
|
||||
$i += 4;
|
||||
$utf16 = self::_utf82utf16($char);
|
||||
$ascii .= sprintf('\u%04s', bin2hex($utf16));
|
||||
break;
|
||||
|
||||
case (($ord_var_c & 0xFE) == 0xFC):
|
||||
// characters U-04000000 - U-7FFFFFFF, mask 1111110X
|
||||
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
$char = pack('C*', $ord_var_c,
|
||||
ord($value[$i + 1]),
|
||||
ord($value[$i + 2]),
|
||||
ord($value[$i + 3]),
|
||||
ord($value[$i + 4]),
|
||||
ord($value[$i + 5]));
|
||||
$i += 5;
|
||||
$utf16 = self::_utf82utf16($char);
|
||||
$ascii .= sprintf('\u%04s', bin2hex($utf16));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $ascii;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a string from one UTF-8 char to one UTF-16 char.
|
||||
*
|
||||
* Normally should be handled by mb_convert_encoding, but
|
||||
* provides a slower PHP-only method for installations
|
||||
* that lack the multibye string extension.
|
||||
*
|
||||
* This method is from the Solar Framework by Paul M. Jones
|
||||
*
|
||||
* @link http://solarphp.com
|
||||
* @param string $utf8 UTF-8 character
|
||||
* @return string UTF-16 character
|
||||
*/
|
||||
protected static function _utf82utf16($utf8)
|
||||
{
|
||||
// Check for mb extension otherwise do by hand.
|
||||
if( function_exists('mb_convert_encoding') ) {
|
||||
return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
|
||||
}
|
||||
|
||||
switch (strlen($utf8)) {
|
||||
case 1:
|
||||
// this case should never be reached, because we are in ASCII range
|
||||
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
return $utf8;
|
||||
|
||||
case 2:
|
||||
// return a UTF-16 character from a 2-byte UTF-8 char
|
||||
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
return chr(0x07 & (ord($utf8{0}) >> 2))
|
||||
. chr((0xC0 & (ord($utf8{0}) << 6))
|
||||
| (0x3F & ord($utf8{1})));
|
||||
|
||||
case 3:
|
||||
// return a UTF-16 character from a 3-byte UTF-8 char
|
||||
// see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
|
||||
return chr((0xF0 & (ord($utf8{0}) << 4))
|
||||
| (0x0F & (ord($utf8{1}) >> 2)))
|
||||
. chr((0xC0 & (ord($utf8{1}) << 6))
|
||||
| (0x7F & ord($utf8{2})));
|
||||
}
|
||||
|
||||
// ignoring UTF-32 for now, sorry
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
|
80
libs/Zend/Json/Expr.php
Normal file
80
libs/Zend/Json/Expr.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Json
|
||||
* @subpackage Expr
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Expr.php 9101 2008-11-14 Oscar Reales $
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class for Zend_Json encode method.
|
||||
*
|
||||
* This class simply holds a string with a native Javascript Expression,
|
||||
* so objects | arrays to be encoded with Zend_Json can contain native
|
||||
* Javascript Expressions.
|
||||
*
|
||||
* Example:
|
||||
* <code>
|
||||
* $foo = array(
|
||||
* 'integer' =>9,
|
||||
* 'string' =>'test string',
|
||||
* 'function' => Zend_Json_Expr(
|
||||
* 'function(){ window.alert("javascript function encoded by Zend_Json") }'
|
||||
* ),
|
||||
* );
|
||||
*
|
||||
* Zend_Json::encode($foo, false, array('enableJsonExprFinder' => true));
|
||||
* // it will returns json encoded string:
|
||||
* // {"integer":9,"string":"test string","function":function(){window.alert("javascript function encoded by Zend_Json")}}
|
||||
* </code>
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Json
|
||||
* @subpackage Expr
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Json_Expr
|
||||
{
|
||||
/**
|
||||
* Storage for javascript expression.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_expression;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $expression the expression to hold.
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($expression)
|
||||
{
|
||||
$this->_expression = (string) $expression;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast to string
|
||||
*
|
||||
* @return string holded javascript expression.
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->_expression;
|
||||
}
|
||||
}
|
@ -50,7 +50,7 @@ class Zend_Json_Server_Request
|
||||
* Regex for method
|
||||
* @var string
|
||||
*/
|
||||
protected $_methodRegex = '/^[a-z][a-z0-9_]*$/i';
|
||||
protected $_methodRegex = '/^[a-z][a-z0-9_.]*$/i';
|
||||
|
||||
/**
|
||||
* Request parameters
|
||||
|
Reference in New Issue
Block a user