Drupal investigation

Client.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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\HttpKernel;
  11. use Symfony\Component\BrowserKit\Client as BaseClient;
  12. use Symfony\Component\BrowserKit\Request as DomRequest;
  13. use Symfony\Component\BrowserKit\Response as DomResponse;
  14. use Symfony\Component\BrowserKit\Cookie as DomCookie;
  15. use Symfony\Component\BrowserKit\History;
  16. use Symfony\Component\BrowserKit\CookieJar;
  17. use Symfony\Component\HttpFoundation\File\UploadedFile;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. /**
  21. * Client simulates a browser and makes requests to a Kernel object.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. *
  25. * @method Request|null getRequest() A Request instance
  26. * @method Response|null getResponse() A Response instance
  27. */
  28. class Client extends BaseClient
  29. {
  30. protected $kernel;
  31. /**
  32. * Constructor.
  33. *
  34. * @param HttpKernelInterface $kernel An HttpKernel instance
  35. * @param array $server The server parameters (equivalent of $_SERVER)
  36. * @param History $history A History instance to store the browser history
  37. * @param CookieJar $cookieJar A CookieJar instance to store the cookies
  38. */
  39. public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
  40. {
  41. // These class properties must be set before calling the parent constructor, as it may depend on it.
  42. $this->kernel = $kernel;
  43. $this->followRedirects = false;
  44. parent::__construct($server, $history, $cookieJar);
  45. }
  46. /**
  47. * Makes a request.
  48. *
  49. * @param Request $request A Request instance
  50. *
  51. * @return Response A Response instance
  52. */
  53. protected function doRequest($request)
  54. {
  55. $response = $this->kernel->handle($request);
  56. if ($this->kernel instanceof TerminableInterface) {
  57. $this->kernel->terminate($request, $response);
  58. }
  59. return $response;
  60. }
  61. /**
  62. * Returns the script to execute when the request must be insulated.
  63. *
  64. * @param Request $request A Request instance
  65. *
  66. * @return string
  67. */
  68. protected function getScript($request)
  69. {
  70. $kernel = str_replace("'", "\\'", serialize($this->kernel));
  71. $request = str_replace("'", "\\'", serialize($request));
  72. $r = new \ReflectionClass('\\Symfony\\Component\\ClassLoader\\ClassLoader');
  73. $requirePath = str_replace("'", "\\'", $r->getFileName());
  74. $symfonyPath = str_replace("'", "\\'", dirname(dirname(dirname(__DIR__))));
  75. $errorReporting = error_reporting();
  76. $code = <<<EOF
  77. <?php
  78. error_reporting($errorReporting);
  79. require_once '$requirePath';
  80. \$loader = new Symfony\Component\ClassLoader\ClassLoader();
  81. \$loader->addPrefix('Symfony', '$symfonyPath');
  82. \$loader->register();
  83. \$kernel = unserialize('$kernel');
  84. \$request = unserialize('$request');
  85. EOF;
  86. return $code.$this->getHandleScript();
  87. }
  88. protected function getHandleScript()
  89. {
  90. return <<<'EOF'
  91. $response = $kernel->handle($request);
  92. if ($kernel instanceof Symfony\Component\HttpKernel\TerminableInterface) {
  93. $kernel->terminate($request, $response);
  94. }
  95. echo serialize($response);
  96. EOF;
  97. }
  98. /**
  99. * Converts the BrowserKit request to a HttpKernel request.
  100. *
  101. * @param DomRequest $request A DomRequest instance
  102. *
  103. * @return Request A Request instance
  104. */
  105. protected function filterRequest(DomRequest $request)
  106. {
  107. $httpRequest = Request::create($request->getUri(), $request->getMethod(), $request->getParameters(), $request->getCookies(), $request->getFiles(), $request->getServer(), $request->getContent());
  108. foreach ($this->filterFiles($httpRequest->files->all()) as $key => $value) {
  109. $httpRequest->files->set($key, $value);
  110. }
  111. return $httpRequest;
  112. }
  113. /**
  114. * Filters an array of files.
  115. *
  116. * This method created test instances of UploadedFile so that the move()
  117. * method can be called on those instances.
  118. *
  119. * If the size of a file is greater than the allowed size (from php.ini) then
  120. * an invalid UploadedFile is returned with an error set to UPLOAD_ERR_INI_SIZE.
  121. *
  122. * @see UploadedFile
  123. *
  124. * @param array $files An array of files
  125. *
  126. * @return array An array with all uploaded files marked as already moved
  127. */
  128. protected function filterFiles(array $files)
  129. {
  130. $filtered = array();
  131. foreach ($files as $key => $value) {
  132. if (is_array($value)) {
  133. $filtered[$key] = $this->filterFiles($value);
  134. } elseif ($value instanceof UploadedFile) {
  135. if ($value->isValid() && $value->getSize() > UploadedFile::getMaxFilesize()) {
  136. $filtered[$key] = new UploadedFile(
  137. '',
  138. $value->getClientOriginalName(),
  139. $value->getClientMimeType(),
  140. 0,
  141. UPLOAD_ERR_INI_SIZE,
  142. true
  143. );
  144. } else {
  145. $filtered[$key] = new UploadedFile(
  146. $value->getPathname(),
  147. $value->getClientOriginalName(),
  148. $value->getClientMimeType(),
  149. $value->getClientSize(),
  150. $value->getError(),
  151. true
  152. );
  153. }
  154. }
  155. }
  156. return $filtered;
  157. }
  158. /**
  159. * Converts the HttpKernel response to a BrowserKit response.
  160. *
  161. * @param Response $response A Response instance
  162. *
  163. * @return DomResponse A DomResponse instance
  164. */
  165. protected function filterResponse($response)
  166. {
  167. $headers = $response->headers->all();
  168. if ($response->headers->getCookies()) {
  169. $cookies = array();
  170. foreach ($response->headers->getCookies() as $cookie) {
  171. $cookies[] = new DomCookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly());
  172. }
  173. $headers['Set-Cookie'] = $cookies;
  174. }
  175. // this is needed to support StreamedResponse
  176. ob_start();
  177. $response->sendContent();
  178. $content = ob_get_clean();
  179. return new DomResponse($content, $response->getStatusCode(), $headers);
  180. }
  181. }