Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 1 | <?php |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | bf6b11d | 2015-01-12 17:27:12 +0200 | [diff] [blame^] | 5 | * An open source application development framework for PHP |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 6 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 8 | * |
Andrey Andreev | bf6b11d | 2015-01-12 17:27:12 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2015, British Columbia Institute of Technology |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 10 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 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: |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 17 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 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 |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | bf6b11d | 2015-01-12 17:27:12 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
| 34 | * @link http://codeigniter.com |
| 35 | * @since Version 3.0.0 |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 36 | * @filesource |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 37 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
| 39 | |
| 40 | /** |
| 41 | * CodeIgniter Session Files Driver |
| 42 | * |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 43 | * @package CodeIgniter |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 44 | * @subpackage Libraries |
| 45 | * @category Sessions |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 46 | * @author Andrey Andreev |
| 47 | * @link http://codeigniter.com/user_guide/libraries/sessions.html |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 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 | |
| 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 Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 91 | if (isset($this->_config['save_path'])) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 92 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 93 | $this->_config['save_path'] = rtrim($this->_config['save_path'], '/\\'); |
| 94 | ini_set('session.save_path', $this->_config['save_path']); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 95 | } |
| 96 | else |
| 97 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 98 | $this->_config['save_path'] = rtrim(ini_get('session.save_path'), '/\\'); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | // ------------------------------------------------------------------------ |
| 103 | |
| 104 | public function open($save_path, $name) |
| 105 | { |
| 106 | if ( ! is_dir($save_path) && ! mkdir($save_path, 0700, TRUE)) |
| 107 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 108 | 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] | 109 | return FALSE; |
| 110 | } |
| 111 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 112 | $this->_config['save_path'] = $save_path; |
| 113 | $this->_file_path = $this->_config['save_path'].DIRECTORY_SEPARATOR |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 114 | .$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] | 115 | .($this->_config['match_ip'] ? md5($_SERVER['REMOTE_ADDR']) : ''); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 116 | |
| 117 | return TRUE; |
| 118 | } |
| 119 | |
| 120 | // ------------------------------------------------------------------------ |
| 121 | |
| 122 | public function read($session_id) |
| 123 | { |
| 124 | // This might seem weird, but PHP 5.6 introduces session_reset(), |
| 125 | // which re-reads session data |
| 126 | if ($this->_file_handle === NULL) |
| 127 | { |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 128 | // Just using fopen() with 'c+b' mode would be perfect, but it is only |
| 129 | // available since PHP 5.2.6 and we have to set permissions for new files, |
| 130 | // so we'd have to hack around this ... |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 131 | if (($this->_file_new = ! file_exists($this->_file_path.$session_id)) === TRUE) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 132 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 133 | 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] | 134 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 135 | 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] | 136 | return FALSE; |
| 137 | } |
| 138 | } |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 139 | 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] | 140 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 141 | 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] | 142 | return FALSE; |
| 143 | } |
| 144 | |
| 145 | if (flock($this->_file_handle, LOCK_EX) === FALSE) |
| 146 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 147 | 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] | 148 | fclose($this->_file_handle); |
| 149 | $this->_file_handle = NULL; |
| 150 | return FALSE; |
| 151 | } |
| 152 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 153 | // Needed by write() to detect session_regenerate_id() calls |
| 154 | $this->_session_id = $session_id; |
| 155 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 156 | if ($this->_file_new) |
| 157 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 158 | chmod($this->_file_path.$session_id, 0600); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 159 | $this->_fingerprint = md5(''); |
| 160 | return ''; |
| 161 | } |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | rewind($this->_file_handle); |
| 166 | } |
| 167 | |
| 168 | $session_data = ''; |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 169 | 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] | 170 | { |
| 171 | if (($buffer = fread($this->_file_handle, $length - $read)) === FALSE) |
| 172 | { |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | $session_data .= $buffer; |
| 177 | } |
| 178 | |
| 179 | $this->_fingerprint = md5($session_data); |
| 180 | return $session_data; |
| 181 | } |
| 182 | |
| 183 | public function write($session_id, $session_data) |
| 184 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 185 | // If the two IDs don't match, we have a session_regenerate_id() call |
| 186 | // and we need to close the old handle and open a new one |
| 187 | if ($session_id !== $this->_session_id && ( ! $this->close() OR $this->read($session_id) === FALSE)) |
| 188 | { |
| 189 | return FALSE; |
| 190 | } |
| 191 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 192 | if ( ! is_resource($this->_file_handle)) |
| 193 | { |
| 194 | return FALSE; |
| 195 | } |
| 196 | elseif ($this->_fingerprint === md5($session_data)) |
| 197 | { |
| 198 | return ($this->_file_new) |
| 199 | ? TRUE |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 200 | : touch($this->_file_path.$session_id); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if ( ! $this->_file_new) |
| 204 | { |
| 205 | ftruncate($this->_file_handle, 0); |
| 206 | rewind($this->_file_handle); |
| 207 | } |
| 208 | |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 209 | if (($length = strlen($session_data)) > 0) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 210 | { |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 211 | for ($written = 0; $written < $length; $written += $result) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 212 | { |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 213 | if (($result = fwrite($this->_file_handle, substr($session_data, $written))) === FALSE) |
| 214 | { |
| 215 | break; |
| 216 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 217 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 218 | |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 219 | if ( ! is_int($result)) |
| 220 | { |
| 221 | $this->_fingerprint = md5(substr($session_data, 0, $written)); |
| 222 | log_message('error', 'Session: Unable to write data.'); |
| 223 | return FALSE; |
| 224 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | $this->_fingerprint = md5($session_data); |
| 228 | return TRUE; |
| 229 | } |
| 230 | |
| 231 | // ------------------------------------------------------------------------ |
| 232 | |
| 233 | public function close() |
| 234 | { |
| 235 | if (is_resource($this->_file_handle)) |
| 236 | { |
| 237 | flock($this->_file_handle, LOCK_UN); |
| 238 | fclose($this->_file_handle); |
| 239 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 240 | $this->_file_handle = $this->_file_new = $this->_session_id = NULL; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 241 | return TRUE; |
| 242 | } |
| 243 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 244 | return TRUE; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // ------------------------------------------------------------------------ |
| 248 | |
| 249 | public function destroy($session_id) |
| 250 | { |
| 251 | if ($this->close()) |
| 252 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 253 | return unlink($this->_file_path.$session_id) && $this->_cookie_destroy(); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 254 | } |
| 255 | elseif ($this->_file_path !== NULL) |
| 256 | { |
| 257 | clearstatcache(); |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 258 | return file_exists($this->_file_path.$session_id) |
| 259 | ? (unlink($this->_file_path.$session_id) && $this->_cookie_destroy()) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 260 | : TRUE; |
| 261 | } |
| 262 | |
| 263 | return FALSE; |
| 264 | } |
| 265 | |
| 266 | // ------------------------------------------------------------------------ |
| 267 | |
| 268 | public function gc($maxlifetime) |
| 269 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 270 | 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] | 271 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 272 | 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] | 273 | return FALSE; |
| 274 | } |
| 275 | |
| 276 | $ts = time() - $maxlifetime; |
| 277 | |
| 278 | foreach ($files as $file) |
| 279 | { |
| 280 | // If the filename doesn't match this pattern, it's either not a session file or is not ours |
| 281 | 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] | 282 | OR ! is_file($this->_config['save_path'].DIRECTORY_SEPARATOR.$file) |
Andrey Andreev | cd48961 | 2014-10-27 16:09:01 +0200 | [diff] [blame] | 283 | OR ($mtime = filemtime($this->_config['save_path'].DIRECTORY_SEPARATOR.$file)) === FALSE |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 284 | OR $mtime > $ts) |
| 285 | { |
| 286 | continue; |
| 287 | } |
| 288 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 289 | unlink($this->_config['save_path'].DIRECTORY_SEPARATOR.$file); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | return TRUE; |
| 293 | } |
| 294 | |
| 295 | } |
| 296 | |
| 297 | /* End of file Session_files_driver.php */ |
| 298 | /* Location: ./system/libraries/Session/drivers/Session_files_driver.php */ |