Repository navigation
Expand file tree
/
Copy pathsessionmanager.php
More file actions
284 lines (251 loc) · 7.06 KB
/
Copy pathsessionmanager.php
File metadata and controls
284 lines (251 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
* Session manager
* @author yassine El hajjy (SevenAJJY) <[email protected]>
*
* @see https://github.com/SevenAJJY
*/
require_once './config.php';
class SessionManager extends SessionHandler
{
const SESSION_NAME = "SEVENAJJYSESS";
const SESSION_LIFE_TIME = 0;
const TIME_TO_LIVE = 10;
const SESSION_CIPHER_ALGO = 'AES-128-ECB';
const SESSION_CIPHER_KEY = 'S3V3NAJJYSN@2022';
/**
* Session Name
*
* @var string
*/
private string $_sessName = self::SESSION_NAME ;
/**
* Specifies the lifetime of the cookie in seconds which is sent to the browser.
* The value 0 means "until the browser is closed.
*
* @var int
*/
private int $_sessMaxLifeTime = self::SESSION_LIFE_TIME;
/**
* Specifies whether cookies should only be sent over secure connections.
*
* @var bool
*/
private bool $_sessSSL = false;
/**
* Specifies how to access the cookies
* The value 'True' means that the cookie won't be accessible by scripting languages, such as JavaScript.
* This setting can effectively help to reduce identity theft through XSS attacks
*
* @var bool
*/
private bool $_sessHTTPOnly = true;
/**
* Specifies path to set in the session cookie.
*
* @var string
*/
private string $_sessPath = "/";
/**
* Specifies the domain to set in the session cookie.
*
* @var string
*/
private string $_sessDomain = ".phpdev.com" ;
/**
* This is the path where the files are created.
*
* @var string
*/
private string $_sessSavePath = SESSION_SAVE_PATH;
/**
* The algorithm used to encrypt session data.
*
* @var string
*/
private string $_sessCipherAlgo = self::SESSION_CIPHER_ALGO;
/**
* The The key used to encrypt and re-decrypt session data .
*
* @var string
*/
private string $_sessCipherKey = self::SESSION_CIPHER_KEY;
/**
* Time to live .
* The value 0 means "until the browser is closed.
*
* @var int
*/
private int $_ttl = self::TIME_TO_LIVE;
public function __construct()
{
/**
* Set a set of settings in a file php.ini
*/
ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 1);
ini_set('session.use_trans_sid', 0);
ini_set('session.save_handler', "files");
session_name($this->_sessName);
session_save_path($this->_sessSavePath);
session_set_cookie_params($this->_sessMaxLifeTime, $this->_sessPath, $this->_sessDomain, $this->_sessSSL, $this->_sessHTTPOnly);
}
/**
* @param mixed $key
* @return mixed
*/
public function __get(mixed $key):mixed
{
if (isset($_SESSION[$key])) {
$data = @unserialize($_SESSION[$key]) ;
if ($data === false) {
return $_SESSION[$key];
}
else {
return $data;
}
} else {
trigger_error("No Session Key ". $key . " exists", E_USER_ERROR);
}
}
/**
* @param mixed $key
* @param mixed $value
* @return void
*/
public function __set(mixed $key, mixed $value):void
{
if (is_object($value)) {
$_SESSION[$key] = @serialize($value);
}
else{
$_SESSION[$key] = $value ;
}
}
/**
* @param mixed $key
* @return bool
*/
public function __isset(mixed $key):bool
{
return isset($_SESSION[$key]) ? true : false ;
}
/**
* Read session data
*
* @param string $sessId — The session id to read data for.
* @return string|false
*/
public function read(string $sessId):string|false
{
return openssl_decrypt(parent::read($sessId), $this->_sessCipherAlgo, $this->_sessCipherKey);
}
/**
* Write session data
*
* @param string $sessId — The session id.
* @param string $data
* The encoded session data. This data is the result of the PHP internally encoding the $_SESSION superglobal to a serialized string and passing it as this parameter. Please note sessions use an alternative serialization method.
*
* @return bool
*/
public function write(string $sessId, string $data):bool
{
return parent::write($sessId, openssl_encrypt($data, $this->_sessCipherAlgo, $this->_sessCipherKey));
}
/**
* Session start.
*
* @return void
*/
public function start():void
{
if ('' === session_id()) {
if (session_start()) {
$this->_setSessionStartTime();
$this->_checkSessionValidity();
}
}
}
/**
* Store the start time of the session in order to compare it with the time we set for the reset.
*
* @return bool
*/
private function _setSessionStartTime():bool
{
if (!isset($this->sessionStartTime)) {
$this->sessionStartTime = time();
}
return true ;
}
/**
* Compare the session start time with the session time to live,
* that we initially set in order to reset the session ID and user fingerprint.
*
* @return bool
*/
private function _checkSessionValidity():bool
{
if ((time() - $this->sessionStartTime) > ($this->_ttl * 60)) {
$this->_rennewSession();
$this->_generateFingerPrint();
}
return true ;
}
/**
* Reset session start time with session ID change.
*
* @return bool
*/
private function _rennewSession():bool
{
$this->sessionStartTime = time();
return session_regenerate_id(true);
}
/**
* Destroy the session and delete the cookie.
*
* @return void
*/
public function kill():void
{
session_unset();
setcookie(
$this->_sessName, "",time() - 1000,
$this->_sessPath, $this->_sessDomain,
$this->_sessSSL,$this->_sessHTTPOnly);
session_destroy();
}
/**
* Storing and encrypting the contents of the user agent in the session.
*
* @return void
*/
private function _generateFingerPrint():void
{
$userAgentId = $_SERVER["HTTP_USER_AGENT"];
$this->cipherKey = openssl_random_pseudo_bytes(16);
$sessionId = session_id();
$this->fingerPrint = sha1($userAgentId . $this->cipherKey . $sessionId);
}
/**
* Check if the contents of the user agent data match in each request.
*
* @return bool
*/
public function isValidFingerPrint():bool
{
if (!isset($this->fingerPrint)) {
$this->_generateFingerPrint();
}
$fingerPrint = sha1($_SERVER['HTTP_USER_AGENT'] . $this->cipherKey . session_id());
if ($fingerPrint === $this->fingerPrint) {
return true ;
}
return false ;
}
}
$session = new SessionManager();
$session->start();
// $session->kill();