diff --git a/install/lib/class.FroxlorInstall.php b/install/lib/class.FroxlorInstall.php index b8eb2dcc..4bb2d7ca 100644 --- a/install/lib/class.FroxlorInstall.php +++ b/install/lib/class.FroxlorInstall.php @@ -163,6 +163,7 @@ class FroxlorInstall $this->_getPostField('mysql_host', '127.0.0.1'); $this->_getPostField('mysql_database', 'froxlor'); + $this->_getPostField('mysql_forcecreate', '0'); $this->_getPostField('mysql_unpriv_user', 'froxlor'); $this->_getPostField('mysql_unpriv_pass'); $this->_getPostField('mysql_root_user', 'root'); @@ -246,10 +247,12 @@ class FroxlorInstall $content .= $this->_status_message('green', "OK"); // check for existing db and create backup if so $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(); + if (!$this->_abort) { + // create unprivileged user and the database itself + $content .= $this->_createDatabaseAndUser($db_root); + // importing data to new database + $content .= $this->_importDatabaseData(); + } if (! $this->_abort) { // create DB object for new database $options = array( @@ -733,12 +736,16 @@ class FroxlorInstall )); $rows = $db_root->query("SELECT FOUND_ROWS()")->fetchColumn(); + $content .= $this->_status_message('begin', $this->_lng['install']['check_db_exists']); + // check result if ($result_stmt !== false && $rows > 0) { $tables_exist = true; } - if ($tables_exist) { + if ($tables_exist && (int)$this->_data['mysql_forcecreate'] > 0) { + // set status + $content .= $this->_status_message('orange', 'exists (' . $this->_data['mysql_database'] . ')'); // tell what's going on $content .= $this->_status_message('begin', $this->_lng['install']['backup_old_db']); @@ -755,17 +762,29 @@ class FroxlorInstall $mysql_dump = '/usr/local/bin/mysqldump'; } + // create temporary .cnf file + $cnffilename = "/tmp/froxlor_dump.cnf"; + $dumpcnf = "[mysqldump]".PHP_EOL."password=\"".$this->_data['mysql_root_pass']."\"".PHP_EOL; + file_put_contents($cnffilename, $dumpcnf); + if ($do_backup) { - $command = $mysql_dump . " " . escapeshellarg($this->_data['mysql_database']) . " -u " . escapeshellarg($this->_data['mysql_root_user']) . " --password='" . escapeshellarg($this->_data['mysql_root_pass']) . "' --result-file=" . $filename; - $output = exec($command); - if (stristr($output, "error")) { + $command = $mysql_dump . " --defaults-extra-file=" . $cnffilename . " " . escapeshellarg($this->_data['mysql_database']) . " -u " . escapeshellarg($this->_data['mysql_root_user']) . " --result-file=" . $filename; + $output = []; + exec($command, $output); + @unlink($cnffilename); + if (stristr(implode(" ", $output), "error") || !file_exists($filename)) { $content .= $this->_status_message('red', $this->_lng['install']['backup_failed']); + $this->_abort = true; } else { $content .= $this->_status_message('green', 'OK (' . $filename . ')'); } } else { $content .= $this->_status_message('red', $this->_lng['install']['backup_binary_missing']); + $this->_abort = true; } + } else { + $content .= $this->_status_message('red', $this->_lng['install']['db_exists']); + $this->_abort = true; } return $content; @@ -801,6 +820,8 @@ class FroxlorInstall $formdata .= $this->_getSectionItemString('mysql_host', true); // database $formdata .= $this->_getSectionItemString('mysql_database', true); + // database overwrite if exists? + $formdata .= $this->_getSectionItemYesNo('mysql_forcecreate', false); // unpriv-user has to be different from root if ($this->_data['mysql_unpriv_user'] == $this->_data['mysql_root_user']) { $style = 'blue'; @@ -1363,7 +1384,14 @@ class FroxlorInstall // read os-release if (file_exists('/etc/os-release')) { - $os_dist = parse_ini_file('/etc/os-release', false); + $os_dist_content = file_get_contents('/etc/os-release'); + $os_dist_arr = explode("\n", $os_dist_content); + $os_dist = []; + foreach ($os_dist_arr as $os_dist_line) { + if (empty(trim($os_dist_line))) continue; + $tmp = explode("=", $os_dist_line); + $os_dist[$tmp[0]] = str_replace('"', "", trim($tmp[1])); + } if (is_array($os_dist) && array_key_exists('ID', $os_dist) && array_key_exists('VERSION_ID', $os_dist)) { $os_version = explode('.', $os_dist['VERSION_ID'])[0]; } diff --git a/install/lng/english.lng.php b/install/lng/english.lng.php index e6e126c5..98967ca6 100644 --- a/install/lng/english.lng.php +++ b/install/lng/english.lng.php @@ -53,6 +53,7 @@ $lng['install']['welcometext'] = 'Thank you for choosing Froxlor. Please fill ou $lng['install']['database'] = 'Database connection'; $lng['install']['mysql_host'] = 'MySQL-Hostname'; $lng['install']['mysql_database'] = 'Database name'; +$lng['install']['mysql_forcecreate'] = 'Backup and overwrite database if exists?'; $lng['install']['mysql_unpriv_user'] = 'Username for the unprivileged MySQL-account'; $lng['install']['mysql_unpriv_pass'] = 'Password for the unprivileged MySQL-account'; $lng['install']['mysql_root_user'] = 'Username for the MySQL-root-account'; @@ -79,6 +80,8 @@ $lng['install']['testing_mysql_fail'] = 'There seems to be a problem with the da $lng['install']['backup_old_db'] = 'Creating backup of old database...'; $lng['install']['backup_binary_missing'] = 'Could not find mysqldump'; $lng['install']['backup_failed'] = 'Could not backup database'; +$lng['install']['check_db_exists'] = 'Checking database...'; +$lng['install']['db_exists'] = 'Unable to create database. A database with the same name exists and should not be overwritten'; $lng['install']['prepare_db'] = 'Preparing database...'; $lng['install']['create_mysqluser_and_db'] = 'Creating database and username...'; $lng['install']['testing_new_db'] = 'Testing if database and user have been created correctly...'; diff --git a/install/lng/german.lng.php b/install/lng/german.lng.php index 4c0aaf68..62493acb 100644 --- a/install/lng/german.lng.php +++ b/install/lng/german.lng.php @@ -53,6 +53,7 @@ $lng['install']['welcometext'] = 'Vielen Dank dass Sie sich für Froxlor entschi $lng['install']['database'] = 'Datenbankverbindung'; $lng['install']['mysql_host'] = 'MySQL-Hostname'; $lng['install']['mysql_database'] = 'Datenbank Name'; +$lng['install']['mysql_forcecreate'] = 'Datenbank sichern und überschreiben wenn vorhanden?'; $lng['install']['mysql_unpriv_user'] = 'Benutzername für den unprivilegierten MySQL-Account'; $lng['install']['mysql_unpriv_pass'] = 'Passwort für den unprivilegierten MySQL-Account'; $lng['install']['mysql_root_user'] = 'Benutzername für den MySQL-Root-Account'; @@ -79,6 +80,8 @@ $lng['install']['testing_mysql_fail'] = 'Bei der Verwendung der Datenbank gibt e $lng['install']['backup_old_db'] = 'Sicherung vorheriger Datenbank...'; $lng['install']['backup_binary_missing'] = 'Konnte mysqldump nicht finden'; $lng['install']['backup_failed'] = 'Sicherung fehlgeschlagen'; +$lng['install']['check_db_exists'] = 'Databenbank wird geprüft...'; +$lng['install']['db_exists'] = 'Datenbank kann nicht erstellt werden. Eine Datenbank mit dem selben Namen existiert bereits und soll nicht überschrieben werden.'; $lng['install']['prepare_db'] = 'Datenbank wird vorbereitet...'; $lng['install']['create_mysqluser_and_db'] = 'Erstelle Datenbank und Benutzer...'; $lng['install']['testing_new_db'] = 'Teste, ob Datenbank und Benutzer korrekt angelegt wurden...';