JsonResponse.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. /**
  12. * Response represents an HTTP response in JSON format.
  13. *
  14. * Note that this class does not force the returned JSON content to be an
  15. * object. It is however recommended that you do return an object as it
  16. * protects yourself against XSSI and JSON-JavaScript Hijacking.
  17. *
  18. * @see https://www.owasp.org/index.php/OWASP_AJAX_Security_Guidelines#Always_return_JSON_with_an_Object_on_the_outside
  19. *
  20. * @author Igor Wiedler <igor@wiedler.ch>
  21. */
  22. class JsonResponse extends Response
  23. {
  24. protected $data;
  25. protected $callback;
  26. /**
  27. * Constructor.
  28. *
  29. * @param mixed $data The response data
  30. * @param integer $status The response status code
  31. * @param array $headers An array of response headers
  32. */
  33. public function __construct($data = null, $status = 200, $headers = array())
  34. {
  35. parent::__construct('', $status, $headers);
  36. if (null === $data) {
  37. $data = new \ArrayObject();
  38. }
  39. $this->setData($data);
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public static function create($data = null, $status = 200, $headers = array())
  45. {
  46. return new static($data, $status, $headers);
  47. }
  48. /**
  49. * Sets the JSONP callback.
  50. *
  51. * @param string $callback
  52. *
  53. * @return JsonResponse
  54. *
  55. * @throws \InvalidArgumentException
  56. */
  57. public function setCallback($callback = null)
  58. {
  59. if (null !== $callback) {
  60. // taken from http://www.geekality.net/2011/08/03/valid-javascript-identifier/
  61. $pattern = '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u';
  62. $parts = explode('.', $callback);
  63. foreach ($parts as $part) {
  64. if (!preg_match($pattern, $part)) {
  65. throw new \InvalidArgumentException('The callback name is not valid.');
  66. }
  67. }
  68. }
  69. $this->callback = $callback;
  70. return $this->update();
  71. }
  72. /**
  73. * Sets the data to be sent as json.
  74. *
  75. * @param mixed $data
  76. *
  77. * @return JsonResponse
  78. */
  79. public function setData($data = array())
  80. {
  81. // Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML.
  82. $this->data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
  83. return $this->update();
  84. }
  85. /**
  86. * Updates the content and headers according to the json data and callback.
  87. *
  88. * @return JsonResponse
  89. */
  90. protected function update()
  91. {
  92. if (null !== $this->callback) {
  93. // Not using application/javascript for compatibility reasons with older browsers.
  94. $this->headers->set('Content-Type', 'text/javascript');
  95. return $this->setContent(sprintf('%s(%s);', $this->callback, $this->data));
  96. }
  97. // Only set the header when there is none or when it equals 'text/javascript' (from a previous update with callback)
  98. // in order to not overwrite a custom definition.
  99. if (!$this->headers->has('Content-Type') || 'text/javascript' === $this->headers->get('Content-Type')) {
  100. $this->headers->set('Content-Type', 'application/json');
  101. }
  102. return $this->setContent($this->data);
  103. }
  104. }