|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: liu21st <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
declare (strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace think\session\driver; |
|
14
|
|
|
|
|
15
|
|
|
use think\App; |
|
16
|
|
|
use think\session\SessionHandler; |
|
17
|
|
|
|
|
18
|
|
|
class File implements SessionHandler |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
protected $config = [ |
|
21
|
|
|
'path' => '', |
|
22
|
|
|
'expire' => 0, |
|
23
|
|
|
'cache_subdir' => true, |
|
24
|
|
|
'data_compress' => false, |
|
25
|
|
|
'serialize' => ['serialize', 'unserialize'], |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(App $app, array $config = []) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
$this->config = array_merge($this->config, $config); |
|
31
|
|
|
|
|
32
|
|
|
if (empty($this->config['path'])) { |
|
33
|
|
|
$this->config['path'] = $app->getRuntimePath() . 'session' . DIRECTORY_SEPARATOR; |
|
34
|
|
|
} elseif (substr($this->config['path'], -1) != DIRECTORY_SEPARATOR) { |
|
35
|
|
|
$this->config['path'] .= DIRECTORY_SEPARATOR; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->init(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* 初始化检查 |
|
43
|
|
|
* @access private |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
private function init(): bool |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
// 创建项目缓存目录 |
|
49
|
|
|
try { |
|
50
|
|
|
if (!is_dir($this->config['path']) && mkdir($this->config['path'], 0755, true)) { |
|
51
|
|
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
} catch (\Exception $e) { |
|
54
|
|
|
// 写入失败 |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* 取得变量的存储文件名 |
|
62
|
|
|
* @access protected |
|
63
|
|
|
* @param string $name 缓存变量名 |
|
64
|
|
|
* @param bool $auto 是否自动创建目录 |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getFileName(string $name, bool $auto = false): string |
|
68
|
|
|
{ |
|
69
|
|
|
if ($this->config['cache_subdir']) { |
|
70
|
|
|
// 使用子目录 |
|
71
|
|
|
$name = substr($name, 0, 2) . DIRECTORY_SEPARATOR . 'sess_' . $name; |
|
72
|
|
|
} else { |
|
73
|
|
|
$name = 'sess_' . $name; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$filename = $this->config['path'] . $name . '.php'; |
|
77
|
|
|
$dir = dirname($filename); |
|
78
|
|
|
|
|
79
|
|
|
if ($auto && !is_dir($dir)) { |
|
80
|
|
|
try { |
|
81
|
|
|
mkdir($dir, 0755, true); |
|
82
|
|
|
} catch (\Exception $e) { |
|
83
|
|
|
// 创建失败 |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $filename; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* 读取Session |
|
92
|
|
|
* @access public |
|
93
|
|
|
* @param string $sessionId |
|
|
|
|
|
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public function read(string $sessionId): array |
|
97
|
|
|
{ |
|
98
|
|
|
$filename = $this->getFileName($sessionId); |
|
99
|
|
|
|
|
100
|
|
|
if (!is_file($filename)) { |
|
101
|
|
|
return []; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$content = file_get_contents($filename); |
|
105
|
|
|
|
|
106
|
|
|
if (false !== $content) { |
|
107
|
|
|
$expire = (int) substr($content, 8, 12); |
|
108
|
|
|
if (0 != $expire && time() > filemtime($filename) + $expire) { |
|
109
|
|
|
//缓存过期删除缓存文件 |
|
110
|
|
|
$this->unlink($filename); |
|
111
|
|
|
return []; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$content = substr($content, 32); |
|
115
|
|
|
|
|
116
|
|
|
if ($this->config['data_compress'] && function_exists('gzcompress')) { |
|
117
|
|
|
//启用数据压缩 |
|
118
|
|
|
$content = gzuncompress($content); |
|
119
|
|
|
} |
|
120
|
|
|
return $this->unserialize($content); |
|
121
|
|
|
} else { |
|
122
|
|
|
return []; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* 写入Session |
|
128
|
|
|
* @access public |
|
129
|
|
|
* @param string $sessionId |
|
|
|
|
|
|
130
|
|
|
* @param array $data |
|
|
|
|
|
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
|
|
public function write(string $sessionId, array $data): bool |
|
134
|
|
|
{ |
|
135
|
|
|
$expire = $this->config['expire']; |
|
136
|
|
|
|
|
137
|
|
|
$expire = $this->getExpireTime($expire); |
|
138
|
|
|
$filename = $this->getFileName($sessionId, true); |
|
139
|
|
|
|
|
140
|
|
|
$data = $this->serialize($data); |
|
141
|
|
|
|
|
142
|
|
|
if ($this->config['data_compress'] && function_exists('gzcompress')) { |
|
143
|
|
|
//数据压缩 |
|
144
|
|
|
$data = gzcompress($data, 3); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$data = "<?php\n//" . sprintf('%012d', $expire) . "\n exit();?>\n" . $data; |
|
148
|
|
|
$result = file_put_contents($filename, $data); |
|
149
|
|
|
|
|
150
|
|
|
if ($result) { |
|
151
|
|
|
clearstatcache(); |
|
152
|
|
|
return true; |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return false; |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* 删除Session |
|
160
|
|
|
* @access public |
|
161
|
|
|
* @param string $sessionId |
|
|
|
|
|
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
|
|
public function delete(string $sessionId): bool |
|
165
|
|
|
{ |
|
166
|
|
|
try { |
|
167
|
|
|
return $this->unlink($this->getFileName($sessionId)); |
|
|
|
|
|
|
168
|
|
|
} catch (\Exception $e) { |
|
169
|
|
|
return false; |
|
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* 获取有效期 |
|
175
|
|
|
* @access protected |
|
176
|
|
|
* @param integer|\DateTimeInterface $expire 有效期 |
|
177
|
|
|
* @return int |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function getExpireTime($expire): int |
|
180
|
|
|
{ |
|
181
|
|
|
if ($expire instanceof \DateTimeInterface) { |
|
182
|
|
|
$expire = $expire->getTimestamp() - time(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return (int) $expire; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
|
|
|
|
|
189
|
|
|
* 判断文件是否存在后,删除 |
|
190
|
|
|
* @access private |
|
191
|
|
|
* @param string $path |
|
|
|
|
|
|
192
|
|
|
* @return bool |
|
193
|
|
|
*/ |
|
194
|
|
|
private function unlink(string $file): bool |
|
|
|
|
|
|
195
|
|
|
{ |
|
196
|
|
|
return is_file($file) && unlink($file); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* 序列化数据 |
|
201
|
|
|
* @access protected |
|
202
|
|
|
* @param mixed $data |
|
|
|
|
|
|
203
|
|
|
* @return string |
|
204
|
|
|
*/ |
|
205
|
|
|
protected function serialize($data): string |
|
206
|
|
|
{ |
|
207
|
|
|
$serialize = $this->config['serialize'][0]; |
|
208
|
|
|
|
|
209
|
|
|
return $serialize($data); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* 反序列化数据 |
|
214
|
|
|
* @access protected |
|
215
|
|
|
* @param string $data |
|
|
|
|
|
|
216
|
|
|
* @return mixed |
|
217
|
|
|
*/ |
|
218
|
|
|
protected function unserialize(string $data) |
|
219
|
|
|
{ |
|
220
|
|
|
$unserialize = $this->config['serialize'][1]; |
|
221
|
|
|
|
|
222
|
|
|
return $unserialize($data); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
} |
|
226
|
|
|
|