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) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2014-12-13 19:19:10 +01:00
parent 28b7cd6e90
commit 7dd6f9b97c
2 changed files with 36 additions and 1 deletions

View File

@@ -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']));

View File

@@ -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