blob: 173b43710f1ddf6b267d0b29a8eb77be1e99ee70 [file] [log] [blame]
Andrey Andreev05afe3e2015-02-02 19:04:37 +02001<?php
Andrey Andreev47a47fb2014-05-31 16:08:30 +03002/**
3 * CodeIgniter
4 *
Andrey Andreevbf6b11d2015-01-12 17:27:12 +02005 * An open source application development framework for PHP
Andrey Andreev47a47fb2014-05-31 16:08:30 +03006 *
Andrey Andreev46f2f262014-11-11 14:37:51 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev47a47fb2014-05-31 16:08:30 +03008 *
Andrey Andreevbf6b11d2015-01-12 17:27:12 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreev47a47fb2014-05-31 16:08:30 +030010 *
Andrey Andreev46f2f262014-11-11 14:37:51 +020011 * 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:
Andrey Andreev47a47fb2014-05-31 16:08:30 +030017 *
Andrey Andreev46f2f262014-11-11 14:37:51 +020018 * 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
Andrey Andreev47a47fb2014-05-31 16:08:30 +030031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbf6b11d2015-01-12 17:27:12 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreev46f2f262014-11-11 14:37:51 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 3.0.0
Andrey Andreev47a47fb2014-05-31 16:08:30 +030036 * @filesource
Andrey Andreev46f2f262014-11-11 14:37:51 +020037*/
Andrey Andreev47a47fb2014-05-31 16:08:30 +030038defined('BASEPATH') OR exit('No direct script access allowed');
39
40/**
41 * CodeIgniter Session Files Driver
42 *
Andrey Andreev46f2f262014-11-11 14:37:51 +020043 * @package CodeIgniter
Andrey Andreev47a47fb2014-05-31 16:08:30 +030044 * @subpackage Libraries
45 * @category Sessions
Andrey Andreev46f2f262014-11-11 14:37:51 +020046 * @author Andrey Andreev
47 * @link http://codeigniter.com/user_guide/libraries/sessions.html
Andrey Andreev47a47fb2014-05-31 16:08:30 +030048 */
49class 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
79 // ------------------------------------------------------------------------
80
81 /**
82 * Class constructor
83 *
84 * @param array $params Configuration parameters
85 * @return void
86 */
87 public function __construct(&$params)
88 {
89 parent::__construct($params);
90
Andrey Andreevdfb39be2014-10-06 01:50:14 +030091 if (isset($this->_config['save_path']))
Andrey Andreev47a47fb2014-05-31 16:08:30 +030092 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +030093 $this->_config['save_path'] = rtrim($this->_config['save_path'], '/\\');
94 ini_set('session.save_path', $this->_config['save_path']);
Andrey Andreev47a47fb2014-05-31 16:08:30 +030095 }
96 else
97 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +030098 $this->_config['save_path'] = rtrim(ini_get('session.save_path'), '/\\');
Andrey Andreev47a47fb2014-05-31 16:08:30 +030099 }
100 }
101
102 // ------------------------------------------------------------------------
103
Andrey Andreev10411fc2015-01-19 13:54:53 +0200104 /**
105 * Open
106 *
107 * Sanitizes the save_path directory.
108 *
109 * @param string $save_path Path to session files' directory
Tom Atkinson388ce592015-02-04 17:54:52 +0100110 * @param string $name Session cookie name
Andrey Andreev10411fc2015-01-19 13:54:53 +0200111 * @return bool
112 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300113 public function open($save_path, $name)
114 {
Andrey Andreev5f4d01a2015-02-02 18:38:00 +0200115 if ( ! is_dir($save_path))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300116 {
Andrey Andreev5f4d01a2015-02-02 18:38:00 +0200117 if ( ! mkdir($save_path, 0700, TRUE))
118 {
119 throw new Exception("Session: Configured save path '".$this->_config['save_path']."' is not a directory, doesn't exist or cannot be created.");
120 }
121 }
122 elseif ( ! is_writable($save_path))
123 {
124 throw new Exception("Session: Configured save path '".$this->_config['save_path']."' is not writable by the PHP process.");
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300125 }
126
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300127 $this->_config['save_path'] = $save_path;
128 $this->_file_path = $this->_config['save_path'].DIRECTORY_SEPARATOR
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300129 .$name // we'll use the session cookie name as a prefix to avoid collisions
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300130 .($this->_config['match_ip'] ? md5($_SERVER['REMOTE_ADDR']) : '');
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300131
132 return TRUE;
133 }
134
135 // ------------------------------------------------------------------------
136
Andrey Andreev10411fc2015-01-19 13:54:53 +0200137 /**
138 * Read
139 *
140 * Reads session data and acquires a lock
141 *
142 * @param string $session_id Session ID
143 * @return string Serialized session data
144 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300145 public function read($session_id)
146 {
147 // This might seem weird, but PHP 5.6 introduces session_reset(),
148 // which re-reads session data
149 if ($this->_file_handle === NULL)
150 {
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300151 // Just using fopen() with 'c+b' mode would be perfect, but it is only
152 // available since PHP 5.2.6 and we have to set permissions for new files,
153 // so we'd have to hack around this ...
Andrey Andreev7474a672014-10-31 23:35:32 +0200154 if (($this->_file_new = ! file_exists($this->_file_path.$session_id)) === TRUE)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300155 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200156 if (($this->_file_handle = fopen($this->_file_path.$session_id, 'w+b')) === FALSE)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300157 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200158 log_message('error', "Session: File '".$this->_file_path.$session_id."' doesn't exist and cannot be created.");
Gabriel Potkány0dd25382015-02-04 12:15:06 +0100159 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300160 }
161 }
Andrey Andreev7474a672014-10-31 23:35:32 +0200162 elseif (($this->_file_handle = fopen($this->_file_path.$session_id, 'r+b')) === FALSE)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300163 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200164 log_message('error', "Session: Unable to open file '".$this->_file_path.$session_id."'.");
Gabriel Potkány0dd25382015-02-04 12:15:06 +0100165 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300166 }
167
168 if (flock($this->_file_handle, LOCK_EX) === FALSE)
169 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200170 log_message('error', "Session: Unable to obtain lock for file '".$this->_file_path.$session_id."'.");
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300171 fclose($this->_file_handle);
172 $this->_file_handle = NULL;
Gabriel Potkány0dd25382015-02-04 12:15:06 +0100173 return FALSE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300174 }
175
Andrey Andreev7474a672014-10-31 23:35:32 +0200176 // Needed by write() to detect session_regenerate_id() calls
177 $this->_session_id = $session_id;
178
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300179 if ($this->_file_new)
180 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200181 chmod($this->_file_path.$session_id, 0600);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300182 $this->_fingerprint = md5('');
183 return '';
184 }
185 }
Andrey Andreev8df6efd2015-12-11 17:55:55 +0200186 // We shouldn't need this, but apparently we do ...
187 // See https://github.com/bcit-ci/CodeIgniter/issues/4039
188 elseif ($this->_file_handler === FALSE)
189 {
190 return FALSE;
191 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300192 else
193 {
194 rewind($this->_file_handle);
195 }
196
197 $session_data = '';
Andrey Andreev7474a672014-10-31 23:35:32 +0200198 for ($read = 0, $length = filesize($this->_file_path.$session_id); $read < $length; $read += strlen($buffer))
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300199 {
200 if (($buffer = fread($this->_file_handle, $length - $read)) === FALSE)
201 {
202 break;
203 }
204
205 $session_data .= $buffer;
206 }
207
208 $this->_fingerprint = md5($session_data);
209 return $session_data;
210 }
211
Andrey Andreev10411fc2015-01-19 13:54:53 +0200212 // ------------------------------------------------------------------------
213
214 /**
215 * Write
216 *
217 * Writes (create / update) session data
218 *
219 * @param string $session_id Session ID
220 * @param string $session_data Serialized session data
221 * @return bool
222 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300223 public function write($session_id, $session_data)
224 {
Andrey Andreev7474a672014-10-31 23:35:32 +0200225 // If the two IDs don't match, we have a session_regenerate_id() call
226 // and we need to close the old handle and open a new one
227 if ($session_id !== $this->_session_id && ( ! $this->close() OR $this->read($session_id) === FALSE))
228 {
229 return FALSE;
230 }
231
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300232 if ( ! is_resource($this->_file_handle))
233 {
234 return FALSE;
235 }
236 elseif ($this->_fingerprint === md5($session_data))
237 {
238 return ($this->_file_new)
239 ? TRUE
Andrey Andreev7474a672014-10-31 23:35:32 +0200240 : touch($this->_file_path.$session_id);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300241 }
242
243 if ( ! $this->_file_new)
244 {
245 ftruncate($this->_file_handle, 0);
246 rewind($this->_file_handle);
247 }
248
Andrey Andreev5995e082014-06-03 15:33:51 +0300249 if (($length = strlen($session_data)) > 0)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300250 {
Andrey Andreev5995e082014-06-03 15:33:51 +0300251 for ($written = 0; $written < $length; $written += $result)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300252 {
Andrey Andreev5995e082014-06-03 15:33:51 +0300253 if (($result = fwrite($this->_file_handle, substr($session_data, $written))) === FALSE)
254 {
255 break;
256 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300257 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300258
Andrey Andreev5995e082014-06-03 15:33:51 +0300259 if ( ! is_int($result))
260 {
261 $this->_fingerprint = md5(substr($session_data, 0, $written));
262 log_message('error', 'Session: Unable to write data.');
263 return FALSE;
264 }
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300265 }
266
267 $this->_fingerprint = md5($session_data);
268 return TRUE;
269 }
270
271 // ------------------------------------------------------------------------
272
Andrey Andreev10411fc2015-01-19 13:54:53 +0200273 /**
274 * Close
275 *
276 * Releases locks and closes file descriptor.
277 *
Gabriel Potkány1fb50002015-02-04 01:45:59 +0100278 * @return bool
Andrey Andreev10411fc2015-01-19 13:54:53 +0200279 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300280 public function close()
281 {
282 if (is_resource($this->_file_handle))
283 {
284 flock($this->_file_handle, LOCK_UN);
285 fclose($this->_file_handle);
286
Andrey Andreev7474a672014-10-31 23:35:32 +0200287 $this->_file_handle = $this->_file_new = $this->_session_id = NULL;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300288 return TRUE;
289 }
290
Andrey Andreev7474a672014-10-31 23:35:32 +0200291 return TRUE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300292 }
293
294 // ------------------------------------------------------------------------
295
Andrey Andreev10411fc2015-01-19 13:54:53 +0200296 /**
297 * Destroy
298 *
299 * Destroys the current session.
300 *
301 * @param string $session_id Session ID
302 * @return bool
303 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300304 public function destroy($session_id)
305 {
306 if ($this->close())
307 {
Andrey Andreevc519b262015-02-21 19:20:03 +0200308 return file_exists($this->_file_path.$session_id)
309 ? (unlink($this->_file_path.$session_id) && $this->_cookie_destroy())
310 : TRUE;
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300311 }
312 elseif ($this->_file_path !== NULL)
313 {
314 clearstatcache();
Andrey Andreev7474a672014-10-31 23:35:32 +0200315 return file_exists($this->_file_path.$session_id)
316 ? (unlink($this->_file_path.$session_id) && $this->_cookie_destroy())
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300317 : TRUE;
318 }
319
320 return FALSE;
321 }
322
323 // ------------------------------------------------------------------------
324
Andrey Andreev10411fc2015-01-19 13:54:53 +0200325 /**
326 * Garbage Collector
327 *
328 * Deletes expired sessions
329 *
330 * @param int $maxlifetime Maximum lifetime of sessions
331 * @return bool
332 */
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300333 public function gc($maxlifetime)
334 {
Andrey Andreev2f79f9a2015-03-26 12:52:05 +0200335 if ( ! is_dir($this->_config['save_path']) OR ($directory = opendir($this->_config['save_path'])) === FALSE)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300336 {
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300337 log_message('debug', "Session: Garbage collector couldn't list files under directory '".$this->_config['save_path']."'.");
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300338 return FALSE;
339 }
340
341 $ts = time() - $maxlifetime;
342
Tom Atkinson388ce592015-02-04 17:54:52 +0100343 $pattern = sprintf(
344 '/^%s[0-9a-f]{%d}$/',
345 preg_quote($this->_config['cookie_name'], '/'),
346 ($this->_config['match_ip'] === TRUE ? 72 : 40)
347 );
348
Andrey Andreev2f79f9a2015-03-26 12:52:05 +0200349 while (($file = readdir($directory)) !== FALSE)
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300350 {
351 // If the filename doesn't match this pattern, it's either not a session file or is not ours
Tom Atkinson388ce592015-02-04 17:54:52 +0100352 if ( ! preg_match($pattern, $file)
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300353 OR ! is_file($this->_config['save_path'].DIRECTORY_SEPARATOR.$file)
Andrey Andreevcd489612014-10-27 16:09:01 +0200354 OR ($mtime = filemtime($this->_config['save_path'].DIRECTORY_SEPARATOR.$file)) === FALSE
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300355 OR $mtime > $ts)
356 {
357 continue;
358 }
359
Andrey Andreevdfb39be2014-10-06 01:50:14 +0300360 unlink($this->_config['save_path'].DIRECTORY_SEPARATOR.$file);
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300361 }
362
Andrey Andreev2f79f9a2015-03-26 12:52:05 +0200363 closedir($directory);
364
Andrey Andreev47a47fb2014-05-31 16:08:30 +0300365 return TRUE;
366 }
367
368}