PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.20.0
GiveWP – Donation Plugin and Fundraising Platform v2.20.0
4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / vendor / symfony / http-foundation / JsonResponse.php
give / vendor / symfony / http-foundation Last commit date
Exception 4 years ago File 4 years ago Session 4 years ago Tests 4 years ago AcceptHeader.php 4 years ago AcceptHeaderItem.php 4 years ago ApacheRequest.php 4 years ago BinaryFileResponse.php 4 years ago Cookie.php 4 years ago ExpressionRequestMatcher.php 4 years ago FileBag.php 4 years ago HeaderBag.php 4 years ago IpUtils.php 4 years ago JsonResponse.php 4 years ago LICENSE 4 years ago ParameterBag.php 4 years ago RedirectResponse.php 4 years ago Request.php 4 years ago RequestMatcher.php 4 years ago RequestMatcherInterface.php 4 years ago RequestStack.php 4 years ago Response.php 4 years ago ResponseHeaderBag.php 4 years ago ServerBag.php 4 years ago StreamedResponse.php 4 years ago
JsonResponse.php
236 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
12 namespace Symfony\Component\HttpFoundation;
13
14 /**
15 * Response represents an HTTP response in JSON format.
16 *
17 * Note that this class does not force the returned JSON content to be an
18 * object. It is however recommended that you do return an object as it
19 * protects yourself against XSSI and JSON-JavaScript Hijacking.
20 *
21 * @see https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/AJAX_Security_Cheat_Sheet.md#always-return-json-with-an-object-on-the-outside
22 *
23 * @author Igor Wiedler <igor@wiedler.ch>
24 */
25 class JsonResponse extends Response
26 {
27 protected $data;
28 protected $callback;
29
30 // Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML.
31 // 15 === JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT
32 const DEFAULT_ENCODING_OPTIONS = 15;
33
34 protected $encodingOptions = self::DEFAULT_ENCODING_OPTIONS;
35
36 /**
37 * @param mixed $data The response data
38 * @param int $status The response status code
39 * @param array $headers An array of response headers
40 * @param bool $json If the data is already a JSON string
41 */
42 public function __construct($data = null, $status = 200, $headers = [], $json = false)
43 {
44 parent::__construct('', $status, $headers);
45
46 if (null === $data) {
47 $data = new \ArrayObject();
48 }
49
50 $json ? $this->setJson($data) : $this->setData($data);
51 }
52
53 /**
54 * Factory method for chainability.
55 *
56 * Example:
57 *
58 * return JsonResponse::create(['key' => 'value'])
59 * ->setSharedMaxAge(300);
60 *
61 * @param mixed $data The JSON response data
62 * @param int $status The response status code
63 * @param array $headers An array of response headers
64 *
65 * @return static
66 */
67 public static function create($data = null, $status = 200, $headers = [])
68 {
69 return new static($data, $status, $headers);
70 }
71
72 /**
73 * Factory method for chainability.
74 *
75 * Example:
76 *
77 * return JsonResponse::fromJsonString('{"key": "value"}')
78 * ->setSharedMaxAge(300);
79 *
80 * @param string|null $data The JSON response string
81 * @param int $status The response status code
82 * @param array $headers An array of response headers
83 *
84 * @return static
85 */
86 public static function fromJsonString($data = null, $status = 200, $headers = [])
87 {
88 return new static($data, $status, $headers, true);
89 }
90
91 /**
92 * Sets the JSONP callback.
93 *
94 * @param string|null $callback The JSONP callback or null to use none
95 *
96 * @return $this
97 *
98 * @throws \InvalidArgumentException When the callback name is not valid
99 */
100 public function setCallback($callback = null)
101 {
102 if (null !== $callback) {
103 // partially taken from https://geekality.net/2011/08/03/valid-javascript-identifier/
104 // partially taken from https://github.com/willdurand/JsonpCallbackValidator
105 // JsonpCallbackValidator is released under the MIT License. See https://github.com/willdurand/JsonpCallbackValidator/blob/v1.1.0/LICENSE for details.
106 // (c) William Durand <william.durand1@gmail.com>
107 $pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/u';
108 $reserved = [
109 'break', 'do', 'instanceof', 'typeof', 'case', 'else', 'new', 'var', 'catch', 'finally', 'return', 'void', 'continue', 'for', 'switch', 'while',
110 'debugger', 'function', 'this', 'with', 'default', 'if', 'throw', 'delete', 'in', 'try', 'class', 'enum', 'extends', 'super', 'const', 'export',
111 'import', 'implements', 'let', 'private', 'public', 'yield', 'interface', 'package', 'protected', 'static', 'null', 'true', 'false',
112 ];
113 $parts = explode('.', $callback);
114 foreach ($parts as $part) {
115 if (!preg_match($pattern, $part) || \in_array($part, $reserved, true)) {
116 throw new \InvalidArgumentException('The callback name is not valid.');
117 }
118 }
119 }
120
121 $this->callback = $callback;
122
123 return $this->update();
124 }
125
126 /**
127 * Sets a raw string containing a JSON document to be sent.
128 *
129 * @param string $json
130 *
131 * @return $this
132 *
133 * @throws \InvalidArgumentException
134 */
135 public function setJson($json)
136 {
137 $this->data = $json;
138
139 return $this->update();
140 }
141
142 /**
143 * Sets the data to be sent as JSON.
144 *
145 * @param mixed $data
146 *
147 * @return $this
148 *
149 * @throws \InvalidArgumentException
150 */
151 public function setData($data = [])
152 {
153 if (\defined('HHVM_VERSION')) {
154 // HHVM does not trigger any warnings and let exceptions
155 // thrown from a JsonSerializable object pass through.
156 // If only PHP did the same...
157 $data = json_encode($data, $this->encodingOptions);
158 } else {
159 if (!interface_exists('JsonSerializable', false)) {
160 set_error_handler(function () { return false; });
161 try {
162 $data = @json_encode($data, $this->encodingOptions);
163 } finally {
164 restore_error_handler();
165 }
166 } else {
167 try {
168 $data = json_encode($data, $this->encodingOptions);
169 } catch (\Exception $e) {
170 if ('Exception' === \get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
171 throw $e->getPrevious() ?: $e;
172 }
173 throw $e;
174 }
175
176 if (\PHP_VERSION_ID >= 70300 && (\JSON_THROW_ON_ERROR & $this->encodingOptions)) {
177 return $this->setJson($data);
178 }
179 }
180 }
181
182 if (\JSON_ERROR_NONE !== json_last_error()) {
183 throw new \InvalidArgumentException(json_last_error_msg());
184 }
185
186 return $this->setJson($data);
187 }
188
189 /**
190 * Returns options used while encoding data to JSON.
191 *
192 * @return int
193 */
194 public function getEncodingOptions()
195 {
196 return $this->encodingOptions;
197 }
198
199 /**
200 * Sets options used while encoding data to JSON.
201 *
202 * @param int $encodingOptions
203 *
204 * @return $this
205 */
206 public function setEncodingOptions($encodingOptions)
207 {
208 $this->encodingOptions = (int) $encodingOptions;
209
210 return $this->setData(json_decode($this->data));
211 }
212
213 /**
214 * Updates the content and headers according to the JSON data and callback.
215 *
216 * @return $this
217 */
218 protected function update()
219 {
220 if (null !== $this->callback) {
221 // Not using application/javascript for compatibility reasons with older browsers.
222 $this->headers->set('Content-Type', 'text/javascript');
223
224 return $this->setContent(sprintf('/**/%s(%s);', $this->callback, $this->data));
225 }
226
227 // Only set the header when there is none or when it equals 'text/javascript' (from a previous update with callback)
228 // in order to not overwrite a custom definition.
229 if (!$this->headers->has('Content-Type') || 'text/javascript' === $this->headers->get('Content-Type')) {
230 $this->headers->set('Content-Type', 'application/json');
231 }
232
233 return $this->setContent($this->data);
234 }
235 }
236