Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

JsonResponse.php

Zuletzt modifiziert: 09.10.2024, 12:54 - Dateigröße: 7.73 KiB


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      // Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML.
031      // 15 === JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT
032      protected $encodingOptions = 15;
033   
034      /**
035       * Constructor.
036       *
037       * @param mixed $data    The response data
038       * @param int   $status  The response status code
039       * @param array $headers An array of response headers
040       */
041      public function __construct($data = null, $status = 200, $headers = array())
042      {
043          parent::__construct('', $status, $headers);
044   
045          if (null === $data) {
046              $data = new \ArrayObject();
047          }
048   
049          $this->setData($data);
050      }
051   
052      /**
053       * Factory method for chainability.
054       *
055       * Example:
056       *
057       *     return JsonResponse::create($data, 200)
058       *         ->setSharedMaxAge(300);
059       *
060       * @param mixed $data    The json response data
061       * @param int   $status  The response status code
062       * @param array $headers An array of response headers
063       *
064       * @return JsonResponse
065       */
066      public static function create($data = null, $status = 200, $headers = array())
067      {
068          return new static($data, $status, $headers);
069      }
070   
071      /**
072       * Sets the JSONP callback.
073       *
074       * @param string|null $callback The JSONP callback or null to use none
075       *
076       * @return JsonResponse
077       *
078       * @throws \InvalidArgumentException When the callback name is not valid
079       */
080      public function setCallback($callback = null)
081      {
082          if (null !== $callback) {
083              // partially token from http://www.geekality.net/2011/08/03/valid-javascript-identifier/
084              // partially token from https://github.com/willdurand/JsonpCallbackValidator
085              //      JsonpCallbackValidator is released under the MIT License. See https://github.com/willdurand/JsonpCallbackValidator/blob/v1.1.0/LICENSE for details.
086              //      (c) William Durand <william.durand1@gmail.com>
087              $pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*(?:\[(?:"(?:\\\.|[^"\\\])*"|\'(?:\\\.|[^\'\\\])*\'|\d+)\])*?$/u';
088              $reserved = array(
089                  'break', 'do', 'instanceof', 'typeof', 'case', 'else', 'new', 'var', 'catch', 'finally', 'return', 'void', 'continue', 'for', 'switch', 'while',
090                  'debugger', 'function', 'this', 'with', 'default', 'if', 'throw', 'delete', 'in', 'try', 'class', 'enum', 'extends', 'super',  'const', 'export',
091                  'import', 'implements', 'let', 'private', 'public', 'yield', 'interface', 'package', 'protected', 'static', 'null', 'true', 'false',
092              );
093              $parts = explode('.', $callback);
094              foreach ($parts as $part) {
095                  if (!preg_match($pattern, $part) || in_array($part, $reserved, true)) {
096                      throw new \InvalidArgumentException('The callback name is not valid.');
097                  }
098              }
099          }
100   
101          $this->callback = $callback;
102   
103          return $this->update();
104      }
105   
106      /**
107       * Sets the data to be sent as JSON.
108       *
109       * @param mixed $data
110       *
111       * @return JsonResponse
112       *
113       * @throws \InvalidArgumentException
114       */
115      public function setData($data = array())
116      {
117          if (defined('HHVM_VERSION')) {
118              // HHVM does not trigger any warnings and let exceptions
119              // thrown from a JsonSerializable object pass through.
120              // If only PHP did the same...
121              $data = json_encode($data, $this->encodingOptions);
122          } else {
123              try {
124                  if (PHP_VERSION_ID < 50400) {
125                      // PHP 5.3 triggers annoying warnings for some
126                      // types that can't be serialized as JSON (INF, resources, etc.)
127                      // but doesn't provide the JsonSerializable interface.
128                      set_error_handler(function () { return false; });
129                      $data = @json_encode($data, $this->encodingOptions);
130                  } else {
131                      // PHP 5.4 and up wrap exceptions thrown by JsonSerializable
132                      // objects in a new exception that needs to be removed.
133                      // Fortunately, PHP 5.5 and up do not trigger any warning anymore.
134                      if (PHP_VERSION_ID < 50500) {
135                          // Clear json_last_error()
136                          json_encode(null);
137                          $errorHandler = set_error_handler('var_dump');
138                          restore_error_handler();
139                          set_error_handler(function () use ($errorHandler) {
140                              if (JSON_ERROR_NONE === json_last_error()) {
141                                  return $errorHandler && false !== call_user_func_array($errorHandler, func_get_args());
142                              }
143                          });
144                      }
145   
146                      $data = json_encode($data, $this->encodingOptions);
147                  }
148   
149                  if (PHP_VERSION_ID < 50500) {
150                      restore_error_handler();
151                  }
152              } catch (\Exception $e) {
153                  if (PHP_VERSION_ID < 50500) {
154                      restore_error_handler();
155                  }
156                  if (PHP_VERSION_ID >= 50400 && 'Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) {
157                      throw $e->getPrevious() ?: $e;
158                  }
159                  throw $e;
160              }
161          }
162   
163          if (JSON_ERROR_NONE !== json_last_error()) {
164              throw new \InvalidArgumentException(json_last_error_msg());
165          }
166   
167          $this->data = $data;
168   
169          return $this->update();
170      }
171   
172      /**
173       * Returns options used while encoding data to JSON.
174       *
175       * @return int
176       */
177      public function getEncodingOptions()
178      {
179          return $this->encodingOptions;
180      }
181   
182      /**
183       * Sets options used while encoding data to JSON.
184       *
185       * @param int $encodingOptions
186       *
187       * @return JsonResponse
188       */
189      public function setEncodingOptions($encodingOptions)
190      {
191          $this->encodingOptions = (int) $encodingOptions;
192   
193          return $this->setData(json_decode($this->data));
194      }
195   
196      /**
197       * Updates the content and headers according to the JSON data and callback.
198       *
199       * @return JsonResponse
200       */
201      protected function update()
202      {
203          if (null !== $this->callback) {
204              // Not using application/javascript for compatibility reasons with older browsers.
205              $this->headers->set('Content-Type', 'text/javascript');
206   
207              return $this->setContent(sprintf('/**/%s(%s);', $this->callback, $this->data));
208          }
209   
210          // Only set the header when there is none or when it equals 'text/javascript' (from a previous update with callback)
211          // in order to not overwrite a custom definition.
212          if (!$this->headers->has('Content-Type') || 'text/javascript' === $this->headers->get('Content-Type')) {
213              $this->headers->set('Content-Type', 'application/json');
214          }
215   
216          return $this->setContent($this->data);
217      }
218  }
219