From: Andrew Dolgov Date: Mon, 10 Nov 2008 05:29:19 +0000 (+0100) Subject: code cleanup, test for db_escape() crazyness in DB sanity check X-Git-Tag: 1.2.29~10 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=f29ba1484f4a230651a40f4b3e26444d9748453f;p=tt-rss.git code cleanup, test for db_escape() crazyness in DB sanity check --- diff --git a/backend.php b/backend.php index fa116458..b298ec73 100644 --- a/backend.php +++ b/backend.php @@ -46,15 +46,7 @@ return; } - if (DB_TYPE == "pgsql") { - pg_query("set client_encoding = 'UTF-8'"); - pg_set_client_encoding("UNICODE"); - } else { - if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { - db_query($link, "SET NAMES " . MYSQL_CHARSET); -// db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); - } - } + init_connection($link); $op = $_REQUEST["op"]; diff --git a/errors.php b/errors.php index dc47f38e..bd7b53fe 100644 --- a/errors.php +++ b/errors.php @@ -28,4 +28,6 @@ official site for more information."); $ERRORS[11] = "[This error is not returned by server]"; + + $ERRORS[12] = __("SQL escaping test failed, check your database and PHP configuration"); ?> diff --git a/functions.php b/functions.php index e1f4a229..48d6e3a8 100644 --- a/functions.php +++ b/functions.php @@ -1980,6 +1980,10 @@ } } + if (db_escape_string("testTEST") != "testTEST") { + $error_code = 12; + } + error_reporting (DEFAULT_ERROR_LEVEL); if ($error_code != 0) { @@ -5843,4 +5847,16 @@ function feed_has_icon($id) { return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0; } + + function init_connection($link) { + if (DB_TYPE == "pgsql") { + pg_query("set client_encoding = 'UTF-8'"); + pg_set_client_encoding("UNICODE"); + } else { + if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { + db_query($link, "SET NAMES " . MYSQL_CHARSET); + // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); + } + } + } ?> diff --git a/mobile/tt-rss.php b/mobile/tt-rss.php index 11316d4d..a8bf6fb5 100644 --- a/mobile/tt-rss.php +++ b/mobile/tt-rss.php @@ -13,15 +13,7 @@ $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); - if (DB_TYPE == "pgsql") { - pg_query("set client_encoding = 'UTF-8'"); - pg_set_client_encoding("UNICODE"); - } else { - if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { - db_query($link, "SET NAMES " . MYSQL_CHARSET); -// db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); - } - } + init_connection($link); login_sequence($link, true); diff --git a/mysql_convert_unicode.php b/mysql_convert_unicode.php index 6fe72994..6d43c234 100644 --- a/mysql_convert_unicode.php +++ b/mysql_convert_unicode.php @@ -9,16 +9,8 @@ require_once "db.php"; $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); - - if (DB_TYPE == "pgsql") { - pg_query($link, "set client_encoding = 'utf-8'"); - pg_set_client_encoding("UNICODE"); - } else { - if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { - db_query($link, "SET NAMES " . MYSQL_CHARSET); -// db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); - } - } + + init_connection($link); login_sequence($link); diff --git a/opml.php b/opml.php index 35d7a070..44e94ee3 100644 --- a/opml.php +++ b/opml.php @@ -10,16 +10,7 @@ $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); - if (DB_TYPE == "pgsql") { - pg_query($link, "set client_encoding = 'utf-8'"); - pg_set_client_encoding("UNICODE"); - } else { - if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { - db_query($link, "SET NAMES " . MYSQL_CHARSET); -// db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); - } - } - + init_connection($link); login_sequence($link); $owner_uid = $_SESSION["uid"]; diff --git a/update.php b/update.php index f59f9ef3..76e30885 100644 --- a/update.php +++ b/update.php @@ -9,17 +9,8 @@ require_once "db.php"; $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); - - if (DB_TYPE == "pgsql") { - pg_query($link, "set client_encoding = 'utf-8'"); - pg_set_client_encoding("UNICODE"); - } else { - if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { - db_query($link, "SET NAMES " . MYSQL_CHARSET); -// db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); - } - } + init_connection($link); login_sequence($link); $owner_uid = $_SESSION["uid"]; diff --git a/update_daemon.php b/update_daemon.php index 67a8781d..35177edd 100644 --- a/update_daemon.php +++ b/update_daemon.php @@ -57,6 +57,8 @@ "Maybe another daemon is already running.\n"); } + // Testing database connection. + // It is unnecessary to start the fork loop if database is not ok. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); if (!$link) { @@ -67,16 +69,6 @@ return; } - if (DB_TYPE == "pgsql") { - pg_query("set client_encoding = 'utf-8'"); - pg_set_client_encoding("UNICODE"); - } else { - if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { - db_query($link, "SET NAMES " . MYSQL_CHARSET); -// db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); - } - } - db_close($link); $last_purge = 0; diff --git a/update_daemon2.php b/update_daemon2.php index be652777..08f5d74c 100644 --- a/update_daemon2.php +++ b/update_daemon2.php @@ -113,7 +113,6 @@ db_close($link); - while (true) { // Since sleep is interupted by SIGCHLD, we need another way to @@ -155,15 +154,7 @@ return; } - if (DB_TYPE == "pgsql") { - pg_query("set client_encoding = 'utf-8'"); - pg_set_client_encoding("UNICODE"); - } else { - if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { - db_query($link, "SET NAMES " . MYSQL_CHARSET); - // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); - } - } + init_connection($link); // We disable stamp file, since it is of no use in a multiprocess update. // not really, tho for the time being -fox diff --git a/update_daemon_loop.php b/update_daemon_loop.php index 737194ae..b1197ff3 100644 --- a/update_daemon_loop.php +++ b/update_daemon_loop.php @@ -54,15 +54,7 @@ return; } - if (DB_TYPE == "pgsql") { - pg_query("set client_encoding = 'utf-8'"); - pg_set_client_encoding("UNICODE"); - } else { - if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { - db_query($link, "SET NAMES " . MYSQL_CHARSET); -// db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); - } - } + init_connection($link); $last_purge = 0; diff --git a/update_feeds.php b/update_feeds.php index 139da699..2bb91dc3 100644 --- a/update_feeds.php +++ b/update_feeds.php @@ -36,15 +36,7 @@ return; } - if (DB_TYPE == "pgsql") { - pg_query("set client_encoding = 'utf-8'"); - pg_set_client_encoding("UNICODE"); - } else { - if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { - db_query($link, "SET NAMES " . MYSQL_CHARSET); -// db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); - } - } + init_connection($link); // Purge all posts (random 30 feeds) global_purge_old_posts($link, true, 30); diff --git a/xml-rpc.php b/xml-rpc.php index 0117657d..2a23f57f 100644 --- a/xml-rpc.php +++ b/xml-rpc.php @@ -19,15 +19,7 @@ return; } - if (DB_TYPE == "pgsql") { - pg_query("set client_encoding = 'utf-8'"); - pg_set_client_encoding("UNICODE"); - } else { - if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { - db_query($link, "SET NAMES " . MYSQL_CHARSET); -// db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET); - } - } + init_connection($link); function getVirtualFeeds($msg) { global $link;