-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsession.php
More file actions
240 lines (213 loc) · 4.85 KB
/
session.php
File metadata and controls
240 lines (213 loc) · 4.85 KB
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
236
237
238
239
240
<?php
/**
* LightPHP Framework
* LitePHP is a framework that has been designed to be lite waight, extensible and fast.
*
* @author Robert Pitt <robertpitt1988@gmail.com>
* @category core
* @copyright 2013 Robert Pitt
* @license GPL v3 - GNU Public License v3
* @version 1.0.0
*/
class Session_Library
{
/**
* Configuration Object
* @var object
*/
protected $_config;
/**
* Session Handler Interface
* @var SessionHandlerInterface
*/
protected $_handler;
/**
* Session Constructor
*/
public function __construct()
{
/**
* Fetch the session configuration
*/
$this->_config = Registry::get('ConfigLoader')->session;
/**
* Attempt to load the session
*/
if(!$this->_config->handler)
{
throw new Exception("A session driver is required within the confio/session.php");
}
/**
* Check that the driver exists
*/
if(!file_exists(__DIR__ . '/handlers/' . $this->_config->handler . '.php'))
{
throw new Exception("Unknown session driver: " . $this->_config->handler);
}
/**
* Load the driver
*/
require_once __DIR__ . '/handlers/' . $this->_config->handler . '.php';
/**
* Generate the class name
*/
$class = 'Session_Library_Driver_' . $this->_config->handler;
/**
* Check to see if the class exists
*/
if(!class_exists($class))
{
throw new Exception("Unable to load session, class name malformed: " . $this->_config->handler);
}
/**
* Instiatiate the handler
* @var SessionHandlerInterface
*/
$this->_handler = new $class();
/**
* Set the PHP session Save Handler
*/
session_set_save_handler($this->_handler);
/**
* Set the cache expire value
*/
if(isset($this->_config->expiration))
{
session_cache_expire((int)$this->_config->expiration);
}
/**
* Configure the session save path.
*/
if(!empty($this->_config->savepath))
{
session_save_path($this->_config->savepath);
}
/**
* Regenerate the session id if enabled
*/
if($this->_config->regenerate)
{
session_regenerate_id($this->_config->delete_old_session);
}
/**
* Set the session name.
*/
if(!empty($this->_config->name))
{
session_name($this->_config->name);
}
/**
* Set the session cookie parameters
*/
if(isset($this->_config->cookie_params) && is_array($this->_config->cookie_params))
{
session_set_cookie_params(
$this->_config->cookie_params['lifetime'],
$this->_config->cookie_params['path'],
$this->_config->cookie_params['domain'],
$this->_config->cookie_params['secure'],
$this->_config->cookie_params['httponly']
);
}
/**
* Start the session
*/
session_start($this->_config->name);
}
/**
* Return the session ID of the current client
* @return string Session Identifier
*/
public function id()
{
return session_id();
}
/**
* Set a value in the session
* @param string $key Index used to store the value.
* @param * $value A serializable value to be stored.
*/
public function set($key, $value, $namespace = "default")
{
/**
* Make sure the namespace exists
*/
$_SESSION[$namespace] = isset($_SESSION[$namespace]) ? $_SESSION[$namespace] : array();
/**
* Store the key/value in the session
*/
$_SESSION[$namespace][$key] = $value;
}
/**
* Get a value from the session store
* @param strint $key the index of the stored value
* @return *
*/
public function get($key, $namespace = "default")
{
if($this->exists($key, $namespace))
{
return $_SESSION[$namespace][$key];
}
return null;
}
/**
* Get a value from the session store
* @param strint $key the index of the stored value
* @return *
*/
public function exists($key, $namespace = "default")
{
return array_key_exists($namespace, $_SESSION) && array_key_exists($key, $_SESSION[$namespace]);
}
/**
* Remove a value from the session
* @param strint $key the index of the value to be removed
*/
public function remove($key, $namespace = "default")
{
unset($_SESSION[$namespace][$key]);
}
/**
* Destroy a session.
*/
public function destroy()
{
session_destroy();
}
/**
* Remove a value from the session
* @param strint $key the index of the value to be removed
*/
public function removeNamespace($namespace)
{
unset($_SESSION[$namespace]);
}
/**
* Returna value using the magic __get call
* @param string $key Index used to get the value
* @return *
*/
public function __get($key)
{
return $this->get($key);
}
/**
* Set a value using the __set call
* @param string $key Index used to store the value
* @param * $value
*/
public function __set($key, $value)
{
return $this->set($key, $value);
}
/**
* check if a value is set using the magic __isset call
* @param string $key Index used to store the value
* @param * $value
*/
public function __isset($key)
{
return $this->exists($key);
}
}