forked from scandio/lmvc-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession.php
235 lines (200 loc) · 7.36 KB
/
Session.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace Scandio\lmvc\modules\session;
/**
* Class Session
* @package Scandio\lmvc\modules\session
*
* Class abstraction from session handling so that nobody needs to modify the global $_SESSION
* varibale. Offers a few simple helper methods to interact with the the user's session.
*/
class Session
{
protected static
$started = false;
/**
* Starts the session if it has not been started yet.
*
* @return bool indicating result of session start.
*/
public static function start()
{
if (static::$started) { return true; }
static::$started = session_start();
return static::$started;
}
/**
* Stops / destroys the session.
*
* @return bool result of closing session.
*/
public static function stop($unset = true)
{
if (static::$started) { static::$started = session_destroy(); }
if ($unset) { session_unset(); }
return static::$started;
}
/**
* Sets a value $value at $attr via dot-notation.
*
* @param string $attr in dot-notation to the session's value to be set
* @param mixed $value value to be set at $attr
* @param bool $serialize boolean indicating if value should be serialized
*/
public static function set($attr, $value, $serialize = false)
{
$value = ( $serialize === false ) ? $value : serialize($value);
static::setByDotNotation($attr, $value);
return $value;
}
/**
* Gets a value at $attr via dot-notation.
*
* @param string $attr in dot-notation to the session's value to be set
* @param mixed $default value that will be returned if the stored value is null
* @param bool $serialized boolean indicating if value was serialized
*
* @return mixed the value behind $attr or the $default value if nothing was set at $attr
*/
public static function get($attr, $default = null, $serialized = false)
{
$ordinary = static::resolveByDotNotation($attr);
if ($ordinary === null) {
return $default;
} else {
return $serialized === false ? $ordinary : unserialize($ordinary);
}
}
/**
* Backups a value at $attr via dot-notation:
* - if an empty value is passed, the backup is returned
* - otherwise the value is stored in the session and returned
* - if an empty value is passed and the backup is null, the default will be returned
*
* @param string $attr in dot-notation to the session's value to backup
* @param mixed $value value to backup
* @param mixed $default value that will be returned as default
* @param bool $serialized boolean indication if value is stored serialized
* @return mixed determined value
*/
public static function backup($attr, $value, $default = null, $serialized = false)
{
return (empty($value) ? static::get($attr, $default, $serialized) : static::set($attr, $value, $serialized));
}
/**
* Backups a value at $attr via dot-notation:
* - if null or an unset value is passed, the backup is returned
* - otherwise the value is stored in the session and returned
* - if an unset value is passed and the backup is null, the default will be returned
*
* Needed for backing-up Numbers, as '0' is recognized as 'empty' (in Session::backup)
*
* @param string $attr in dot-notation to the session's value to backup
* @param mixed $value value to backup
* @param mixed $default value that will be returned as default
* @param bool $serialized boolean indication if value is stored serialized
* @return mixed determined value
*/
public static function backupNumber($attr, $value, $default = null, $serialized = false)
{
return (!isset($value) ? static::get($attr, $default, $serialized) : static::set($attr, $value, $serialized));
}
/**
* Recursively replaces all values in session by key and value.
*
* @param array $array nested array containing values to be set in session.
*/
public static function replace($array)
{
$_SESSION = array_replace_recursive($_SESSION, $array);
}
/**
* Recursively merges all values in session by key and value.
*
* @param array $array nested array containing values to merged into session.
*/
public static function merge($array)
{
$_SESSION = array_merge_recursive($_SESSION, $array);
}
/**
* Checks if a value is set in the session and returns a bool indicator not the actual value.
*
* @param $key requested to be possibly set in session.
*/
public static function has($key)
{
return ( static::resolveByDotNotation($key) !== null );
}
/**
* Flushes the whole session data.
*/
public static function flush()
{
static::setByDotNotation(null, []);
}
/**
* Regenrates the session and its id.
*
* @param bool $flush indicating if the session should also be flushed (true by default)
* @param int|string $lifetime of the session in seconds (null by default)
*/
public static function regenerate($flush = true, $lifetime = null)
{
if ($lifetime !== null) {
ini_set('session.cookie_lifetime', $lifetime);
}
$response = session_regenerate_id($flush);
session_write_close();
$backup = $flush ? [] : $_SESSION;
static::$started = session_start();
$_SESSION = $backup;
return $response;
}
/**
* A little helper resolving dot-notation at an array.
*
* @param $dot notation returning the value at the last pointer of the dot-notation.
* @param array $array to be accessed.
*/
private static function resolveByDotNotation($dot, $scalar = false)
{
if (!static::$started) { return null; }
# Explode the $def string to array by "."
$exploded = explode(".", $dot);
# Initial pointer to array is the private array variable
$arrPointer = $_SESSION;
# Each subsequent dot-value
foreach ($exploded as $explode) {
# The value if set, so goto new array pointer
if ( isset($arrPointer[$explode]) ) {
$arrPointer = $arrPointer[$explode];
# Not found the value by dot-notation at given loop run so return null
} else {
return null;
}
# Finished iterating the array and the value array pointer is pointing to is not another array (except you want a non scalar value)...
if ( end($exploded) == $explode && ($scalar === false || !is_array($arrPointer)) ) {
return $arrPointer;
}
}
}
/**
* Sets a value in the $_SESSION array by dot notation.
*
* @param $dot notation for value to be set
* @param $value value to be set
*/
private static function setByDotNotation($dot, $value)
{
if ($dot == null) { $_SESSION = $value; }
$exploded = explode('.', $dot);
$valuePointer = &$_SESSION;
# Loop until end of explodes and reset $_SESSION pointer to $valuePointer by reference
$length = count($exploded);
for($i = 0; $i < $length; $i++){
$key = $exploded[$i];
$valuePointer = &$valuePointer[$key];
}
$valuePointer = $value;
}
}