mirror of
https://git.tt-rss.org/git/tt-rss.git
synced 2025-12-13 15:35:55 +00:00
more work on singleton-based DB
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
class Db implements IDb {
|
||||
private static $instance;
|
||||
private $adapter;
|
||||
private $link;
|
||||
|
||||
private function __construct() {
|
||||
switch (DB_TYPE) {
|
||||
@@ -12,11 +13,11 @@ class Db implements IDb {
|
||||
$this->adapter = new Db_Pgsql();
|
||||
break;
|
||||
default:
|
||||
die("Unknown DB_TYPE: " . DB_TYPE);
|
||||
user_error("Unknown DB_TYPE: " . DB_TYPE);
|
||||
}
|
||||
|
||||
$this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
|
||||
$this->adapter->init();
|
||||
$this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
|
||||
|
||||
}
|
||||
|
||||
private function __clone() {
|
||||
@@ -40,6 +41,7 @@ class Db implements IDb {
|
||||
|
||||
function connect($host, $user, $pass, $db, $port) {
|
||||
//return $this->adapter->connect($host, $user, $pass, $db, $port);
|
||||
return $this->link;
|
||||
}
|
||||
|
||||
function escape_string($s, $strip_tags = true) {
|
||||
|
||||
@@ -9,6 +9,9 @@ class Db_Mysql implements IDb {
|
||||
if (!$result) {
|
||||
die("Can't select DB: " . mysql_error($this->link));
|
||||
}
|
||||
|
||||
$this->init();
|
||||
|
||||
return $this->link;
|
||||
} else {
|
||||
die("Unable to connect to database (as $user to $host, database $db): " . mysql_error());
|
||||
|
||||
@@ -23,6 +23,8 @@ class Db_Pgsql implements IDb {
|
||||
die("Unable to connect to database (as $user to $host, database $db):" . pg_last_error());
|
||||
}
|
||||
|
||||
$this->init();
|
||||
|
||||
return $this->link;
|
||||
}
|
||||
|
||||
|
||||
73
classes/sessionhandler.php
Normal file
73
classes/sessionhandler.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
class SessionHandler implements SessionHandlerInterface {
|
||||
private static $instance;
|
||||
private $db;
|
||||
|
||||
public static function get() {
|
||||
if (self::$instance == null)
|
||||
self::$instance = new self();
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
private function __construct() {
|
||||
$this->db = Db::get();
|
||||
|
||||
session_set_save_handler("SessionHandler::open", "SessionHandler::close",
|
||||
"SessionHandler::read", "SessionHandler::write", "SessionHandler::destroy",
|
||||
"SessionHandler::gc");
|
||||
}
|
||||
|
||||
public static function open($save_path, $name) { }
|
||||
|
||||
|
||||
public static function read ($id){
|
||||
|
||||
$query = "SELECT data FROM ttrss_sessions WHERE id='$id'";
|
||||
|
||||
$res = $this->db->query("SELECT data FROM ttrss_sessions WHERE id='$id'");
|
||||
|
||||
if ($this->db->num_rows($res) != 1) {
|
||||
|
||||
"INSERT INTO ttrss_sessions (id, data, expire)
|
||||
VALUES ('$id', '$data', '$expire')";
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
$data = $this->db->fetch_result($res, 0, "data");
|
||||
return base64_decode($data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static function write($id, $data) {
|
||||
if (! $data) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = $this->db->escape_string( base64_encode($data), false);
|
||||
|
||||
$expire = time() + max(SESSION_COOKIE_LIFETIME, 86400);
|
||||
|
||||
$query = "UPDATE ttrss_sessions SET data='$data',
|
||||
expire = '$expire' WHERE id='$id'";
|
||||
|
||||
$this->db->query( $query);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function close () { }
|
||||
|
||||
public static function destroy($session_id) {
|
||||
$this->db->query("DELETE FROM ttrss_sessions WHERE id = '$session_id'");
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function gc($maxLifeTime) {
|
||||
$this->db->query("DELETE FROM ttrss_sessions WHERE expire < " time() - $maxLifeTime);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user