From 8c0496f74c60e47d9e2f87bb107d79b99c0070aa Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 15 Mar 2013 19:54:18 +0400 Subject: [PATCH] Revert "Rewrote database support to classes, Fixed strict warning in sanitizedummy.php" This reverts commit 65d0cc64a2ca1ce259fbba501b13deaf1870e540. --- classes/Db/Abstract.php | 56 ---------------- classes/Db/Interface.php | 18 ----- classes/Db/Mysql.php | 71 -------------------- classes/Db/Pgsql.php | 80 ---------------------- classes/sanitizedummy.php | 2 +- include/db.php | 135 ++++++++++++++++++++++++++++---------- include/functions.php | 41 +++++++----- 7 files changed, 129 insertions(+), 274 deletions(-) delete mode 100644 classes/Db/Abstract.php delete mode 100644 classes/Db/Interface.php delete mode 100644 classes/Db/Mysql.php delete mode 100644 classes/Db/Pgsql.php diff --git a/classes/Db/Abstract.php b/classes/Db/Abstract.php deleted file mode 100644 index d84701fd7..000000000 --- a/classes/Db/Abstract.php +++ /dev/null @@ -1,56 +0,0 @@ -dbconn; - } - - public function init() { } - - public function escape_string($s, $strip_tags = true) { } - - public function query($query, $die_on_error = true) { } - - public function fetch_assoc($result) { } - - public function num_rows($result) { } - - public function fetch_result($result, $row, $param) { } - - public function unescape_string($str) - { - $tmp = str_replace("\\\"", "\"", $str); - $tmp = str_replace("\\'", "'", $tmp); - return $tmp; - } - - public function close() { } - - public function affected_rows($result) { } - - public function last_error() { } - - public function quote($str) - { - return("'$str'"); - } - -} \ No newline at end of file diff --git a/classes/Db/Interface.php b/classes/Db/Interface.php deleted file mode 100644 index c5bfad56f..000000000 --- a/classes/Db/Interface.php +++ /dev/null @@ -1,18 +0,0 @@ -dbconn = $link; - return $link; - } else { - die("Unable to connect to database (as $user to $host, database $db): " . mysql_error()); - } - } - - public function init() - { - db_query($this->dbconn, "SET time_zone = '+0:0'"); - - if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { - db_query($this->dbconn, "SET NAMES " . MYSQL_CHARSET); - } - } - - public function escape_string($s, $strip_tags = true) - { - if ($strip_tags) $s = strip_tags($s); - return mysql_real_escape_string($s); - } - - public function query($query, $die_on_error = true) - { - $result = mysql_query($query, $this->dbconn); - if (!$result) { - $query = htmlspecialchars($query); - if ($die_on_error) { - die("Query $query failed: " . ($this->dbconn ? mysql_error($this->dbconn) : "No connection")); - } - } - return $result; - } - - public function fetch_assoc($result) { - return mysql_fetch_assoc($result); - } - - public function num_rows($result) { - return mysql_num_rows($result); - } - - public function fetch_result($result, $row, $param) { - // I hate incoherent naming of PHP functions - return mysql_result($result, $row, $param); - } - - public function close() { - return mysql_close($this->dbconn); - } - - public function affected_rows($result) { - return mysql_affected_rows($this->dbconn); - } - - public function last_error() { - return mysql_error($this->dbconn); - } -} diff --git a/classes/Db/Pgsql.php b/classes/Db/Pgsql.php deleted file mode 100644 index 39ed181d7..000000000 --- a/classes/Db/Pgsql.php +++ /dev/null @@ -1,80 +0,0 @@ -dbconn = $link; - return $link; - } - - public function init() - { - pg_query($this->dbconn, "set client_encoding = 'UTF-8'"); - pg_set_client_encoding("UNICODE"); - pg_query($this->dbconn, "set datestyle = 'ISO, european'"); - pg_query($this->dbconn, "set TIME ZONE 0"); - } - - public function escape_string($s, $strip_tags = true) - { - if ($strip_tags) $s = strip_tags($s); - return pg_escape_string($s); - } - - public function query($query, $die_on_error = true) - { - $result = pg_query($this->dbconn, $query); - if (!$result) { - $query = htmlspecialchars($query); // just in case - if ($die_on_error) { - die("Query $query failed [$result]: " . ($this->dbconn ? pg_last_error($this->dbconn) : "No connection")); - } - } - return $result; - } - - public function fetch_assoc($result) { - return pg_fetch_assoc($result); - } - - public function num_rows($result) { - return pg_num_rows($result); - } - - public function fetch_result($result, $row, $param) { - return pg_fetch_result($result, $row, $param); - } - - public function close() { - return pg_close($this->dbconn); - } - - public function affected_rows($result) { - return pg_affected_rows($result); - } - - public function last_error() { - return pg_last_error($this->dbconn); - } -} diff --git a/classes/sanitizedummy.php b/classes/sanitizedummy.php index 7182fb7f5..a184bec30 100644 --- a/classes/sanitizedummy.php +++ b/classes/sanitizedummy.php @@ -1,6 +1,6 @@ connect($host, $user, $pass, $db_name); +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')) { + $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()); + } + } } function db_escape_string($s, $strip_tags = true) { - global $db; - return $db->escape_string($s, $strip_tags); + if ($strip_tags) $s = strip_tags($s); + + if (DB_TYPE == "pgsql") { + return pg_escape_string($s); + } else { + return mysql_real_escape_string($s); + } } function db_query($link, $query, $die_on_error = true) { - global $db; - return $db->query($query, $die_on_error); + if (DB_TYPE == "pgsql") { + $result = pg_query($link, $query); + if (!$result) { + $query = htmlspecialchars($query); // just in case + if ($die_on_error) { + die("Query $query 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 $query failed: " . ($link ? mysql_error($link) : "No connection")); + } + } + return $result; + } } function db_fetch_assoc($result) { - global $db; - return $db->fetch_assoc($result); + if (DB_TYPE == "pgsql") { + return pg_fetch_assoc($result); + } else if (DB_TYPE == "mysql") { + return mysql_fetch_assoc($result); + } } + function db_num_rows($result) { - global $db; - return $db->num_rows($result); + if (DB_TYPE == "pgsql") { + return pg_num_rows($result); + } else if (DB_TYPE == "mysql") { + return mysql_num_rows($result); + } } function db_fetch_result($result, $row, $param) { - global $db; - return $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) { - global $db; - return $db->unescape_string($str); + $tmp = str_replace("\\\"", "\"", $str); + $tmp = str_replace("\\'", "'", $tmp); + return $tmp; } function db_close($link) { - global $db; - return $db->close(); + if (DB_TYPE == "pgsql") { + + return pg_close($link); + + } else if (DB_TYPE == "mysql") { + return mysql_close($link); + } } function db_affected_rows($link, $result) { - global $db; - return $db->affected_rows($result); + if (DB_TYPE == "pgsql") { + return pg_affected_rows($result); + } else if (DB_TYPE == "mysql") { + return mysql_affected_rows($link); + } } function db_last_error($link) { - global $db; - return $db->last_error(); + if (DB_TYPE == "pgsql") { + return pg_last_error($link); + } else if (DB_TYPE == "mysql") { + return mysql_error($link); + } } function db_quote($str){ - global $db; - return $db->quote($str); + return("'$str'"); } -?> \ No newline at end of file +?> diff --git a/include/functions.php b/include/functions.php index 4dc286eb6..54b840329 100644 --- a/include/functions.php +++ b/include/functions.php @@ -6,17 +6,14 @@ $pluginhost = false; function __autoload($class) { - $class_file1 = str_replace("_", "/", basename($class)); // PSR-0 - $class_file2 = str_replace("_", "/", strtolower(basename($class))); + $class_file = str_replace("_", "/", strtolower(basename($class))); - $file1 = dirname(__FILE__)."/../classes/$class_file1.php"; - $file2 = dirname(__FILE__)."/../classes/$class_file2.php"; + $file = dirname(__FILE__)."/../classes/$class_file.php"; - if (file_exists($file1)) { - require $file1; - } elseif (file_exists($file2)) { - require $file2; + if (file_exists($file)) { + require $file; } + } mb_internal_encoding("UTF-8"); @@ -202,6 +199,8 @@ ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'"); } + $rows = pg_affected_rows($result); + } else { /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE @@ -217,8 +216,9 @@ $query_limit ttrss_entries.date_updated < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); + $rows = mysql_affected_rows($link); + } - $rows = db_affected_rows($link, $result); ccache_update($link, $feed_id, $owner_uid); @@ -952,7 +952,7 @@ } function sql_random_function() { - if (DB_TYPE == "mysql") { + if (DB_TYPE == "mysql") { return "RAND()"; } else { return "RANDOM()"; @@ -3208,13 +3208,24 @@ } function init_connection($link) { - global $db; - if ($db) { - $db->init(); + if ($link) { + + if (DB_TYPE == "pgsql") { + pg_query($link, "set client_encoding = 'UTF-8'"); + pg_set_client_encoding("UNICODE"); + pg_query($link, "set datestyle = 'ISO, european'"); + pg_query($link, "set TIME ZONE 0"); + } else { + db_query($link, "SET time_zone = '+0:0'"); + + if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { + db_query($link, "SET NAMES " . MYSQL_CHARSET); + } + } global $pluginhost; - $pluginhost = new PluginHost($db->getLink()); + $pluginhost = new PluginHost($link); $pluginhost->load(PLUGINS, $pluginhost::KIND_ALL); return true; @@ -3903,4 +3914,4 @@ return in_array($interface, class_implements($class)); } -?> \ No newline at end of file +?>