Drupal investigation

RequestDataCollector.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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\DataCollector;
  11. use Symfony\Component\HttpFoundation\ParameterBag;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. /**
  18. * RequestDataCollector.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class RequestDataCollector extends DataCollector implements EventSubscriberInterface
  23. {
  24. protected $controllers;
  25. public function __construct()
  26. {
  27. $this->controllers = new \SplObjectStorage();
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function collect(Request $request, Response $response, \Exception $exception = null)
  33. {
  34. $responseHeaders = $response->headers->all();
  35. foreach ($response->headers->getCookies() as $cookie) {
  36. $responseHeaders['set-cookie'][] = (string) $cookie;
  37. }
  38. // attributes are serialized and as they can be anything, they need to be converted to strings.
  39. $attributes = array();
  40. foreach ($request->attributes->all() as $key => $value) {
  41. if ('_route' === $key && is_object($value)) {
  42. $attributes[$key] = $this->varToString($value->getPath());
  43. } elseif ('_route_params' === $key) {
  44. // we need to keep route params as an array (see getRouteParams())
  45. foreach ($value as $k => $v) {
  46. $value[$k] = $this->varToString($v);
  47. }
  48. $attributes[$key] = $value;
  49. } else {
  50. $attributes[$key] = $this->varToString($value);
  51. }
  52. }
  53. $content = null;
  54. try {
  55. $content = $request->getContent();
  56. } catch (\LogicException $e) {
  57. // the user already got the request content as a resource
  58. $content = false;
  59. }
  60. $sessionMetadata = array();
  61. $sessionAttributes = array();
  62. $flashes = array();
  63. if ($request->hasSession()) {
  64. $session = $request->getSession();
  65. if ($session->isStarted()) {
  66. $sessionMetadata['Created'] = date(DATE_RFC822, $session->getMetadataBag()->getCreated());
  67. $sessionMetadata['Last used'] = date(DATE_RFC822, $session->getMetadataBag()->getLastUsed());
  68. $sessionMetadata['Lifetime'] = $session->getMetadataBag()->getLifetime();
  69. $sessionAttributes = $session->all();
  70. $flashes = $session->getFlashBag()->peekAll();
  71. }
  72. }
  73. $statusCode = $response->getStatusCode();
  74. $this->data = array(
  75. 'format' => $request->getRequestFormat(),
  76. 'content' => $content,
  77. 'content_type' => $response->headers->get('Content-Type', 'text/html'),
  78. 'status_text' => isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : '',
  79. 'status_code' => $statusCode,
  80. 'request_query' => $request->query->all(),
  81. 'request_request' => $request->request->all(),
  82. 'request_headers' => $request->headers->all(),
  83. 'request_server' => $request->server->all(),
  84. 'request_cookies' => $request->cookies->all(),
  85. 'request_attributes' => $attributes,
  86. 'response_headers' => $responseHeaders,
  87. 'session_metadata' => $sessionMetadata,
  88. 'session_attributes' => $sessionAttributes,
  89. 'flashes' => $flashes,
  90. 'path_info' => $request->getPathInfo(),
  91. 'controller' => 'n/a',
  92. 'locale' => $request->getLocale(),
  93. );
  94. if (isset($this->data['request_headers']['php-auth-pw'])) {
  95. $this->data['request_headers']['php-auth-pw'] = '******';
  96. }
  97. if (isset($this->data['request_server']['PHP_AUTH_PW'])) {
  98. $this->data['request_server']['PHP_AUTH_PW'] = '******';
  99. }
  100. if (isset($this->data['request_request']['_password'])) {
  101. $this->data['request_request']['_password'] = '******';
  102. }
  103. foreach ($this->data as $key => $value) {
  104. if (!is_array($value)) {
  105. continue;
  106. }
  107. if ('request_headers' === $key || 'response_headers' === $key) {
  108. $value = array_map(function ($v) { return isset($v[0]) && !isset($v[1]) ? $v[0] : $v; }, $value);
  109. }
  110. if ('request_server' !== $key && 'request_cookies' !== $key) {
  111. $this->data[$key] = $value;
  112. }
  113. }
  114. if (isset($this->controllers[$request])) {
  115. $controller = $this->controllers[$request];
  116. if (is_array($controller)) {
  117. try {
  118. $r = new \ReflectionMethod($controller[0], $controller[1]);
  119. $this->data['controller'] = array(
  120. 'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0],
  121. 'method' => $controller[1],
  122. 'file' => $r->getFileName(),
  123. 'line' => $r->getStartLine(),
  124. );
  125. } catch (\ReflectionException $e) {
  126. if (is_callable($controller)) {
  127. // using __call or __callStatic
  128. $this->data['controller'] = array(
  129. 'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0],
  130. 'method' => $controller[1],
  131. 'file' => 'n/a',
  132. 'line' => 'n/a',
  133. );
  134. }
  135. }
  136. } elseif ($controller instanceof \Closure) {
  137. $r = new \ReflectionFunction($controller);
  138. $this->data['controller'] = array(
  139. 'class' => $r->getName(),
  140. 'method' => null,
  141. 'file' => $r->getFileName(),
  142. 'line' => $r->getStartLine(),
  143. );
  144. } elseif (is_object($controller)) {
  145. $r = new \ReflectionClass($controller);
  146. $this->data['controller'] = array(
  147. 'class' => $r->getName(),
  148. 'method' => null,
  149. 'file' => $r->getFileName(),
  150. 'line' => $r->getStartLine(),
  151. );
  152. } else {
  153. $this->data['controller'] = (string) $controller ?: 'n/a';
  154. }
  155. unset($this->controllers[$request]);
  156. }
  157. }
  158. public function getPathInfo()
  159. {
  160. return $this->data['path_info'];
  161. }
  162. public function getRequestRequest()
  163. {
  164. return new ParameterBag($this->data['request_request']);
  165. }
  166. public function getRequestQuery()
  167. {
  168. return new ParameterBag($this->data['request_query']);
  169. }
  170. public function getRequestHeaders()
  171. {
  172. return new ParameterBag($this->data['request_headers']);
  173. }
  174. public function getRequestServer()
  175. {
  176. return new ParameterBag($this->data['request_server']);
  177. }
  178. public function getRequestCookies()
  179. {
  180. return new ParameterBag($this->data['request_cookies']);
  181. }
  182. public function getRequestAttributes()
  183. {
  184. return new ParameterBag($this->data['request_attributes']);
  185. }
  186. public function getResponseHeaders()
  187. {
  188. return new ParameterBag($this->data['response_headers']);
  189. }
  190. public function getSessionMetadata()
  191. {
  192. return $this->data['session_metadata'];
  193. }
  194. public function getSessionAttributes()
  195. {
  196. return $this->data['session_attributes'];
  197. }
  198. public function getFlashes()
  199. {
  200. return $this->data['flashes'];
  201. }
  202. public function getContent()
  203. {
  204. return $this->data['content'];
  205. }
  206. public function getContentType()
  207. {
  208. return $this->data['content_type'];
  209. }
  210. public function getStatusText()
  211. {
  212. return $this->data['status_text'];
  213. }
  214. public function getStatusCode()
  215. {
  216. return $this->data['status_code'];
  217. }
  218. public function getFormat()
  219. {
  220. return $this->data['format'];
  221. }
  222. public function getLocale()
  223. {
  224. return $this->data['locale'];
  225. }
  226. /**
  227. * Gets the route name.
  228. *
  229. * The _route request attributes is automatically set by the Router Matcher.
  230. *
  231. * @return string The route
  232. */
  233. public function getRoute()
  234. {
  235. return isset($this->data['request_attributes']['_route']) ? $this->data['request_attributes']['_route'] : '';
  236. }
  237. /**
  238. * Gets the route parameters.
  239. *
  240. * The _route_params request attributes is automatically set by the RouterListener.
  241. *
  242. * @return array The parameters
  243. */
  244. public function getRouteParams()
  245. {
  246. return isset($this->data['request_attributes']['_route_params']) ? $this->data['request_attributes']['_route_params'] : array();
  247. }
  248. /**
  249. * Gets the controller.
  250. *
  251. * @return string The controller as a string
  252. */
  253. public function getController()
  254. {
  255. return $this->data['controller'];
  256. }
  257. public function onKernelController(FilterControllerEvent $event)
  258. {
  259. $this->controllers[$event->getRequest()] = $event->getController();
  260. }
  261. public static function getSubscribedEvents()
  262. {
  263. return array(KernelEvents::CONTROLLER => 'onKernelController');
  264. }
  265. /**
  266. * {@inheritdoc}
  267. */
  268. public function getName()
  269. {
  270. return 'request';
  271. }
  272. }