Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
JsonResponse.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\HttpFoundation;
013
014 /**
015 * Response represents an HTTP response in JSON format.
016 *
017 * Note that this class does not force the returned JSON content to be an
018 * object. It is however recommended that you do return an object as it
019 * protects yourself against XSSI and JSON-JavaScript Hijacking.
020 *
021 * @see https://www.owasp.org/index.php/OWASP_AJAX_Security_Guidelines#Always_return_JSON_with_an_Object_on_the_outside
022 *
023 * @author Igor Wiedler <igor@wiedler.ch>
024 */
025 class JsonResponse extends Response
026 {
027 protected $data;
028 protected $callback;
029
030 /**
031 * Constructor.
032 *
033 * @param mixed $data The response data
034 * @param int $status The response status code
035 * @param array $headers An array of response headers
036 */
037 public function __construct($data = null, $status = 200, $headers = array())
038 {
039 parent::__construct('', $status, $headers);
040
041 if (null === $data) {
042 $data = new \ArrayObject();
043 }
044 $this->setData($data);
045 }
046
047 /**
048 * {@inheritdoc}
049 */
050 public static function create($data = null, $status = 200, $headers = array())
051 {
052 return new static($data, $status, $headers);
053 }
054
055 /**
056 * Sets the JSONP callback.
057 *
058 * @param string $callback
059 *
060 * @return JsonResponse
061 *
062 * @throws \InvalidArgumentException
063 */
064 public function setCallback($callback = null)
065 {
066 if (null !== $callback) {
067 // taken from http://www.geekality.net/2011/08/03/valid-javascript-identifier/
068 $pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u';
069 $parts = explode('.', $callback);
070 foreach ($parts as $part) {
071 if (!preg_match($pattern, $part)) {
072 throw new \InvalidArgumentException('The callback name is not valid.');
073 }
074 }
075 }
076
077 $this->callback = $callback;
078
079 return $this->update();
080 }
081
082 /**
083 * Sets the data to be sent as json.
084 *
085 * @param mixed $data
086 *
087 * @return JsonResponse
088 *
089 * @throws \InvalidArgumentException
090 */
091 public function setData($data = array())
092 {
093 // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML.
094 $this->data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
095
096 if (JSON_ERROR_NONE !== json_last_error()) {
097 throw new \InvalidArgumentException($this->transformJsonError());
098 }
099
100 return $this->update();
101 }
102
103 /**
104 * Updates the content and headers according to the json data and callback.
105 *
106 * @return JsonResponse
107 */
108 protected function update()
109 {
110 if (null !== $this->callback) {
111 // Not using application/javascript for compatibility reasons with older browsers.
112 $this->headers->set('Content-Type', 'text/javascript');
113
114 return $this->setContent(sprintf('/**/%s(%s);', $this->callback, $this->data));
115 }
116
117 // Only set the header when there is none or when it equals 'text/javascript' (from a previous update with callback)
118 // in order to not overwrite a custom definition.
119 if (!$this->headers->has('Content-Type') || 'text/javascript' === $this->headers->get('Content-Type')) {
120 $this->headers->set('Content-Type', 'application/json');
121 }
122
123 return $this->setContent($this->data);
124 }
125
126 private function transformJsonError()
127 {
128 if (function_exists('json_last_error_msg')) {
129 return json_last_error_msg();
130 }
131
132 switch (json_last_error()) {
133 case JSON_ERROR_DEPTH:
134 return 'Maximum stack depth exceeded.';
135
136 case JSON_ERROR_STATE_MISMATCH:
137 return 'Underflow or the modes mismatch.';
138
139 case JSON_ERROR_CTRL_CHAR:
140 return 'Unexpected control character found.';
141
142 case JSON_ERROR_SYNTAX:
143 return 'Syntax error, malformed JSON.';
144
145 case JSON_ERROR_UTF8:
146 return 'Malformed UTF-8 characters, possibly incorrectly encoded.';
147
148 default:
149 return 'Unknown error.';
150 }
151 }
152 }
153