Andrey Andreev | 05afe3e | 2015-02-02 19:04:37 +0200 | [diff] [blame] | 1 | <?php |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 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 | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, 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 | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, 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 |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | 46f2f26 | 2014-11-11 14:37:51 +0200 | [diff] [blame] | 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 |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://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 | |
Andrey Andreev | 103a426 | 2016-10-03 11:19:11 +0300 | [diff] [blame] | 79 | /** |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 80 | * Validate SID regular expression |
| 81 | * |
| 82 | * @var string |
| 83 | */ |
| 84 | protected $_sid_regexp; |
| 85 | |
| 86 | /** |
Andrey Andreev | c0c74d5 | 2017-01-19 15:26:35 +0200 | [diff] [blame] | 87 | * mbstring.func_overload flag |
Andrey Andreev | 103a426 | 2016-10-03 11:19:11 +0300 | [diff] [blame] | 88 | * |
| 89 | * @var bool |
| 90 | */ |
Andrey Andreev | c0c74d5 | 2017-01-19 15:26:35 +0200 | [diff] [blame] | 91 | protected static $func_overload; |
Andrey Andreev | 103a426 | 2016-10-03 11:19:11 +0300 | [diff] [blame] | 92 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 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 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 105 | if (isset($this->_config['save_path'])) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 106 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 107 | $this->_config['save_path'] = rtrim($this->_config['save_path'], '/\\'); |
| 108 | ini_set('session.save_path', $this->_config['save_path']); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 109 | } |
| 110 | else |
| 111 | { |
Andrey Andreev | 85dfc2a | 2016-04-01 22:54:15 +0300 | [diff] [blame] | 112 | log_message('debug', 'Session: "sess_save_path" is empty; using "session.save_path" value from php.ini.'); |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 113 | $this->_config['save_path'] = rtrim(ini_get('session.save_path'), '/\\'); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 114 | } |
Andrey Andreev | 103a426 | 2016-10-03 11:19:11 +0300 | [diff] [blame] | 115 | |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 116 | $this->_sid_regexp = $this->_config['_sid_regexp']; |
| 117 | |
Andrey Andreev | c0c74d5 | 2017-01-19 15:26:35 +0200 | [diff] [blame] | 118 | isset(self::$func_overload) OR self::$func_overload = (extension_loaded('mbstring') && ini_get('mbstring.func_overload')); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // ------------------------------------------------------------------------ |
| 122 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 123 | /** |
| 124 | * Open |
| 125 | * |
| 126 | * Sanitizes the save_path directory. |
| 127 | * |
| 128 | * @param string $save_path Path to session files' directory |
Tom Atkinson | 388ce59 | 2015-02-04 17:54:52 +0100 | [diff] [blame] | 129 | * @param string $name Session cookie name |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 130 | * @return bool |
| 131 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 132 | public function open($save_path, $name) |
| 133 | { |
Andrey Andreev | 5f4d01a | 2015-02-02 18:38:00 +0200 | [diff] [blame] | 134 | if ( ! is_dir($save_path)) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 135 | { |
Andrey Andreev | 5f4d01a | 2015-02-02 18:38:00 +0200 | [diff] [blame] | 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."); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 144 | } |
| 145 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 146 | $this->_config['save_path'] = $save_path; |
| 147 | $this->_file_path = $this->_config['save_path'].DIRECTORY_SEPARATOR |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 148 | .$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] | 149 | .($this->_config['match_ip'] ? md5($_SERVER['REMOTE_ADDR']) : ''); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 150 | |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 151 | return $this->_success; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // ------------------------------------------------------------------------ |
| 155 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 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 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 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 | { |
Andrey Andreev | a838279 | 2016-07-28 16:40:12 +0300 | [diff] [blame] | 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) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 173 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 174 | log_message('error', "Session: Unable to open file '".$this->_file_path.$session_id."'."); |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 175 | return $this->_failure; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | if (flock($this->_file_handle, LOCK_EX) === FALSE) |
| 179 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 180 | 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] | 181 | fclose($this->_file_handle); |
| 182 | $this->_file_handle = NULL; |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 183 | return $this->_failure; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 184 | } |
| 185 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 186 | // Needed by write() to detect session_regenerate_id() calls |
| 187 | $this->_session_id = $session_id; |
| 188 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 189 | if ($this->_file_new) |
| 190 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 191 | chmod($this->_file_path.$session_id, 0600); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 192 | $this->_fingerprint = md5(''); |
| 193 | return ''; |
| 194 | } |
| 195 | } |
Andrey Andreev | 8df6efd | 2015-12-11 17:55:55 +0200 | [diff] [blame] | 196 | // We shouldn't need this, but apparently we do ... |
| 197 | // See https://github.com/bcit-ci/CodeIgniter/issues/4039 |
Andrey Andreev | 2d6d9ab | 2015-12-15 12:32:50 +0200 | [diff] [blame] | 198 | elseif ($this->_file_handle === FALSE) |
Andrey Andreev | 8df6efd | 2015-12-11 17:55:55 +0200 | [diff] [blame] | 199 | { |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 200 | return $this->_failure; |
Andrey Andreev | 8df6efd | 2015-12-11 17:55:55 +0200 | [diff] [blame] | 201 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 202 | else |
| 203 | { |
| 204 | rewind($this->_file_handle); |
| 205 | } |
| 206 | |
| 207 | $session_data = ''; |
Andrey Andreev | 103a426 | 2016-10-03 11:19:11 +0300 | [diff] [blame] | 208 | for ($read = 0, $length = filesize($this->_file_path.$session_id); $read < $length; $read += self::strlen($buffer)) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 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 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 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 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 233 | public function write($session_id, $session_data) |
| 234 | { |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 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 |
Andrey Andreev | bb71dba | 2015-12-15 13:00:52 +0200 | [diff] [blame] | 237 | if ($session_id !== $this->_session_id && ($this->close() === $this->_failure OR $this->read($session_id) === $this->_failure)) |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 238 | { |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 239 | return $this->_failure; |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 242 | if ( ! is_resource($this->_file_handle)) |
| 243 | { |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 244 | return $this->_failure; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 245 | } |
| 246 | elseif ($this->_fingerprint === md5($session_data)) |
| 247 | { |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 248 | return ( ! $this->_file_new && ! touch($this->_file_path.$session_id)) |
| 249 | ? $this->_failure |
| 250 | : $this->_success; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | if ( ! $this->_file_new) |
| 254 | { |
| 255 | ftruncate($this->_file_handle, 0); |
| 256 | rewind($this->_file_handle); |
| 257 | } |
| 258 | |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 259 | if (($length = strlen($session_data)) > 0) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 260 | { |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 261 | for ($written = 0; $written < $length; $written += $result) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 262 | { |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 263 | if (($result = fwrite($this->_file_handle, substr($session_data, $written))) === FALSE) |
| 264 | { |
| 265 | break; |
| 266 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 267 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 268 | |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 269 | if ( ! is_int($result)) |
| 270 | { |
| 271 | $this->_fingerprint = md5(substr($session_data, 0, $written)); |
| 272 | log_message('error', 'Session: Unable to write data.'); |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 273 | return $this->_failure; |
Andrey Andreev | 5995e08 | 2014-06-03 15:33:51 +0300 | [diff] [blame] | 274 | } |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | $this->_fingerprint = md5($session_data); |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 278 | return $this->_success; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // ------------------------------------------------------------------------ |
| 282 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 283 | /** |
| 284 | * Close |
| 285 | * |
| 286 | * Releases locks and closes file descriptor. |
| 287 | * |
Gabriel Potkány | 1fb5000 | 2015-02-04 01:45:59 +0100 | [diff] [blame] | 288 | * @return bool |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 289 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 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 | |
Andrey Andreev | 7474a67 | 2014-10-31 23:35:32 +0200 | [diff] [blame] | 297 | $this->_file_handle = $this->_file_new = $this->_session_id = NULL; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 298 | } |
| 299 | |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 300 | return $this->_success; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | // ------------------------------------------------------------------------ |
| 304 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 305 | /** |
| 306 | * Destroy |
| 307 | * |
| 308 | * Destroys the current session. |
| 309 | * |
| 310 | * @param string $session_id Session ID |
| 311 | * @return bool |
| 312 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 313 | public function destroy($session_id) |
| 314 | { |
Andrey Andreev | bb71dba | 2015-12-15 13:00:52 +0200 | [diff] [blame] | 315 | if ($this->close() === $this->_success) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 316 | { |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 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; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 326 | } |
| 327 | elseif ($this->_file_path !== NULL) |
| 328 | { |
| 329 | clearstatcache(); |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 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; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 339 | } |
| 340 | |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 341 | return $this->_failure; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | // ------------------------------------------------------------------------ |
| 345 | |
Andrey Andreev | 10411fc | 2015-01-19 13:54:53 +0200 | [diff] [blame] | 346 | /** |
| 347 | * Garbage Collector |
| 348 | * |
| 349 | * Deletes expired sessions |
| 350 | * |
| 351 | * @param int $maxlifetime Maximum lifetime of sessions |
| 352 | * @return bool |
| 353 | */ |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 354 | public function gc($maxlifetime) |
| 355 | { |
Andrey Andreev | 2f79f9a | 2015-03-26 12:52:05 +0200 | [diff] [blame] | 356 | if ( ! is_dir($this->_config['save_path']) OR ($directory = opendir($this->_config['save_path'])) === FALSE) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 357 | { |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 358 | log_message('debug', "Session: Garbage collector couldn't list files under directory '".$this->_config['save_path']."'."); |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 359 | return $this->_failure; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | $ts = time() - $maxlifetime; |
| 363 | |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 364 | $pattern = ($this->_config['match_ip'] === TRUE) |
| 365 | ? '[0-9a-f]{32}' |
| 366 | : ''; |
| 367 | |
Tom Atkinson | 388ce59 | 2015-02-04 17:54:52 +0100 | [diff] [blame] | 368 | $pattern = sprintf( |
Andrey Andreev | 6c6ee1a | 2016-10-22 16:33:06 +0300 | [diff] [blame] | 369 | '#\A%s'.$pattern.$this->_sid_regexp.'\z#', |
| 370 | preg_quote($this->_config['cookie_name']) |
Tom Atkinson | 388ce59 | 2015-02-04 17:54:52 +0100 | [diff] [blame] | 371 | ); |
| 372 | |
Andrey Andreev | 2f79f9a | 2015-03-26 12:52:05 +0200 | [diff] [blame] | 373 | while (($file = readdir($directory)) !== FALSE) |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 374 | { |
| 375 | // If the filename doesn't match this pattern, it's either not a session file or is not ours |
Tom Atkinson | 388ce59 | 2015-02-04 17:54:52 +0100 | [diff] [blame] | 376 | if ( ! preg_match($pattern, $file) |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 377 | OR ! is_file($this->_config['save_path'].DIRECTORY_SEPARATOR.$file) |
Andrey Andreev | cd48961 | 2014-10-27 16:09:01 +0200 | [diff] [blame] | 378 | OR ($mtime = filemtime($this->_config['save_path'].DIRECTORY_SEPARATOR.$file)) === FALSE |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 379 | OR $mtime > $ts) |
| 380 | { |
| 381 | continue; |
| 382 | } |
| 383 | |
Andrey Andreev | dfb39be | 2014-10-06 01:50:14 +0300 | [diff] [blame] | 384 | unlink($this->_config['save_path'].DIRECTORY_SEPARATOR.$file); |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 385 | } |
| 386 | |
Andrey Andreev | 2f79f9a | 2015-03-26 12:52:05 +0200 | [diff] [blame] | 387 | closedir($directory); |
| 388 | |
Andrey Andreev | af84969 | 2015-12-12 14:07:39 +0200 | [diff] [blame] | 389 | return $this->_success; |
Andrey Andreev | 47a47fb | 2014-05-31 16:08:30 +0300 | [diff] [blame] | 390 | } |
| 391 | |
Andrey Andreev | 103a426 | 2016-10-03 11:19:11 +0300 | [diff] [blame] | 392 | // -------------------------------------------------------------------- |
| 393 | |
| 394 | /** |
| 395 | * Byte-safe strlen() |
| 396 | * |
| 397 | * @param string $str |
| 398 | * @return int |
| 399 | */ |
| 400 | protected static function strlen($str) |
| 401 | { |
Andrey Andreev | c0c74d5 | 2017-01-19 15:26:35 +0200 | [diff] [blame] | 402 | return (self::$func_overload) |
Andrey Andreev | 103a426 | 2016-10-03 11:19:11 +0300 | [diff] [blame] | 403 | ? mb_strlen($str, '8bit') |
| 404 | : strlen($str); |
| 405 | } |
| 406 | } |