|
1
|
|
|
<?php |
|
2
|
|
|
namespace coossions\base; |
|
3
|
|
|
|
|
4
|
|
|
use coossions\crypt\Encryptor; |
|
5
|
|
|
use coossions\exceptions\AlgoNotFoundException; |
|
6
|
|
|
use coossions\exceptions\CookieSizeException; |
|
7
|
|
|
use coossions\exceptions\OpenSSLException; |
|
8
|
|
|
|
|
9
|
|
|
class Coossions extends Encryptor implements BaseInterface |
|
10
|
|
|
{ |
|
11
|
|
|
private $sidLength = 0; |
|
12
|
|
|
private $sessionNameLength = 0; |
|
13
|
|
|
private $cookieParams = []; |
|
14
|
|
|
|
|
15
|
|
|
private $isOpened = false; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Coossions constructor. |
|
19
|
|
|
* @param string $secret the secret key to be used in openssl_digest |
|
20
|
|
|
*/ |
|
21
|
2 |
|
public function __construct(string $secret) |
|
22
|
|
|
{ |
|
23
|
2 |
|
parent::__construct($secret); |
|
24
|
2 |
|
$this->expire = $this->getExpire(); |
|
25
|
2 |
|
$this->digestAlgo = $this->getDigestAlgo(); |
|
26
|
2 |
|
$this->cipherAlgo = $this->getCipherAlgo(); |
|
27
|
2 |
|
$this->cipherIvLen = openssl_cipher_iv_length($this->cipherAlgo); |
|
28
|
2 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Setter for DI via Encryptor ex. if user wants to override params |
|
32
|
|
|
* |
|
33
|
|
|
* @param Encryptor $encryptor |
|
34
|
|
|
* @throws AlgoNotFoundException |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function setEncryption(Encryptor $encryptor) |
|
37
|
|
|
{ |
|
38
|
1 |
|
$this->expire = $encryptor->getExpire(); |
|
39
|
1 |
|
$this->digestAlgo = $encryptor->getDigestAlgo(); |
|
40
|
1 |
|
$this->cipherAlgo = $encryptor->getCipherAlgo(); |
|
41
|
|
|
// check if there are cipher and digest algos exist |
|
42
|
1 |
|
$cipherMethods = openssl_get_cipher_methods(); |
|
43
|
1 |
|
$digestMethods = openssl_get_md_methods(); |
|
44
|
1 |
|
if (in_array($this->digestAlgo, $digestMethods) === false) |
|
45
|
|
|
{ |
|
46
|
|
|
throw new AlgoNotFoundException('Digest algorithm ' . $this->digestAlgo . ' not found'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
if (in_array($this->cipherAlgo, $cipherMethods) === false) |
|
50
|
|
|
{ |
|
51
|
|
|
throw new AlgoNotFoundException('Cipher algorithm ' . $this->cipherAlgo . ' not found'); |
|
52
|
|
|
} |
|
53
|
|
|
// if user changed cipher algo - re-get length |
|
54
|
1 |
|
$this->cipherIvLen = openssl_cipher_iv_length($this->cipherAlgo); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Close the session |
|
59
|
|
|
* |
|
60
|
|
|
* @link http://php.net/manual/en/sessionhandlerinterface.close.php |
|
61
|
|
|
* @return bool <p> |
|
62
|
|
|
* The return value (usually TRUE on success, FALSE on failure). |
|
63
|
|
|
* Note this value is returned internally to PHP for processing. |
|
64
|
|
|
* </p> |
|
65
|
|
|
* @since 5.4.0 |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function close(): bool |
|
68
|
|
|
{ |
|
69
|
1 |
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Destroy a session |
|
74
|
|
|
* |
|
75
|
|
|
* @link http://php.net/manual/en/sessionhandlerinterface.destroy.php |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $sid The session ID being destroyed. |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool <p> |
|
80
|
|
|
* The return value (usually TRUE on success, FALSE on failure). |
|
81
|
|
|
* Note this value is returned internally to PHP for processing. |
|
82
|
|
|
* </p> |
|
83
|
|
|
* @since 5.4.0 |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function destroy($sid): bool |
|
86
|
|
|
{ |
|
87
|
1 |
|
setcookie($sid, '', time() - 3600); // erase cookie in path that they were set in |
|
88
|
1 |
|
setcookie($sid, '', time() - 3600, '/'); // erase cookie for current domain |
|
89
|
|
|
|
|
90
|
1 |
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Cleanup old sessions |
|
95
|
|
|
* |
|
96
|
|
|
* @link http://php.net/manual/en/sessionhandlerinterface.gc.php |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $maxlifetime <p> |
|
99
|
|
|
* Sessions that have not updated for |
|
100
|
|
|
* the last maxlifetime seconds will be removed. |
|
101
|
|
|
* </p> |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool <p> |
|
104
|
|
|
* The return value (usually TRUE on success, FALSE on failure). |
|
105
|
|
|
* Note this value is returned internally to PHP for processing. |
|
106
|
|
|
* </p> |
|
107
|
|
|
* @since 5.4.0 |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function gc($maxlifetime): bool |
|
110
|
|
|
{ |
|
111
|
1 |
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Initialize session |
|
116
|
|
|
* |
|
117
|
|
|
* @link http://php.net/manual/en/sessionhandlerinterface.open.php |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $savePath The path where to store/retrieve the session. |
|
120
|
|
|
* @param string $sid The session id. |
|
121
|
|
|
* |
|
122
|
|
|
* @return bool <p> |
|
123
|
|
|
* The return value (usually TRUE on success, FALSE on failure). |
|
124
|
|
|
* Note this value is returned internally to PHP for processing. |
|
125
|
|
|
* </p> |
|
126
|
|
|
* @since 5.4.0 |
|
127
|
|
|
*/ |
|
128
|
1 |
|
public function open($savePath, $sid): bool |
|
129
|
|
|
{ |
|
130
|
1 |
|
$this->cookieParams = session_get_cookie_params(); |
|
131
|
1 |
|
$this->digestLength = strlen(hash($this->digestAlgo, '', true)); |
|
132
|
1 |
|
$this->cipherIvLen = openssl_cipher_iv_length($this->cipherAlgo); |
|
133
|
1 |
|
$this->sidLength = strlen($sid); |
|
134
|
1 |
|
$this->sessionNameLength = strlen(session_name()); |
|
135
|
|
|
|
|
136
|
1 |
|
return $this->isOpened = true; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Read session data |
|
141
|
|
|
* |
|
142
|
|
|
* @link http://php.net/manual/en/sessionhandlerinterface.read.php |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $sid The session id to read data for. |
|
145
|
|
|
* |
|
146
|
|
|
* @return string <p> |
|
147
|
|
|
* Returns an encoded string of the read data. |
|
148
|
|
|
* If nothing was read, it must return an empty string. |
|
149
|
|
|
* Note this value is returned internally to PHP for processing. |
|
150
|
|
|
* </p> |
|
151
|
|
|
* @since 5.4.0 |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function read($sid): string |
|
154
|
|
|
{ |
|
155
|
1 |
|
if ($this->isOpened === false) { |
|
156
|
|
|
$this->open(self::SESSION_PATH, self::SESSION_NAME); |
|
157
|
|
|
} |
|
158
|
1 |
|
if (isset($_COOKIE[$sid]) === false) { |
|
159
|
1 |
|
return ''; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $this->decryptString($_COOKIE[$sid], $this->secret, $sid); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Write session data |
|
167
|
|
|
* |
|
168
|
|
|
* @link http://php.net/manual/en/sessionhandlerinterface.write.php |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $sid The session id. |
|
171
|
|
|
* @param string $sessionData <p> |
|
172
|
|
|
* The encoded session data. This data is the |
|
173
|
|
|
* result of the PHP internally encoding |
|
174
|
|
|
* the $_SESSION superglobal to a serialized |
|
175
|
|
|
* string and passing it as this parameter. |
|
176
|
|
|
* Please note sessions use an alternative serialization method. |
|
177
|
|
|
* </p> |
|
178
|
|
|
* |
|
179
|
|
|
* @return bool <p> |
|
180
|
|
|
* The return value (usually TRUE on success, FALSE on failure). |
|
181
|
|
|
* Note this value is returned internally to PHP for processing. |
|
182
|
|
|
* </p> |
|
183
|
|
|
* @throws CookieSizeException |
|
184
|
|
|
* @throws OpenSSLException |
|
185
|
|
|
* @since 5.4.0 |
|
186
|
|
|
*/ |
|
187
|
1 |
|
public function write($sid, $sessionData): bool |
|
188
|
|
|
{ |
|
189
|
1 |
|
if ($this->isOpened === false) { |
|
190
|
|
|
$this->open(self::SESSION_PATH, self::SESSION_NAME); |
|
191
|
|
|
} |
|
192
|
1 |
|
$output = $this->encryptString($sessionData, $this->secret, $sid); |
|
193
|
|
|
|
|
194
|
1 |
|
if ((strlen($output) + $this->sessionNameLength + |
|
195
|
1 |
|
$this->sidLength + self::MIN_LEN_PER_COOKIE) > self::COOKIE_SIZE |
|
196
|
|
|
) { |
|
197
|
|
|
throw new CookieSizeException( |
|
198
|
|
|
'The cookie size of ' |
|
199
|
|
|
. self::COOKIE_SIZE . ' bytes was exceeded.' |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
$isSet = setcookie( |
|
204
|
1 |
|
$sid, |
|
205
|
1 |
|
$output, |
|
206
|
1 |
|
($this->cookieParams["lifetime"] > 0) ? time() + $this->cookieParams["lifetime"] : 0, |
|
207
|
1 |
|
$this->cookieParams["path"], |
|
208
|
1 |
|
$this->cookieParams["domain"], |
|
209
|
1 |
|
$this->cookieParams["secure"], |
|
210
|
1 |
|
$this->cookieParams["httponly"] |
|
211
|
|
|
); |
|
212
|
|
|
|
|
213
|
1 |
|
return $isSet; |
|
214
|
|
|
} |
|
215
|
|
|
} |