| | |
| | | protected $gc_enabled = 0; |
| | | protected $gc_handlers = array(); |
| | | protected $cookiename = 'roundcube_sessauth'; |
| | | protected $secret = ''; |
| | | protected $ip_check = false; |
| | | protected $logging = false; |
| | | |
| | | |
| | | /** |
| | | * Blocks session data from being written to database. |
| | |
| | | public function __construct($config) |
| | | { |
| | | $this->config = $config; |
| | | |
| | | // set secret |
| | | $this->set_secret($this->config->get('des_key') . dirname($_SERVER['SCRIPT_NAME'])); |
| | | |
| | | // set ip check |
| | | $this->set_ip_check($this->config->get('ip_check')); |
| | |
| | | $oldvars = $this->get_cache($key); |
| | | |
| | | // if there are cached vars, update store, else insert new data |
| | | if ($oldvars !== null) { |
| | | if ($oldvars) { |
| | | $newvars = $this->_fixvars($vars, $oldvars); |
| | | return $this->update($key, $newvars, $oldvars); |
| | | } |
| | |
| | | { |
| | | // move gc execution to the script shutdown function |
| | | // see rcube::shutdown() and rcube_session::write_close() |
| | | return $this->gc_enabled = $maxlifetime; |
| | | $this->gc_enabled = $maxlifetime; |
| | | |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * see if we have vars of this key already cached, and if so, return them. |
| | | * See if we have vars of this key already cached, and if so, return them. |
| | | * |
| | | * @param $key |
| | | * @return null|array |
| | | * @param string $key Session ID |
| | | * |
| | | * @return string |
| | | */ |
| | | protected function get_cache($key) |
| | | { |
| | |
| | | else { // else read data again |
| | | $cache = $this->read($key); |
| | | } |
| | | |
| | | return $cache; |
| | | } |
| | | |
| | | /** |
| | | * Append the given value to the certain node in the session data array |
| | | * |
| | | * Warning: Do not use if you already modified $_SESSION in the same request (#1490608) |
| | | * |
| | | * @param string Path denoting the session variable where to append the value |
| | | * @param string Key name under which to append the new value (use null for appending to an indexed list) |
| | |
| | | $this->appends[] = $path; |
| | | |
| | | // when overwriting a previously unset variable |
| | | if ($this->unsets[$path]) |
| | | if ($this->unsets[$path]) { |
| | | unset($this->unsets[$path]); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | $node[$k] = $value; |
| | | } |
| | | |
| | | if($this->key) { |
| | | if ($this->key) { |
| | | $data = $this->read($this->key); |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * Setter for cookie encryption secret |
| | | */ |
| | | function set_secret($secret) |
| | | function set_secret($secret = null) |
| | | { |
| | | $this->secret = $secret; |
| | | // generate random hash and store in session |
| | | if (!$secret) { |
| | | if (!empty($_SESSION['auth_secret'])) { |
| | | $secret = $_SESSION['auth_secret']; |
| | | } |
| | | else { |
| | | $secret = rcube_utils::random_bytes(strlen($this->key)); |
| | | } |
| | | } |
| | | |
| | | $_SESSION['auth_secret'] = $secret; |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * Set session authentication cookie |
| | | */ |
| | | function set_auth_cookie() |
| | | public function set_auth_cookie() |
| | | { |
| | | $this->cookie = $this->_mkcookie($this->now); |
| | | rcube_utils::setcookie($this->cookiename, $this->cookie, 0); |
| | |
| | | } |
| | | |
| | | /** |
| | | * Create session cookie from session data. |
| | | * The cookie will be hashed using a method defined by session.hash_function. |
| | | * Create session cookie for specified time slot. |
| | | * |
| | | * @param int Time slot to use |
| | | * |
| | |
| | | */ |
| | | protected function _mkcookie($timeslot) |
| | | { |
| | | $auth_string = "$this->key,$this->secret,$timeslot"; |
| | | $hash_method = (string) ini_get('session.hash_function'); |
| | | $hash_nbits = (int) ini_get('session.hash_bits_per_character'); |
| | | // make sure the secret key exists |
| | | $this->set_secret(); |
| | | |
| | | if (!$hash_method) { |
| | | $hash_method = 'md5'; |
| | | } |
| | | else if ($hash_method === '1') { |
| | | $hash_method = 'sha1'; |
| | | } |
| | | |
| | | if ($hash_nbits < 4 || $hash_nbits > 6) { |
| | | $hash_nbits = 4; |
| | | } |
| | | |
| | | // Hash the authentication string |
| | | $hash = openssl_digest($auth_string, $hash_method, true); |
| | | |
| | | // Above method returns "hexits". To be really compatible |
| | | // with session hashes generated by PHP core we'll get |
| | | // a raw (binary) hash and convert it to a readable form |
| | | // as in bin_to_readable() function in ext/session/session.c. |
| | | $hextab = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,-"; |
| | | $length = strlen($hash); |
| | | $result = ''; |
| | | $char = 0; |
| | | $i = 0; |
| | | $have = 0; |
| | | $mask = (1 << $hash_nbits) - 1; |
| | | |
| | | while (true) { |
| | | if ($have < $hash_nbits) { |
| | | if ($i < $length) { |
| | | $char |= ord($hash[$i++]) << $have; |
| | | $have += 8; |
| | | } |
| | | else if (!$have) { |
| | | break; |
| | | } |
| | | else { |
| | | $have = $hash_nbits; |
| | | } |
| | | } |
| | | |
| | | // consume nbits |
| | | $result .= $hextab[$char & $mask]; |
| | | $char >>= $hash_nbits; |
| | | $have -= $hash_nbits; |
| | | } |
| | | |
| | | return $result; |
| | | // no need to hash this, it's just a random string |
| | | return $_SESSION['auth_secret'] . '-' . $timeslot; |
| | | } |
| | | |
| | | /** |