Drupal investigation

HttpFoundationFactory.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\Bridge\PsrHttpMessage\Factory;
  11. use Psr\Http\Message\ServerRequestInterface;
  12. use Psr\Http\Message\ResponseInterface;
  13. use Psr\Http\Message\UploadedFileInterface;
  14. use Psr\Http\Message\UriInterface;
  15. use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
  16. use Symfony\Component\HttpFoundation\Cookie;
  17. use Symfony\Component\HttpFoundation\File\UploadedFile;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. /**
  21. * {@inheritdoc}
  22. *
  23. * @author Kévin Dunglas <dunglas@gmail.com>
  24. */
  25. class HttpFoundationFactory implements HttpFoundationFactoryInterface
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function createRequest(ServerRequestInterface $psrRequest)
  31. {
  32. $server = array();
  33. $uri = $psrRequest->getUri();
  34. if ($uri instanceof UriInterface) {
  35. $server['SERVER_NAME'] = $uri->getHost();
  36. $server['SERVER_PORT'] = $uri->getPort();
  37. $server['REQUEST_URI'] = $uri->getPath();
  38. $server['QUERY_STRING'] = $uri->getQuery();
  39. }
  40. $server['REQUEST_METHOD'] = $psrRequest->getMethod();
  41. $server = array_replace($server, $psrRequest->getServerParams());
  42. $parsedBody = $psrRequest->getParsedBody();
  43. $parsedBody = is_array($parsedBody) ? $parsedBody : array();
  44. $request = new Request(
  45. $psrRequest->getQueryParams(),
  46. $parsedBody,
  47. $psrRequest->getAttributes(),
  48. $psrRequest->getCookieParams(),
  49. $this->getFiles($psrRequest->getUploadedFiles()),
  50. $server,
  51. $psrRequest->getBody()->__toString()
  52. );
  53. $request->headers->replace($psrRequest->getHeaders());
  54. return $request;
  55. }
  56. /**
  57. * Converts to the input array to $_FILES structure.
  58. *
  59. * @param array $uploadedFiles
  60. *
  61. * @return array
  62. */
  63. private function getFiles(array $uploadedFiles)
  64. {
  65. $files = array();
  66. foreach ($uploadedFiles as $key => $value) {
  67. if ($value instanceof UploadedFileInterface) {
  68. $files[$key] = $this->createUploadedFile($value);
  69. } else {
  70. $files[$key] = $this->getFiles($value);
  71. }
  72. }
  73. return $files;
  74. }
  75. /**
  76. * Creates Symfony UploadedFile instance from PSR-7 ones.
  77. *
  78. * @param UploadedFileInterface $psrUploadedFile
  79. *
  80. * @return UploadedFile
  81. */
  82. private function createUploadedFile(UploadedFileInterface $psrUploadedFile)
  83. {
  84. $temporaryPath = '';
  85. $clientFileName = '';
  86. if (UPLOAD_ERR_NO_FILE !== $psrUploadedFile->getError()) {
  87. $temporaryPath = $this->getTemporaryPath();
  88. $psrUploadedFile->moveTo($temporaryPath);
  89. $clientFileName = $psrUploadedFile->getClientFilename();
  90. }
  91. return new UploadedFile(
  92. $temporaryPath,
  93. null === $clientFileName ? '' : $clientFileName,
  94. $psrUploadedFile->getClientMediaType(),
  95. $psrUploadedFile->getSize(),
  96. $psrUploadedFile->getError(),
  97. true
  98. );
  99. }
  100. /**
  101. * Gets a temporary file path.
  102. *
  103. * @return string
  104. */
  105. protected function getTemporaryPath()
  106. {
  107. return tempnam(sys_get_temp_dir(), uniqid('symfony', true));
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function createResponse(ResponseInterface $psrResponse)
  113. {
  114. $response = new Response(
  115. $psrResponse->getBody()->__toString(),
  116. $psrResponse->getStatusCode(),
  117. $psrResponse->getHeaders()
  118. );
  119. $response->setProtocolVersion($psrResponse->getProtocolVersion());
  120. foreach ($psrResponse->getHeader('Set-Cookie') as $cookie) {
  121. $response->headers->setCookie($this->createCookie($cookie));
  122. }
  123. return $response;
  124. }
  125. /**
  126. * Creates a Cookie instance from a cookie string.
  127. *
  128. * Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34
  129. *
  130. * @param string $cookie
  131. *
  132. * @return Cookie
  133. *
  134. * @throws \InvalidArgumentException
  135. */
  136. private function createCookie($cookie)
  137. {
  138. foreach (explode(';', $cookie) as $part) {
  139. $part = trim($part);
  140. $data = explode('=', $part, 2);
  141. $name = $data[0];
  142. $value = isset($data[1]) ? trim($data[1], " \n\r\t\0\x0B\"") : null;
  143. if (!isset($cookieName)) {
  144. $cookieName = $name;
  145. $cookieValue = $value;
  146. continue;
  147. }
  148. if ('expires' === strtolower($name) && null !== $value) {
  149. $cookieExpire = new \DateTime($value);
  150. continue;
  151. }
  152. if ('path' === strtolower($name) && null !== $value) {
  153. $cookiePath = $value;
  154. continue;
  155. }
  156. if ('domain' === strtolower($name) && null !== $value) {
  157. $cookieDomain = $value;
  158. continue;
  159. }
  160. if ('secure' === strtolower($name)) {
  161. $cookieSecure = true;
  162. continue;
  163. }
  164. if ('httponly' === strtolower($name)) {
  165. $cookieHttpOnly = true;
  166. continue;
  167. }
  168. }
  169. if (!isset($cookieName)) {
  170. throw new \InvalidArgumentException('The value of the Set-Cookie header is malformed.');
  171. }
  172. return new Cookie(
  173. $cookieName,
  174. $cookieValue,
  175. isset($cookieExpire) ? $cookieExpire : 0,
  176. isset($cookiePath) ? $cookiePath : '/',
  177. isset($cookieDomain) ? $cookieDomain : null,
  178. isset($cookieSecure),
  179. isset($cookieHttpOnly)
  180. );
  181. }
  182. }