1: <?php
2: namespace Opencart\System\Library\Session;
3: /**
4: * Class File
5: *
6: * @package Opencart\System\Library\Session
7: */
8: class File {
9: private object $config;
10:
11: /**
12: * Constructor
13: *
14: * @param \Opencart\System\Engine\Registry $registry
15: */
16: public function __construct(\Opencart\System\Engine\Registry $registry) {
17: $this->config = $registry->get('config');
18: }
19:
20: /**
21: * Read
22: *
23: * @param string $session_id
24: *
25: * @return array<mixed>
26: */
27: public function read(string $session_id): array {
28: $file = DIR_SESSION . 'sess_' . basename($session_id);
29:
30: if (is_file($file)) {
31: return json_decode(file_get_contents($file), true);
32: } else {
33: return [];
34: }
35: }
36:
37: /**
38: * Write
39: *
40: * @param string $session_id
41: * @param array<mixed> $data
42: *
43: * @return bool
44: */
45: public function write(string $session_id, array $data): bool {
46: file_put_contents(DIR_SESSION . 'sess_' . basename($session_id), json_encode($data));
47:
48: return true;
49: }
50:
51: /**
52: * Destroy
53: *
54: * @param string $session_id
55: *
56: * @return void
57: */
58: public function destroy(string $session_id): void {
59: $file = DIR_SESSION . 'sess_' . basename($session_id);
60:
61: if (is_file($file)) {
62: unlink($file);
63: }
64: }
65:
66: /**
67: * GC
68: *
69: * @return void
70: */
71: public function gc(): void {
72: if (round(mt_rand(1, $this->config->get('session_divisor') / $this->config->get('session_probability'))) == 1) {
73: $expire = time() - $this->config->get('session_expire');
74:
75: $files = glob(DIR_SESSION . 'sess_*');
76:
77: foreach ($files as $file) {
78: if (is_file($file) && filemtime($file) < $expire) {
79: unlink($file);
80: }
81: }
82: }
83: }
84: }
85: