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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * CodeIgniter Native Session Library * * @package Session * @subpackage Libraries * @category Session * @author Bo-Yi Wu (appleboy) <appleboy.tw@gmail.com> * @author Marko Martinović <marko@techytalk.info> */
class Session { protected $sess_namespace = ''; protected $ci; protected $store = array(); protected $flashdata_key = 'flash'; private $_config = array();
/** * Constructor * * @access public * @param array config preferences * * @return void **/ public function __construct() { $this->ci = get_instance(); // Default to 2 years if expiration is "0" $this->_expiration = 60 * 60 * 24 * 365 * 2;
$this->initialize();
// Delete 'old' flashdata (from last request) $this->_flashdata_sweep();
// Mark all new flashdata as old (data will be deleted before next request) $this->_flashdata_mark(); }
/** * Initialize the configuration options * * @access private * @return void */ private function initialize() { $this->ci->load->config('session');
$this->_config = array(); $prefs = array( 'sess_cookie_name', 'sess_expire_on_close', 'sess_expiration', 'sess_match_ip', 'sess_match_useragent', 'sess_time_to_update', 'cookie_prefix', 'cookie_path', 'cookie_domain', 'cookie_secure', 'cookie_httponly' );
foreach ($prefs as $key) { $this->_config[$key] = $this->ci->config->item($key); }
$this->_config = array_merge( array( 'sess_namespace' => $this->ci->config->item('sess_namespace') ), $this->_config );
foreach ($this->_config as $key => $val) { if (method_exists($this, 'set_'.$key)) { $this->{'set_'.$key}($val); } elseif (isset($this->$key)) { $this->$key = $val; } }
// Set expiration, path, and domain $expire = 7200; $path = '/'; $domain = ''; $secure = (bool) $this->_config['cookie_secure']; $http_only = (bool) $this->_config['cookie_httponly'];
if ($this->_config['sess_expiration'] !== false) { // Default to 2 years if expiration is "0" $expire = ($this->_config['sess_expiration'] == 0) ? $this->_expiration : $this->_config['sess_expiration']; }
if ($this->_config['cookie_path']) { // Use specified path $path = $this->_config['cookie_path']; }
if ($this->_config['cookie_domain']) { // Use specified domain $domain = $this->_config['cookie_domain']; }
session_set_cookie_params($this->_config['sess_expire_on_close'] ? 0 : $expire, $path, $domain, $secure, $http_only);
if ( ! isset($_SESSION)) { session_start(); }
if (isset($_SESSION[$this->sess_namespace]) ) { $this->store = $_SESSION[$this->sess_namespace]; }
$destroy = false; $now = time();
$last_activity = $this->userdata('last_activity'); $ip_address = $this->userdata('ip_address'); $user_agent = $this->userdata('user_agent'); if (!empty($last_activity) && (($last_activity + $expire) < $now or $last_activity > $now)) { // Expired - destroy log_message('debug', 'Session: Expired'); $destroy = true; } elseif ($this->_config['sess_match_ip'] === true && !empty($ip_address) && $ip_address !== $this->ci->input->ip_address()) { // IP doesn't match - destroy log_message('debug', 'Session: IP address mismatch'); $destroy = true; } elseif ($this->_config['sess_match_useragent'] === true && !empty($user_agent) && $user_agent !== trim(substr($this->ci->input->user_agent(), 0, 50))) { // Agent doesn't match - destroy log_message('debug', 'Session: User Agent string mismatch'); $destroy = true; }
// update last activity time $this->set_userdata('last_activity', $now);
if (!$destroy) { return; }
$this->sess_create(); }
/** * Create Session * * @access public * @return void */ public function sess_create() { // Send a new session id to client session_regenerate_id(); $_SESSION[$this->sess_namespace] = array( 'session_id' => md5(microtime()), 'last_activity' => time() );
// Set matching values as required if ($this->_config['sess_match_ip'] === true) { // Store user IP address $_SESSION[$this->sess_namespace]['ip_address'] = $this->ci->input->ip_address(); }
if ($this->_config['sess_match_useragent'] === true) { // Store user agent string $_SESSION[$this->sess_namespace]['user_agent'] = trim(substr($this->ci->input->user_agent(), 0, 50)); }
$this->store = $_SESSION[$this->sess_namespace]; }
/** * Destroy session * * @access public */ public function sess_destroy() { // get session name. $name = session_name(); if (isset($_COOKIE[$name])) { // Clear session cookie $params = session_get_cookie_params(); setcookie($name, '', time() - 42000, $params['path'], $params['domain'], $params['secure'], $params['httponly']); unset($_COOKIE[$name]); }
$this->sess_create(); }
/** * Get specific user data element * * @access public * @param string element key * @return object element value */ public function userdata($value) { if (isset($this->store[$value])) { return $this->store[$value]; } else { return false; } }
/** * Set value for specific user data element * * @access public * @param array list of data to be stored * @param object value to be stored if only one element is passed * @return void */ public function set_userdata($data = array(), $value = '') { if (is_string($data)) { $data = array($data => $value); } foreach ($data as $key => $val) { $this->store[$key] = $val; } $_SESSION[$this->sess_namespace] = $this->store; }
/** * remove array value for specific user data element * * @access public * @param array list of data to be removed * @return void */ public function unset_userdata($data = array()) { if (is_string($data)) { $data = array($data => ''); }
if (count($data) > 0) { foreach ($data as $key => $val) { unset($this->store[$key]); } }
$_SESSION[$this->sess_namespace] = $this->store; }
/** * Fetch all session data * * @access public * @return array */ public function all_userdata() { return $this->store; }
/** * Add or change flashdata, only available * until the next request * * @access public * @param mixed * @param string * @return void */ public function set_flashdata($newdata = array(), $newval = '') { if (is_string($newdata)) { $newdata = array($newdata => $newval); }
if (count($newdata) > 0) { foreach ($newdata as $key => $val) { $flashdata_key = $this->flashdata_key.':new:'.$key; $this->set_userdata($flashdata_key, $val); } } }
/** * Keeps existing flashdata available to next request. * * @access public * @param string * @return void */ public function keep_flashdata($key) { // 'old' flashdata gets removed. Here we mark all // flashdata as 'new' to preserve it from _flashdata_sweep() // Note the function will return false if the $key // provided cannot be found $old_flashdata_key = $this->flashdata_key.':old:'.$key; $value = $this->userdata($old_flashdata_key);
$new_flashdata_key = $this->flashdata_key.':new:'.$key; $this->set_userdata($new_flashdata_key, $value); }
/** * Fetch a specific flashdata item from the session array * * @access public * @param string * @return string */ public function flashdata($key) { $flashdata_key = $this->flashdata_key.':old:'.$key; return $this->userdata($flashdata_key); }
/** * Identifies flashdata as 'old' for removal * when _flashdata_sweep() runs. * * @access private * @return void */ private function _flashdata_mark() { $userdata = $this->all_userdata(); foreach ($userdata as $name => $value) { $parts = explode(':new:', $name); if (is_array($parts) && count($parts) === 2) { $new_name = $this->flashdata_key.':old:'.$parts[1]; $this->set_userdata($new_name, $value); $this->unset_userdata($name); } } }
/** * Removes all flashdata marked as 'old' * * @access private * @return void */ private function _flashdata_sweep() { $userdata = $this->all_userdata(); foreach ($userdata as $key => $value) { if (strpos($key, ':old:')) { $this->unset_userdata($key); } } } }
|