Drupal investigation

PdoProfilerStorage.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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\Profiler;
  11. @trigger_error('The '.__NAMESPACE__.'\PdoProfilerStorage class is deprecated since Symfony 2.8 and will be removed in 3.0. Use FileProfilerStorage instead.', E_USER_DEPRECATED);
  12. /**
  13. * Base PDO storage for profiling information in a PDO database.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. * @author Jan Schumann <js@schumann-it.com>
  17. *
  18. * @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
  19. * Use {@link FileProfilerStorage} instead.
  20. */
  21. abstract class PdoProfilerStorage implements ProfilerStorageInterface
  22. {
  23. protected $dsn;
  24. protected $username;
  25. protected $password;
  26. protected $lifetime;
  27. protected $db;
  28. /**
  29. * Constructor.
  30. *
  31. * @param string $dsn A data source name
  32. * @param string $username The username for the database
  33. * @param string $password The password for the database
  34. * @param int $lifetime The lifetime to use for the purge
  35. */
  36. public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
  37. {
  38. $this->dsn = $dsn;
  39. $this->username = $username;
  40. $this->password = $password;
  41. $this->lifetime = (int) $lifetime;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function find($ip, $url, $limit, $method, $start = null, $end = null)
  47. {
  48. if (null === $start) {
  49. $start = 0;
  50. }
  51. if (null === $end) {
  52. $end = time();
  53. }
  54. list($criteria, $args) = $this->buildCriteria($ip, $url, $start, $end, $limit, $method);
  55. $criteria = $criteria ? 'WHERE '.implode(' AND ', $criteria) : '';
  56. $db = $this->initDb();
  57. $tokens = $this->fetch($db, 'SELECT token, ip, method, url, time, parent, status_code FROM sf_profiler_data '.$criteria.' ORDER BY time DESC LIMIT '.((int) $limit), $args);
  58. $this->close($db);
  59. return $tokens;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function read($token)
  65. {
  66. $db = $this->initDb();
  67. $args = array(':token' => $token);
  68. $data = $this->fetch($db, 'SELECT data, parent, ip, method, url, time FROM sf_profiler_data WHERE token = :token LIMIT 1', $args);
  69. $this->close($db);
  70. if (isset($data[0]['data'])) {
  71. return $this->createProfileFromData($token, $data[0]);
  72. }
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function write(Profile $profile)
  78. {
  79. $db = $this->initDb();
  80. $args = array(
  81. ':token' => $profile->getToken(),
  82. ':parent' => $profile->getParentToken(),
  83. ':data' => base64_encode(serialize($profile->getCollectors())),
  84. ':ip' => $profile->getIp(),
  85. ':method' => $profile->getMethod(),
  86. ':url' => $profile->getUrl(),
  87. ':time' => $profile->getTime(),
  88. ':created_at' => time(),
  89. ':status_code' => $profile->getStatusCode(),
  90. );
  91. try {
  92. if ($this->has($profile->getToken())) {
  93. $this->exec($db, 'UPDATE sf_profiler_data SET parent = :parent, data = :data, ip = :ip, method = :method, url = :url, time = :time, created_at = :created_at, status_code = :status_code WHERE token = :token', $args);
  94. } else {
  95. $this->exec($db, 'INSERT INTO sf_profiler_data (token, parent, data, ip, method, url, time, created_at, status_code) VALUES (:token, :parent, :data, :ip, :method, :url, :time, :created_at, :status_code)', $args);
  96. }
  97. $this->cleanup();
  98. $status = true;
  99. } catch (\Exception $e) {
  100. $status = false;
  101. }
  102. $this->close($db);
  103. return $status;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function purge()
  109. {
  110. $db = $this->initDb();
  111. $this->exec($db, 'DELETE FROM sf_profiler_data');
  112. $this->close($db);
  113. }
  114. /**
  115. * Build SQL criteria to fetch records by ip and url.
  116. *
  117. * @param string $ip The IP
  118. * @param string $url The URL
  119. * @param string $start The start period to search from
  120. * @param string $end The end period to search to
  121. * @param string $limit The maximum number of tokens to return
  122. * @param string $method The request method
  123. *
  124. * @return array An array with (criteria, args)
  125. */
  126. abstract protected function buildCriteria($ip, $url, $start, $end, $limit, $method);
  127. /**
  128. * Initializes the database.
  129. *
  130. * @throws \RuntimeException When the requested database driver is not installed
  131. */
  132. abstract protected function initDb();
  133. protected function cleanup()
  134. {
  135. $db = $this->initDb();
  136. $this->exec($db, 'DELETE FROM sf_profiler_data WHERE created_at < :time', array(':time' => time() - $this->lifetime));
  137. $this->close($db);
  138. }
  139. protected function exec($db, $query, array $args = array())
  140. {
  141. $stmt = $this->prepareStatement($db, $query);
  142. foreach ($args as $arg => $val) {
  143. $stmt->bindValue($arg, $val, is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
  144. }
  145. $success = $stmt->execute();
  146. if (!$success) {
  147. throw new \RuntimeException(sprintf('Error executing query "%s"', $query));
  148. }
  149. }
  150. protected function prepareStatement($db, $query)
  151. {
  152. try {
  153. $stmt = $db->prepare($query);
  154. } catch (\Exception $e) {
  155. $stmt = false;
  156. }
  157. if (false === $stmt) {
  158. throw new \RuntimeException('The database cannot successfully prepare the statement');
  159. }
  160. return $stmt;
  161. }
  162. protected function fetch($db, $query, array $args = array())
  163. {
  164. $stmt = $this->prepareStatement($db, $query);
  165. foreach ($args as $arg => $val) {
  166. $stmt->bindValue($arg, $val, is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
  167. }
  168. $stmt->execute();
  169. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  170. }
  171. protected function close($db)
  172. {
  173. }
  174. protected function createProfileFromData($token, $data, $parent = null)
  175. {
  176. $profile = new Profile($token);
  177. $profile->setIp($data['ip']);
  178. $profile->setMethod($data['method']);
  179. $profile->setUrl($data['url']);
  180. $profile->setTime($data['time']);
  181. $profile->setCollectors(unserialize(base64_decode($data['data'])));
  182. if (!$parent && !empty($data['parent'])) {
  183. $parent = $this->read($data['parent']);
  184. }
  185. if ($parent) {
  186. $profile->setParent($parent);
  187. }
  188. $profile->setChildren($this->readChildren($token, $profile));
  189. return $profile;
  190. }
  191. /**
  192. * Reads the child profiles for the given token.
  193. *
  194. * @param string $token The parent token
  195. * @param string $parent The parent instance
  196. *
  197. * @return Profile[] An array of Profile instance
  198. */
  199. protected function readChildren($token, $parent)
  200. {
  201. $db = $this->initDb();
  202. $data = $this->fetch($db, 'SELECT token, data, ip, method, url, time FROM sf_profiler_data WHERE parent = :token', array(':token' => $token));
  203. $this->close($db);
  204. if (!$data) {
  205. return array();
  206. }
  207. $profiles = array();
  208. foreach ($data as $d) {
  209. $profiles[] = $this->createProfileFromData($d['token'], $d, $parent);
  210. }
  211. return $profiles;
  212. }
  213. /**
  214. * Returns whether data for the given token already exists in storage.
  215. *
  216. * @param string $token The profile token
  217. *
  218. * @return string
  219. */
  220. protected function has($token)
  221. {
  222. $db = $this->initDb();
  223. $tokenExists = $this->fetch($db, 'SELECT 1 FROM sf_profiler_data WHERE token = :token LIMIT 1', array(':token' => $token));
  224. $this->close($db);
  225. return !empty($tokenExists);
  226. }
  227. }