Dashboard sipadu mbip
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ResponseHeaderBag.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. * ResponseHeaderBag is a container for Response HTTP headers.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class ResponseHeaderBag extends HeaderBag
  17. {
  18. const COOKIES_FLAT = 'flat';
  19. const COOKIES_ARRAY = 'array';
  20. const DISPOSITION_ATTACHMENT = 'attachment';
  21. const DISPOSITION_INLINE = 'inline';
  22. protected $computedCacheControl = [];
  23. protected $cookies = [];
  24. protected $headerNames = [];
  25. public function __construct(array $headers = [])
  26. {
  27. parent::__construct($headers);
  28. if (!isset($this->headers['cache-control'])) {
  29. $this->set('Cache-Control', '');
  30. }
  31. /* RFC2616 - 14.18 says all Responses need to have a Date */
  32. if (!isset($this->headers['date'])) {
  33. $this->initDate();
  34. }
  35. }
  36. /**
  37. * Returns the headers, with original capitalizations.
  38. *
  39. * @return array An array of headers
  40. */
  41. public function allPreserveCase()
  42. {
  43. $headers = [];
  44. foreach ($this->all() as $name => $value) {
  45. $headers[isset($this->headerNames[$name]) ? $this->headerNames[$name] : $name] = $value;
  46. }
  47. return $headers;
  48. }
  49. public function allPreserveCaseWithoutCookies()
  50. {
  51. $headers = $this->allPreserveCase();
  52. if (isset($this->headerNames['set-cookie'])) {
  53. unset($headers[$this->headerNames['set-cookie']]);
  54. }
  55. return $headers;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function replace(array $headers = [])
  61. {
  62. $this->headerNames = [];
  63. parent::replace($headers);
  64. if (!isset($this->headers['cache-control'])) {
  65. $this->set('Cache-Control', '');
  66. }
  67. if (!isset($this->headers['date'])) {
  68. $this->initDate();
  69. }
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function all()
  75. {
  76. $headers = parent::all();
  77. foreach ($this->getCookies() as $cookie) {
  78. $headers['set-cookie'][] = (string) $cookie;
  79. }
  80. return $headers;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function set($key, $values, $replace = true)
  86. {
  87. $uniqueKey = str_replace('_', '-', strtolower($key));
  88. if ('set-cookie' === $uniqueKey) {
  89. if ($replace) {
  90. $this->cookies = [];
  91. }
  92. foreach ((array) $values as $cookie) {
  93. $this->setCookie(Cookie::fromString($cookie));
  94. }
  95. $this->headerNames[$uniqueKey] = $key;
  96. return;
  97. }
  98. $this->headerNames[$uniqueKey] = $key;
  99. parent::set($key, $values, $replace);
  100. // ensure the cache-control header has sensible defaults
  101. if (\in_array($uniqueKey, ['cache-control', 'etag', 'last-modified', 'expires'], true) && '' !== $computed = $this->computeCacheControlValue()) {
  102. $this->headers['cache-control'] = [$computed];
  103. $this->headerNames['cache-control'] = 'Cache-Control';
  104. $this->computedCacheControl = $this->parseCacheControl($computed);
  105. }
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function remove($key)
  111. {
  112. $uniqueKey = str_replace('_', '-', strtolower($key));
  113. unset($this->headerNames[$uniqueKey]);
  114. if ('set-cookie' === $uniqueKey) {
  115. $this->cookies = [];
  116. return;
  117. }
  118. parent::remove($key);
  119. if ('cache-control' === $uniqueKey) {
  120. $this->computedCacheControl = [];
  121. }
  122. if ('date' === $uniqueKey) {
  123. $this->initDate();
  124. }
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function hasCacheControlDirective($key)
  130. {
  131. return \array_key_exists($key, $this->computedCacheControl);
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function getCacheControlDirective($key)
  137. {
  138. return \array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] : null;
  139. }
  140. public function setCookie(Cookie $cookie)
  141. {
  142. $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
  143. $this->headerNames['set-cookie'] = 'Set-Cookie';
  144. }
  145. /**
  146. * Removes a cookie from the array, but does not unset it in the browser.
  147. *
  148. * @param string $name
  149. * @param string $path
  150. * @param string $domain
  151. */
  152. public function removeCookie($name, $path = '/', $domain = null)
  153. {
  154. if (null === $path) {
  155. $path = '/';
  156. }
  157. unset($this->cookies[$domain][$path][$name]);
  158. if (empty($this->cookies[$domain][$path])) {
  159. unset($this->cookies[$domain][$path]);
  160. if (empty($this->cookies[$domain])) {
  161. unset($this->cookies[$domain]);
  162. }
  163. }
  164. if (empty($this->cookies)) {
  165. unset($this->headerNames['set-cookie']);
  166. }
  167. }
  168. /**
  169. * Returns an array with all cookies.
  170. *
  171. * @param string $format
  172. *
  173. * @return Cookie[]
  174. *
  175. * @throws \InvalidArgumentException When the $format is invalid
  176. */
  177. public function getCookies($format = self::COOKIES_FLAT)
  178. {
  179. if (!\in_array($format, [self::COOKIES_FLAT, self::COOKIES_ARRAY])) {
  180. throw new \InvalidArgumentException(sprintf('Format "%s" invalid (%s).', $format, implode(', ', [self::COOKIES_FLAT, self::COOKIES_ARRAY])));
  181. }
  182. if (self::COOKIES_ARRAY === $format) {
  183. return $this->cookies;
  184. }
  185. $flattenedCookies = [];
  186. foreach ($this->cookies as $path) {
  187. foreach ($path as $cookies) {
  188. foreach ($cookies as $cookie) {
  189. $flattenedCookies[] = $cookie;
  190. }
  191. }
  192. }
  193. return $flattenedCookies;
  194. }
  195. /**
  196. * Clears a cookie in the browser.
  197. *
  198. * @param string $name
  199. * @param string $path
  200. * @param string $domain
  201. * @param bool $secure
  202. * @param bool $httpOnly
  203. */
  204. public function clearCookie($name, $path = '/', $domain = null, $secure = false, $httpOnly = true)
  205. {
  206. $this->setCookie(new Cookie($name, null, 1, $path, $domain, $secure, $httpOnly, false, null));
  207. }
  208. /**
  209. * @see HeaderUtils::makeDisposition()
  210. */
  211. public function makeDisposition($disposition, $filename, $filenameFallback = '')
  212. {
  213. return HeaderUtils::makeDisposition((string) $disposition, (string) $filename, (string) $filenameFallback);
  214. }
  215. /**
  216. * Returns the calculated value of the cache-control header.
  217. *
  218. * This considers several other headers and calculates or modifies the
  219. * cache-control header to a sensible, conservative value.
  220. *
  221. * @return string
  222. */
  223. protected function computeCacheControlValue()
  224. {
  225. if (!$this->cacheControl && !$this->has('ETag') && !$this->has('Last-Modified') && !$this->has('Expires')) {
  226. return 'no-cache, private';
  227. }
  228. if (!$this->cacheControl) {
  229. // conservative by default
  230. return 'private, must-revalidate';
  231. }
  232. $header = $this->getCacheControlHeader();
  233. if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) {
  234. return $header;
  235. }
  236. // public if s-maxage is defined, private otherwise
  237. if (!isset($this->cacheControl['s-maxage'])) {
  238. return $header.', private';
  239. }
  240. return $header;
  241. }
  242. private function initDate()
  243. {
  244. $now = \DateTime::createFromFormat('U', time());
  245. $now->setTimezone(new \DateTimeZone('UTC'));
  246. $this->set('Date', $now->format('D, d M Y H:i:s').' GMT');
  247. }
  248. }