1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodeZero\Cookie; |
4
|
|
|
|
5
|
|
|
use CodeZero\Encrypter\Encrypter; |
6
|
|
|
|
7
|
|
|
class VanillaCookie implements Cookie |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Encrypter |
11
|
|
|
* |
12
|
|
|
* @var \CodeZero\Encrypter\Encrypter |
13
|
|
|
*/ |
14
|
|
|
protected $encrypter; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Create a new instance of VanillaCookie |
18
|
|
|
* |
19
|
|
|
* @param \CodeZero\Encrypter\Encrypter $encrypter |
20
|
|
|
*/ |
21
|
10 |
|
public function __construct(Encrypter $encrypter = null) |
22
|
|
|
{ |
23
|
10 |
|
$this->encrypter = $encrypter; |
24
|
10 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the value of a cookie |
28
|
|
|
* |
29
|
|
|
* @param string $cookieName |
30
|
|
|
* |
31
|
|
|
* @return null|string |
32
|
|
|
* @throws \CodeZero\Encrypter\DecryptException |
33
|
|
|
*/ |
34
|
5 |
|
public function get($cookieName) |
35
|
|
|
{ |
36
|
5 |
|
if ( ! $this->exists($cookieName)) { |
37
|
2 |
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
return $this->decrypt($_COOKIE[$cookieName]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Store a cookie |
45
|
|
|
* |
46
|
|
|
* @param string $cookieName |
47
|
|
|
* @param string $cookieValue |
48
|
|
|
* @param int $minutes |
49
|
|
|
* @param string $path |
50
|
|
|
* @param string $domain |
51
|
|
|
* @param bool $secure |
52
|
|
|
* @param bool $httpOnly |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
6 |
|
public function store($cookieName, $cookieValue, $minutes = 60, $path = "/", $domain = null, $secure = true, $httpOnly = true) |
57
|
|
|
{ |
58
|
6 |
|
return setcookie( |
59
|
6 |
|
$cookieName, |
60
|
6 |
|
$this->encrypt($cookieValue), |
61
|
6 |
|
$this->calculateExpirationTime($minutes), |
62
|
6 |
|
$path, |
63
|
6 |
|
$domain, |
64
|
6 |
|
$secure, |
65
|
6 |
|
$httpOnly |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Store a cookie for a long, long time |
71
|
|
|
* |
72
|
|
|
* @param string $cookieName |
73
|
|
|
* @param string $cookieValue |
74
|
|
|
* @param string $path |
75
|
|
|
* @param string $domain |
76
|
|
|
* @param bool $secure |
77
|
|
|
* @param bool $httpOnly |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
1 |
|
public function forever($cookieName, $cookieValue, $path = '/', $domain = null, $secure = null, $httpOnly = true) |
82
|
|
|
{ |
83
|
1 |
|
return $this->store($cookieName, $cookieValue, 60 * 24 * 365 * 5, $path, $domain, $secure, $httpOnly); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Delete a cookie |
88
|
|
|
* |
89
|
|
|
* @param string $cookieName |
90
|
|
|
* @param string $path |
91
|
|
|
* @param string $domain |
92
|
|
|
* |
93
|
|
|
* @return null|bool |
94
|
|
|
*/ |
95
|
2 |
|
public function delete($cookieName, $path = '/', $domain = null) |
96
|
|
|
{ |
97
|
2 |
|
if ( ! $this->exists($cookieName)) { |
98
|
1 |
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
unset($_COOKIE[$cookieName]); |
102
|
|
|
|
103
|
1 |
|
return $this->store($cookieName, '', -60, $path, $domain); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check if a cookie exists |
108
|
|
|
* |
109
|
|
|
* @param string $cookieName |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
6 |
|
public function exists($cookieName) |
114
|
|
|
{ |
115
|
6 |
|
return isset($_COOKIE[$cookieName]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Calculate the expiration time |
120
|
|
|
* |
121
|
|
|
* @param int $minutes |
122
|
|
|
* |
123
|
|
|
* @return int |
124
|
|
|
*/ |
125
|
6 |
|
protected function calculateExpirationTime($minutes) |
126
|
|
|
{ |
127
|
6 |
|
return time() + (60 * $minutes); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Encrypt a cookie |
132
|
|
|
* |
133
|
|
|
* @param string $cookieValue |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
6 |
|
protected function encrypt($cookieValue) |
138
|
|
|
{ |
139
|
6 |
|
if ($this->encrypter) { |
140
|
2 |
|
return $this->encrypter->encrypt($cookieValue); |
141
|
|
|
} |
142
|
|
|
|
143
|
4 |
|
return $cookieValue; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Decrypt a cookie |
148
|
|
|
* |
149
|
|
|
* @param string $cookieValue |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
* @throws \CodeZero\Encrypter\DecryptException |
153
|
|
|
*/ |
154
|
3 |
|
protected function decrypt($cookieValue) |
155
|
|
|
{ |
156
|
3 |
|
if ($this->encrypter && ! empty($cookieValue)) { |
157
|
2 |
|
return $this->encrypter->decrypt($cookieValue); |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
return $cookieValue; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|