|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Tracy (https://tracy.nette.org) |
| 5 | + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Tracy; |
| 11 | + |
| 12 | + |
| 13 | +class FileSession implements SessionHandler |
| 14 | +{ |
| 15 | + private const FILE_PREFIX = 'tracy-'; |
| 16 | + private const COOKIE_LIFETIME = 31557600; |
| 17 | + |
| 18 | + /** @var string */ |
| 19 | + public $cookieName = 'tracy-session'; |
| 20 | + |
| 21 | + /** @var float probability that the clean() routine is started */ |
| 22 | + public $gcProbability = 0.001; |
| 23 | + |
| 24 | + /** @var string */ |
| 25 | + private $dir; |
| 26 | + |
| 27 | + /** @var resource */ |
| 28 | + private $file; |
| 29 | + |
| 30 | + /** @var array */ |
| 31 | + private $data = []; |
| 32 | + |
| 33 | + |
| 34 | + public function __construct(string $dir) |
| 35 | + { |
| 36 | + $this->dir = $dir; |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + public function isAvailable(): bool |
| 41 | + { |
| 42 | + if (!$this->file) { |
| 43 | + $this->open(); |
| 44 | + } |
| 45 | + return (bool) $this->file; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + private function open(): void |
| 50 | + { |
| 51 | + $id = $_COOKIE[$this->cookieName] ?? null; |
| 52 | + if ( |
| 53 | + !is_string($id) |
| 54 | + || !preg_match('#^\w{10}\z#i', $id) |
| 55 | + || !($file = @fopen($path = $this->dir . '/' . self::FILE_PREFIX . $id, 'r+')) // intentionally @ |
| 56 | + ) { |
| 57 | + $id = Helpers::createId(); |
| 58 | + setcookie($this->cookieName, $id, time() + self::COOKIE_LIFETIME, '/', '', false, true); |
| 59 | + |
| 60 | + $file = @fopen($path = $this->dir . '/' . self::FILE_PREFIX . $id, 'c+'); // intentionally @ |
| 61 | + if ($file === false) { |
| 62 | + throw new \RuntimeException("Unable to create file '$path'. " . error_get_last()['message']); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (!@flock($file, LOCK_EX)) { // intentionally @ |
| 67 | + throw new \RuntimeException("Unable to acquire exclusive lock on '$path'. ", error_get_last()['message']); |
| 68 | + } |
| 69 | + |
| 70 | + $this->file = $file; |
| 71 | + $this->data = @unserialize(stream_get_contents($this->file)) ?: []; // @ - file may be empty |
| 72 | + |
| 73 | + if (mt_rand() / mt_getrandmax() < $this->gcProbability) { |
| 74 | + $this->clean(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + public function &getData(): array |
| 80 | + { |
| 81 | + return $this->data; |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + public function clean(): void |
| 86 | + { |
| 87 | + $old = strtotime('-1 week'); |
| 88 | + foreach (glob($this->dir . '/' . self::FILE_PREFIX . '*') as $file) { |
| 89 | + if (filemtime($file) < $old) { |
| 90 | + unlink($file); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + public function __destruct() |
| 97 | + { |
| 98 | + if (!$this->file) { |
| 99 | + return; |
| 100 | + } |
| 101 | + ftruncate($this->file, 0); |
| 102 | + fseek($this->file, 0); |
| 103 | + fwrite($this->file, serialize($this->data)); |
| 104 | + fclose($this->file); |
| 105 | + $this->file = null; |
| 106 | + } |
| 107 | +} |
0 commit comments