1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework\Libraries; |
3
|
|
|
|
4
|
|
|
use WebServCo\Framework\Settings as S; |
5
|
|
|
|
6
|
|
|
final class Session extends \WebServCo\Framework\AbstractLibrary |
7
|
|
|
{ |
8
|
|
|
protected function checkSession() |
9
|
|
|
{ |
10
|
|
|
if (session_status() === \PHP_SESSION_NONE) { |
11
|
|
|
throw new \ErrorException( |
12
|
|
|
'Session is not started.' |
13
|
|
|
); |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function start($storagePath = null) |
18
|
|
|
{ |
19
|
|
|
if (session_status() === \PHP_SESSION_ACTIVE) { |
20
|
|
|
throw new \ErrorException( |
21
|
|
|
'Could not start session, already started.' |
22
|
|
|
); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set cache limiter. |
27
|
|
|
*/ |
28
|
|
|
session_cache_limiter('public, must-revalidate'); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set cache expire (minutes). |
32
|
|
|
*/ |
33
|
|
|
session_cache_expire($this->setting('expire', '36000') / 60); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set garbage collector timeout (seconds). |
37
|
|
|
*/ |
38
|
|
|
ini_set('session.gc_maxlifetime', $this->setting('expire', '36000')); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set custom session storage path. |
42
|
|
|
*/ |
43
|
|
|
if (!empty($storagePath)) { |
44
|
|
|
ini_set('session.save_path', $storagePath); |
45
|
|
|
session_save_path($storagePath); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Make sure garbage collector visits us. |
50
|
|
|
*/ |
51
|
|
|
ini_set('session.gc_probability', 1); |
52
|
|
|
|
53
|
|
|
session_set_cookie_params( |
54
|
|
|
$this->setting(sprintf('cookie%slifetime', S::DIVIDER), 60 * 60 * 24 * 14), |
55
|
|
|
$this->setting(sprintf('cookie%spath', S::DIVIDER), '/'), |
56
|
|
|
$this->setting(sprintf('cookie%sdomain', S::DIVIDER), ''), |
57
|
|
|
$this->setting(sprintf('cookie%ssecure', S::DIVIDER), false), |
58
|
|
|
$this->setting(sprintf('cookie%shttponly', S::DIVIDER), true) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
session_name('webservco'); |
62
|
|
|
|
63
|
|
|
if (!session_start()) { |
64
|
|
|
throw new \ErrorException('Failed to start session'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function set($setting, $value) |
71
|
|
|
{ |
72
|
|
|
$this->checkSession(); |
73
|
|
|
|
74
|
|
|
$_SESSION = \WebServCo\Framework\ArrayStorage::set( |
75
|
|
|
$_SESSION, |
76
|
|
|
$setting, |
77
|
|
|
$value |
78
|
|
|
); |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function get($setting, $defaultValue = false) |
83
|
|
|
{ |
84
|
|
|
$this->checkSession(); |
85
|
|
|
|
86
|
|
|
return \WebServCo\Framework\ArrayStorage::get( |
87
|
|
|
$_SESSION, |
88
|
|
|
$setting, |
89
|
|
|
$defaultValue |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function has($setting) |
94
|
|
|
{ |
95
|
|
|
return (bool) $this->get($setting); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function remove($setting) |
99
|
|
|
{ |
100
|
|
|
$this->checkSession(); |
101
|
|
|
|
102
|
|
|
$_SESSION = \WebServCo\Framework\ArrayStorage::remove( |
103
|
|
|
$_SESSION, |
104
|
|
|
$setting |
105
|
|
|
); |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function clear($setting) |
110
|
|
|
{ |
111
|
|
|
return $this->set($setting, null); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|