diff options
author | Andrey Andreev <narf@devilix.net> | 2014-10-06 00:50:14 +0200 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2014-10-06 00:50:14 +0200 |
commit | dfb39bec5faf77e806e55f3ee9d2138e57d55010 (patch) | |
tree | 8060a4e8f7a28d230e689880bba022cd1ba081d4 /system/libraries/Session/drivers | |
parent | 4a485a73d64a8bebc7625aabc5fdc361d5e7dc56 (diff) |
feature/session (#3073): Refactor configuration & fix cookie expiry times
Diffstat (limited to 'system/libraries/Session/drivers')
4 files changed, 42 insertions, 63 deletions
diff --git a/system/libraries/Session/drivers/Session_database_driver.php b/system/libraries/Session/drivers/Session_database_driver.php index 563d1fd6a..e3a3c505e 100644 --- a/system/libraries/Session/drivers/Session_database_driver.php +++ b/system/libraries/Session/drivers/Session_database_driver.php @@ -45,13 +45,6 @@ class CI_Session_database_driver extends CI_Session_driver implements SessionHan protected $_db; /** - * DB table - * - * @var string - */ - protected $_table; - - /** * Row exists flag * * @var bool @@ -100,7 +93,7 @@ class CI_Session_database_driver extends CI_Session_driver implements SessionHan $this->_lock_driver = 'postgre'; } - isset($this->_table) OR $this->_table = config_item('sess_table_name'); + isset($this->_config['save_path']) OR $this->_config['save_path'] = config_item('sess_table_name'); } // ------------------------------------------------------------------------ @@ -120,10 +113,10 @@ class CI_Session_database_driver extends CI_Session_driver implements SessionHan { $this->_db ->select('data') - ->from($this->_table) + ->from($this->_config['save_path']) ->where('id', $session_id); - if ($this->_match_ip) + if ($this->_config['match_ip']) { $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']); } @@ -152,7 +145,7 @@ class CI_Session_database_driver extends CI_Session_driver implements SessionHan if ($this->_row_exists === FALSE) { - if ($this->_db->insert($this->_table, array('id' => $session_id, 'ip_address' => $_SERVER['REMOTE_ADDR'], 'timestamp' => time(), 'data' => $session_data))) + if ($this->_db->insert($this->_config['save_path'], array('id' => $session_id, 'ip_address' => $_SERVER['REMOTE_ADDR'], 'timestamp' => time(), 'data' => $session_data))) { $this->_fingerprint = md5($session_data); return $this->_row_exists = TRUE; @@ -162,7 +155,7 @@ class CI_Session_database_driver extends CI_Session_driver implements SessionHan } $this->_db->where('id', $session_id); - if ($this->_match_ip) + if ($this->_config['match_ip']) { $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']); } @@ -171,7 +164,7 @@ class CI_Session_database_driver extends CI_Session_driver implements SessionHan ? array('timestamp' => time()) : array('timestamp' => time(), 'data' => $session_data); - if ($this->_db->update($this->_table, $update_data)) + if ($this->_db->update($this->_config['save_path'], $update_data)) { $this->_fingerprint = md5($session_data); return TRUE; @@ -196,12 +189,12 @@ class CI_Session_database_driver extends CI_Session_driver implements SessionHan if ($this->_lock) { $this->_db->where('id', $session_id); - if ($this->_match_ip) + if ($this->_config['match_ip']) { $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']); } - return $this->_db->delete($this->_table) + return $this->_db->delete($this->_config['save_path']) ? ($this->close() && $this->_cookie_destroy()) : FALSE; } @@ -213,7 +206,7 @@ class CI_Session_database_driver extends CI_Session_driver implements SessionHan public function gc($maxlifetime) { - return $this->_db->delete($this->_table, 'timestamp < '.(time() - $maxlifetime)); + return $this->_db->delete($this->_config['save_path'], 'timestamp < '.(time() - $maxlifetime)); } // ------------------------------------------------------------------------ @@ -222,7 +215,7 @@ class CI_Session_database_driver extends CI_Session_driver implements SessionHan { if ($this->_lock_driver === 'mysql') { - $arg = $session_id.($this->_match_ip ? '_'.$_SERVER['REMOTE_ADDR'] : ''); + $arg = $session_id.($this->_config['match_ip'] ? '_'.$_SERVER['REMOTE_ADDR'] : ''); if ($this->_db->query("SELECT GET_LOCK('".$arg."', 10) AS ci_session_lock")->row()->ci_session_lock) { $this->_lock = $arg; @@ -233,7 +226,7 @@ class CI_Session_database_driver extends CI_Session_driver implements SessionHan } elseif ($this->_lock_driver === 'postgre') { - $arg = "hashtext('".$session_id."')".($this->_match_ip ? ", hashtext('".$_SERVER['REMOTE_ADDR']."')" : ''); + $arg = "hashtext('".$session_id."')".($this->_config['match_ip'] ? ", hashtext('".$_SERVER['REMOTE_ADDR']."')" : ''); if ($this->_db->simple_query('SELECT pg_advisory_lock('.$arg.')')) { $this->_lock = $arg; diff --git a/system/libraries/Session/drivers/Session_files_driver.php b/system/libraries/Session/drivers/Session_files_driver.php index 7779e9beb..a4f1b9f2f 100644 --- a/system/libraries/Session/drivers/Session_files_driver.php +++ b/system/libraries/Session/drivers/Session_files_driver.php @@ -77,14 +77,14 @@ class CI_Session_files_driver extends CI_Session_driver implements SessionHandle { parent::__construct($params); - if (isset($this->_save_path)) + if (isset($this->_config['save_path'])) { - $this->_save_path = rtrim($this->_save_path, '/\\'); - ini_set('session.save_path', $this->_save_path); + $this->_config['save_path'] = rtrim($this->_config['save_path'], '/\\'); + ini_set('session.save_path', $this->_config['save_path']); } else { - $this->_save_path = rtrim(ini_get('session.save_path'), '/\\'); + $this->_config['save_path'] = rtrim(ini_get('session.save_path'), '/\\'); } } @@ -94,14 +94,14 @@ class CI_Session_files_driver extends CI_Session_driver implements SessionHandle { if ( ! is_dir($save_path) && ! mkdir($save_path, 0700, TRUE)) { - log_message('error', "Session: Configured save path '".$this->_save_path."' is not a directory, doesn't exist or cannot be created."); + log_message('error', "Session: Configured save path '".$this->_config['save_path']."' is not a directory, doesn't exist or cannot be created."); return FALSE; } - $this->_save_path = $save_path; - $this->_file_path = $this->_save_path.DIRECTORY_SEPARATOR + $this->_config['save_path'] = $save_path; + $this->_file_path = $this->_config['save_path'].DIRECTORY_SEPARATOR .$name // we'll use the session cookie name as a prefix to avoid collisions - .($this->_match_ip ? md5($_SERVER['REMOTE_ADDR']) : ''); + .($this->_config['match_ip'] ? md5($_SERVER['REMOTE_ADDR']) : ''); return TRUE; } @@ -248,9 +248,9 @@ class CI_Session_files_driver extends CI_Session_driver implements SessionHandle public function gc($maxlifetime) { - if ( ! is_dir($this->_save_path) OR ($files = scandir($this->_save_path)) === FALSE) + if ( ! is_dir($this->_config['save_path']) OR ($files = scandir($this->_config['save_path'])) === FALSE) { - log_message('debug', "Session: Garbage collector couldn't list files under directory '".$this->_save_path."'."); + log_message('debug', "Session: Garbage collector couldn't list files under directory '".$this->_config['save_path']."'."); return FALSE; } @@ -260,14 +260,14 @@ class CI_Session_files_driver extends CI_Session_driver implements SessionHandle { // If the filename doesn't match this pattern, it's either not a session file or is not ours if ( ! preg_match('/(?:[0-9a-f]{32})?[0-9a-f]{40}$/i', $file) - OR ! is_file($this->_save_path.DIRECTORY_SEPARATOR.$file) - OR ($mtime = filemtime($this->_save_path.DIRECTORY_SEPARATOR.$file)) === FALSE + OR ! is_file($this->_config['save_path'].DIRECTORY_SEPARATOR.$file) + OR ($mtime = filemtime($this->_config['save_path'].DIRECTORY_SEPARATOR.$file)) === FALSE OR $mtime > $ts) { continue; } - unlink($this->_save_path.DIRECTORY_SEPARATOR.$file); + unlink($this->_config['save_path'].DIRECTORY_SEPARATOR.$file); } return TRUE; diff --git a/system/libraries/Session/drivers/Session_memcached_driver.php b/system/libraries/Session/drivers/Session_memcached_driver.php index c6ad56511..318c11afa 100644 --- a/system/libraries/Session/drivers/Session_memcached_driver.php +++ b/system/libraries/Session/drivers/Session_memcached_driver.php @@ -38,13 +38,6 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class CI_Session_memcached_driver extends CI_Session_driver implements SessionHandlerInterface { /** - * Save path - * - * @var string - */ - protected $_save_path; - - /** * Memcached instance * * @var Memcached @@ -77,12 +70,12 @@ class CI_Session_memcached_driver extends CI_Session_driver implements SessionHa { parent::__construct($params); - if (empty($this->_save_path)) + if (empty($this->_config['save_path'])) { log_message('error', 'Session: No Memcached save path configured.'); } - if ($this->_match_ip === TRUE) + if ($this->_config['match_ip'] === TRUE) { $this->_key_prefix .= $_SERVER['REMOTE_ADDR'].':'; } @@ -99,10 +92,10 @@ class CI_Session_memcached_driver extends CI_Session_driver implements SessionHa $server_list[] = $server['host'].':'.$server['port']; } - if ( ! preg_match_all('#,?([^,:]+)\:(\d{1,5})(?:\:(\d+))?#', $this->_save_path, $matches, PREG_SET_ORDER)) + if ( ! preg_match_all('#,?([^,:]+)\:(\d{1,5})(?:\:(\d+))?#', $this->_config['save_path'], $matches, PREG_SET_ORDER)) { $this->_memcached = NULL; - log_message('error', 'Session: Invalid Memcached save path format: '.$this->_save_path); + log_message('error', 'Session: Invalid Memcached save path format: '.$this->_config['save_path']); return FALSE; } @@ -155,7 +148,7 @@ class CI_Session_memcached_driver extends CI_Session_driver implements SessionHa $this->_memcached->replace($this->_lock_key, time(), 5); if ($this->_fingerprint !== ($fingerprint = md5($session_data))) { - if ($this->_memcached->set($this->_key_prefix.$session_id, $session_data, $this->_expiration)) + if ($this->_memcached->set($this->_key_prefix.$session_id, $session_data, $this->_config['expiration'])) { $this->_fingerprint = $fingerprint; return TRUE; @@ -164,7 +157,7 @@ class CI_Session_memcached_driver extends CI_Session_driver implements SessionHa return FALSE; } - return $this->_memcached->touch($this->_key_prefix.$session_id, $this->_expiration); + return $this->_memcached->touch($this->_key_prefix.$session_id, $this->_config['expiration']); } return FALSE; diff --git a/system/libraries/Session/drivers/Session_redis_driver.php b/system/libraries/Session/drivers/Session_redis_driver.php index 6d8044da1..ef18defe2 100644 --- a/system/libraries/Session/drivers/Session_redis_driver.php +++ b/system/libraries/Session/drivers/Session_redis_driver.php @@ -38,13 +38,6 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class CI_Session_redis_driver extends CI_Session_driver implements SessionHandlerInterface { /** - * Save path - * - * @var string - */ - protected $_save_path; - - /** * phpRedis instance * * @var resource @@ -77,14 +70,14 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle { parent::__construct($params); - if (empty($this->_save_path)) + if (empty($this->_config['save_path'])) { log_message('error', 'Session: No Redis save path configured.'); } - elseif (preg_match('#(?:tcp://)?([^:?]+)(?:\:(\d+))?(\?.+)?#', $this->_save_path, $matches)) + elseif (preg_match('#(?:tcp://)?([^:?]+)(?:\:(\d+))?(\?.+)?#', $this->_config['save_path'], $matches)) { isset($matches[3]) OR $matches[3] = ''; // Just to avoid undefined index notices below - $this->_save_path = array( + $this->_config['save_path'] = array( 'host' => $matches[1], 'port' => empty($matches[2]) ? NULL : $matches[2], 'password' => preg_match('#auth=([^\s&]+)#', $matches[3], $match) ? $match[1] : NULL, @@ -96,10 +89,10 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle } else { - log_message('error', 'Session: Invalid Redis save path format: '.$this->_save_path); + log_message('error', 'Session: Invalid Redis save path format: '.$this->_config['save_path']); } - if ($this->_match_ip === TRUE) + if ($this->_config['match_ip'] === TRUE) { $this->_key_prefix .= $_SERVER['REMOTE_ADDR'].':'; } @@ -109,23 +102,23 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle public function open($save_path, $name) { - if (empty($this->_save_path)) + if (empty($this->_config['save_path'])) { return FALSE; } $redis = new Redis(); - if ( ! $redis->connect($this->_save_path['host'], $this->_save_path['port'], $this->_save_path['timeout'])) + if ( ! $redis->connect($this->_config['save_path']['host'], $this->_config['save_path']['port'], $this->_config['save_path']['timeout'])) { log_message('error', 'Session: Unable to connect to Redis with the configured settings.'); } - elseif (isset($this->_save_path['password']) && ! $redis->auth($this->_save_path['password'])) + elseif (isset($this->_config['save_path']['password']) && ! $redis->auth($this->_config['save_path']['password'])) { log_message('error', 'Session: Unable to authenticate to Redis instance.'); } - elseif (isset($this->_save_path['database']) && ! $redis->select($this->_save_path['database'])) + elseif (isset($this->_config['save_path']['database']) && ! $redis->select($this->_config['save_path']['database'])) { - log_message('error', 'Session: Unable to select Redis database with index '.$this->_save_path['database']); + log_message('error', 'Session: Unable to select Redis database with index '.$this->_config['save_path']['database']); } else { @@ -157,7 +150,7 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle $this->_redis->setTimeout($this->_lock_key, 5); if ($this->_fingerprint !== ($fingerprint = md5($session_data))) { - if ($this->_redis->set($this->_key_prefix.$session_id, $session_data, $this->_expiration)) + if ($this->_redis->set($this->_key_prefix.$session_id, $session_data, $this->_config['expiration'])) { $this->_fingerprint = $fingerprint; return TRUE; @@ -166,7 +159,7 @@ class CI_Session_redis_driver extends CI_Session_driver implements SessionHandle return FALSE; } - return $this->_redis->setTimeout($this->_key_prefix.$session_id, $this->_expiration); + return $this->_redis->setTimeout($this->_key_prefix.$session_id, $this->_config['expiration']); } return FALSE; |