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