1<?php
2/**
3 * Zend Framework
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
14 *
15 * @category   Zend
16 * @package    Zend_Json
17 * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
19 * @version    $Id$
20 */
21
22/**
23 * Zend_Json_Expr.
24 *
25 * @see Zend_Json_Expr
26 */
27
28/** @see Zend_Xml_Security */
29
30/**
31 * Class for encoding to and decoding from JSON.
32 *
33 * @category   Zend
34 * @package    Zend_Json
35 * @uses       Zend_Json_Expr
36 * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license    http://framework.zend.com/license/new-bsd     New BSD License
38 */
39class Zend_Json
40{
41    /**
42     * How objects should be encoded -- arrays or as StdClass. TYPE_ARRAY is 1
43     * so that it is a boolean true value, allowing it to be used with
44     * ext/json's functions.
45     */
46    const TYPE_ARRAY  = 1;
47    const TYPE_OBJECT = 0;
48
49     /**
50      * To check the allowed nesting depth of the XML tree during xml2json conversion.
51      *
52      * @var int
53      */
54    public static $maxRecursionDepthAllowed=25;
55
56    /**
57     * @var bool
58     */
59    public static $useBuiltinEncoderDecoder = false;
60
61    /**
62     * Decodes the given $encodedValue string which is
63     * encoded in the JSON format
64     *
65     * Uses ext/json's json_decode if available.
66     *
67     * @param string $encodedValue Encoded in JSON format
68     * @param int $objectDecodeType Optional; flag indicating how to decode
69     * objects. See {@link Zend_Json_Decoder::decode()} for details.
70     * @return mixed
71     */
72    public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
73    {
74        $encodedValue = (string) $encodedValue;
75        if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) {
76            $decode = json_decode($encodedValue, $objectDecodeType);
77
78            // php < 5.3
79            if (!function_exists('json_last_error')) {
80                if (strtolower($encodedValue) === 'null') {
81                    return null;
82                } elseif ($decode === null) {
83                    throw new Zend_Json_Exception('Decoding failed');
84                }
85            // php >= 5.3
86            } elseif (($jsonLastErr = json_last_error()) != JSON_ERROR_NONE) {
87                switch ($jsonLastErr) {
88                    case JSON_ERROR_DEPTH:
89                        throw new Zend_Json_Exception('Decoding failed: Maximum stack depth exceeded');
90                    case JSON_ERROR_CTRL_CHAR:
91                        throw new Zend_Json_Exception('Decoding failed: Unexpected control character found');
92                    case JSON_ERROR_SYNTAX:
93                        throw new Zend_Json_Exception('Decoding failed: Syntax error');
94                    default:
95                        throw new Zend_Json_Exception('Decoding failed');
96                }
97            }
98
99            return $decode;
100        }
101
102        return Zend_Json_Decoder::decode($encodedValue, $objectDecodeType);
103    }
104
105    /**
106     * Encode the mixed $valueToEncode into the JSON format
107     *
108     * Encodes using ext/json's json_encode() if available.
109     *
110     * NOTE: Object should not contain cycles; the JSON format
111     * does not allow object reference.
112     *
113     * NOTE: Only public variables will be encoded
114     *
115     * NOTE: Encoding native javascript expressions are possible using Zend_Json_Expr.
116     *       You can enable this by setting $options['enableJsonExprFinder'] = true
117     *
118     * @see Zend_Json_Expr
119     *
120     * @param  mixed $valueToEncode
121     * @param  boolean $cycleCheck Optional; whether or not to check for object recursion; off by default
122     * @param  array $options Additional options used during encoding
123     * @return string JSON encoded object
124     */
125    public static function encode($valueToEncode, $cycleCheck = false, $options = array())
126    {
127        if (is_object($valueToEncode)) {
128            if (method_exists($valueToEncode, 'toJson')) {
129                return $valueToEncode->toJson();
130            } elseif (method_exists($valueToEncode, 'toArray')) {
131                return self::encode($valueToEncode->toArray(), $cycleCheck, $options);
132            }
133        }
134
135        // Pre-encoding look for Zend_Json_Expr objects and replacing by tmp ids
136        $javascriptExpressions = array();
137        if(isset($options['enableJsonExprFinder'])
138           && ($options['enableJsonExprFinder'] == true)
139        ) {
140            /**
141             * @see Zend_Json_Encoder
142             */
143            $valueToEncode = self::_recursiveJsonExprFinder($valueToEncode, $javascriptExpressions);
144        }
145
146        // Encoding
147        if (function_exists('json_encode') && self::$useBuiltinEncoderDecoder !== true) {
148            $encodedResult = json_encode($valueToEncode);
149        } else {
150            $encodedResult = Zend_Json_Encoder::encode($valueToEncode, $cycleCheck, $options);
151        }
152
153        //only do post-proccessing to revert back the Zend_Json_Expr if any.
154        if (count($javascriptExpressions) > 0) {
155            $count = count($javascriptExpressions);
156            for($i = 0; $i < $count; $i++) {
157                $magicKey = $javascriptExpressions[$i]['magicKey'];
158                $value    = $javascriptExpressions[$i]['value'];
159
160                $encodedResult = str_replace(
161                    //instead of replacing "key:magicKey", we replace directly magicKey by value because "key" never changes.
162                    '"' . $magicKey . '"',
163                    $value,
164                    $encodedResult
165                );
166            }
167        }
168
169         return $encodedResult;
170    }
171
172    /**
173     * Check & Replace Zend_Json_Expr for tmp ids in the valueToEncode
174     *
175     * Check if the value is a Zend_Json_Expr, and if replace its value
176     * with a magic key and save the javascript expression in an array.
177     *
178     * NOTE this method is recursive.
179     *
180     * NOTE: This method is used internally by the encode method.
181     *
182     * @see encode
183     * @param array|object|Zend_Json_Expr $value a string - object property to be encoded
184     * @param array $javascriptExpressions
185     * @param null $currentKey
186     *
187     * @internal param mixed $valueToCheck
188     * @return void
189     */
190    protected static function _recursiveJsonExprFinder(&$value, array &$javascriptExpressions, $currentKey = null)
191    {
192         if ($value instanceof Zend_Json_Expr) {
193            // TODO: Optimize with ascii keys, if performance is bad
194            $magicKey = "____" . $currentKey . "_" . (count($javascriptExpressions));
195            $javascriptExpressions[] = array(
196
197                //if currentKey is integer, encodeUnicodeString call is not required.
198                "magicKey" => (is_int($currentKey)) ? $magicKey : Zend_Json_Encoder::encodeUnicodeString($magicKey),
199                "value"    => $value->__toString(),
200            );
201            $value = $magicKey;
202        } elseif (is_array($value)) {
203            foreach ($value as $k => $v) {
204                $value[$k] = self::_recursiveJsonExprFinder($value[$k], $javascriptExpressions, $k);
205            }
206        } elseif (is_object($value)) {
207            foreach ($value as $k => $v) {
208                $value->$k = self::_recursiveJsonExprFinder($value->$k, $javascriptExpressions, $k);
209            }
210        }
211        return $value;
212    }
213
214    /**
215     * Return the value of an XML attribute text or the text between
216     * the XML tags
217     *
218     * In order to allow Zend_Json_Expr from xml, we check if the node
219     * matchs the pattern that try to detect if it is a new Zend_Json_Expr
220     * if it matches, we return a new Zend_Json_Expr instead of a text node
221     *
222     * @param SimpleXMLElement $simpleXmlElementObject
223     * @return Zend_Json_Expr|string
224     */
225    protected static function _getXmlValue($simpleXmlElementObject) {
226        $pattern = '/^[\s]*new Zend_Json_Expr[\s]*\([\s]*[\"\']{1}(.*)[\"\']{1}[\s]*\)[\s]*$/';
227        $matchings = array();
228        $match = preg_match ($pattern, $simpleXmlElementObject, $matchings);
229        if ($match) {
230            return new Zend_Json_Expr($matchings[1]);
231        } else {
232            return (trim(strval($simpleXmlElementObject)));
233        }
234    }
235    /**
236     * _processXml - Contains the logic for xml2json
237     *
238     * The logic in this function is a recursive one.
239     *
240     * The main caller of this function (i.e. fromXml) needs to provide
241     * only the first two parameters i.e. the SimpleXMLElement object and
242     * the flag for ignoring or not ignoring XML attributes. The third parameter
243     * will be used internally within this function during the recursive calls.
244     *
245     * This function converts the SimpleXMLElement object into a PHP array by
246     * calling a recursive (protected static) function in this class. Once all
247     * the XML elements are stored in the PHP array, it is returned to the caller.
248     *
249     * Throws a Zend_Json_Exception if the XML tree is deeper than the allowed limit.
250     *
251     * @param SimpleXMLElement $simpleXmlElementObject
252     * @param boolean $ignoreXmlAttributes
253     * @param integer $recursionDepth
254     * @return array
255     */
256    protected static function _processXml($simpleXmlElementObject, $ignoreXmlAttributes, $recursionDepth=0)
257    {
258        // Keep an eye on how deeply we are involved in recursion.
259        if ($recursionDepth > self::$maxRecursionDepthAllowed) {
260            // XML tree is too deep. Exit now by throwing an exception.
261            throw new Zend_Json_Exception(
262                "Function _processXml exceeded the allowed recursion depth of " .
263                self::$maxRecursionDepthAllowed);
264        } // End of if ($recursionDepth > self::$maxRecursionDepthAllowed)
265
266        $children = $simpleXmlElementObject->children();
267        $name = $simpleXmlElementObject->getName();
268        $value = self::_getXmlValue($simpleXmlElementObject);
269        $attributes = (array) $simpleXmlElementObject->attributes();
270
271        if (count($children) == 0) {
272            if (!empty($attributes) && !$ignoreXmlAttributes) {
273                foreach ($attributes['@attributes'] as $k => $v) {
274                    $attributes['@attributes'][$k]= self::_getXmlValue($v);
275                }
276                if (!empty($value)) {
277                    $attributes['@text'] = $value;
278                }
279                return array($name => $attributes);
280            } else {
281               return array($name => $value);
282            }
283        } else {
284            $childArray= array();
285            foreach ($children as $child) {
286                $childname = $child->getName();
287                $element = self::_processXml($child,$ignoreXmlAttributes,$recursionDepth+1);
288                if (array_key_exists($childname, $childArray)) {
289                    if (empty($subChild[$childname])) {
290                        $childArray[$childname] = array($childArray[$childname]);
291                        $subChild[$childname] = true;
292                    }
293                    $childArray[$childname][] = $element[$childname];
294                } else {
295                    $childArray[$childname] = $element[$childname];
296                }
297            }
298            if (!empty($attributes) && !$ignoreXmlAttributes) {
299                foreach ($attributes['@attributes'] as $k => $v) {
300                    $attributes['@attributes'][$k] = self::_getXmlValue($v);
301                }
302                $childArray['@attributes'] = $attributes['@attributes'];
303            }
304            if (!empty($value)) {
305                $childArray['@text'] = $value;
306            }
307            return array($name => $childArray);
308        }
309    }
310
311    /**
312     * fromXml - Converts XML to JSON
313     *
314     * Converts a XML formatted string into a JSON formatted string.
315     * The value returned will be a string in JSON format.
316     *
317     * The caller of this function needs to provide only the first parameter,
318     * which is an XML formatted String. The second parameter is optional, which
319     * lets the user to select if the XML attributes in the input XML string
320     * should be included or ignored in xml2json conversion.
321     *
322     * This function converts the XML formatted string into a PHP array by
323     * calling a recursive (protected static) function in this class. Then, it
324     * converts that PHP array into JSON by calling the "encode" static funcion.
325     *
326     * Throws a Zend_Json_Exception if the input not a XML formatted string.
327     * NOTE: Encoding native javascript expressions via Zend_Json_Expr is not possible.
328     *
329     * @static
330     * @access public
331     * @param string $xmlStringContents XML String to be converted
332     * @param boolean $ignoreXmlAttributes Include or exclude XML attributes in
333     * the xml2json conversion process.
334     * @return mixed - JSON formatted string on success
335     * @throws Zend_Json_Exception
336     */
337    public static function fromXml($xmlStringContents, $ignoreXmlAttributes=true)
338    {
339        // Load the XML formatted string into a Simple XML Element object.
340        $simpleXmlElementObject = Zend_Xml_Security::scan($xmlStringContents);
341
342        // If it is not a valid XML content, throw an exception.
343        if ($simpleXmlElementObject == null) {
344            throw new Zend_Json_Exception('Function fromXml was called with an invalid XML formatted string.');
345        } // End of if ($simpleXmlElementObject == null)
346
347        $resultArray = null;
348
349        // Call the recursive function to convert the XML into a PHP array.
350        $resultArray = self::_processXml($simpleXmlElementObject, $ignoreXmlAttributes);
351
352        // Convert the PHP array to JSON using Zend_Json encode method.
353        // It is just that simple.
354        $jsonStringOutput = self::encode($resultArray);
355        return($jsonStringOutput);
356    }
357
358
359
360    /**
361     * Pretty-print JSON string
362     *
363     * Use 'format' option to select output format - currently html and txt supported, txt is default
364     * Use 'indent' option to override the indentation string set in the format - by default for the 'txt' format it's a tab
365     *
366     * @param string $json Original JSON string
367     * @param array $options Encoding options
368     * @return string
369     */
370    public static function prettyPrint($json, $options = array())
371    {
372        $tokens = preg_split('|([\{\}\]\[,])|', $json, -1, PREG_SPLIT_DELIM_CAPTURE);
373        $result = '';
374        $indent = 0;
375
376        $format= 'txt';
377
378        $ind = "\t";
379
380        if (isset($options['format'])) {
381            $format = $options['format'];
382        }
383
384        switch ($format) {
385            case 'html':
386                $lineBreak = '<br />';
387                $ind = '&nbsp;&nbsp;&nbsp;&nbsp;';
388                break;
389            default:
390            case 'txt':
391                $lineBreak = "\n";
392                $ind = "\t";
393                break;
394        }
395
396        // override the defined indent setting with the supplied option
397        if (isset($options['indent'])) {
398            $ind = $options['indent'];
399        }
400
401        $inLiteral = false;
402        foreach($tokens as $token) {
403            if($token == '') {
404                continue;
405            }
406
407            $prefix = str_repeat($ind, $indent);
408            if (!$inLiteral && ($token == '{' || $token == '[')) {
409                $indent++;
410                if (($result != '') && ($result[(strlen($result)-1)] == $lineBreak)) {
411                    $result .= $prefix;
412                }
413                $result .= $token . $lineBreak;
414            } elseif (!$inLiteral && ($token == '}' || $token == ']')) {
415                $indent--;
416                $prefix = str_repeat($ind, $indent);
417                $result .= $lineBreak . $prefix . $token;
418            } elseif (!$inLiteral && $token == ',') {
419                $result .= $token . $lineBreak;
420            } else {
421                $result .= ( $inLiteral ? '' : $prefix ) . $token;
422
423                // Count # of unescaped double-quotes in token, subtract # of
424                // escaped double-quotes and if the result is odd then we are
425                // inside a string literal
426                if ((substr_count($token, "\"")-substr_count($token, "\\\"")) % 2 != 0) {
427                    $inLiteral = !$inLiteral;
428                }
429            }
430        }
431        return $result;
432   }
433}
434