1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\SqlQueries; |
6
|
|
|
use PasswordHash; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @see WP Session Manager (1.2.0) |
10
|
|
|
*/ |
11
|
|
|
class Session |
12
|
|
|
{ |
13
|
|
|
const SESSION_COOKIE = '_glsr_session'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
protected $expiryTimestamp; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
protected $expiryTimestampReset; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $sessionData; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $sessionId; |
34
|
|
|
|
35
|
|
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
if( $cookieId = filter_input( INPUT_COOKIE, static::SESSION_COOKIE )) { |
38
|
|
|
$cookie = explode( '||', stripslashes( $cookieId )); |
39
|
|
|
$this->sessionId = preg_replace( '/[^A-Za-z0-9_]/', '', $cookie[0] ); |
40
|
|
|
$this->expiryTimestamp = absint( $cookie[1] ); |
41
|
|
|
$this->expiryTimestampReset = absint( $cookie[2] ); |
42
|
|
|
if( time() > $this->expiryTimestampReset ) { |
43
|
|
|
$this->setCookieExpiration(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
else { |
47
|
|
|
$this->sessionId = $this->generateSessionId(); |
48
|
|
|
$this->setCookieExpiration(); |
49
|
|
|
} |
50
|
|
|
$this->getSessionData(); |
51
|
|
|
$this->setCookie(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function clear() |
58
|
|
|
{ |
59
|
|
|
$this->setCookieExpiration(); |
60
|
|
|
$this->regenerateSessionId( 'and delete session!' ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return int|false |
65
|
|
|
*/ |
66
|
|
|
public function deleteAllSessions() |
67
|
|
|
{ |
68
|
|
|
return glsr( SqlQueries::class )->deleteAllSessions( static::SESSION_COOKIE ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $limit |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function deleteExpiredSessions( $limit = 1000 ) |
76
|
|
|
{ |
77
|
|
|
if( $expiredSessions = implode( "','", $this->getExpiredSessions( $limit ))) { |
78
|
|
|
glsr( SqlQueries::class )->deleteExpiredSessions( $expiredSessions ); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $key |
84
|
|
|
* @param string|array $fallback |
85
|
|
|
* @param bool|string $unset |
86
|
|
|
* @return string|array |
87
|
|
|
*/ |
88
|
|
|
public function get( $key, $fallback = '', $unset = false ) |
89
|
|
|
{ |
90
|
|
|
$key = sanitize_key( $key ); |
91
|
|
|
$value = isset( $this->sessionData[$key] ) |
92
|
|
|
? maybe_unserialize( $this->sessionData[$key] ) |
93
|
|
|
: $fallback; |
94
|
|
|
if( isset( $this->sessionData[$key] ) && $unset ) { |
95
|
|
|
unset( $this->sessionData[$key] ); |
96
|
|
|
$this->updateSession(); |
97
|
|
|
} |
98
|
|
|
return $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $key |
103
|
|
|
* @param mixed $value |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
public function set( $key, $value ) |
107
|
|
|
{ |
108
|
|
|
$key = sanitize_key( $key ); |
109
|
|
|
$this->sessionData[$key] = maybe_serialize( $value ); |
110
|
|
|
$this->updateSession(); |
111
|
|
|
return $this->sessionData[$key]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected function createSession() |
118
|
|
|
{ |
119
|
|
|
add_option( $this->getSessionId(), $this->sessionData, '', false ); |
120
|
|
|
add_option( $this->getSessionId( 'expires' ), $this->expiryTimestamp, '', false ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
protected function deleteSession() |
127
|
|
|
{ |
128
|
|
|
delete_option( $this->getSessionId() ); |
129
|
|
|
delete_option( $this->getSessionId( 'expires' )); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected function generateSessionId() |
136
|
|
|
{ |
137
|
|
|
return md5(( new PasswordHash( 8, false ))->get_random_bytes( 32 )); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param int $limit |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
protected function getExpiredSessions( $limit ) |
145
|
|
|
{ |
146
|
|
|
$expiredSessions = []; |
147
|
|
|
$sessions = glsr( SqlQueries::class )->getExpiredSessions( static::SESSION_COOKIE, absint( $limit )); |
148
|
|
|
if( !empty( $sessions )) { |
149
|
|
|
$now = time(); |
150
|
|
|
foreach( $sessions as $session ) { |
151
|
|
|
if( $now <= $session->expiration )continue; |
152
|
|
|
$expiredSessions[] = $session->name; |
153
|
|
|
$expiredSessions[] = str_replace( '_expires_', '_', $session->name ); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
return $expiredSessions; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $separator |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
protected function getSessionId( $separator = '' ) |
164
|
|
|
{ |
165
|
|
|
return implode( '_', array_filter( [static::SESSION_COOKIE, $separator, $this->sessionId] )); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
protected function getSessionData() |
172
|
|
|
{ |
173
|
|
|
return $this->sessionData = (array)get_option( $this->getSessionId(), [] ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param bool|string $deleteOld |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
|
|
protected function regenerateSessionId( $deleteOld = false ) |
181
|
|
|
{ |
182
|
|
|
if( $deleteOld ) { |
183
|
|
|
$this->deleteSession(); |
184
|
|
|
} |
185
|
|
|
$this->sessionId = $this->generateSessionId(); |
186
|
|
|
$this->setCookie(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
protected function setCookie() |
193
|
|
|
{ |
194
|
|
|
if( headers_sent() )return; |
195
|
|
|
$cookie = $this->sessionId.'||'.$this->expiryTimestamp.'||'.$this->expiryTimestampReset; |
196
|
|
|
$cookiePath = preg_replace( '|https?://[^/]+|i', '', trailingslashit( (string)get_option( 'home' ))); |
197
|
|
|
setcookie( static::SESSION_COOKIE, $cookie, $this->expiryTimestamp, $cookiePath ); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return void |
202
|
|
|
*/ |
203
|
|
|
protected function setCookieExpiration() |
204
|
|
|
{ |
205
|
|
|
$this->expiryTimestampReset = time() + (24 * 60); // 24 minutes |
206
|
|
|
$this->expiryTimestamp = time() + (30 * 60); // 30 minutes |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return void |
211
|
|
|
*/ |
212
|
|
|
protected function updateSession() |
213
|
|
|
{ |
214
|
|
|
if( false === get_option( $this->getSessionId() )) { |
215
|
|
|
return $this->createSession(); |
216
|
|
|
} |
217
|
|
|
update_option( $this->getSessionId(), $this->sessionData, false ); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|