Import Ruty

This commit is contained in:
2024-03-11 00:58:34 +01:00
parent 34a31bb184
commit 985f1ab418
618 changed files with 225414 additions and 0 deletions
@@ -0,0 +1,208 @@
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| Copyright (C) Kolab Systems AG |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Provide database supported session management |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
| Author: Aleksander Machniak <alec@alec.pl> |
| Author: Cor Bosman <cor@roundcu.be> |
+-----------------------------------------------------------------------+
*/
/**
* Class to provide database session storage
*
* @package Framework
* @subpackage Core
*/
class rcube_session_db extends rcube_session
{
/** @var rcube_db Database handler */
private $db;
/** @var string Session table name (quoted) */
private $table_name;
/**
* Object constructor
*
* @param rcube_config $config Configuration
*/
public function __construct($config)
{
parent::__construct($config);
// get db instance
$this->db = rcube::get_instance()->get_dbh();
// session table name
$this->table_name = $this->db->table_name('session', true);
// register sessions handler
$this->register_session_handler();
// register db gc handler
$this->register_gc_handler([$this, 'gc_db']);
}
/**
* Opens the session
*
* @param string $save_path Session save path
* @param string $session_name Session name
*
* @return bool True on success, False on failure
*/
public function open($save_path, $session_name)
{
return true;
}
/**
* Close the session
*
* @return bool True on success, False on failure
*/
public function close()
{
return true;
}
/**
* Destroy the session
*
* @param string $key Session identifier
*
* @return bool True on success, False on failure
*/
public function destroy($key)
{
if ($key) {
$this->db->query("DELETE FROM {$this->table_name} WHERE `sess_id` = ?", $key);
}
return true;
}
/**
* Read session data from database
*
* @param string $key Session identifier
*
* @return string Session vars (serialized string)
*/
public function read($key)
{
if ($this->lifetime) {
$expire_time = $this->db->now(-$this->lifetime);
$expire_check = "CASE WHEN `changed` < $expire_time THEN 1 ELSE 0 END AS expired";
}
$sql_result = $this->db->query(
"SELECT `vars`, `ip`, `changed`, " . $this->db->now() . " AS ts"
. (isset($expire_check) ? ", $expire_check" : '')
. " FROM {$this->table_name} WHERE `sess_id` = ?", $key
);
if ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) {
// Remove expired sessions (we use gc, but it may not be precise enough or disabled)
if (!empty($sql_arr['expired'])) {
$this->destroy($key);
return '';
}
$time_diff = time() - strtotime($sql_arr['ts']);
$this->changed = strtotime($sql_arr['changed']) + $time_diff; // local (PHP) time
$this->ip = $sql_arr['ip'];
$this->vars = base64_decode($sql_arr['vars']);
$this->key = $key;
$this->db->reset();
return !empty($this->vars) ? (string) $this->vars : '';
}
return '';
}
/**
* Insert new data into db session store
*
* @param string $key Session identifier
* @param string $vars Serialized data string
*
* @return bool True on success, False on failure
*/
public function write($key, $vars)
{
if ($this->ignore_write) {
return true;
}
$now = $this->db->now();
$this->db->query("INSERT INTO {$this->table_name}"
. " (`sess_id`, `vars`, `ip`, `changed`)"
. " VALUES (?, ?, ?, $now)",
$key, base64_encode($vars), (string)$this->ip
);
return true;
}
/**
* Update session data
*
* @param string $key Session identifier
* @param string $newvars New session data string
* @param string $oldvars Old session data string
*
* @return bool True on success, False on failure
*/
public function update($key, $newvars, $oldvars)
{
$now = $this->db->now();
$ts = microtime(true);
// if new and old data are not the same, update data
// else update expire timestamp only when certain conditions are met
if ($newvars !== $oldvars) {
$this->db->query("UPDATE {$this->table_name} "
. "SET `changed` = $now, `vars` = ? WHERE `sess_id` = ?",
base64_encode($newvars), $key);
}
else if ($ts - $this->changed > $this->lifetime / 2) {
$this->db->query("UPDATE {$this->table_name} SET `changed` = $now"
. " WHERE `sess_id` = ?", $key);
}
return true;
}
/**
* Clean up db sessions.
*/
public function gc_db()
{
// just clean all old sessions when this GC is called
$this->db->query("DELETE FROM " . $this->db->table_name('session')
. " WHERE `changed` < " . $this->db->now(-$this->gc_enabled));
$this->log("Session GC (DB): remove records < "
. date('Y-m-d H:i:s', time() - $this->gc_enabled)
. '; rows = ' . intval($this->db->affected_rows()));
}
}
@@ -0,0 +1,200 @@
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| Copyright (C) Kolab Systems AG |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Provide memcache supported session management |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
| Author: Aleksander Machniak <alec@alec.pl> |
| Author: Cor Bosman <cor@roundcu.bet> |
+-----------------------------------------------------------------------+
*/
/**
* Class to provide memcache session storage
*
* @package Framework
* @subpackage Core
*/
class rcube_session_memcache extends rcube_session
{
/** @var Memcache The memcache driver */
private $memcache;
/** @var bool Debug state */
private $debug;
/**
* Object constructor
*
* @param rcube_config $config Configuration
*/
public function __construct($config)
{
parent::__construct($config);
$this->memcache = rcube::get_instance()->get_memcache();
$this->debug = $config->get('memcache_debug');
if (!$this->memcache) {
rcube::raise_error([
'code' => 604, 'type' => 'memcache',
'line' => __LINE__, 'file' => __FILE__,
'message' => "Failed to connect to memcached. Please check configuration"
],
true, true);
}
// register sessions handler
$this->register_session_handler();
}
/**
* Opens the session
*
* @param string $save_path Session save path
* @param string $session_name Session name
*
* @return bool True on success, False on failure
*/
public function open($save_path, $session_name)
{
return true;
}
/**
* Close the session
*
* @return bool True on success, False on failure
*/
public function close()
{
return true;
}
/**
* Destroy the session
*
* @param string $key Session identifier
*
* @return bool True on success, False on failure
*/
public function destroy($key)
{
if ($key) {
// #1488592: use 2nd argument
$result = $this->memcache->delete($key, 0);
if ($this->debug) {
$this->debug('delete', $key, null, $result);
}
}
return true;
}
/**
* Read session data from memcache
*
* @param string $key Session identifier
*
* @return string Serialized data string
*/
public function read($key)
{
if ($value = $this->memcache->get($key)) {
$arr = unserialize($value);
$this->changed = $arr['changed'];
$this->ip = $arr['ip'];
$this->vars = $arr['vars'];
$this->key = $key;
}
if ($this->debug) {
$this->debug('get', $key, $value);
}
return $this->vars ?: '';
}
/**
* Write data to memcache storage
*
* @param string $key Session identifier
* @param string $vars Session data string
*
* @return bool True on success, False on failure
*/
public function write($key, $vars)
{
if ($this->ignore_write) {
return true;
}
$data = serialize(['changed' => time(), 'ip' => $this->ip, 'vars' => $vars]);
$result = $this->memcache->set($key, $data, MEMCACHE_COMPRESSED, $this->lifetime + 60);
if ($this->debug) {
$this->debug('set', $key, $data, $result);
}
return $result;
}
/**
* Update memcache session data
*
* @param string $key Session identifier
* @param string $newvars New session data string
* @param string $oldvars Old session data string
*
* @return bool True on success, False on failure
*/
public function update($key, $newvars, $oldvars)
{
$ts = microtime(true);
if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 3) {
$data = serialize(['changed' => time(), 'ip' => $this->ip, 'vars' => $newvars]);
$result = $this->memcache->set($key, $data, MEMCACHE_COMPRESSED, $this->lifetime + 60);
if ($this->debug) {
$this->debug('set', $key, $data, $result);
}
return $result;
}
return true;
}
/**
* Write memcache debug info to the log
*
* @param string $type Operation type
* @param string $key Session identifier
* @param string $data Data to log
* @param bool $result Operation result
*/
protected function debug($type, $key, $data = null, $result = null)
{
$line = strtoupper($type) . ' ' . $key;
if ($data !== null) {
$line .= ' ' . $data;
}
rcube::debug('memcache', $line, $result);
}
}
@@ -0,0 +1,199 @@
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| Copyright (C) Kolab Systems AG |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Provide memcached supported session management |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
| Author: Aleksander Machniak <alec@alec.pl> |
| Author: Cor Bosman <cor@roundcu.bet> |
+-----------------------------------------------------------------------+
*/
/**
* Class to provide memcached session storage
*
* @package Framework
* @subpackage Core
*/
class rcube_session_memcached extends rcube_session
{
/** @var Memcached The memcache driver */
private $memcache;
/** @var bool Debug state */
private $debug;
/**
* Object constructor
*
* @param rcube_config $config Configuration
*/
public function __construct($config)
{
parent::__construct($config);
$this->memcache = rcube::get_instance()->get_memcached();
$this->debug = $config->get('memcache_debug');
if (!$this->memcache) {
rcube::raise_error([
'code' => 604, 'type' => 'memcache',
'line' => __LINE__, 'file' => __FILE__,
'message' => "Failed to connect to memcached. Please check configuration"
],
true, true);
}
// register sessions handler
$this->register_session_handler();
}
/**
* Opens the session
*
* @param string $save_path Session save path
* @param string $session_name Session name
*
* @return bool True on success, False on failure
*/
public function open($save_path, $session_name)
{
return true;
}
/**
* Close the session
*
* @return bool True on success, False on failure
*/
public function close()
{
return true;
}
/**
* Destroy the session
*
* @param string $key Session identifier
*
* @return bool True on success, False on failure
*/
public function destroy($key)
{
if ($key) {
// #1488592: use 2nd argument
$result = $this->memcache->delete($key, 0);
if ($this->debug) {
$this->debug('delete', $key, null, $result);
}
}
return true;
}
/**
* Read session data from memcache
*
* @param string $key Session identifier
*
* @return string Serialized data string
*/
public function read($key)
{
if ($arr = $this->memcache->get($key)) {
$this->changed = $arr['changed'];
$this->ip = $arr['ip'];
$this->vars = $arr['vars'];
$this->key = $key;
}
if ($this->debug) {
$this->debug('get', $key, $arr ? serialize($arr) : '');
}
return $this->vars ?: '';
}
/**
* Write data to memcache storage
*
* @param string $key Session identifier
* @param string $vars Session data string
*
* @return bool True on success, False on failure
*/
public function write($key, $vars)
{
if ($this->ignore_write) {
return true;
}
$data = ['changed' => time(), 'ip' => $this->ip, 'vars' => $vars];
$result = $this->memcache->set($key, $data, $this->lifetime + 60);
if ($this->debug) {
$this->debug('set', $key, serialize($data), $result);
}
return $result;
}
/**
* Update memcache session data
*
* @param string $key Session identifier
* @param string $newvars New session data string
* @param string $oldvars Old session data string
*
* @return bool True on success, False on failure
*/
public function update($key, $newvars, $oldvars)
{
$ts = microtime(true);
if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 3) {
$data = ['changed' => time(), 'ip' => $this->ip, 'vars' => $newvars];
$result = $this->memcache->set($key, $data, $this->lifetime + 60);
if ($this->debug) {
$this->debug('set', $key, serialize($data), $result);
}
return $result;
}
return true;
}
/**
* Write memcache debug info to the log
*
* @param string $type Operation type
* @param string $key Session identifier
* @param string $data Data to log
* @param bool $result Operation result
*/
protected function debug($type, $key, $data = null, $result = null)
{
$line = strtoupper($type) . ' ' . $key;
if ($data !== null) {
$line .= ' ' . $data;
}
rcube::debug('memcache', $line, $result);
}
}
@@ -0,0 +1,75 @@
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| Copyright (C) Kolab Systems AG |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Provide database supported session management |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
| Author: Aleksander Machniak <alec@alec.pl> |
| Author: Cor Bosman <cor@roundcu.be> |
+-----------------------------------------------------------------------+
*/
/**
* Class to provide native php session storage
*
* @package Framework
* @subpackage Core
*/
class rcube_session_php extends rcube_session
{
/**
* Native php sessions don't need a save handler.
* We do need to define abstract function implementations but they are not used.
*/
public function open($save_path, $session_name) {}
public function close() {}
public function destroy($key) {}
public function read($key) {}
public function write($key, $vars) {}
public function update($key, $newvars, $oldvars) {}
/**
* Object constructor
*
* @param rcube_config $config Configuration
*/
public function __construct($config)
{
parent::__construct($config);
}
/**
* Wrapper for session_write_close()
*/
public function write_close()
{
$_SESSION['__IP'] = $this->ip;
$_SESSION['__MTIME'] = time();
parent::write_close();
}
/**
* Wrapper for session_start()
*/
public function start()
{
parent::start();
$this->key = session_id();
$this->ip = $_SESSION['__IP'] ?? null;
$this->changed = $_SESSION['__MTIME'] ?? null;
}
}
@@ -0,0 +1,227 @@
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Provide redis supported session management |
+-----------------------------------------------------------------------+
| Author: Cor Bosman <cor@roundcu.be> |
| Author: Aleksander Machniak <alec@alec.pl> |
+-----------------------------------------------------------------------+
*/
/**
* Class to provide redis session storage
*
* @package Framework
* @subpackage Core
*/
class rcube_session_redis extends rcube_session
{
/** @var Redis The redis engine */
private $redis;
/** @var bool Debug state */
private $debug;
/**
* Object constructor
*
* @param rcube_config $config Configuration
*/
public function __construct($config)
{
parent::__construct($config);
$this->redis = rcube::get_instance()->get_redis();
$this->debug = $config->get('redis_debug');
if (!$this->redis) {
rcube::raise_error([
'code' => 604, 'type' => 'redis',
'line' => __LINE__, 'file' => __FILE__,
'message' => "Failed to connect to redis. Please check configuration"
],
true, true);
}
// register sessions handler
$this->register_session_handler();
}
/**
* Opens the session
*
* @param string $save_path Session save path
* @param string $session_name Session name
*
* @return bool True on success, False on failure
*/
public function open($save_path, $session_name)
{
return true;
}
/**
* Close the session
*
* @return bool True on success, False on failure
*/
public function close()
{
return true;
}
/**
* Destroy the session
*
* @param string $key Session identifier
*
* @return bool True on success, False on failure
*/
public function destroy($key)
{
if ($key) {
try {
$fname = method_exists($this->redis, 'del') ? 'del' : 'delete';
$result = $this->redis->$fname($key);
}
catch (Exception $e) {
rcube::raise_error($e, true, true);
}
if ($this->debug) {
$this->debug('delete', $key, null, $result ?? false);
}
}
return true;
}
/**
* Read data from redis store
*
* @param string $key Session identifier
*
* @return string Serialized data string
*/
public function read($key)
{
$value = null;
try {
$value = $this->redis->get($key);
}
catch (Exception $e) {
rcube::raise_error($e, true, true);
}
if ($this->debug) {
$this->debug('get', $key, $value);
}
if ($value) {
$arr = unserialize($value);
$this->changed = $arr['changed'];
$this->ip = $arr['ip'];
$this->vars = $arr['vars'];
$this->key = $key;
}
return $this->vars ?: '';
}
/**
* Write data to redis store
*
* @param string $key Session identifier
* @param string $newvars New session data string
* @param string $oldvars Old session data string
*
* @return bool True on success, False on failure
*/
public function update($key, $newvars, $oldvars)
{
$ts = microtime(true);
if ($newvars !== $oldvars || $ts - $this->changed > $this->lifetime / 3) {
$data = serialize(['changed' => time(), 'ip' => $this->ip, 'vars' => $newvars]);
$result = false;
try {
$result = $this->redis->setex($key, $this->lifetime + 60, $data);
}
catch (Exception $e) {
rcube::raise_error($e, true, true);
}
if ($this->debug) {
$this->debug('set', $key, $data, $result);
}
return $result;
}
return true;
}
/**
* Write data to redis store
*
* @param string $key Session identifier
* @param array $vars Session data
*
* @return bool True on success, False on failure
*/
public function write($key, $vars)
{
if ($this->ignore_write) {
return true;
}
$result = false;
$data = null;
try {
$data = serialize(['changed' => time(), 'ip' => $this->ip, 'vars' => $vars]);
$result = $this->redis->setex($key, $this->lifetime + 60, $data);
}
catch (Exception $e) {
rcube::raise_error($e, true, true);
}
if ($this->debug) {
$this->debug('set', $key, $data, $result);
}
return $result;
}
/**
* Write memcache debug info to the log
*
* @param string $type Operation type
* @param string $key Session identifier
* @param string $data Data to log
* @param bool $result Operation result
*/
protected function debug($type, $key, $data = null, $result = null)
{
$line = strtoupper($type) . ' ' . $key;
if ($data !== null) {
$line .= ' ' . $data;
}
rcube::debug('redis', $line, $result);
}
}