| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2257 |
lars |
9 |
* Copyright (c) 2014 - 2018, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
11 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
12 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
13 |
* in the Software without restriction, including without limitation the rights
|
|
|
14 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
15 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
16 |
* furnished to do so, subject to the following conditions:
|
|
|
17 |
*
|
|
|
18 |
* The above copyright notice and this permission notice shall be included in
|
|
|
19 |
* all copies or substantial portions of the Software.
|
|
|
20 |
*
|
|
|
21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
24 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
26 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
27 |
* THE SOFTWARE.
|
|
|
28 |
*
|
|
|
29 |
* @package CodeIgniter
|
|
|
30 |
* @author EllisLab Dev Team
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2257 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
|
| 68 |
lars |
33 |
* @license http://opensource.org/licenses/MIT MIT License
|
|
|
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 3.0.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* CodeIgniter Session Files Driver
|
|
|
42 |
*
|
|
|
43 |
* @package CodeIgniter
|
|
|
44 |
* @subpackage Libraries
|
|
|
45 |
* @category Sessions
|
|
|
46 |
* @author Andrey Andreev
|
|
|
47 |
* @link https://codeigniter.com/user_guide/libraries/sessions.html
|
|
|
48 |
*/
|
|
|
49 |
class CI_Session_files_driver extends CI_Session_driver implements SessionHandlerInterface {
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Save path
|
|
|
53 |
*
|
|
|
54 |
* @var string
|
|
|
55 |
*/
|
|
|
56 |
protected $_save_path;
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* File handle
|
|
|
60 |
*
|
|
|
61 |
* @var resource
|
|
|
62 |
*/
|
|
|
63 |
protected $_file_handle;
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* File name
|
|
|
67 |
*
|
|
|
68 |
* @var resource
|
|
|
69 |
*/
|
|
|
70 |
protected $_file_path;
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* File new flag
|
|
|
74 |
*
|
|
|
75 |
* @var bool
|
|
|
76 |
*/
|
|
|
77 |
protected $_file_new;
|
|
|
78 |
|
| 1257 |
lars |
79 |
/**
|
|
|
80 |
* Validate SID regular expression
|
|
|
81 |
*
|
|
|
82 |
* @var string
|
|
|
83 |
*/
|
|
|
84 |
protected $_sid_regexp;
|
|
|
85 |
|
|
|
86 |
/**
|
| 2107 |
lars |
87 |
* mbstring.func_overload flag
|
| 1257 |
lars |
88 |
*
|
|
|
89 |
* @var bool
|
|
|
90 |
*/
|
| 2107 |
lars |
91 |
protected static $func_overload;
|
| 1257 |
lars |
92 |
|
| 68 |
lars |
93 |
// ------------------------------------------------------------------------
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Class constructor
|
|
|
97 |
*
|
|
|
98 |
* @param array $params Configuration parameters
|
|
|
99 |
* @return void
|
|
|
100 |
*/
|
|
|
101 |
public function __construct(&$params)
|
|
|
102 |
{
|
|
|
103 |
parent::__construct($params);
|
|
|
104 |
|
|
|
105 |
if (isset($this->_config['save_path']))
|
|
|
106 |
{
|
|
|
107 |
$this->_config['save_path'] = rtrim($this->_config['save_path'], '/\\');
|
|
|
108 |
ini_set('session.save_path', $this->_config['save_path']);
|
|
|
109 |
}
|
|
|
110 |
else
|
|
|
111 |
{
|
|
|
112 |
log_message('debug', 'Session: "sess_save_path" is empty; using "session.save_path" value from php.ini.');
|
|
|
113 |
$this->_config['save_path'] = rtrim(ini_get('session.save_path'), '/\\');
|
|
|
114 |
}
|
| 1257 |
lars |
115 |
|
|
|
116 |
$this->_sid_regexp = $this->_config['_sid_regexp'];
|
|
|
117 |
|
| 2107 |
lars |
118 |
isset(self::$func_overload) OR self::$func_overload = (extension_loaded('mbstring') && ini_get('mbstring.func_overload'));
|
| 68 |
lars |
119 |
}
|
|
|
120 |
|
|
|
121 |
// ------------------------------------------------------------------------
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Open
|
|
|
125 |
*
|
|
|
126 |
* Sanitizes the save_path directory.
|
|
|
127 |
*
|
|
|
128 |
* @param string $save_path Path to session files' directory
|
|
|
129 |
* @param string $name Session cookie name
|
|
|
130 |
* @return bool
|
|
|
131 |
*/
|
|
|
132 |
public function open($save_path, $name)
|
|
|
133 |
{
|
|
|
134 |
if ( ! is_dir($save_path))
|
|
|
135 |
{
|
|
|
136 |
if ( ! mkdir($save_path, 0700, TRUE))
|
|
|
137 |
{
|
|
|
138 |
throw new Exception("Session: Configured save path '".$this->_config['save_path']."' is not a directory, doesn't exist or cannot be created.");
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
elseif ( ! is_writable($save_path))
|
|
|
142 |
{
|
|
|
143 |
throw new Exception("Session: Configured save path '".$this->_config['save_path']."' is not writable by the PHP process.");
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
$this->_config['save_path'] = $save_path;
|
|
|
147 |
$this->_file_path = $this->_config['save_path'].DIRECTORY_SEPARATOR
|
|
|
148 |
.$name // we'll use the session cookie name as a prefix to avoid collisions
|
|
|
149 |
.($this->_config['match_ip'] ? md5($_SERVER['REMOTE_ADDR']) : '');
|
|
|
150 |
|
|
|
151 |
return $this->_success;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
// ------------------------------------------------------------------------
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Read
|
|
|
158 |
*
|
|
|
159 |
* Reads session data and acquires a lock
|
|
|
160 |
*
|
|
|
161 |
* @param string $session_id Session ID
|
|
|
162 |
* @return string Serialized session data
|
|
|
163 |
*/
|
|
|
164 |
public function read($session_id)
|
|
|
165 |
{
|
|
|
166 |
// This might seem weird, but PHP 5.6 introduces session_reset(),
|
|
|
167 |
// which re-reads session data
|
|
|
168 |
if ($this->_file_handle === NULL)
|
|
|
169 |
{
|
| 1257 |
lars |
170 |
$this->_file_new = ! file_exists($this->_file_path.$session_id);
|
|
|
171 |
|
|
|
172 |
if (($this->_file_handle = fopen($this->_file_path.$session_id, 'c+b')) === FALSE)
|
| 68 |
lars |
173 |
{
|
|
|
174 |
log_message('error', "Session: Unable to open file '".$this->_file_path.$session_id."'.");
|
|
|
175 |
return $this->_failure;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
if (flock($this->_file_handle, LOCK_EX) === FALSE)
|
|
|
179 |
{
|
|
|
180 |
log_message('error', "Session: Unable to obtain lock for file '".$this->_file_path.$session_id."'.");
|
|
|
181 |
fclose($this->_file_handle);
|
|
|
182 |
$this->_file_handle = NULL;
|
|
|
183 |
return $this->_failure;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
// Needed by write() to detect session_regenerate_id() calls
|
|
|
187 |
$this->_session_id = $session_id;
|
|
|
188 |
|
|
|
189 |
if ($this->_file_new)
|
|
|
190 |
{
|
|
|
191 |
chmod($this->_file_path.$session_id, 0600);
|
|
|
192 |
$this->_fingerprint = md5('');
|
|
|
193 |
return '';
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
// We shouldn't need this, but apparently we do ...
|
|
|
197 |
// See https://github.com/bcit-ci/CodeIgniter/issues/4039
|
|
|
198 |
elseif ($this->_file_handle === FALSE)
|
|
|
199 |
{
|
|
|
200 |
return $this->_failure;
|
|
|
201 |
}
|
|
|
202 |
else
|
|
|
203 |
{
|
|
|
204 |
rewind($this->_file_handle);
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
$session_data = '';
|
| 1257 |
lars |
208 |
for ($read = 0, $length = filesize($this->_file_path.$session_id); $read < $length; $read += self::strlen($buffer))
|
| 68 |
lars |
209 |
{
|
|
|
210 |
if (($buffer = fread($this->_file_handle, $length - $read)) === FALSE)
|
|
|
211 |
{
|
|
|
212 |
break;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
$session_data .= $buffer;
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
$this->_fingerprint = md5($session_data);
|
|
|
219 |
return $session_data;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
// ------------------------------------------------------------------------
|
|
|
223 |
|
|
|
224 |
/**
|
|
|
225 |
* Write
|
|
|
226 |
*
|
|
|
227 |
* Writes (create / update) session data
|
|
|
228 |
*
|
|
|
229 |
* @param string $session_id Session ID
|
|
|
230 |
* @param string $session_data Serialized session data
|
|
|
231 |
* @return bool
|
|
|
232 |
*/
|
|
|
233 |
public function write($session_id, $session_data)
|
|
|
234 |
{
|
|
|
235 |
// If the two IDs don't match, we have a session_regenerate_id() call
|
|
|
236 |
// and we need to close the old handle and open a new one
|
|
|
237 |
if ($session_id !== $this->_session_id && ($this->close() === $this->_failure OR $this->read($session_id) === $this->_failure))
|
|
|
238 |
{
|
|
|
239 |
return $this->_failure;
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
if ( ! is_resource($this->_file_handle))
|
|
|
243 |
{
|
|
|
244 |
return $this->_failure;
|
|
|
245 |
}
|
|
|
246 |
elseif ($this->_fingerprint === md5($session_data))
|
|
|
247 |
{
|
|
|
248 |
return ( ! $this->_file_new && ! touch($this->_file_path.$session_id))
|
|
|
249 |
? $this->_failure
|
|
|
250 |
: $this->_success;
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
if ( ! $this->_file_new)
|
|
|
254 |
{
|
|
|
255 |
ftruncate($this->_file_handle, 0);
|
|
|
256 |
rewind($this->_file_handle);
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
if (($length = strlen($session_data)) > 0)
|
|
|
260 |
{
|
|
|
261 |
for ($written = 0; $written < $length; $written += $result)
|
|
|
262 |
{
|
|
|
263 |
if (($result = fwrite($this->_file_handle, substr($session_data, $written))) === FALSE)
|
|
|
264 |
{
|
|
|
265 |
break;
|
|
|
266 |
}
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
if ( ! is_int($result))
|
|
|
270 |
{
|
|
|
271 |
$this->_fingerprint = md5(substr($session_data, 0, $written));
|
|
|
272 |
log_message('error', 'Session: Unable to write data.');
|
|
|
273 |
return $this->_failure;
|
|
|
274 |
}
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
$this->_fingerprint = md5($session_data);
|
|
|
278 |
return $this->_success;
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
// ------------------------------------------------------------------------
|
|
|
282 |
|
|
|
283 |
/**
|
|
|
284 |
* Close
|
|
|
285 |
*
|
|
|
286 |
* Releases locks and closes file descriptor.
|
|
|
287 |
*
|
|
|
288 |
* @return bool
|
|
|
289 |
*/
|
|
|
290 |
public function close()
|
|
|
291 |
{
|
|
|
292 |
if (is_resource($this->_file_handle))
|
|
|
293 |
{
|
|
|
294 |
flock($this->_file_handle, LOCK_UN);
|
|
|
295 |
fclose($this->_file_handle);
|
|
|
296 |
|
|
|
297 |
$this->_file_handle = $this->_file_new = $this->_session_id = NULL;
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
return $this->_success;
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
// ------------------------------------------------------------------------
|
|
|
304 |
|
|
|
305 |
/**
|
|
|
306 |
* Destroy
|
|
|
307 |
*
|
|
|
308 |
* Destroys the current session.
|
|
|
309 |
*
|
|
|
310 |
* @param string $session_id Session ID
|
|
|
311 |
* @return bool
|
|
|
312 |
*/
|
|
|
313 |
public function destroy($session_id)
|
|
|
314 |
{
|
|
|
315 |
if ($this->close() === $this->_success)
|
|
|
316 |
{
|
|
|
317 |
if (file_exists($this->_file_path.$session_id))
|
|
|
318 |
{
|
|
|
319 |
$this->_cookie_destroy();
|
|
|
320 |
return unlink($this->_file_path.$session_id)
|
|
|
321 |
? $this->_success
|
|
|
322 |
: $this->_failure;
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
return $this->_success;
|
|
|
326 |
}
|
|
|
327 |
elseif ($this->_file_path !== NULL)
|
|
|
328 |
{
|
|
|
329 |
clearstatcache();
|
|
|
330 |
if (file_exists($this->_file_path.$session_id))
|
|
|
331 |
{
|
|
|
332 |
$this->_cookie_destroy();
|
|
|
333 |
return unlink($this->_file_path.$session_id)
|
|
|
334 |
? $this->_success
|
|
|
335 |
: $this->_failure;
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
return $this->_success;
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
return $this->_failure;
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
// ------------------------------------------------------------------------
|
|
|
345 |
|
|
|
346 |
/**
|
|
|
347 |
* Garbage Collector
|
|
|
348 |
*
|
|
|
349 |
* Deletes expired sessions
|
|
|
350 |
*
|
|
|
351 |
* @param int $maxlifetime Maximum lifetime of sessions
|
|
|
352 |
* @return bool
|
|
|
353 |
*/
|
|
|
354 |
public function gc($maxlifetime)
|
|
|
355 |
{
|
|
|
356 |
if ( ! is_dir($this->_config['save_path']) OR ($directory = opendir($this->_config['save_path'])) === FALSE)
|
|
|
357 |
{
|
|
|
358 |
log_message('debug', "Session: Garbage collector couldn't list files under directory '".$this->_config['save_path']."'.");
|
|
|
359 |
return $this->_failure;
|
|
|
360 |
}
|
|
|
361 |
|
|
|
362 |
$ts = time() - $maxlifetime;
|
|
|
363 |
|
| 1257 |
lars |
364 |
$pattern = ($this->_config['match_ip'] === TRUE)
|
|
|
365 |
? '[0-9a-f]{32}'
|
|
|
366 |
: '';
|
|
|
367 |
|
| 68 |
lars |
368 |
$pattern = sprintf(
|
| 1257 |
lars |
369 |
'#\A%s'.$pattern.$this->_sid_regexp.'\z#',
|
|
|
370 |
preg_quote($this->_config['cookie_name'])
|
| 68 |
lars |
371 |
);
|
|
|
372 |
|
|
|
373 |
while (($file = readdir($directory)) !== FALSE)
|
|
|
374 |
{
|
|
|
375 |
// If the filename doesn't match this pattern, it's either not a session file or is not ours
|
|
|
376 |
if ( ! preg_match($pattern, $file)
|
|
|
377 |
OR ! is_file($this->_config['save_path'].DIRECTORY_SEPARATOR.$file)
|
|
|
378 |
OR ($mtime = filemtime($this->_config['save_path'].DIRECTORY_SEPARATOR.$file)) === FALSE
|
|
|
379 |
OR $mtime > $ts)
|
|
|
380 |
{
|
|
|
381 |
continue;
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
unlink($this->_config['save_path'].DIRECTORY_SEPARATOR.$file);
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
closedir($directory);
|
|
|
388 |
|
|
|
389 |
return $this->_success;
|
|
|
390 |
}
|
|
|
391 |
|
| 1257 |
lars |
392 |
// --------------------------------------------------------------------
|
|
|
393 |
|
|
|
394 |
/**
|
|
|
395 |
* Byte-safe strlen()
|
|
|
396 |
*
|
|
|
397 |
* @param string $str
|
|
|
398 |
* @return int
|
|
|
399 |
*/
|
|
|
400 |
protected static function strlen($str)
|
|
|
401 |
{
|
| 2107 |
lars |
402 |
return (self::$func_overload)
|
| 1257 |
lars |
403 |
? mb_strlen($str, '8bit')
|
|
|
404 |
: strlen($str);
|
|
|
405 |
}
|
|
|
406 |
}
|