PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / vendor / prefixed / symfony / http-foundation / JsonResponse.php
matomo / app / vendor / prefixed / symfony / http-foundation Last commit date
Exception 2 years ago File 1 year ago RateLimiter 2 years ago Session 1 year ago Test 1 year ago AcceptHeader.php 1 year ago AcceptHeaderItem.php 2 years ago BinaryFileResponse.php 1 year ago Cookie.php 1 year ago ExpressionRequestMatcher.php 2 years ago FileBag.php 1 year ago HeaderBag.php 1 year ago HeaderUtils.php 1 year ago InputBag.php 2 years ago IpUtils.php 1 year ago JsonResponse.php 1 year ago LICENSE 2 years ago ParameterBag.php 1 year ago README.md 2 years ago RedirectResponse.php 2 years ago Request.php 7 months ago RequestMatcher.php 1 year ago RequestMatcherInterface.php 2 years ago RequestStack.php 2 years ago Response.php 1 year ago ResponseHeaderBag.php 1 year ago ServerBag.php 1 year ago StreamedResponse.php 1 year ago UrlHelper.php 2 years ago
JsonResponse.php
192 lines
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 namespace Matomo\Dependencies\Symfony\Component\HttpFoundation;
12
13 /**
14 * Response represents an HTTP response in JSON format.
15 *
16 * Note that this class does not force the returned JSON content to be an
17 * object. It is however recommended that you do return an object as it
18 * protects yourself against XSSI and JSON-JavaScript Hijacking.
19 *
20 * @see https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/AJAX_Security_Cheat_Sheet.md#always-return-json-with-an-object-on-the-outside
21 *
22 * @author Igor Wiedler <igor@wiedler.ch>
23 */
24 class JsonResponse extends Response
25 {
26 protected $data;
27 protected $callback;
28 // Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML.
29 // 15 === JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT
30 public const DEFAULT_ENCODING_OPTIONS = 15;
31 protected $encodingOptions = self::DEFAULT_ENCODING_OPTIONS;
32 /**
33 * @param mixed $data The response data
34 * @param int $status The response status code
35 * @param array $headers An array of response headers
36 * @param bool $json If the data is already a JSON string
37 */
38 public function __construct($data = null, int $status = 200, array $headers = [], bool $json = \false)
39 {
40 parent::__construct('', $status, $headers);
41 if ($json && !\is_string($data) && !is_numeric($data) && !\is_callable([$data, '__toString'])) {
42 throw new \TypeError(sprintf('"%s": If $json is set to true, argument $data must be a string or object implementing __toString(), "%s" given.', __METHOD__, get_debug_type($data)));
43 }
44 if (null === $data) {
45 $data = new \ArrayObject();
46 }
47 $json ? $this->setJson($data) : $this->setData($data);
48 }
49 /**
50 * Factory method for chainability.
51 *
52 * Example:
53 *
54 * return JsonResponse::create(['key' => 'value'])
55 * ->setSharedMaxAge(300);
56 *
57 * @param mixed $data The JSON response data
58 * @param int $status The response status code
59 * @param array $headers An array of response headers
60 *
61 * @return static
62 *
63 * @deprecated since Symfony 5.1, use __construct() instead.
64 */
65 public static function create($data = null, int $status = 200, array $headers = [])
66 {
67 trigger_deprecation('symfony/http-foundation', '5.1', 'The "%s()" method is deprecated, use "new %s()" instead.', __METHOD__, static::class);
68 return new static($data, $status, $headers);
69 }
70 /**
71 * Factory method for chainability.
72 *
73 * Example:
74 *
75 * return JsonResponse::fromJsonString('{"key": "value"}')
76 * ->setSharedMaxAge(300);
77 *
78 * @param string $data The JSON response string
79 * @param int $status The response status code
80 * @param array $headers An array of response headers
81 *
82 * @return static
83 */
84 public static function fromJsonString(string $data, int $status = 200, array $headers = [])
85 {
86 return new static($data, $status, $headers, \true);
87 }
88 /**
89 * Sets the JSONP callback.
90 *
91 * @param string|null $callback The JSONP callback or null to use none
92 *
93 * @return $this
94 *
95 * @throws \InvalidArgumentException When the callback name is not valid
96 */
97 public function setCallback(?string $callback = null)
98 {
99 if (null !== $callback) {
100 // partially taken from https://geekality.net/2011/08/03/valid-javascript-identifier/
101 // partially taken from https://github.com/willdurand/JsonpCallbackValidator
102 // JsonpCallbackValidator is released under the MIT License. See https://github.com/willdurand/JsonpCallbackValidator/blob/v1.1.0/LICENSE for details.
103 // (c) William Durand <william.durand1@gmail.com>
104 $pattern = '/^[$_\\p{L}][$_\\p{L}\\p{Mn}\\p{Mc}\\p{Nd}\\p{Pc}\\x{200C}\\x{200D}]*(?:\\[(?:"(?:\\\\.|[^"\\\\])*"|\'(?:\\\\.|[^\'\\\\])*\'|\\d+)\\])*?$/u';
105 $reserved = ['break', 'do', 'instanceof', 'typeof', 'case', 'else', 'new', 'var', 'catch', 'finally', 'return', 'void', 'continue', 'for', 'switch', 'while', 'debugger', 'function', 'this', 'with', 'default', 'if', 'throw', 'delete', 'in', 'try', 'class', 'enum', 'extends', 'super', 'const', 'export', 'import', 'implements', 'let', 'private', 'public', 'yield', 'interface', 'package', 'protected', 'static', 'null', 'true', 'false'];
106 $parts = explode('.', $callback);
107 foreach ($parts as $part) {
108 if (!preg_match($pattern, $part) || \in_array($part, $reserved, \true)) {
109 throw new \InvalidArgumentException('The callback name is not valid.');
110 }
111 }
112 }
113 $this->callback = $callback;
114 return $this->update();
115 }
116 /**
117 * Sets a raw string containing a JSON document to be sent.
118 *
119 * @return $this
120 */
121 public function setJson(string $json)
122 {
123 $this->data = $json;
124 return $this->update();
125 }
126 /**
127 * Sets the data to be sent as JSON.
128 *
129 * @param mixed $data
130 *
131 * @return $this
132 *
133 * @throws \InvalidArgumentException
134 */
135 public function setData($data = [])
136 {
137 try {
138 $data = json_encode($data, $this->encodingOptions);
139 } catch (\Exception $e) {
140 if ('Exception' === \get_class($e) && str_starts_with($e->getMessage(), 'Failed calling ')) {
141 throw $e->getPrevious() ?: $e;
142 }
143 throw $e;
144 }
145 if (\PHP_VERSION_ID >= 70300 && \JSON_THROW_ON_ERROR & $this->encodingOptions) {
146 return $this->setJson($data);
147 }
148 if (\JSON_ERROR_NONE !== json_last_error()) {
149 throw new \InvalidArgumentException(json_last_error_msg());
150 }
151 return $this->setJson($data);
152 }
153 /**
154 * Returns options used while encoding data to JSON.
155 *
156 * @return int
157 */
158 public function getEncodingOptions()
159 {
160 return $this->encodingOptions;
161 }
162 /**
163 * Sets options used while encoding data to JSON.
164 *
165 * @return $this
166 */
167 public function setEncodingOptions(int $encodingOptions)
168 {
169 $this->encodingOptions = $encodingOptions;
170 return $this->setData(json_decode($this->data));
171 }
172 /**
173 * Updates the content and headers according to the JSON data and callback.
174 *
175 * @return $this
176 */
177 protected function update()
178 {
179 if (null !== $this->callback) {
180 // Not using application/javascript for compatibility reasons with older browsers.
181 $this->headers->set('Content-Type', 'text/javascript');
182 return $this->setContent(sprintf('/**/%s(%s);', $this->callback, $this->data));
183 }
184 // Only set the header when there is none or when it equals 'text/javascript' (from a previous update with callback)
185 // in order to not overwrite a custom definition.
186 if (!$this->headers->has('Content-Type') || 'text/javascript' === $this->headers->get('Content-Type')) {
187 $this->headers->set('Content-Type', 'application/json');
188 }
189 return $this->setContent($this->data);
190 }
191 }
192