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_start( |
35
|
|
|
[ |
36
|
|
|
'use_cookies' => true, |
37
|
|
|
'cookie_secure' => $this->secureOnly, |
38
|
|
|
'cookie_httponly' => true, |
39
|
|
|
'use_only_cookies' => true, |
40
|
|
|
] |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// https://paragonie.com/blog/2015/04/fast-track-safe-and-secure-php-sessions |
45
|
|
|
// Make sure we have a canary set |
46
|
|
View Code Duplication |
if (!isset($_SESSION['canary'])) { |
|
|
|
|
47
|
|
|
session_regenerate_id(true); |
48
|
|
|
$_SESSION['canary'] = time(); |
49
|
|
|
} |
50
|
|
|
// Regenerate session ID every five minutes: |
51
|
|
View Code Duplication |
if ($_SESSION['canary'] < time() - 300) { |
|
|
|
|
52
|
|
|
session_regenerate_id(true); |
53
|
|
|
$_SESSION['canary'] = time(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function set($key, $value) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->startSession(); |
60
|
|
|
$_SESSION[$key] = $value; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function delete($key) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->startSession(); |
66
|
|
|
if ($this->has($key)) { |
67
|
|
|
unset($_SESSION[$key]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function has($key) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$this->startSession(); |
74
|
|
|
|
75
|
|
|
return array_key_exists($key, $_SESSION); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function get($key) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$this->startSession(); |
81
|
|
|
if ($this->has($key)) { |
82
|
|
|
return $_SESSION[$key]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function destroy() |
89
|
|
|
{ |
90
|
|
|
$this->startSession(); |
91
|
|
|
session_destroy(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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: