]> git.wh0rd.org Git - tt-rss.git/commitdiff
more work on singleton-based DB
authorAndrew Dolgov <fox@fakecake.org>
Wed, 17 Apr 2013 11:36:34 +0000 (15:36 +0400)
committerAndrew Dolgov <fox@fakecake.org>
Wed, 17 Apr 2013 11:36:48 +0000 (15:36 +0400)
18 files changed:
api/index.php
backend.php
classes/db.php
classes/db/mysql.php
classes/db/pgsql.php
classes/sessionhandler.php [new file with mode: 0644]
include/autoload.php [new file with mode: 0644]
include/db.php
include/errorhandler.php
include/functions.php
include/sessions.php
index.php
opml.php
prefs.php
public.php
register.php
update.php
update_daemon2.php

index 823b9527e90319645c98d58e387420b906c514d9..d28ab763a0a27b08a4ac892d748bf063b5cc81f5 100644 (file)
@@ -13,6 +13,7 @@
        define('TTRSS_SESSION_NAME', 'ttrss_api_sid');
        define('NO_SESSION_AUTOSTART', true);
 
+       require_once "autoload.php";
        require_once "db.php";
        require_once "db-prefs.php";
        require_once "functions.php";
index 40e40aeb323dc8b9ae4cc9c05d23079a9f3254f5..b583d379e46fbbe726655adbfe7698ecb10a7027 100644 (file)
@@ -37,6 +37,7 @@
 
        @$csrf_token = $_REQUEST['csrf_token'];
 
+       require_once "autoload.php";
        require_once "sessions.php";
        require_once "functions.php";
        require_once "config.php";
index 71fc01ae1b6ea5b4701a81200e3f94d2a1b4ba47..558d3e6b717b8a438ce494b115da7b3eb48e90a4 100644 (file)
@@ -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) {
index fa97dcff16d9af59d931abf5cfb270c21afc556d..64c35ebdcd1fba302337ca5996036a4731bee20a 100644 (file)
@@ -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());
index c9ec338876dd2e150a2e6b5ca2e18c679d9b6a50..0f38fb8cb5d124fd47cbcee815d89a4e6cf98d05 100644 (file)
@@ -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;
        }
 
diff --git a/classes/sessionhandler.php b/classes/sessionhandler.php
new file mode 100644 (file)
index 0000000..66d8dd8
--- /dev/null
@@ -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;
+       }
+
+}
+?>
diff --git a/include/autoload.php b/include/autoload.php
new file mode 100644 (file)
index 0000000..40c63d5
--- /dev/null
@@ -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;
+               }
+
+       }
+?>
index a70a1d87889ad4528141e966e4aee5711c2431b0..cfa4ccda5b17948031b1d2e2a607998d516f89f8 100644 (file)
 <?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);
 }
 
 ?>
index f7fadc17236a01b187897c23f6d0fe8cb0f25928..45496b18bab04b475ae0b247dc6543ee66e39718 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 // TODO: make configurable
-require_once "classes/logger.php";
-require_once "classes/logger/sql.php";
+//require_once "classes/logger.php";
+//require_once "classes/logger/sql.php";
 
 function ttrss_error_handler($errno, $errstr, $file, $line, $context) {
        global $logger;
@@ -42,6 +42,6 @@ function ttrss_fatal_handler() {
        }
 }
 
-register_shutdown_function('ttrss_fatal_handler');
-set_error_handler('ttrss_error_handler');
+//register_shutdown_function('ttrss_fatal_handler');
+//set_error_handler('ttrss_error_handler');
 ?>
index 0a686e71e6342e9226617ebede1a202df67244d0..f0d5e85faf8325250ae1dc24d16b21f2ebfeabe3 100644 (file)
        $fetch_last_content_type = false;
        $pluginhost = false;
 
-       function __autoload($class) {
-               $class_file = str_replace("_", "/", strtolower(basename($class)));
-
-               $file = dirname(__FILE__)."/../classes/$class_file.php";
-
-               if (file_exists($file)) {
-                       require $file;
-               }
-
-       }
-
        mb_internal_encoding("UTF-8");
        date_default_timezone_set('UTC');
        if (defined('E_DEPRECATED')) {
index bc8b7cff1db94053d62eda507ee9ab197cb1c134..f6e8bfe615067595371724671c7b42bd08a5a8ad 100644 (file)
@@ -2,7 +2,8 @@
        // Original from http://www.daniweb.com/code/snippet43.html
 
        require_once "config.php";
-       require_once "db.php";
+       require_once "classes/db.php";
+       require_once "autoload.php";
        require_once "errorhandler.php";
        require_once "lib/accept-to-gettext.php";
        require_once "lib/gettext/gettext.inc";
        ini_set("session.gc_maxlifetime", $session_expire);
        ini_set("session.cookie_lifetime", min(0, SESSION_COOKIE_LIFETIME));
 
-       global $session_connection;
-
-       function session_get_schema_version($link, $nocache = false) {
+       function session_get_schema_version($nocache = false) {
                global $schema_version;
 
                if (!$schema_version) {
-                       $result = db_query($link, "SELECT schema_version FROM ttrss_version");
-                       $version = db_fetch_result($result, 0, "schema_version");
+                       $result = Db::get()->query("SELECT schema_version FROM ttrss_version");
+                       $version = Db::get()->fetch_result($result, 0, "schema_version");
                        $schema_version = $version;
                        return $version;
                } else {
@@ -39,7 +38,6 @@
 
        function validate_session($link) {
                if (SINGLE_USER_MODE) return true;
-               if (!$link) return false;
 
                if (VERSION != $_SESSION["version"]) return false;
 
                        return false;
                }
 
-               if ($_SESSION["ref_schema_version"] != session_get_schema_version($link, true))
+               if ($_SESSION["ref_schema_version"] != session_get_schema_version(true))
                        return false;
 
                if (sha1($_SERVER['HTTP_USER_AGENT']) != $_SESSION["user_agent"])
                        return false;
 
                if ($_SESSION["uid"]) {
-                       $result = db_query($link,
+                       $result = Db::get()->query(
                                "SELECT pwd_hash FROM ttrss_users WHERE id = '".$_SESSION["uid"]."'");
 
                        // user not found
-                       if (db_num_rows($result) == 0) {
+                       if (Db::get()->num_rows($result) == 0) {
                                return false;
                        } else {
-                               $pwd_hash = db_fetch_result($result, 0, "pwd_hash");
+                               $pwd_hash = Db::get()->fetch_result($result, 0, "pwd_hash");
 
                                if ($pwd_hash != $_SESSION["pwd_hash"]) {
                                        return false;
                        }
                }
 
-/*             if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
-
-                       //print_r($_SESSION);
-
-                       if (time() > $_SESSION["cookie_lifetime"]) {
-                               return false;
-                       }
-               } */
-
                return true;
        }
 
 
        function ttrss_open ($s, $n) {
-               global $session_connection;
-
-               $session_connection = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
-
                return true;
        }
 
        function ttrss_read ($id){
+               global $session_expire;
+
+               $res = Db::get()->query("SELECT data FROM ttrss_sessions WHERE id='$id'");
 
-               global $session_connection,$session_read;
+               if (Db::get()->num_rows($res) != 1) {
 
-               $query = "SELECT data FROM ttrss_sessions WHERE id='$id'";
+                       $expire = time() + $session_expire;
 
-               $res = db_query($session_connection, $query);
+                       Db::get()->query("INSERT INTO ttrss_sessions (id, data, expire)
+                                       VALUES ('$id', '', '$expire')");
 
-               if (db_num_rows($res) != 1) {
                        return "";
                } else {
-                       $session_read = db_fetch_assoc($res);
-                       $session_read["data"] = base64_decode($session_read["data"]);
-                       return $session_read["data"];
+                       return base64_decode(Db::get()->fetch_result($res, 0, "data"));
                }
+
        }
 
        function ttrss_write ($id, $data) {
+               global $session_expire;
 
-               if (! $data) {
-                       return false;
-               }
-
-               global $session_connection, $session_read, $session_expire;
-
+               $data = base64_encode($data);
                $expire = time() + $session_expire;
 
-               $data = db_escape_string($session_connection, base64_encode($data), false);
-
-               if ($session_read) {
-                       $query = "UPDATE ttrss_sessions SET data='$data',
-                                       expire='$expire' WHERE id='$id'";
-               } else {
-                       $query = "INSERT INTO ttrss_sessions (id, data, expire)
-                                       VALUES ('$id', '$data', '$expire')";
-               }
+               Db::get()->query("UPDATE ttrss_sessions SET data='$data', expire='$expire' WHERE id='$id'");
 
-               db_query($session_connection, $query);
                return true;
        }
 
        function ttrss_close () {
-
-               global $session_connection;
-
-               //db_close($session_connection);
-
                return true;
        }
 
-       function ttrss_destroy ($id) {
-
-               global $session_connection;
-
-               $query = "DELETE FROM ttrss_sessions WHERE id = '$id'";
-
-               db_query($session_connection, $query);
+       function ttrss_destroy($id) {
+               Db::get()->query("DELETE FROM ttrss_sessions WHERE id = '$id'");
 
                return true;
        }
 
        function ttrss_gc ($expire) {
-
-               global $session_connection;
-
-               $query = "DELETE FROM ttrss_sessions WHERE expire < " . time();
-
-               db_query($session_connection, $query);
+               Db::get()->query("DELETE FROM ttrss_sessions WHERE expire < " . time());
        }
 
        if (!SINGLE_USER_MODE /* && DB_TYPE == "pgsql" */) {
                session_set_save_handler("ttrss_open",
                        "ttrss_close", "ttrss_read", "ttrss_write",
                        "ttrss_destroy", "ttrss_gc");
+               register_shutdown_function('session_write_close');
        }
 
        if (!defined('NO_SESSION_AUTOSTART')) {
index c21b46809f7d5a776501adaa1ba183300115b9fc..66e236dae28664e781243d8603f767b831d0a2fe 100644 (file)
--- a/index.php
+++ b/index.php
@@ -19,6 +19,7 @@
        set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
                get_include_path());
 
+       require_once "autoload.php";
        require_once "sessions.php";
        require_once "functions.php";
        require_once "sanity_check.php";
index ad866fbd5b2e9b20de2652b04978dbbd3dfa5ac7..709cfd4ccb79fbffc9e23ad10f14f243255d4e51 100644 (file)
--- a/opml.php
+++ b/opml.php
@@ -2,6 +2,7 @@
        set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
                get_include_path());
 
+       require_once "autoload.php";
        require_once "functions.php";
        require_once "sessions.php";
        require_once "sanity_check.php";
index 046d5eb76aabee33aa3cfa1ab4ab8b7382b0b860..7a7d2842cc99f4dd2f9a0834768363a3891a7d97 100644 (file)
--- a/prefs.php
+++ b/prefs.php
@@ -12,6 +12,7 @@
                exit;
        }
 
+       require_once "autoload.php";
        require_once "sessions.php";
        require_once "functions.php";
        require_once "sanity_check.php";
index 1a50538fb7dd8cdbd321927716b6979bfa65d3b2..ae6ae44b20b322a9f55a2c3dbfc3d8811c1cba48 100644 (file)
@@ -17,6 +17,7 @@
                $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
        }
 
+       require_once "autoload.php";
        require_once "sessions.php";
        require_once "functions.php";
        require_once "sanity_check.php";
index 9aec6dde7f1c001289739a7674a514ff0fc61a58..d7c60d4f01674886ab78c5d2ec926a56d61b2e27 100644 (file)
@@ -8,7 +8,7 @@
                get_include_path());
 
        require_once 'classes/ttrssmailer.php';
-
+       require_once "autoload.php";
        require_once "functions.php";
        require_once "sessions.php";
        require_once "sanity_check.php";
index ffe54cc7cdac8d44592e77541f08af05b23984a3..d9009f9652e47e134dd5b41427e0a91eeea81790 100755 (executable)
@@ -7,6 +7,7 @@
 
        chdir(dirname(__FILE__));
 
+       require_once "autoload.php";
        require_once "functions.php";
        require_once "rssfuncs.php";
        require_once "config.php";
index e8a56eec92a5396368fda7adbebc53aa2b01b563..6d13add0051135c4e65858184ba7ecfc13a11926 100755 (executable)
@@ -14,6 +14,7 @@
                define('DAEMON_EXTENDED_DEBUG', true);
        }
 
+       require_once "autoload.php";
        require_once "functions.php";
        require_once "rssfuncs.php";
        require_once "sanity_check.php";