1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2016 SURFnet. |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
8
|
|
|
* License, or (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Affero General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage\Http; |
20
|
|
|
|
21
|
|
|
class Session implements SessionInterface |
22
|
|
|
{ |
23
|
|
|
/** @var bool */ |
24
|
|
|
private $secureOnly = true; |
25
|
|
|
|
26
|
|
|
public function setSecureOnly($secureOnly) |
27
|
|
|
{ |
28
|
|
|
$this->secureOnly = (bool) $secureOnly; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function startSession() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
if ('' === session_id()) { |
34
|
|
|
session_set_cookie_params(0, '/', '', $this->secureOnly, true); |
35
|
|
|
session_start(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// https://paragonie.com/blog/2015/04/fast-track-safe-and-secure-php-sessions |
39
|
|
|
// Make sure we have a canary set |
40
|
|
View Code Duplication |
if (!isset($_SESSION['canary'])) { |
|
|
|
|
41
|
|
|
session_regenerate_id(true); |
42
|
|
|
$_SESSION['canary'] = time(); |
43
|
|
|
} |
44
|
|
|
// Regenerate session ID every five minutes: |
45
|
|
View Code Duplication |
if ($_SESSION['canary'] < time() - 300) { |
|
|
|
|
46
|
|
|
session_regenerate_id(true); |
47
|
|
|
$_SESSION['canary'] = time(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function set($key, $value) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this->startSession(); |
54
|
|
|
$_SESSION[$key] = $value; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function delete($key) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->startSession(); |
60
|
|
|
if ($this->has($key)) { |
61
|
|
|
unset($_SESSION[$key]); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function has($key) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this->startSession(); |
68
|
|
|
|
69
|
|
|
return array_key_exists($key, $_SESSION); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function get($key) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$this->startSession(); |
75
|
|
|
if ($this->has($key)) { |
76
|
|
|
return $_SESSION[$key]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function destroy() |
83
|
|
|
{ |
84
|
|
|
$this->startSession(); |
85
|
|
|
session_destroy(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: