Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / http-foundation / JsonResponse.php
index a83d0077a84293b58fc6dd58a9b02a0733f5ccda..cf1a11ea2c07b690939d7b19600c5b668c0ee54b 100644 (file)
@@ -29,16 +29,17 @@ class JsonResponse extends Response
 
     // Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML.
     // 15 === JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT
-    protected $encodingOptions = 15;
+    const DEFAULT_ENCODING_OPTIONS = 15;
+
+    protected $encodingOptions = self::DEFAULT_ENCODING_OPTIONS;
 
     /**
-     * Constructor.
-     *
      * @param mixed $data    The response data
      * @param int   $status  The response status code
      * @param array $headers An array of response headers
+     * @param bool  $json    If the data is already a JSON string
      */
-    public function __construct($data = null, $status = 200, $headers = array())
+    public function __construct($data = null, $status = 200, $headers = array(), $json = false)
     {
         parent::__construct('', $status, $headers);
 
@@ -46,7 +47,7 @@ class JsonResponse extends Response
             $data = new \ArrayObject();
         }
 
-        $this->setData($data);
+        $json ? $this->setJson($data) : $this->setData($data);
     }
 
     /**
@@ -68,6 +69,14 @@ class JsonResponse extends Response
         return new static($data, $status, $headers);
     }
 
+    /**
+     * Make easier the creation of JsonResponse from raw json.
+     */
+    public static function fromJsonString($data = null, $status = 200, $headers = array())
+    {
+        return new static($data, $status, $headers, true);
+    }
+
     /**
      * Sets the JSONP callback.
      *
@@ -80,8 +89,8 @@ class JsonResponse extends Response
     public function setCallback($callback = null)
     {
         if (null !== $callback) {
-            // partially token from http://www.geekality.net/2011/08/03/valid-javascript-identifier/
-            // partially token from https://github.com/willdurand/JsonpCallbackValidator
+            // partially taken from http://www.geekality.net/2011/08/03/valid-javascript-identifier/
+            // partially taken from https://github.com/willdurand/JsonpCallbackValidator
             //      JsonpCallbackValidator is released under the MIT License. See https://github.com/willdurand/JsonpCallbackValidator/blob/v1.1.0/LICENSE for details.
             //      (c) William Durand <william.durand1@gmail.com>
             $pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/u';
@@ -103,6 +112,22 @@ class JsonResponse extends Response
         return $this->update();
     }
 
+    /**
+     * Sets a raw string containing a JSON document to be sent.
+     *
+     * @param string $json
+     *
+     * @return $this
+     *
+     * @throws \InvalidArgumentException
+     */
+    public function setJson($json)
+    {
+        $this->data = $json;
+
+        return $this->update();
+    }
+
     /**
      * Sets the data to be sent as JSON.
      *
@@ -121,39 +146,12 @@ class JsonResponse extends Response
             $data = json_encode($data, $this->encodingOptions);
         } else {
             try {
-                if (\PHP_VERSION_ID < 50400) {
-                    // PHP 5.3 triggers annoying warnings for some
-                    // types that can't be serialized as JSON (INF, resources, etc.)
-                    // but doesn't provide the JsonSerializable interface.
-                    set_error_handler(function () { return false; });
-                    $data = @json_encode($data, $this->encodingOptions);
-                } else {
-                    // PHP 5.4 and up wrap exceptions thrown by JsonSerializable
-                    // objects in a new exception that needs to be removed.
-                    // Fortunately, PHP 5.5 and up do not trigger any warning anymore.
-                    if (\PHP_VERSION_ID < 50500) {
-                        // Clear json_last_error()
-                        json_encode(null);
-                        $errorHandler = set_error_handler('var_dump');
-                        restore_error_handler();
-                        set_error_handler(function () use ($errorHandler) {
-                            if (JSON_ERROR_NONE === json_last_error()) {
-                                return $errorHandler && false !== call_user_func_array($errorHandler, func_get_args());
-                            }
-                        });
-                    }
-
-                    $data = json_encode($data, $this->encodingOptions);
-                }
-
-                if (\PHP_VERSION_ID < 50500) {
-                    restore_error_handler();
-                }
+                // PHP 5.4 and up wrap exceptions thrown by JsonSerializable
+                // objects in a new exception that needs to be removed.
+                // Fortunately, PHP 5.5 and up do not trigger any warning anymore.
+                $data = json_encode($data, $this->encodingOptions);
             } catch (\Exception $e) {
-                if (\PHP_VERSION_ID < 50500) {
-                    restore_error_handler();
-                }
-                if (\PHP_VERSION_ID >= 50400 && 'Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
+                if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
                     throw $e->getPrevious() ?: $e;
                 }
                 throw $e;
@@ -164,9 +162,7 @@ class JsonResponse extends Response
             throw new \InvalidArgumentException(json_last_error_msg());
         }
 
-        $this->data = $data;
-
-        return $this->update();
+        return $this->setJson($data);
     }
 
     /**