From 7dd6f9b97c39bd6fac4ed143a0974154a6eb25a5 Mon Sep 17 00:00:00 2001 From: "Michael Kaufmann (d00p)" Date: Sat, 13 Dec 2014 19:19:10 +0100 Subject: [PATCH] explicitly set charset / collation of database when installing + new integrity-check-function to validate the database charset / collation and optionally fix it; fixes #1426 Signed-off-by: Michael Kaufmann (d00p) --- install/lib/class.FroxlorInstall.php | 2 +- .../integrity/class.IntegrityCheck.php | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/install/lib/class.FroxlorInstall.php b/install/lib/class.FroxlorInstall.php index 4d85e355..305017d3 100644 --- a/install/lib/class.FroxlorInstall.php +++ b/install/lib/class.FroxlorInstall.php @@ -567,7 +567,7 @@ class FroxlorInstall { // we have to create a new user and database for the froxlor unprivileged mysql access $content .= $this->_status_message('begin', $this->_lng['install']['create_mysqluser_and_db']); - $ins_stmt = $db_root->prepare("CREATE DATABASE `".str_replace('`', '', $this->_data['mysql_database'])."`"); + $ins_stmt = $db_root->prepare("CREATE DATABASE `".str_replace('`', '', $this->_data['mysql_database'])."` CHARACTER SET=utf8 COLLATE=utf8_general_ci"); $ins_stmt->execute(); $mysql_access_host_array = array_map('trim', explode(',', $this->_data['mysql_access_host'])); diff --git a/lib/classes/integrity/class.IntegrityCheck.php b/lib/classes/integrity/class.IntegrityCheck.php index 456a4647..0216fc87 100644 --- a/lib/classes/integrity/class.IntegrityCheck.php +++ b/lib/classes/integrity/class.IntegrityCheck.php @@ -58,6 +58,41 @@ class IntegrityCheck { return $integrityok; } + /** + * check whether the froxlor database and its tables are in utf-8 character-set + * + * @param bool $fix fix db charset/collation if not utf8 + * + * @return boolean + */ + public function DatabaseCharset($fix = false) { + + // get characterset + $cs_stmt = Database::prepare('SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = :dbname'); + $resp = Database::pexecute_first($cs_stmt, array('dbname' => Database::getDbName())); + $charset = isset($resp['default_character_set_name']) ? $resp['default_character_set_name'] : null; + if (!empty($charset) && strtolower($charset) != 'utf8') { + if ($fix) { + // fix database + Database::query('ALTER DATABASE `' . Database::getDbName() . '` CHARACTER SET utf8 COLLATE utf8_general_ci'); + // fix all tables + $handle = Database::query('SHOW TABLES'); + while ($row = $handle->fetch(PDO::FETCH_ASSOC)) { + foreach ($row as $table) { + Database::query('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;'); + } + } + } else { + return false; + } + } + + if ($fix) { + return $this->DatabaseCharset(); + } + return true; + } + /** * Check the integrity of the domain to ip/port - association * @param $fix Fix everything found directly