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
|
<?php
/**
* Session manager for Stud.IP
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author André Noack <noack@data-quest.de>
*/
namespace Studip\Session;
class Manager
{
public const STATE_UNKNOWN = false;
public const STATE_AUTHENTICATED = 'authenticated';
public const STATE_NOBODY = 'nobody';
protected array $options = [
'name' => 'Studip_Session',
'lifetime' => 7200,
'path' => null,
'domain' => null,
'secure' => false,
'httponly' => true,
'samesite' => 'Lax',
'cache_limiter' => 'nocache'
];
protected string|false|null $current_session_state = null;
public function __construct(
protected \SessionHandlerInterface $handler,
array $session_options = []
) {
$keys = array_keys($this->options);
foreach ($keys as $key) {
if (array_key_exists($key, $session_options)) {
$this->options[$key] = $session_options[$key];
if ($key === 'path') {
$this->options[$key] = implode('/', array_map('rawurlencode', explode('/', $this->options[$key] )));
}
}
}
}
/**
* @return void
*/
public function start(): void
{
if (!$this->isStarted()) {
ini_set('session.use_strict_mode', 1);
session_set_cookie_params([
'lifetime' => 0,
'path' => $this->getCookieParam('path'),
'domain' => $this->getCookieParam('domain'),
'secure' => (bool) $this->getCookieParam('secure', false),
'samesite' => $this->getCookieParam('samesite'),
'httponly' => (bool) $this->getCookieParam('httponly', false),
]);
session_name($this->options['name']);
session_cache_limiter('nocache');
session_set_save_handler($this->handler, true);
session_start([
'gc_maxlifetime' => (int) $this->getCookieParam('lifetime'),
]);
}
}
public function isStarted(): bool
{
return session_status() === PHP_SESSION_ACTIVE;
}
public function regenerateId(array $keep_session_vars = []): void
{
if (!$this->isStarted()) {
return;
}
$keep = [];
if (is_array($_SESSION)) {
foreach (array_keys($_SESSION) as $k) {
if (in_array($k, $keep_session_vars)) {
$keep[$k] = $_SESSION[$k];
}
}
$_SESSION = [];
}
session_regenerate_id(true);
foreach ($keep_session_vars as $k) {
$_SESSION[$k] = $keep[$k] ?? null;
}
}
public function getName(): string
{
return $this->options['name'];
}
/**
* Returns the value for the given cookie parameter. The value is taken
* from the configured options array (or from the current session
* configuration in php).
*
* If no value is found, null is retuned.
*/
public function getCookieParam(string $key, bool $from_config = true): mixed
{
$value = $this->options[$key] ?? null;
if ($from_config) {
$current = session_get_cookie_params();
$value = $value ?: $current[$key] ?? null;
}
return $value;
}
public function destroy(): void
{
if (!$this->isStarted()) {
return;
}
if (ini_get('session.use_cookies')) {
$params = session_get_cookie_params();
setcookie(
$this->getName(),
'',
time() - 42000,
$params['path'],
$params['domain'],
$params['secure'],
$params['httponly']
);
}
$_COOKIE[$this->getName()] = '';
session_unset();
session_destroy();
}
public function save() : void
{
session_write_close();
}
/**
* Returns true, if the current session is valid and belongs to an
* authenticated user. Does not start a session.
*/
public function isCurrentSessionAuthenticated(): bool
{
return $this->getCurrentSessionState() === self::STATE_AUTHENTICATED;
}
/**
* Returns the state of the current session. Does not start a session.
* possible return values:
* 'authenticated' - session is valid and user is authenticated
* 'nobody' - session is valid, but user is not authenticated
* false - no valid session
*/
public function getCurrentSessionState(): false|string|null
{
if ($this->current_session_state !== null) {
return $this->current_session_state;
}
$state = self::STATE_UNKNOWN;
if (isset($GLOBALS['user']) && is_object($GLOBALS['user'])) {
$state = in_array($GLOBALS['user']->id, ['nobody', 'form']) ? self::STATE_NOBODY : self::STATE_AUTHENTICATED;
} else {
$sid = $_COOKIE[$this->getName()];
if ($sid) {
$session_vars = $this->getSessionVars($sid);
$session_auth = $session_vars['auth'];
if ($session_auth['uid'] && !in_array($session_auth['uid'], ['nobody', 'form'])) {
$state = self::STATE_AUTHENTICATED;
} else {
$state = in_array($session_auth['uid'], ['nobody', 'form']) ? self::STATE_NOBODY : self::STATE_UNKNOWN;
}
}
}
return ($this->current_session_state = $state);
}
/**
* returns a SessionDecoder object containing the session variables
* for the given session id
*/
public function getSessionVars(string $sid): \SessionDecoder
{
$data = $this->handler->read($sid);
return new \SessionDecoder($data);
}
/**
* force garbage collect
*
* @return void
*/
public function doGarbageCollect(): void
{
$this->handler->gc($this->options['lifetime']);
}
}
|