]>
Commit | Line | Data |
---|---|---|
ebd650c0 | 1 | <?php |
fab87c26 | 2 | namespace Shaarli\Security; |
ebd650c0 | 3 | |
c7721487 V |
4 | use Shaarli\Config\ConfigManager; |
5 | ||
ebd650c0 V |
6 | /** |
7 | * Manages the server-side session | |
8 | */ | |
9 | class SessionManager | |
10 | { | |
af290059 A |
11 | public const KEY_LINKS_PER_PAGE = 'LINKS_PER_PAGE'; |
12 | public const KEY_VISIBILITY = 'visibility'; | |
13 | public const KEY_UNTAGGED_ONLY = 'untaggedonly'; | |
14 | ||
c7721487 | 15 | /** @var int Session expiration timeout, in seconds */ |
51f0128c V |
16 | public static $SHORT_TIMEOUT = 3600; // 1 hour |
17 | ||
18 | /** @var int Session expiration timeout, in seconds */ | |
19 | public static $LONG_TIMEOUT = 31536000; // 1 year | |
db45a36a | 20 | |
c7721487 | 21 | /** @var array Local reference to the global $_SESSION array */ |
ebd650c0 V |
22 | protected $session = []; |
23 | ||
c7721487 | 24 | /** @var ConfigManager Configuration Manager instance **/ |
49f18323 V |
25 | protected $conf = null; |
26 | ||
51f0128c V |
27 | /** @var bool Whether the user should stay signed in (LONG_TIMEOUT) */ |
28 | protected $staySignedIn = false; | |
29 | ||
ebd650c0 V |
30 | /** |
31 | * Constructor | |
32 | * | |
33 | * @param array $session The $_SESSION array (reference) | |
dd883aaf | 34 | * @param ConfigManager $conf ConfigManager instance |
ebd650c0 | 35 | */ |
dd883aaf | 36 | public function __construct(& $session, $conf) |
ebd650c0 V |
37 | { |
38 | $this->session = &$session; | |
dd883aaf | 39 | $this->conf = $conf; |
ebd650c0 V |
40 | } |
41 | ||
51f0128c V |
42 | /** |
43 | * Define whether the user should stay signed in across browser sessions | |
44 | * | |
45 | * @param bool $staySignedIn Keep the user signed in | |
46 | */ | |
47 | public function setStaySignedIn($staySignedIn) | |
48 | { | |
49 | $this->staySignedIn = $staySignedIn; | |
50 | } | |
51 | ||
ebd650c0 V |
52 | /** |
53 | * Generates a session token | |
54 | * | |
55 | * @return string token | |
56 | */ | |
57 | public function generateToken() | |
58 | { | |
59 | $token = sha1(uniqid('', true) .'_'. mt_rand() . $this->conf->get('credentials.salt')); | |
60 | $this->session['tokens'][$token] = 1; | |
61 | return $token; | |
62 | } | |
63 | ||
64 | /** | |
65 | * Checks the validity of a session token, and destroys it afterwards | |
66 | * | |
67 | * @param string $token The token to check | |
68 | * | |
69 | * @return bool true if the token is valid, else false | |
70 | */ | |
71 | public function checkToken($token) | |
72 | { | |
73 | if (! isset($this->session['tokens'][$token])) { | |
74 | // the token is wrong, or has already been used | |
75 | return false; | |
76 | } | |
77 | ||
78 | // destroy the token to prevent future use | |
79 | unset($this->session['tokens'][$token]); | |
80 | return true; | |
81 | } | |
fd7d8461 V |
82 | |
83 | /** | |
84 | * Validate session ID to prevent Full Path Disclosure. | |
85 | * | |
86 | * See #298. | |
87 | * The session ID's format depends on the hash algorithm set in PHP settings | |
88 | * | |
89 | * @param string $sessionId Session ID | |
90 | * | |
91 | * @return true if valid, false otherwise. | |
92 | * | |
93 | * @see http://php.net/manual/en/function.hash-algos.php | |
94 | * @see http://php.net/manual/en/session.configuration.php | |
95 | */ | |
96 | public static function checkId($sessionId) | |
97 | { | |
98 | if (empty($sessionId)) { | |
99 | return false; | |
100 | } | |
101 | ||
102 | if (!$sessionId) { | |
103 | return false; | |
104 | } | |
105 | ||
106 | if (!preg_match('/^[a-zA-Z0-9,-]{2,128}$/', $sessionId)) { | |
107 | return false; | |
108 | } | |
109 | ||
110 | return true; | |
111 | } | |
49f18323 V |
112 | |
113 | /** | |
114 | * Store user login information after a successful login | |
115 | * | |
c7721487 | 116 | * @param string $clientIpId Client IP address identifier |
49f18323 | 117 | */ |
c7721487 | 118 | public function storeLoginInfo($clientIpId) |
49f18323 | 119 | { |
c7721487 | 120 | $this->session['ip'] = $clientIpId; |
49f18323 | 121 | $this->session['username'] = $this->conf->get('credentials.login'); |
51f0128c | 122 | $this->extendTimeValidityBy(self::$SHORT_TIMEOUT); |
49f18323 V |
123 | } |
124 | ||
c7721487 V |
125 | /** |
126 | * Extend session validity | |
127 | */ | |
128 | public function extendSession() | |
129 | { | |
51f0128c V |
130 | if ($this->staySignedIn) { |
131 | return $this->extendTimeValidityBy(self::$LONG_TIMEOUT); | |
c7721487 | 132 | } |
51f0128c V |
133 | return $this->extendTimeValidityBy(self::$SHORT_TIMEOUT); |
134 | } | |
135 | ||
136 | /** | |
137 | * Extend expiration time | |
138 | * | |
139 | * @param int $duration Expiration time extension (seconds) | |
140 | * | |
141 | * @return int New session expiration time | |
142 | */ | |
143 | protected function extendTimeValidityBy($duration) | |
144 | { | |
145 | $expirationTime = time() + $duration; | |
146 | $this->session['expires_on'] = $expirationTime; | |
147 | return $expirationTime; | |
c7721487 V |
148 | } |
149 | ||
49f18323 V |
150 | /** |
151 | * Logout a user by unsetting all login information | |
152 | * | |
153 | * See: | |
154 | * - https://secure.php.net/manual/en/function.setcookie.php | |
49f18323 | 155 | */ |
51f0128c | 156 | public function logout() |
49f18323 V |
157 | { |
158 | if (isset($this->session)) { | |
49f18323 | 159 | unset($this->session['ip']); |
51f0128c | 160 | unset($this->session['expires_on']); |
49f18323 V |
161 | unset($this->session['username']); |
162 | unset($this->session['visibility']); | |
163 | unset($this->session['untaggedonly']); | |
164 | } | |
49f18323 | 165 | } |
c7721487 V |
166 | |
167 | /** | |
168 | * Check whether the session has expired | |
169 | * | |
170 | * @param string $clientIpId Client IP address identifier | |
171 | * | |
172 | * @return bool true if the session has expired, false otherwise | |
173 | */ | |
174 | public function hasSessionExpired() | |
175 | { | |
8edd7f15 V |
176 | if (empty($this->session['expires_on'])) { |
177 | return true; | |
178 | } | |
c7721487 V |
179 | if (time() >= $this->session['expires_on']) { |
180 | return true; | |
181 | } | |
182 | return false; | |
183 | } | |
184 | ||
185 | /** | |
186 | * Check whether the client IP address has changed | |
187 | * | |
188 | * @param string $clientIpId Client IP address identifier | |
189 | * | |
190 | * @return bool true if the IP has changed, false if it has not, or | |
191 | * if session protection has been disabled | |
192 | */ | |
193 | public function hasClientIpChanged($clientIpId) | |
194 | { | |
195 | if ($this->conf->get('security.session_protection_disabled') === true) { | |
196 | return false; | |
197 | } | |
8edd7f15 | 198 | if (isset($this->session['ip']) && $this->session['ip'] === $clientIpId) { |
c7721487 V |
199 | return false; |
200 | } | |
201 | return true; | |
202 | } | |
6c50a6cc A |
203 | |
204 | /** @return array Local reference to the global $_SESSION array */ | |
205 | public function getSession(): array | |
206 | { | |
207 | return $this->session; | |
208 | } | |
c266a89d A |
209 | |
210 | /** | |
211 | * @param mixed $default value which will be returned if the $key is undefined | |
212 | * | |
213 | * @return mixed Content stored in session | |
214 | */ | |
215 | public function getSessionParameter(string $key, $default = null) | |
216 | { | |
217 | return $this->session[$key] ?? $default; | |
218 | } | |
af290059 A |
219 | |
220 | /** | |
221 | * Store a variable in user session. | |
222 | * | |
223 | * @param string $key Session key | |
224 | * @param mixed $value Session value to store | |
225 | * | |
226 | * @return $this | |
227 | */ | |
228 | public function setSessionParameter(string $key, $value): self | |
229 | { | |
230 | $this->session[$key] = $value; | |
231 | ||
232 | return $this; | |
233 | } | |
234 | ||
235 | /** | |
236 | * Store a variable in user session. | |
237 | * | |
238 | * @param string $key Session key | |
239 | * | |
240 | * @return $this | |
241 | */ | |
242 | public function deleteSessionParameter(string $key): self | |
243 | { | |
244 | unset($this->session[$key]); | |
245 | ||
246 | return $this; | |
247 | } | |
ebd650c0 | 248 | } |