diff --git a/core/_install.php b/core/_install.php index dcc622be..99fa864d 100644 --- a/core/_install.php +++ b/core/_install.php @@ -110,7 +110,7 @@ function do_install() { // {{{ if (file_exists("data/config/auto_install.conf.php")) { require_once "data/config/auto_install.conf.php"; - } elseif (@$_POST["database_type"] == "sqlite") { + } elseif (@$_POST["database_type"] == Database::SQLITE_DRIVER) { $id = bin2hex(random_bytes(5)); define('DATABASE_DSN', "sqlite:data/shimmie.{$id}.sqlite"); } elseif (isset($_POST['database_type']) && isset($_POST['database_host']) && isset($_POST['database_user']) && isset($_POST['database_name'])) { @@ -153,9 +153,9 @@ function ask_questions() $drivers = PDO::getAvailableDrivers(); if ( - !in_array("mysql", $drivers) && - !in_array("pgsql", $drivers) && - !in_array("sqlite", $drivers) + !in_array(Database::MYSQL_DRIVER, $drivers) && + !in_array(Database::PGSQL_DRIVER, $drivers) && + !in_array(Database::SQLITE_DRIVER, $drivers) ) { $errors[] = " No database connection library could be found; shimmie needs @@ -163,9 +163,9 @@ function ask_questions() "; } - $db_m = in_array("mysql", $drivers) ? '' : ""; - $db_p = in_array("pgsql", $drivers) ? '' : ""; - $db_s = in_array("sqlite", $drivers) ? '' : ""; + $db_m = in_array(Database::MYSQL_DRIVER, $drivers) ? '' : ""; + $db_p = in_array(Database::PGSQL_DRIVER, $drivers) ? '' : ""; + $db_s = in_array(Database::SQLITE_DRIVER, $drivers) ? '' : ""; $warn_msg = $warnings ? "

Warnings

".implode("\n

", $warnings) : ""; $err_msg = $errors ? "

Errors

".implode("\n

", $errors) : ""; diff --git a/core/database.php b/core/database.php index c6135878..29381209 100644 --- a/core/database.php +++ b/core/database.php @@ -4,6 +4,10 @@ */ class Database { + const MYSQL_DRIVER = "mysql"; + const PGSQL_DRIVER = "pgsql"; + const SQLITE_DRIVER = "sqlite"; + /** * The PDO database connection object, for anyone who wants direct access. * @var null|PDO @@ -72,7 +76,7 @@ class Database // https://bugs.php.net/bug.php?id=70221 $ka = DATABASE_KA; - if (version_compare(PHP_VERSION, "6.9.9") == 1 && $this->get_driver_name() == "sqlite") { + if (version_compare(PHP_VERSION, "6.9.9") == 1 && $this->get_driver_name() == self::SQLITE_DRIVER) { $ka = false; } @@ -96,11 +100,11 @@ class Database throw new SCoreException("Can't figure out database engine"); } - if ($db_proto === "mysql") { + if ($db_proto === self::MYSQL_DRIVER) { $this->engine = new MySQL(); - } elseif ($db_proto === "pgsql") { + } elseif ($db_proto === self::PGSQL_DRIVER) { $this->engine = new PostgreSQL(); - } elseif ($db_proto === "sqlite") { + } elseif ($db_proto === self::SQLITE_DRIVER) { $this->engine = new SQLite(); } else { die('Unknown PDO driver: '.$db_proto); @@ -224,7 +228,7 @@ class Database } return $stmt; } catch (PDOException $pdoe) { - throw new SCoreException($pdoe->getMessage()."

Query: ".$query); + throw new SCoreException($pdoe->getMessage()."

Query: ".$query, $pdoe->getCode(), $pdoe); } } @@ -296,7 +300,7 @@ class Database */ public function get_last_insert_id(string $seq): int { - if ($this->engine->name == "pgsql") { + if ($this->engine->name == self::PGSQL_DRIVER) { return $this->db->lastInsertId($seq); } else { return $this->db->lastInsertId(); @@ -326,15 +330,15 @@ class Database $this->connect_db(); } - if ($this->engine->name === "mysql") { + if ($this->engine->name === self::MYSQL_DRIVER) { return count( $this->get_all("SHOW TABLES") ); - } elseif ($this->engine->name === "pgsql") { + } elseif ($this->engine->name === self::PGSQL_DRIVER) { return count( $this->get_all("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'") ); - } elseif ($this->engine->name === "sqlite") { + } elseif ($this->engine->name === self::SQLITE_DRIVER) { return count( $this->get_all("SELECT name FROM sqlite_master WHERE type = 'table'") ); diff --git a/core/dbengine.php b/core/dbengine.php index bb7c674b..d76a1a43 100644 --- a/core/dbengine.php +++ b/core/dbengine.php @@ -22,7 +22,7 @@ class DBEngine class MySQL extends DBEngine { /** @var string */ - public $name = "mysql"; + public $name = Database::MYSQL_DRIVER; public function init(PDO $db) { @@ -54,7 +54,7 @@ class MySQL extends DBEngine class PostgreSQL extends DBEngine { /** @var string */ - public $name = "pgsql"; + public $name = Database::PGSQL_DRIVER; public function init(PDO $db) { @@ -136,7 +136,7 @@ function _ln($n) class SQLite extends DBEngine { /** @var string */ - public $name = "sqlite"; + public $name = Database::SQLITE_DRIVER; public function init(PDO $db) { diff --git a/core/imageboard/image.php b/core/imageboard/image.php index 928d4914..af6a15d8 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -590,7 +590,7 @@ class Image public function delete_tags_from_image(): void { global $database; - if ($database->get_driver_name() == "mysql") { + if ($database->get_driver_name() == Database::MYSQL_DRIVER) { //mysql < 5.6 has terrible subquery optimization, using EXISTS / JOIN fixes this $database->execute( " @@ -907,7 +907,7 @@ class Image // more than one positive tag, or more than zero negative tags else { - if ($database->get_driver_name() === "mysql") { + if ($database->get_driver_name() === Database::MYSQL_DRIVER) { $query = Image::build_ugly_search_querylet($tag_querylets); } else { $query = Image::build_accurate_search_querylet($tag_querylets); diff --git a/core/user.php b/core/user.php index 098c7723..a2a4d537 100644 --- a/core/user.php +++ b/core/user.php @@ -69,7 +69,7 @@ class User global $config, $database; $row = $database->cache->get("user-session:$name-$session"); if (!$row) { - if ($database->get_driver_name() === "mysql") { + if ($database->get_driver_name() === Database::MYSQL_DRIVER) { $query = "SELECT * FROM users WHERE name = :name AND md5(concat(pass, :ip)) = :sess"; } else { $query = "SELECT * FROM users WHERE name = :name AND md5(pass || :ip) = :sess"; diff --git a/ext/admin/main.php b/ext/admin/main.php index 212b07fb..2b484bc1 100644 --- a/ext/admin/main.php +++ b/ext/admin/main.php @@ -201,14 +201,14 @@ class AdminPage extends Extension $database = $matches['dbname']; switch ($software) { - case 'mysql': + case Database::MYSQL_DRIVER: $cmd = "mysqldump -h$hostname -u$username -p$password $database"; break; - case 'pgsql': + case Database::PGSQL_DRIVER: putenv("PGPASSWORD=$password"); $cmd = "pg_dump -h $hostname -U $username $database"; break; - case 'sqlite': + case Database::SQLITE_DRIVER: $cmd = "sqlite3 $database .dump"; break; default: @@ -257,7 +257,7 @@ class AdminPage extends Extension //TODO: Update score_log (Having an optional ID column for score_log would be nice..) preg_match("#^(?P\w+)\:(?:user=(?P\w+)(?:;|$)|password=(?P\w*)(?:;|$)|host=(?P[\w\.\-]+)(?:;|$)|dbname=(?P[\w_]+)(?:;|$))+#", DATABASE_DSN, $matches); - if ($matches['proto'] == "mysql") { + if ($matches['proto'] == Database::MYSQL_DRIVER) { $tables = $database->get_col("SELECT TABLE_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = :db @@ -280,9 +280,9 @@ class AdminPage extends Extension $i++; } $database->execute("ALTER TABLE images AUTO_INCREMENT=".(count($ids) + 1)); - } elseif ($matches['proto'] == "pgsql") { + } elseif ($matches['proto'] == Database::PGSQL_DRIVER) { //TODO: Make this work with PostgreSQL - } elseif ($matches['proto'] == "sqlite") { + } elseif ($matches['proto'] == Database::SQLITE_DRIVER) { //TODO: Make this work with SQLite } return true; diff --git a/ext/admin/theme.php b/ext/admin/theme.php index 3e60d224..5d3de30f 100644 --- a/ext/admin/theme.php +++ b/ext/admin/theme.php @@ -45,7 +45,7 @@ class AdminPageTheme extends Themelet $html .= $this->button("Download all images", "download_all_images", false); } $html .= $this->button("Download database contents", "database_dump", false); - if ($database->get_driver_name() == "mysql") { + if ($database->get_driver_name() == Database::MYSQL_DRIVER) { $html .= $this->button("Reset image IDs", "reset_image_ids", true); } $page->add_block(new Block("Misc Admin Tools", $html)); diff --git a/ext/comment/main.php b/ext/comment/main.php index 85a9672b..75b171d4 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -480,14 +480,14 @@ class CommentList extends Extension global $config, $database; // sqlite fails at intervals - if ($database->get_driver_name() === "sqlite") { + if ($database->get_driver_name() === Database::SQLITE_DRIVER) { return false; } $window = int_escape($config->get_int('comment_window')); $max = int_escape($config->get_int('comment_limit')); - if ($database->get_driver_name() == "mysql") { + if ($database->get_driver_name() == Database::MYSQL_DRIVER) { $window_sql = "interval $window minute"; } else { $window_sql = "interval '$window minute'"; diff --git a/ext/index/test.php b/ext/index/test.php index e9debe74..8f57bdb2 100644 --- a/ext/index/test.php +++ b/ext/index/test.php @@ -157,7 +157,7 @@ class IndexTest extends ShimmiePHPUnitTestCase global $database; $db = $database->get_driver_name(); - if ($db == "pgsql" || $db == "sqlite") { + if ($db == Database::PGSQL_DRIVER || $db == Database::SQLITE_DRIVER) { $this->markTestIncomplete(); } diff --git a/ext/ipban/main.php b/ext/ipban/main.php index 659246f9..541c853b 100644 --- a/ext/ipban/main.php +++ b/ext/ipban/main.php @@ -235,7 +235,7 @@ class IPBan extends Extension { global $config, $database; - $prefix = ($database->get_driver_name() == "sqlite" ? "bans." : ""); + $prefix = ($database->get_driver_name() == Database::SQLITE_DRIVER ? "bans." : ""); $bans = $this->get_active_bans(); diff --git a/ext/ipban/theme.php b/ext/ipban/theme.php index 6529c51f..67979128 100644 --- a/ext/ipban/theme.php +++ b/ext/ipban/theme.php @@ -16,7 +16,7 @@ class IPBanTheme extends Themelet { global $database, $user; $h_bans = ""; - $prefix = ($database->get_driver_name() == "sqlite" ? "bans." : ""); + $prefix = ($database->get_driver_name() == Database::SQLITE_DRIVER ? "bans." : ""); foreach ($bans as $ban) { $end_human = date('Y-m-d', $ban[$prefix.'end_timestamp']); $h_bans .= " diff --git a/ext/log_db/main.php b/ext/log_db/main.php index b185c783..5b400b12 100644 --- a/ext/log_db/main.php +++ b/ext/log_db/main.php @@ -68,7 +68,7 @@ class LogDatabase extends Extension $args["module"] = $_GET["module"]; } if (!empty($_GET["user"])) { - if ($database->get_driver_name() == "pgsql") { + if ($database->get_driver_name() == Database::PGSQL_DRIVER) { if (preg_match("#\d+\.\d+\.\d+\.\d+(/\d+)?#", $_GET["user"])) { $wheres[] = "(username = :user1 OR text(address) = :user2)"; $args["user1"] = $_GET["user"]; diff --git a/ext/rating/main.php b/ext/rating/main.php index 18b40823..9f32280d 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -37,7 +37,7 @@ class RatingSetEvent extends Event class Ratings extends Extension { - protected $db_support = ['mysql','pgsql']; // ? + protected $db_support = [Database::MYSQL_DRIVER,Database::PGSQL_DRIVER]; public function get_priority(): int { @@ -331,10 +331,10 @@ class Ratings extends Extension if ($config->get_int("ext_ratings2_version") < 3) { $database->Execute("UPDATE images SET rating = 'u' WHERE rating is null"); switch ($database->get_driver_name()) { - case "mysql": + case Database::MYSQL_DRIVER: $database->Execute("ALTER TABLE images CHANGE rating rating CHAR(1) NOT NULL DEFAULT 'u'"); break; - case "pgsql": + case Database::PGSQL_DRIVER: $database->Execute("ALTER TABLE images ALTER COLUMN rating SET DEFAULT 'u'"); $database->Execute("ALTER TABLE images ALTER COLUMN rating SET NOT NULL"); break; diff --git a/ext/relatationships/main.php b/ext/relatationships/main.php index 28e785d1..402f4fd0 100644 --- a/ext/relatationships/main.php +++ b/ext/relatationships/main.php @@ -8,7 +8,7 @@ class Relationships extends Extension { - protected $db_support = ['mysql', 'pgsql']; + protected $db_support = [Database::MYSQL_DRIVER, Database::PGSQL_DRIVER]; public function onInitExt(InitExtEvent $event) { diff --git a/ext/rss_comments/main.php b/ext/rss_comments/main.php index 4f3b5ed5..dfc37717 100644 --- a/ext/rss_comments/main.php +++ b/ext/rss_comments/main.php @@ -9,7 +9,7 @@ class RSS_Comments extends Extension { - protected $db_support = ['mysql', 'sqlite']; // pgsql has no UNIX_TIMESTAMP + protected $db_support = [Database::MYSQL_DRIVER, Database::SQLITE_DRIVER]; // pgsql has no UNIX_TIMESTAMP public function onPostListBuilding(PostListBuildingEvent $event) { diff --git a/ext/rule34/main.php b/ext/rule34/main.php index a39b6949..577ca5af 100644 --- a/ext/rule34/main.php +++ b/ext/rule34/main.php @@ -19,7 +19,7 @@ if ( // kill these glitched requests immediately class Rule34 extends Extension { - protected $db_support = ['pgsql']; # Only PG has the NOTIFY pubsub system + protected $db_support = [Database::PGSQL_DRIVER]; # Only PG has the NOTIFY pubsub system public function onImageDeletion(ImageDeletionEvent $event) { diff --git a/ext/rule34/theme.php b/ext/rule34/theme.php index 09712b3d..d4dd29e1 100644 --- a/ext/rule34/theme.php +++ b/ext/rule34/theme.php @@ -19,7 +19,7 @@ class Rule34Theme extends Themelet { global $database, $user; $h_bans = ""; - $prefix = ($database->get_driver_name() == "sqlite" ? "bans." : ""); + $prefix = ($database->get_driver_name() == Database::SQLITE_DRIVER ? "bans." : ""); foreach ($bans as $ban) { $h_bans .= " diff --git a/ext/tips/main.php b/ext/tips/main.php index f4f7d619..04fb5124 100644 --- a/ext/tips/main.php +++ b/ext/tips/main.php @@ -10,7 +10,7 @@ class Tips extends Extension { - protected $db_support = ['mysql', 'sqlite']; // rand() ? + protected $db_support = [Database::MYSQL_DRIVER, Database::SQLITE_DRIVER]; // rand() ? public function onInitExt(InitExtEvent $event) { diff --git a/ext/upgrade/main.php b/ext/upgrade/main.php index 3321b409..0aef4530 100644 --- a/ext/upgrade/main.php +++ b/ext/upgrade/main.php @@ -44,7 +44,7 @@ class Upgrade extends Extension $config->set_bool("in_upgrade", true); $config->set_int("db_version", 9); - if ($database->get_driver_name() == 'mysql') { + if ($database->get_driver_name() == Database::MYSQL_DRIVER) { $tables = $database->get_col("SHOW TABLES"); foreach ($tables as $table) { log_info("upgrade", "converting $table to innodb"); @@ -84,7 +84,7 @@ class Upgrade extends Extension $config->set_bool("in_upgrade", true); $config->set_int("db_version", 12); - if ($database->get_driver_name() == 'pgsql') { + if ($database->get_driver_name() == Database::PGSQL_DRIVER) { log_info("upgrade", "Changing ext column to VARCHAR"); $database->execute("ALTER TABLE images ALTER COLUMN ext SET DATA TYPE VARCHAR(4)"); } @@ -101,9 +101,9 @@ class Upgrade extends Extension $config->set_int("db_version", 13); log_info("upgrade", "Changing password column to VARCHAR(250)"); - if ($database->get_driver_name() == 'pgsql') { + if ($database->get_driver_name() == Database::PGSQL_DRIVER) { $database->execute("ALTER TABLE users ALTER COLUMN pass SET DATA TYPE VARCHAR(250)"); - } elseif ($database->get_driver_name() == 'mysql') { + } elseif ($database->get_driver_name() == Database::MYSQL_DRIVER) { $database->execute("ALTER TABLE users CHANGE pass pass VARCHAR(250)"); } @@ -116,11 +116,11 @@ class Upgrade extends Extension $config->set_int("db_version", 14); log_info("upgrade", "Changing tag column to VARCHAR(255)"); - if ($database->get_driver_name() == 'pgsql') { + if ($database->get_driver_name() == Database::PGSQL_DRIVER) { $database->execute('ALTER TABLE tags ALTER COLUMN tag SET DATA TYPE VARCHAR(255)'); $database->execute('ALTER TABLE aliases ALTER COLUMN oldtag SET DATA TYPE VARCHAR(255)'); $database->execute('ALTER TABLE aliases ALTER COLUMN newtag SET DATA TYPE VARCHAR(255)'); - } elseif ($database->get_driver_name() == 'mysql') { + } elseif ($database->get_driver_name() == Database::MYSQL_DRIVER) { $database->execute('ALTER TABLE tags MODIFY COLUMN tag VARCHAR(255) NOT NULL'); $database->execute('ALTER TABLE aliases MODIFY COLUMN oldtag VARCHAR(255) NOT NULL'); $database->execute('ALTER TABLE aliases MODIFY COLUMN newtag VARCHAR(255) NOT NULL');