Drupal investigation

Client.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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\BrowserKit;
  11. use Symfony\Component\DomCrawler\Crawler;
  12. use Symfony\Component\DomCrawler\Link;
  13. use Symfony\Component\DomCrawler\Form;
  14. use Symfony\Component\Process\PhpProcess;
  15. /**
  16. * Client simulates a browser.
  17. *
  18. * To make the actual request, you need to implement the doRequest() method.
  19. *
  20. * If you want to be able to run requests in their own process (insulated flag),
  21. * you need to also implement the getScript() method.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. */
  25. abstract class Client
  26. {
  27. protected $history;
  28. protected $cookieJar;
  29. protected $server = array();
  30. protected $internalRequest;
  31. protected $request;
  32. protected $internalResponse;
  33. protected $response;
  34. protected $crawler;
  35. protected $insulated = false;
  36. protected $redirect;
  37. protected $followRedirects = true;
  38. private $maxRedirects = -1;
  39. private $redirectCount = 0;
  40. private $isMainRequest = true;
  41. /**
  42. * Constructor.
  43. *
  44. * @param array $server The server parameters (equivalent of $_SERVER)
  45. * @param History $history A History instance to store the browser history
  46. * @param CookieJar $cookieJar A CookieJar instance to store the cookies
  47. */
  48. public function __construct(array $server = array(), History $history = null, CookieJar $cookieJar = null)
  49. {
  50. $this->setServerParameters($server);
  51. $this->history = $history ?: new History();
  52. $this->cookieJar = $cookieJar ?: new CookieJar();
  53. }
  54. /**
  55. * Sets whether to automatically follow redirects or not.
  56. *
  57. * @param bool $followRedirect Whether to follow redirects
  58. */
  59. public function followRedirects($followRedirect = true)
  60. {
  61. $this->followRedirects = (bool) $followRedirect;
  62. }
  63. /**
  64. * Returns whether client automatically follows redirects or not.
  65. *
  66. * @return bool
  67. */
  68. public function isFollowingRedirects()
  69. {
  70. return $this->followRedirects;
  71. }
  72. /**
  73. * Sets the maximum number of requests that crawler can follow.
  74. *
  75. * @param int $maxRedirects
  76. */
  77. public function setMaxRedirects($maxRedirects)
  78. {
  79. $this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects;
  80. $this->followRedirects = -1 != $this->maxRedirects;
  81. }
  82. /**
  83. * Returns the maximum number of requests that crawler can follow.
  84. *
  85. * @return int
  86. */
  87. public function getMaxRedirects()
  88. {
  89. return $this->maxRedirects;
  90. }
  91. /**
  92. * Sets the insulated flag.
  93. *
  94. * @param bool $insulated Whether to insulate the requests or not
  95. *
  96. * @throws \RuntimeException When Symfony Process Component is not installed
  97. */
  98. public function insulate($insulated = true)
  99. {
  100. if ($insulated && !class_exists('Symfony\\Component\\Process\\Process')) {
  101. throw new \RuntimeException('Unable to isolate requests as the Symfony Process Component is not installed.');
  102. }
  103. $this->insulated = (bool) $insulated;
  104. }
  105. /**
  106. * Sets server parameters.
  107. *
  108. * @param array $server An array of server parameters
  109. */
  110. public function setServerParameters(array $server)
  111. {
  112. $this->server = array_merge(array(
  113. 'HTTP_USER_AGENT' => 'Symfony2 BrowserKit',
  114. ), $server);
  115. }
  116. /**
  117. * Sets single server parameter.
  118. *
  119. * @param string $key A key of the parameter
  120. * @param string $value A value of the parameter
  121. */
  122. public function setServerParameter($key, $value)
  123. {
  124. $this->server[$key] = $value;
  125. }
  126. /**
  127. * Gets single server parameter for specified key.
  128. *
  129. * @param string $key A key of the parameter to get
  130. * @param string $default A default value when key is undefined
  131. *
  132. * @return string A value of the parameter
  133. */
  134. public function getServerParameter($key, $default = '')
  135. {
  136. return isset($this->server[$key]) ? $this->server[$key] : $default;
  137. }
  138. /**
  139. * Returns the History instance.
  140. *
  141. * @return History A History instance
  142. */
  143. public function getHistory()
  144. {
  145. return $this->history;
  146. }
  147. /**
  148. * Returns the CookieJar instance.
  149. *
  150. * @return CookieJar A CookieJar instance
  151. */
  152. public function getCookieJar()
  153. {
  154. return $this->cookieJar;
  155. }
  156. /**
  157. * Returns the current Crawler instance.
  158. *
  159. * @return Crawler|null A Crawler instance
  160. */
  161. public function getCrawler()
  162. {
  163. return $this->crawler;
  164. }
  165. /**
  166. * Returns the current BrowserKit Response instance.
  167. *
  168. * @return Response|null A BrowserKit Response instance
  169. */
  170. public function getInternalResponse()
  171. {
  172. return $this->internalResponse;
  173. }
  174. /**
  175. * Returns the current origin response instance.
  176. *
  177. * The origin response is the response instance that is returned
  178. * by the code that handles requests.
  179. *
  180. * @return object|null A response instance
  181. *
  182. * @see doRequest()
  183. */
  184. public function getResponse()
  185. {
  186. return $this->response;
  187. }
  188. /**
  189. * Returns the current BrowserKit Request instance.
  190. *
  191. * @return Request|null A BrowserKit Request instance
  192. */
  193. public function getInternalRequest()
  194. {
  195. return $this->internalRequest;
  196. }
  197. /**
  198. * Returns the current origin Request instance.
  199. *
  200. * The origin request is the request instance that is sent
  201. * to the code that handles requests.
  202. *
  203. * @return object|null A Request instance
  204. *
  205. * @see doRequest()
  206. */
  207. public function getRequest()
  208. {
  209. return $this->request;
  210. }
  211. /**
  212. * Clicks on a given link.
  213. *
  214. * @param Link $link A Link instance
  215. *
  216. * @return Crawler
  217. */
  218. public function click(Link $link)
  219. {
  220. if ($link instanceof Form) {
  221. return $this->submit($link);
  222. }
  223. return $this->request($link->getMethod(), $link->getUri());
  224. }
  225. /**
  226. * Submits a form.
  227. *
  228. * @param Form $form A Form instance
  229. * @param array $values An array of form field values
  230. *
  231. * @return Crawler
  232. */
  233. public function submit(Form $form, array $values = array())
  234. {
  235. $form->setValues($values);
  236. return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles());
  237. }
  238. /**
  239. * Calls a URI.
  240. *
  241. * @param string $method The request method
  242. * @param string $uri The URI to fetch
  243. * @param array $parameters The Request parameters
  244. * @param array $files The files
  245. * @param array $server The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does)
  246. * @param string $content The raw body data
  247. * @param bool $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())
  248. *
  249. * @return Crawler
  250. */
  251. public function request($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true)
  252. {
  253. if ($this->isMainRequest) {
  254. $this->redirectCount = 0;
  255. } else {
  256. ++$this->redirectCount;
  257. }
  258. $uri = $this->getAbsoluteUri($uri);
  259. $server = array_merge($this->server, $server);
  260. if (isset($server['HTTPS'])) {
  261. $uri = preg_replace('{^'.parse_url($uri, PHP_URL_SCHEME).'}', $server['HTTPS'] ? 'https' : 'http', $uri);
  262. }
  263. if (!$this->history->isEmpty()) {
  264. $server['HTTP_REFERER'] = $this->history->current()->getUri();
  265. }
  266. if (empty($server['HTTP_HOST'])) {
  267. $server['HTTP_HOST'] = $this->extractHost($uri);
  268. }
  269. $server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME);
  270. $this->internalRequest = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content);
  271. $this->request = $this->filterRequest($this->internalRequest);
  272. if (true === $changeHistory) {
  273. $this->history->add($this->internalRequest);
  274. }
  275. if ($this->insulated) {
  276. $this->response = $this->doRequestInProcess($this->request);
  277. } else {
  278. $this->response = $this->doRequest($this->request);
  279. }
  280. $this->internalResponse = $this->filterResponse($this->response);
  281. $this->cookieJar->updateFromResponse($this->internalResponse, $uri);
  282. $status = $this->internalResponse->getStatus();
  283. if ($status >= 300 && $status < 400) {
  284. $this->redirect = $this->internalResponse->getHeader('Location');
  285. } else {
  286. $this->redirect = null;
  287. }
  288. if ($this->followRedirects && $this->redirect) {
  289. return $this->crawler = $this->followRedirect();
  290. }
  291. return $this->crawler = $this->createCrawlerFromContent($this->internalRequest->getUri(), $this->internalResponse->getContent(), $this->internalResponse->getHeader('Content-Type'));
  292. }
  293. /**
  294. * Makes a request in another process.
  295. *
  296. * @param object $request An origin request instance
  297. *
  298. * @return object An origin response instance
  299. *
  300. * @throws \RuntimeException When processing returns exit code
  301. */
  302. protected function doRequestInProcess($request)
  303. {
  304. $process = new PhpProcess($this->getScript($request), null, null);
  305. $process->run();
  306. if (!$process->isSuccessful() || !preg_match('/^O\:\d+\:/', $process->getOutput())) {
  307. throw new \RuntimeException(sprintf('OUTPUT: %s ERROR OUTPUT: %s', $process->getOutput(), $process->getErrorOutput()));
  308. }
  309. return unserialize($process->getOutput());
  310. }
  311. /**
  312. * Makes a request.
  313. *
  314. * @param object $request An origin request instance
  315. *
  316. * @return object An origin response instance
  317. */
  318. abstract protected function doRequest($request);
  319. /**
  320. * Returns the script to execute when the request must be insulated.
  321. *
  322. * @param object $request An origin request instance
  323. *
  324. * @throws \LogicException When this abstract class is not implemented
  325. */
  326. protected function getScript($request)
  327. {
  328. throw new \LogicException('To insulate requests, you need to override the getScript() method.');
  329. }
  330. /**
  331. * Filters the BrowserKit request to the origin one.
  332. *
  333. * @param Request $request The BrowserKit Request to filter
  334. *
  335. * @return object An origin request instance
  336. */
  337. protected function filterRequest(Request $request)
  338. {
  339. return $request;
  340. }
  341. /**
  342. * Filters the origin response to the BrowserKit one.
  343. *
  344. * @param object $response The origin response to filter
  345. *
  346. * @return Response An BrowserKit Response instance
  347. */
  348. protected function filterResponse($response)
  349. {
  350. return $response;
  351. }
  352. /**
  353. * Creates a crawler.
  354. *
  355. * This method returns null if the DomCrawler component is not available.
  356. *
  357. * @param string $uri A URI
  358. * @param string $content Content for the crawler to use
  359. * @param string $type Content type
  360. *
  361. * @return Crawler|null
  362. */
  363. protected function createCrawlerFromContent($uri, $content, $type)
  364. {
  365. if (!class_exists('Symfony\Component\DomCrawler\Crawler')) {
  366. return;
  367. }
  368. $crawler = new Crawler(null, $uri);
  369. $crawler->addContent($content, $type);
  370. return $crawler;
  371. }
  372. /**
  373. * Goes back in the browser history.
  374. *
  375. * @return Crawler
  376. */
  377. public function back()
  378. {
  379. return $this->requestFromRequest($this->history->back(), false);
  380. }
  381. /**
  382. * Goes forward in the browser history.
  383. *
  384. * @return Crawler
  385. */
  386. public function forward()
  387. {
  388. return $this->requestFromRequest($this->history->forward(), false);
  389. }
  390. /**
  391. * Reloads the current browser.
  392. *
  393. * @return Crawler
  394. */
  395. public function reload()
  396. {
  397. return $this->requestFromRequest($this->history->current(), false);
  398. }
  399. /**
  400. * Follow redirects?
  401. *
  402. * @return Crawler
  403. *
  404. * @throws \LogicException If request was not a redirect
  405. */
  406. public function followRedirect()
  407. {
  408. if (empty($this->redirect)) {
  409. throw new \LogicException('The request was not redirected.');
  410. }
  411. if (-1 !== $this->maxRedirects) {
  412. if ($this->redirectCount > $this->maxRedirects) {
  413. throw new \LogicException(sprintf('The maximum number (%d) of redirections was reached.', $this->maxRedirects));
  414. }
  415. }
  416. $request = $this->internalRequest;
  417. if (in_array($this->internalResponse->getStatus(), array(302, 303))) {
  418. $method = 'GET';
  419. $files = array();
  420. $content = null;
  421. } else {
  422. $method = $request->getMethod();
  423. $files = $request->getFiles();
  424. $content = $request->getContent();
  425. }
  426. if ('GET' === strtoupper($method)) {
  427. // Don't forward parameters for GET request as it should reach the redirection URI
  428. $parameters = array();
  429. } else {
  430. $parameters = $request->getParameters();
  431. }
  432. $server = $request->getServer();
  433. $server = $this->updateServerFromUri($server, $this->redirect);
  434. $this->isMainRequest = false;
  435. $response = $this->request($method, $this->redirect, $parameters, $files, $server, $content);
  436. $this->isMainRequest = true;
  437. return $response;
  438. }
  439. /**
  440. * Restarts the client.
  441. *
  442. * It flushes history and all cookies.
  443. */
  444. public function restart()
  445. {
  446. $this->cookieJar->clear();
  447. $this->history->clear();
  448. }
  449. /**
  450. * Takes a URI and converts it to absolute if it is not already absolute.
  451. *
  452. * @param string $uri A URI
  453. *
  454. * @return string An absolute URI
  455. */
  456. protected function getAbsoluteUri($uri)
  457. {
  458. // already absolute?
  459. if (0 === strpos($uri, 'http://') || 0 === strpos($uri, 'https://')) {
  460. return $uri;
  461. }
  462. if (!$this->history->isEmpty()) {
  463. $currentUri = $this->history->current()->getUri();
  464. } else {
  465. $currentUri = sprintf('http%s://%s/',
  466. isset($this->server['HTTPS']) ? 's' : '',
  467. isset($this->server['HTTP_HOST']) ? $this->server['HTTP_HOST'] : 'localhost'
  468. );
  469. }
  470. // protocol relative URL
  471. if (0 === strpos($uri, '//')) {
  472. return parse_url($currentUri, PHP_URL_SCHEME).':'.$uri;
  473. }
  474. // anchor or query string parameters?
  475. if (!$uri || '#' == $uri[0] || '?' == $uri[0]) {
  476. return preg_replace('/[#?].*?$/', '', $currentUri).$uri;
  477. }
  478. if ('/' !== $uri[0]) {
  479. $path = parse_url($currentUri, PHP_URL_PATH);
  480. if ('/' !== substr($path, -1)) {
  481. $path = substr($path, 0, strrpos($path, '/') + 1);
  482. }
  483. $uri = $path.$uri;
  484. }
  485. return preg_replace('#^(.*?//[^/]+)\/.*$#', '$1', $currentUri).$uri;
  486. }
  487. /**
  488. * Makes a request from a Request object directly.
  489. *
  490. * @param Request $request A Request instance
  491. * @param bool $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())
  492. *
  493. * @return Crawler
  494. */
  495. protected function requestFromRequest(Request $request, $changeHistory = true)
  496. {
  497. return $this->request($request->getMethod(), $request->getUri(), $request->getParameters(), $request->getFiles(), $request->getServer(), $request->getContent(), $changeHistory);
  498. }
  499. private function updateServerFromUri($server, $uri)
  500. {
  501. $server['HTTP_HOST'] = $this->extractHost($uri);
  502. $scheme = parse_url($uri, PHP_URL_SCHEME);
  503. $server['HTTPS'] = null === $scheme ? $server['HTTPS'] : 'https' == $scheme;
  504. unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']);
  505. return $server;
  506. }
  507. private function extractHost($uri)
  508. {
  509. $host = parse_url($uri, PHP_URL_HOST);
  510. if ($port = parse_url($uri, PHP_URL_PORT)) {
  511. return $host.':'.$port;
  512. }
  513. return $host;
  514. }
  515. }