Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 1 | <?php |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 5.2.4 or newer |
| 6 | * |
| 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
| 19 | * @package CodeIgniter |
| 20 | * @author Andrey Andreev |
| 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
| 23 | * @link http://codeigniter.com |
| 24 | * @since Version 3.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
| 28 | |
| 29 | /** |
| 30 | * CodeIgniter Session Files Driver |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category Sessions |
| 35 | * @author Andrey Andreev |
| 36 | * @link http://codeigniter.com/user_guide/libraries/sessions.html |
| 37 | */ |
| 38 | class CI_Session_files_driver extends CI_Session_driver implements SessionHandlerInterface { |
| 39 | |
| 40 | /** |
| 41 | * Save path |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | protected $_save_path; |
| 46 | |
| 47 | /** |
| 48 | * File handle |
| 49 | * |
| 50 | * @var resource |
| 51 | */ |
| 52 | protected $_file_handle; |
| 53 | |
| 54 | /** |
| 55 | * File name |
| 56 | * |
| 57 | * @var resource |
| 58 | */ |
| 59 | protected $_file_path; |
| 60 | |
| 61 | /** |
| 62 | * File new flag |
| 63 | * |
| 64 | * @var bool |
| 65 | */ |
| 66 | protected $_file_new; |
| 67 | |
| 68 | // ------------------------------------------------------------------------ |
| 69 | |
| 70 | /** |
| 71 | * Class constructor |
| 72 | * |
| 73 | * @param array $params Configuration parameters |
| 74 | * @return void |
| 75 | */ |
| 76 | public function __construct(&$params) |
| 77 | { |
| 78 | parent::__construct($params); |
| 79 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 80 | if (isset($this->_config['save_path'])) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 81 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 82 | $this->_config['save_path'] = rtrim($this->_config['save_path'], '/\\'); |
| 83 | ini_set('session.save_path', $this->_config['save_path']); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 84 | } |
| 85 | else |
| 86 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 87 | $this->_config['save_path'] = rtrim(ini_get('session.save_path'), '/\\'); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | // ------------------------------------------------------------------------ |
| 92 | |
| 93 | public function open($save_path, $name) |
| 94 | { |
| 95 | if ( ! is_dir($save_path) && ! mkdir($save_path, 0700, TRUE)) |
| 96 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 97 | log_message('error', "Session: Configured save path '".$this->_config['save_path']."' is not a directory, doesn't exist or cannot be created."); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 98 | return FALSE; |
| 99 | } |
| 100 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 101 | $this->_config['save_path'] = $save_path; |
| 102 | $this->_file_path = $this->_config['save_path'].DIRECTORY_SEPARATOR |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 103 | .$name // we'll use the session cookie name as a prefix to avoid collisions |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 104 | .($this->_config['match_ip'] ? md5($_SERVER['REMOTE_ADDR']) : ''); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 105 | |
| 106 | return TRUE; |
| 107 | } |
| 108 | |
| 109 | // ------------------------------------------------------------------------ |
| 110 | |
| 111 | public function read($session_id) |
| 112 | { |
| 113 | // This might seem weird, but PHP 5.6 introduces session_reset(), |
| 114 | // which re-reads session data |
| 115 | if ($this->_file_handle === NULL) |
| 116 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 117 | // Just using fopen() with 'c+b' mode would be perfect, but it is only |
| 118 | // available since PHP 5.2.6 and we have to set permissions for new files, |
| 119 | // so we'd have to hack around this ... |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 120 | if (($this->_file_new = ! file_exists($this->_file_path.$session_id)) === TRUE) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 121 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 122 | if (($this->_file_handle = fopen($this->_file_path.$session_id, 'w+b')) === FALSE) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 123 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 124 | log_message('error', "Session: File '".$this->_file_path.$session_id."' doesn't exist and cannot be created."); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 125 | return FALSE; |
| 126 | } |
| 127 | } |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 128 | elseif (($this->_file_handle = fopen($this->_file_path.$session_id, 'r+b')) === FALSE) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 129 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 130 | log_message('error', "Session: Unable to open file '".$this->_file_path.$session_id."'."); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 131 | return FALSE; |
| 132 | } |
| 133 | |
| 134 | if (flock($this->_file_handle, LOCK_EX) === FALSE) |
| 135 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 136 | log_message('error', "Session: Unable to obtain lock for file '".$this->_file_path.$session_id."'."); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 137 | fclose($this->_file_handle); |
| 138 | $this->_file_handle = NULL; |
| 139 | return FALSE; |
| 140 | } |
| 141 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 142 | // Needed by write() to detect session_regenerate_id() calls |
| 143 | $this->_session_id = $session_id; |
| 144 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 145 | if ($this->_file_new) |
| 146 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 147 | chmod($this->_file_path.$session_id, 0600); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 148 | $this->_fingerprint = md5(''); |
| 149 | return ''; |
| 150 | } |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | rewind($this->_file_handle); |
| 155 | } |
| 156 | |
| 157 | $session_data = ''; |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 158 | for ($read = 0, $length = filesize($this->_file_path.$session_id); $read < $length; $read += strlen($buffer)) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 159 | { |
| 160 | if (($buffer = fread($this->_file_handle, $length - $read)) === FALSE) |
| 161 | { |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | $session_data .= $buffer; |
| 166 | } |
| 167 | |
| 168 | $this->_fingerprint = md5($session_data); |
| 169 | return $session_data; |
| 170 | } |
| 171 | |
| 172 | public function write($session_id, $session_data) |
| 173 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 174 | // If the two IDs don't match, we have a session_regenerate_id() call |
| 175 | // and we need to close the old handle and open a new one |
| 176 | if ($session_id !== $this->_session_id && ( ! $this->close() OR $this->read($session_id) === FALSE)) |
| 177 | { |
| 178 | return FALSE; |
| 179 | } |
| 180 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 181 | if ( ! is_resource($this->_file_handle)) |
| 182 | { |
| 183 | return FALSE; |
| 184 | } |
| 185 | elseif ($this->_fingerprint === md5($session_data)) |
| 186 | { |
| 187 | return ($this->_file_new) |
| 188 | ? TRUE |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 189 | : touch($this->_file_path.$session_id); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | if ( ! $this->_file_new) |
| 193 | { |
| 194 | ftruncate($this->_file_handle, 0); |
| 195 | rewind($this->_file_handle); |
| 196 | } |
| 197 | |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 198 | if (($length = strlen($session_data)) > 0) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 199 | { |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 200 | for ($written = 0; $written < $length; $written += $result) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 201 | { |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 202 | if (($result = fwrite($this->_file_handle, substr($session_data, $written))) === FALSE) |
| 203 | { |
| 204 | break; |
| 205 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 206 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 207 | |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 208 | if ( ! is_int($result)) |
| 209 | { |
| 210 | $this->_fingerprint = md5(substr($session_data, 0, $written)); |
| 211 | log_message('error', 'Session: Unable to write data.'); |
| 212 | return FALSE; |
| 213 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | $this->_fingerprint = md5($session_data); |
| 217 | return TRUE; |
| 218 | } |
| 219 | |
| 220 | // ------------------------------------------------------------------------ |
| 221 | |
| 222 | public function close() |
| 223 | { |
| 224 | if (is_resource($this->_file_handle)) |
| 225 | { |
| 226 | flock($this->_file_handle, LOCK_UN); |
| 227 | fclose($this->_file_handle); |
| 228 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 229 | $this->_file_handle = $this->_file_new = $this->_session_id = NULL; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 230 | return TRUE; |
| 231 | } |
| 232 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 233 | return TRUE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // ------------------------------------------------------------------------ |
| 237 | |
| 238 | public function destroy($session_id) |
| 239 | { |
| 240 | if ($this->close()) |
| 241 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 242 | return unlink($this->_file_path.$session_id) && $this->_cookie_destroy(); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 243 | } |
| 244 | elseif ($this->_file_path !== NULL) |
| 245 | { |
| 246 | clearstatcache(); |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame^] | 247 | return file_exists($this->_file_path.$session_id) |
| 248 | ? (unlink($this->_file_path.$session_id) && $this->_cookie_destroy()) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 249 | : TRUE; |
| 250 | } |
| 251 | |
| 252 | return FALSE; |
| 253 | } |
| 254 | |
| 255 | // ------------------------------------------------------------------------ |
| 256 | |
| 257 | public function gc($maxlifetime) |
| 258 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 259 | if ( ! is_dir($this->_config['save_path']) OR ($files = scandir($this->_config['save_path'])) === FALSE) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 260 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 261 | log_message('debug', "Session: Garbage collector couldn't list files under directory '".$this->_config['save_path']."'."); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 262 | return FALSE; |
| 263 | } |
| 264 | |
| 265 | $ts = time() - $maxlifetime; |
| 266 | |
| 267 | foreach ($files as $file) |
| 268 | { |
| 269 | // If the filename doesn't match this pattern, it's either not a session file or is not ours |
| 270 | if ( ! preg_match('/(?:[0-9a-f]{32})?[0-9a-f]{40}$/i', $file) |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 271 | OR ! is_file($this->_config['save_path'].DIRECTORY_SEPARATOR.$file) |
Andrey Andreev | c5519ce | 2014-10-26 11:57:20 +0200 | [diff] [blame] | 272 | OR ($mtime = fileatime($this->_config['save_path'].DIRECTORY_SEPARATOR.$file)) === FALSE |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 273 | OR $mtime > $ts) |
| 274 | { |
| 275 | continue; |
| 276 | } |
| 277 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 278 | unlink($this->_config['save_path'].DIRECTORY_SEPARATOR.$file); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | return TRUE; |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | |
| 286 | /* End of file Session_files_driver.php */ |
| 287 | /* Location: ./system/libraries/Session/drivers/Session_files_driver.php */ |