1 | <?php |
||
2 | // +---------------------------------------------------------------------- |
||
3 | // | ThinkPHP [ WE CAN DO IT JUST THINK ] |
||
4 | // +---------------------------------------------------------------------- |
||
5 | // | Copyright (c) 2006~2021 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; |
||
14 | |||
15 | use DateTimeInterface; |
||
16 | |||
17 | /** |
||
18 | * Cookie管理类 |
||
19 | * @package think |
||
20 | */ |
||
21 | class Cookie |
||
22 | { |
||
23 | /** |
||
24 | * 配置参数 |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $config = [ |
||
28 | // cookie 保存时间 |
||
29 | 'expire' => 0, |
||
30 | // cookie 保存路径 |
||
31 | 'path' => '/', |
||
32 | // cookie 有效域名 |
||
33 | 'domain' => '', |
||
34 | // cookie 启用安全传输 |
||
35 | 'secure' => false, |
||
36 | // httponly设置 |
||
37 | 'httponly' => false, |
||
38 | // samesite 设置,支持 'strict' 'lax' |
||
39 | 'samesite' => '', |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * Cookie写入数据 |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $cookie = []; |
||
47 | |||
48 | /** |
||
49 | * 当前Request对象 |
||
50 | * @var Request |
||
51 | */ |
||
52 | protected $request; |
||
53 | |||
54 | /** |
||
55 | * 构造方法 |
||
56 | * @access public |
||
57 | */ |
||
58 | 33 | public function __construct(Request $request, array $config = []) |
|
59 | { |
||
60 | 33 | $this->request = $request; |
|
61 | 33 | $this->config = array_merge($this->config, array_change_key_case($config)); |
|
62 | 33 | } |
|
63 | |||
64 | 33 | public static function __make(Request $request, Config $config) |
|
65 | { |
||
66 | 33 | return new static($request, $config->get('cookie')); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
67 | } |
||
68 | |||
69 | /** |
||
70 | * 获取cookie |
||
71 | * @access public |
||
72 | * @param mixed $name 数据名称 |
||
73 | * @param string $default 默认值 |
||
74 | * @return mixed |
||
75 | */ |
||
76 | public function get(string $name = '', $default = null) |
||
77 | { |
||
78 | return $this->request->cookie($name, $default); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * 是否存在Cookie参数 |
||
83 | * @access public |
||
84 | * @param string $name 变量名 |
||
85 | * @return bool |
||
86 | */ |
||
87 | public function has(string $name): bool |
||
88 | { |
||
89 | return $this->request->has($name, 'cookie'); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Cookie 设置 |
||
94 | * |
||
95 | * @access public |
||
96 | * @param string $name cookie名称 |
||
97 | * @param string $value cookie值 |
||
98 | * @param mixed $option 可选参数 |
||
99 | * @return void |
||
100 | */ |
||
101 | public function set(string $name, string $value, $option = null): void |
||
102 | { |
||
103 | // 参数设置(会覆盖黙认设置) |
||
104 | if (!is_null($option)) { |
||
105 | if (is_numeric($option) || $option instanceof DateTimeInterface) { |
||
106 | $option = ['expire' => $option]; |
||
107 | } |
||
108 | |||
109 | $config = array_merge($this->config, array_change_key_case($option)); |
||
110 | } else { |
||
111 | $config = $this->config; |
||
112 | } |
||
113 | |||
114 | if ($config['expire'] instanceof DateTimeInterface) { |
||
115 | $expire = $config['expire']->getTimestamp(); |
||
116 | } else { |
||
117 | $expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0; |
||
118 | } |
||
119 | |||
120 | $this->setCookie($name, $value, $expire, $config); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Cookie 保存 |
||
125 | * |
||
126 | * @access public |
||
127 | * @param string $name cookie名称 |
||
128 | * @param string $value cookie值 |
||
129 | * @param int $expire 有效期 |
||
130 | * @param array $option 可选参数 |
||
131 | * @return void |
||
132 | */ |
||
133 | protected function setCookie(string $name, string $value, int $expire, array $option = []): void |
||
134 | { |
||
135 | $this->cookie[$name] = [$value, $expire, $option]; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * 永久保存Cookie数据 |
||
140 | * @access public |
||
141 | * @param string $name cookie名称 |
||
142 | * @param string $value cookie值 |
||
143 | * @param mixed $option 可选参数 可能会是 null|integer|string |
||
144 | * @return void |
||
145 | */ |
||
146 | public function forever(string $name, string $value = '', $option = null): void |
||
147 | { |
||
148 | if (is_null($option) || is_numeric($option)) { |
||
149 | $option = []; |
||
150 | } |
||
151 | |||
152 | $option['expire'] = 315360000; |
||
153 | |||
154 | $this->set($name, $value, $option); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Cookie删除 |
||
159 | * @access public |
||
160 | * @param string $name cookie名称 |
||
161 | * @param array $options cookie参数 |
||
162 | * @return void |
||
163 | */ |
||
164 | public function delete(string $name, array $options = []): void |
||
165 | { |
||
166 | $config = array_merge($this->config, array_change_key_case($options)); |
||
167 | $this->setCookie($name, '', time() - 3600, $config); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * 获取cookie保存数据 |
||
172 | * @access public |
||
173 | * @return array |
||
174 | */ |
||
175 | public function getCookie(): array |
||
176 | { |
||
177 | return $this->cookie; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * 保存Cookie |
||
182 | * @access public |
||
183 | * @return void |
||
184 | */ |
||
185 | public function save(): void |
||
186 | { |
||
187 | foreach ($this->cookie as $name => $val) { |
||
188 | [$value, $expire, $option] = $val; |
||
189 | |||
190 | $this->saveCookie( |
||
191 | $name, |
||
192 | $value, |
||
193 | $expire, |
||
194 | $option['path'], |
||
195 | $option['domain'], |
||
196 | $option['secure'] ? true : false, |
||
197 | $option['httponly'] ? true : false, |
||
198 | $option['samesite'] |
||
199 | ); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * 保存Cookie |
||
205 | * @access public |
||
206 | * @param string $name cookie名称 |
||
207 | * @param string $value cookie值 |
||
208 | * @param int $expire cookie过期时间 |
||
209 | * @param string $path 有效的服务器路径 |
||
210 | * @param string $domain 有效域名/子域名 |
||
211 | * @param bool $secure 是否仅仅通过HTTPS |
||
212 | * @param bool $httponly 仅可通过HTTP访问 |
||
213 | * @param string $samesite 防止CSRF攻击和用户追踪 |
||
214 | * @return void |
||
215 | */ |
||
216 | protected function saveCookie(string $name, string $value, int $expire, string $path, string $domain, bool $secure, bool $httponly, string $samesite): void |
||
217 | { |
||
218 | if (version_compare(PHP_VERSION, '7.3.0', '>=')) { |
||
219 | setcookie($name, $value, [ |
||
220 | 'expires' => $expire, |
||
221 | 'path' => $path, |
||
222 | 'domain' => $domain, |
||
223 | 'secure' => $secure, |
||
224 | 'httponly' => $httponly, |
||
225 | 'samesite' => $samesite, |
||
226 | ]); |
||
227 | } else { |
||
228 | setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | } |
||
233 |