minor fixes in installation-class, better error-handling

Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2013-12-03 09:46:00 +01:00
parent 84f973ce26
commit cf23980f0b
3 changed files with 80 additions and 52 deletions

View File

@@ -209,6 +209,7 @@ class FroxlorInstall {
$options = array('PDO::MYSQL_ATTR_INIT_COMMAND' => 'set names utf8'); $options = array('PDO::MYSQL_ATTR_INIT_COMMAND' => 'set names utf8');
$dsn = "mysql:host=".$this->_data['mysql_host'].";"; $dsn = "mysql:host=".$this->_data['mysql_host'].";";
$fatal_fail = false;
try { try {
$db_root = new PDO( $db_root = new PDO(
$dsn, $this->_data['mysql_root_user'], $this->_data['mysql_root_pass'], $options $dsn, $this->_data['mysql_root_user'], $this->_data['mysql_root_pass'], $options
@@ -227,47 +228,63 @@ class FroxlorInstall {
} catch (PDOException $e) { } catch (PDOException $e) {
// nope // nope
$content .= $this->_status_message('red', $e->getMessage()); $content .= $this->_status_message('red', $e->getMessage());
$fatal_fail = true;
} }
} }
// ok, if we are here, the database class is build up if (!$fatal_fail) {
// (otherwise it would have already die'd this script)
$content .= $this->_status_message('green', "OK");
// check for existing db
$content .= $this->_backupExistingDatabase($db_root);
// create unprivileged user and the database itself
$content .= $this->_createDatabaseAndUser($db_root);
// importing data to new database
$content .= $this->_importDatabaseData();
// create DB object for new database
$options = array('PDO::MYSQL_ATTR_INIT_COMMAND' => 'set names utf8');
$dsn = "mysql:host=".$this->_data['mysql_host'].";dbname=".$this->_data['mysql_database'].";";
try {
$db = new PDO(
$dsn, $this->_data['mysql_unpriv_user'], $this->_data['mysql_unpriv_pass'], $options
);
} catch (PDOException $e) {
// dafuq? this should have happened in _importDatabaseData()
$content .= $this->_status_message('red', $e->getMessage());
die;
};
// change settings accordingly // ok, if we are here, the database connection is up and running
$content .= $this->_doSettings($db); $content .= $this->_status_message('green', "OK");
// create entries // check for existing db and create backup if so
$content .= $this->_doDataEntries($db); $content .= $this->_backupExistingDatabase($db_root);
$db = null; // create unprivileged user and the database itself
// create config-file $content .= $this->_createDatabaseAndUser($db_root);
$content .= $this->_createUserdataConf(); // importing data to new database
$content .= $this->_importDatabaseData();
// create DB object for new database
$options = array('PDO::MYSQL_ATTR_INIT_COMMAND' => 'set names utf8');
$dsn = "mysql:host=".$this->_data['mysql_host'].";dbname=".$this->_data['mysql_database'].";";
$another_fail = false;
try {
$db = new PDO(
$dsn, $this->_data['mysql_unpriv_user'], $this->_data['mysql_unpriv_pass'], $options
);
} catch (PDOException $e) {
// dafuq? this should have happened in _importDatabaseData()
$content .= $this->_status_message('red', $e->getMessage());
$another_fail = true;
};
if (!$another_fail) {
// change settings accordingly
$content .= $this->_doSettings($db);
// create entries
$content .= $this->_doDataEntries($db);
$db = null;
// create config-file
$content .= $this->_createUserdataConf();
}
}
$content .= "</table>"; $content .= "</table>";
// check if we have unrecoverable errors // check if we have unrecoverable errors
$navigation = ''; if ($fatal_fail || $another_fail) {
$msgcolor = 'green'; // D'oh
$message = $this->_lng['install']['froxlor_succ_installed']; $navigation = '';
$link = '../index.php'; $msgcolor = 'red';
$linktext = $this->_lng['click_here_to_login']; $message = $this->_lng['install']['testing_mysql_fail'];
$link = 'install.php';
$linktext = $this->_lng['click_here_to_goback'];
} else {
// all good
$navigation = '';
$msgcolor = 'green';
$message = $this->_lng['install']['froxlor_succ_installed'];
$link = '../index.php';
$linktext = $this->_lng['click_here_to_login'];
}
eval("\$navigation .= \"" . $this->_getTemplate("pagebottom") . "\";"); eval("\$navigation .= \"" . $this->_getTemplate("pagebottom") . "\";");
@@ -477,34 +494,41 @@ class FroxlorInstall {
* @return string status messages * @return string status messages
*/ */
private function _importDatabaseData() { private function _importDatabaseData() {
$content = "";
$content = "";
$content .= $this->_status_message('begin', $this->_lng['install']['testing_new_db']); $content .= $this->_status_message('begin', $this->_lng['install']['testing_new_db']);
$options = array('PDO::MYSQL_ATTR_INIT_COMMAND' => 'set names utf8'); $options = array('PDO::MYSQL_ATTR_INIT_COMMAND' => 'set names utf8');
$dsn = "mysql:host=".$this->_data['mysql_host'].";dbname=".$this->_data['mysql_database'].";"; $dsn = "mysql:host=".$this->_data['mysql_host'].";dbname=".$this->_data['mysql_database'].";";
$fatal_fail = false;
try { try {
$db = new PDO( $db = new PDO(
$dsn, $this->_data['mysql_unpriv_user'], $this->_data['mysql_unpriv_pass'], $options $dsn, $this->_data['mysql_unpriv_user'], $this->_data['mysql_unpriv_pass'], $options
); );
} catch (PDOException $e) { } catch (PDOException $e) {
$content .= $this->_status_message('red', $e->getMessage()); $content .= $this->_status_message('red', $e->getMessage());
die; $fatal_fail = true;
}; };
$content .= $this->_status_message('green', 'OK');
$content .= $this->_status_message('begin', $this->_lng['install']['importing_data']); if (!$fatal_fail) {
$db_schema = dirname(dirname(__FILE__)).'/froxlor.sql';
$sql_query = @file_get_contents($db_schema); $content .= $this->_status_message('green', 'OK');
$sql_query = $this->_remove_remarks($sql_query);
$sql_query = $this->_split_sql_file($sql_query, ';'); $content .= $this->_status_message('begin', $this->_lng['install']['importing_data']);
for ($i = 0; $i < sizeof($sql_query); $i++) { $db_schema = dirname(dirname(__FILE__)).'/froxlor.sql';
if (trim($sql_query[$i]) != '') { $sql_query = @file_get_contents($db_schema);
$result = $db->query($sql_query[$i]); $sql_query = $this->_remove_remarks($sql_query);
$sql_query = $this->_split_sql_file($sql_query, ';');
for ($i = 0; $i < sizeof($sql_query); $i++) {
if (trim($sql_query[$i]) != '') {
$result = $db->query($sql_query[$i]);
}
} }
} $db = null;
$db = null;
$content .= $this->_status_message('green', 'OK'); $content .= $this->_status_message('green', 'OK');
}
return $content;
} }
/** /**
@@ -534,16 +558,16 @@ class FroxlorInstall {
$del_stmt = $db_root->prepare("DELETE FROM `mysql`.`columns_priv` WHERE `User` = :user AND `Host` = :accesshost"); $del_stmt = $db_root->prepare("DELETE FROM `mysql`.`columns_priv` WHERE `User` = :user AND `Host` = :accesshost");
$del_stmt->execute(array('user' => $this->_data['mysql_unpriv_user'], 'accesshost' => $this->_data['mysql_access_host'])); $del_stmt->execute(array('user' => $this->_data['mysql_unpriv_user'], 'accesshost' => $this->_data['mysql_access_host']));
$del_stmt = $db_root->prepare("DROP DATABASE IF EXISTS :database;"); $del_stmt = $db_root->prepare("DROP DATABASE IF EXISTS `".str_replace('`', '', $this->_data['mysql_database'])."`;");
$del_stmt->execute(array('database' => str_replace('`', '', $this->_data['mysql_database']))); $del_stmt->execute();
$db_root->query("FLUSH PRIVILEGES;"); $db_root->query("FLUSH PRIVILEGES;");
$content .= $this->_status_message('green', 'OK'); $content .= $this->_status_message('green', 'OK');
// we have to create a new user and database for the froxlor unprivileged mysql access // 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']); $content .= $this->_status_message('begin', $this->_lng['install']['create_mysqluser_and_db']);
$ins_stmt = $db_root->prepare("CREATE DATABASE :database"); $ins_stmt = $db_root->prepare("CREATE DATABASE `".str_replace('`', '', $this->_data['mysql_database'])."`");
$ins_stmt->execute(array('database' => str_replace('`', '', $this->_data['mysql_database']))); $ins_stmt->execute();
$mysql_access_host_array = array_map('trim', explode(',', $this->_data['mysql_access_host'])); $mysql_access_host_array = array_map('trim', explode(',', $this->_data['mysql_access_host']));
@@ -598,7 +622,7 @@ class FroxlorInstall {
$rows = $db_root->query("SELECT FOUND_ROWS()")->fetchColumn(); $rows = $db_root->query("SELECT FOUND_ROWS()")->fetchColumn();
// check result // check result
if ($result !== false && $rows > 0) { if ($result_stmt !== false && $rows > 0) {
$tables_exist = true; $tables_exist = true;
} }

View File

@@ -64,6 +64,7 @@ $lng['install']['httpuser'] = 'HTTP username';
$lng['install']['httpgroup'] = 'HTTP groupname'; $lng['install']['httpgroup'] = 'HTTP groupname';
$lng['install']['testing_mysql'] = 'Checking MySQL-root access...'; $lng['install']['testing_mysql'] = 'Checking MySQL-root access...';
$lng['install']['testing_mysql_fail'] = 'There seems to be a problem with the database-connection. Cannot continue. Please go back and check your credentials.';
$lng['install']['backup_old_db'] = 'Creating backup of old database...'; $lng['install']['backup_old_db'] = 'Creating backup of old database...';
$lng['install']['backup_binary_missing'] = 'Could not find mysqldump'; $lng['install']['backup_binary_missing'] = 'Could not find mysqldump';
$lng['install']['backup_failed'] = 'Could not backup database'; $lng['install']['backup_failed'] = 'Could not backup database';
@@ -80,5 +81,6 @@ $lng['install']['creating_configfile_failed'] = 'Could not create lib/userdata.i
$lng['install']['froxlor_succ_installed'] = 'Froxlor was installed successfully.'; $lng['install']['froxlor_succ_installed'] = 'Froxlor was installed successfully.';
$lng['click_here_to_refresh'] = 'Click here to check again'; $lng['click_here_to_refresh'] = 'Click here to check again';
$lng['click_here_to_goback'] = 'Click here to go back';
$lng['click_here_to_continue'] = 'Click here to continue'; $lng['click_here_to_continue'] = 'Click here to continue';
$lng['click_here_to_login'] = 'Click here to login.'; $lng['click_here_to_login'] = 'Click here to login.';

View File

@@ -64,6 +64,7 @@ $lng['install']['httpuser'] = 'HTTP Username';
$lng['install']['httpgroup'] = 'HTTP Gruppenname'; $lng['install']['httpgroup'] = 'HTTP Gruppenname';
$lng['install']['testing_mysql'] = 'Teste MySQL-Root Zugang...'; $lng['install']['testing_mysql'] = 'Teste MySQL-Root Zugang...';
$lng['install']['testing_mysql_fail'] = 'Bei der Verwendung der Datenbank gibt es scheinbar Probleme. Installation kann nicht fortgesetzt werden. Bitte Zugangsdaten prüfen und erneut versuchen.';
$lng['install']['backup_old_db'] = 'Sicherung vorheriger Datenbank...'; $lng['install']['backup_old_db'] = 'Sicherung vorheriger Datenbank...';
$lng['install']['backup_binary_missing'] = 'Konnte mysqldump nicht finden'; $lng['install']['backup_binary_missing'] = 'Konnte mysqldump nicht finden';
$lng['install']['backup_failed'] = 'Sicherung fehlgeschlagen'; $lng['install']['backup_failed'] = 'Sicherung fehlgeschlagen';
@@ -80,5 +81,6 @@ $lng['install']['creating_configfile_failed'] = 'Konnte lib/userdata.inc.php nic
$lng['install']['froxlor_succ_installed'] = 'Froxlor wurde erfolgreich installiert.'; $lng['install']['froxlor_succ_installed'] = 'Froxlor wurde erfolgreich installiert.';
$lng['click_here_to_refresh'] = 'Hier klicken, um erneut zu prüfen'; $lng['click_here_to_refresh'] = 'Hier klicken, um erneut zu prüfen';
$lng['click_here_to_goback'] = 'Einen Schritt zurück';
$lng['click_here_to_continue'] = 'Installation fortführen'; $lng['click_here_to_continue'] = 'Installation fortführen';
$lng['click_here_to_login'] = 'Hier geht es weiter zum Login-Fenster.'; $lng['click_here_to_login'] = 'Hier geht es weiter zum Login-Fenster.';