more work on singleton-based DB
parent
ba68b6815a
commit
404e2e3603
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
function __autoload($class) {
|
||||
$class_file = str_replace("_", "/", strtolower(basename($class)));
|
||||
|
||||
$file = dirname(__FILE__)."/../classes/$class_file.php";
|
||||
|
||||
if (file_exists($file)) {
|
||||
require $file;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
@ -1,138 +1,44 @@
|
||||
<?php
|
||||
|
||||
require_once "config.php";
|
||||
|
||||
function db_connect($host, $user, $pass, $db) {
|
||||
if (DB_TYPE == "pgsql") {
|
||||
|
||||
$string = "dbname=$db user=$user";
|
||||
|
||||
if ($pass) {
|
||||
$string .= " password=$pass";
|
||||
}
|
||||
|
||||
if ($host) {
|
||||
$string .= " host=$host";
|
||||
}
|
||||
|
||||
if (defined('DB_PORT') && DB_PORT) {
|
||||
$string = "$string port=" . DB_PORT;
|
||||
}
|
||||
|
||||
$link = pg_connect($string);
|
||||
|
||||
if (!$link) {
|
||||
die("Unable to connect to database (as $user to $host, database $db):" . pg_last_error());
|
||||
}
|
||||
|
||||
return $link;
|
||||
|
||||
} else if (DB_TYPE == "mysql") {
|
||||
$link = mysql_connect($host, $user, $pass);
|
||||
if ($link) {
|
||||
$result = mysql_select_db($db, $link);
|
||||
if (!$result) {
|
||||
die("Can't select DB: " . mysql_error($link));
|
||||
}
|
||||
return $link;
|
||||
} else {
|
||||
die("Unable to connect to database (as $user to $host, database $db): " . mysql_error());
|
||||
}
|
||||
}
|
||||
return Db::get()->connect($host, $user, $pass, $db, 0);
|
||||
}
|
||||
|
||||
function db_escape_string($link, $s, $strip_tags = true) {
|
||||
if ($strip_tags) $s = strip_tags($s);
|
||||
|
||||
if (DB_TYPE == "pgsql") {
|
||||
return pg_escape_string($link, $s);
|
||||
} else {
|
||||
return mysql_real_escape_string($s, $link);
|
||||
}
|
||||
return Db::get()->escape_string($s, $strip_tags);
|
||||
}
|
||||
|
||||
function db_query($link, $query, $die_on_error = true) {
|
||||
if (DB_TYPE == "pgsql") {
|
||||
$result = pg_query($link, $query);
|
||||
if (!$result) {
|
||||
$query = htmlspecialchars($query); // just in case
|
||||
if ($die_on_error) {
|
||||
die("Query <i>$query</i> failed [$result]: " . ($link ? pg_last_error($link) : "No connection"));
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
} else if (DB_TYPE == "mysql") {
|
||||
$result = mysql_query($query, $link);
|
||||
if (!$result) {
|
||||
$query = htmlspecialchars($query);
|
||||
if ($die_on_error) {
|
||||
die("Query <i>$query</i> failed: " . ($link ? mysql_error($link) : "No connection"));
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
return Db::get()->query($query, $die_on_error);
|
||||
}
|
||||
|
||||
function db_fetch_assoc($result) {
|
||||
if (DB_TYPE == "pgsql") {
|
||||
return pg_fetch_assoc($result);
|
||||
} else if (DB_TYPE == "mysql") {
|
||||
return mysql_fetch_assoc($result);
|
||||
}
|
||||
return Db::get()->fetch_assoc($result);
|
||||
}
|
||||
|
||||
|
||||
function db_num_rows($result) {
|
||||
if (DB_TYPE == "pgsql") {
|
||||
return pg_num_rows($result);
|
||||
} else if (DB_TYPE == "mysql") {
|
||||
return mysql_num_rows($result);
|
||||
}
|
||||
return Db::get()->num_rows($result);
|
||||
}
|
||||
|
||||
function db_fetch_result($result, $row, $param) {
|
||||
if (DB_TYPE == "pgsql") {
|
||||
return pg_fetch_result($result, $row, $param);
|
||||
} else if (DB_TYPE == "mysql") {
|
||||
// I hate incoherent naming of PHP functions
|
||||
return mysql_result($result, $row, $param);
|
||||
}
|
||||
}
|
||||
|
||||
function db_unescape_string($str) {
|
||||
$tmp = str_replace("\\\"", "\"", $str);
|
||||
$tmp = str_replace("\\'", "'", $tmp);
|
||||
return $tmp;
|
||||
return Db::get()->fetch_result($result, $row, $param);
|
||||
}
|
||||
|
||||
function db_close($link) {
|
||||
if (DB_TYPE == "pgsql") {
|
||||
|
||||
return pg_close($link);
|
||||
|
||||
} else if (DB_TYPE == "mysql") {
|
||||
return mysql_close($link);
|
||||
}
|
||||
return Db::get()->close();
|
||||
}
|
||||
|
||||
function db_affected_rows($link, $result) {
|
||||
if (DB_TYPE == "pgsql") {
|
||||
return pg_affected_rows($result);
|
||||
} else if (DB_TYPE == "mysql") {
|
||||
return mysql_affected_rows($link);
|
||||
}
|
||||
return Db::get()->affected_rows($result);
|
||||
}
|
||||
|
||||
function db_last_error($link) {
|
||||
if (DB_TYPE == "pgsql") {
|
||||
return pg_last_error($link);
|
||||
} else if (DB_TYPE == "mysql") {
|
||||
return mysql_error($link);
|
||||
}
|
||||
return Db::get()->last_error();
|
||||
}
|
||||
|
||||
function db_quote($str){
|
||||
return("'$str'");
|
||||
return Db::get()->quote($str);
|
||||
}
|
||||
|
||||
?>
|
||||
|
Loading…
Reference in New Issue