Drupal investigation

FileProfilerStorage.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. /**
  12. * Storage for profiler using files.
  13. *
  14. * @author Alexandre Salomé <alexandre.salome@gmail.com>
  15. */
  16. class FileProfilerStorage implements ProfilerStorageInterface
  17. {
  18. /**
  19. * Folder where profiler data are stored.
  20. *
  21. * @var string
  22. */
  23. private $folder;
  24. /**
  25. * Constructs the file storage using a "dsn-like" path.
  26. *
  27. * Example : "file:/path/to/the/storage/folder"
  28. *
  29. * @param string $dsn The DSN
  30. *
  31. * @throws \RuntimeException
  32. */
  33. public function __construct($dsn)
  34. {
  35. if (0 !== strpos($dsn, 'file:')) {
  36. throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use FileStorage with an invalid dsn "%s". The expected format is "file:/path/to/the/storage/folder".', $dsn));
  37. }
  38. $this->folder = substr($dsn, 5);
  39. if (!is_dir($this->folder) && false === @mkdir($this->folder, 0777, true) && !is_dir($this->folder)) {
  40. throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $this->folder));
  41. }
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function find($ip, $url, $limit, $method, $start = null, $end = null)
  47. {
  48. $file = $this->getIndexFilename();
  49. if (!file_exists($file)) {
  50. return array();
  51. }
  52. $file = fopen($file, 'r');
  53. fseek($file, 0, SEEK_END);
  54. $result = array();
  55. while (count($result) < $limit && $line = $this->readLineFromFile($file)) {
  56. $values = str_getcsv($line);
  57. list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent) = $values;
  58. $csvStatusCode = isset($values[6]) ? $values[6] : null;
  59. $csvTime = (int) $csvTime;
  60. if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method)) {
  61. continue;
  62. }
  63. if (!empty($start) && $csvTime < $start) {
  64. continue;
  65. }
  66. if (!empty($end) && $csvTime > $end) {
  67. continue;
  68. }
  69. $result[$csvToken] = array(
  70. 'token' => $csvToken,
  71. 'ip' => $csvIp,
  72. 'method' => $csvMethod,
  73. 'url' => $csvUrl,
  74. 'time' => $csvTime,
  75. 'parent' => $csvParent,
  76. 'status_code' => $csvStatusCode,
  77. );
  78. }
  79. fclose($file);
  80. return array_values($result);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function purge()
  86. {
  87. $flags = \FilesystemIterator::SKIP_DOTS;
  88. $iterator = new \RecursiveDirectoryIterator($this->folder, $flags);
  89. $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
  90. foreach ($iterator as $file) {
  91. if (is_file($file)) {
  92. unlink($file);
  93. } else {
  94. rmdir($file);
  95. }
  96. }
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function read($token)
  102. {
  103. if (!$token || !file_exists($file = $this->getFilename($token))) {
  104. return;
  105. }
  106. return $this->createProfileFromData($token, unserialize(file_get_contents($file)));
  107. }
  108. /**
  109. * {@inheritdoc}
  110. *
  111. * @throws \RuntimeException
  112. */
  113. public function write(Profile $profile)
  114. {
  115. $file = $this->getFilename($profile->getToken());
  116. $profileIndexed = is_file($file);
  117. if (!$profileIndexed) {
  118. // Create directory
  119. $dir = dirname($file);
  120. if (!is_dir($dir) && false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
  121. throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $dir));
  122. }
  123. }
  124. // Store profile
  125. $data = array(
  126. 'token' => $profile->getToken(),
  127. 'parent' => $profile->getParentToken(),
  128. 'children' => array_map(function ($p) { return $p->getToken(); }, $profile->getChildren()),
  129. 'data' => $profile->getCollectors(),
  130. 'ip' => $profile->getIp(),
  131. 'method' => $profile->getMethod(),
  132. 'url' => $profile->getUrl(),
  133. 'time' => $profile->getTime(),
  134. );
  135. if (false === file_put_contents($file, serialize($data))) {
  136. return false;
  137. }
  138. if (!$profileIndexed) {
  139. // Add to index
  140. if (false === $file = fopen($this->getIndexFilename(), 'a')) {
  141. return false;
  142. }
  143. fputcsv($file, array(
  144. $profile->getToken(),
  145. $profile->getIp(),
  146. $profile->getMethod(),
  147. $profile->getUrl(),
  148. $profile->getTime(),
  149. $profile->getParentToken(),
  150. $profile->getStatusCode(),
  151. ));
  152. fclose($file);
  153. }
  154. return true;
  155. }
  156. /**
  157. * Gets filename to store data, associated to the token.
  158. *
  159. * @param string $token
  160. *
  161. * @return string The profile filename
  162. */
  163. protected function getFilename($token)
  164. {
  165. // Uses 4 last characters, because first are mostly the same.
  166. $folderA = substr($token, -2, 2);
  167. $folderB = substr($token, -4, 2);
  168. return $this->folder.'/'.$folderA.'/'.$folderB.'/'.$token;
  169. }
  170. /**
  171. * Gets the index filename.
  172. *
  173. * @return string The index filename
  174. */
  175. protected function getIndexFilename()
  176. {
  177. return $this->folder.'/index.csv';
  178. }
  179. /**
  180. * Reads a line in the file, backward.
  181. *
  182. * This function automatically skips the empty lines and do not include the line return in result value.
  183. *
  184. * @param resource $file The file resource, with the pointer placed at the end of the line to read
  185. *
  186. * @return mixed A string representing the line or null if beginning of file is reached
  187. */
  188. protected function readLineFromFile($file)
  189. {
  190. $line = '';
  191. $position = ftell($file);
  192. if (0 === $position) {
  193. return;
  194. }
  195. while (true) {
  196. $chunkSize = min($position, 1024);
  197. $position -= $chunkSize;
  198. fseek($file, $position);
  199. if (0 === $chunkSize) {
  200. // bof reached
  201. break;
  202. }
  203. $buffer = fread($file, $chunkSize);
  204. if (false === ($upTo = strrpos($buffer, "\n"))) {
  205. $line = $buffer.$line;
  206. continue;
  207. }
  208. $position += $upTo;
  209. $line = substr($buffer, $upTo + 1).$line;
  210. fseek($file, max(0, $position), SEEK_SET);
  211. if ('' !== $line) {
  212. break;
  213. }
  214. }
  215. return '' === $line ? null : $line;
  216. }
  217. protected function createProfileFromData($token, $data, $parent = null)
  218. {
  219. $profile = new Profile($token);
  220. $profile->setIp($data['ip']);
  221. $profile->setMethod($data['method']);
  222. $profile->setUrl($data['url']);
  223. $profile->setTime($data['time']);
  224. $profile->setCollectors($data['data']);
  225. if (!$parent && $data['parent']) {
  226. $parent = $this->read($data['parent']);
  227. }
  228. if ($parent) {
  229. $profile->setParent($parent);
  230. }
  231. foreach ($data['children'] as $token) {
  232. if (!$token || !file_exists($file = $this->getFilename($token))) {
  233. continue;
  234. }
  235. $profile->addChild($this->createProfileFromData($token, unserialize(file_get_contents($file)), $profile));
  236. }
  237. return $profile;
  238. }
  239. }