From fc243466da3bf507744c5d75e47c44a3b1e921ec Mon Sep 17 00:00:00 2001
From: slawkens1 <slawkens@gmail.com>
Date: Sat, 13 Jan 2018 21:18:21 +0100
Subject: [PATCH] * install: better error message on database failed connect

* changed <p> tag to <div> in notification messages
---
 install/includes/database.php | 18 ++++++--
 install/index.php             |  2 +
 install/steps/5-database.php  | 77 +++++++++++++++++++----------------
 install/steps/6-admin.php     |  4 ++
 install/template/style.css    |  2 +
 install/tools/5-database.php  | 24 ++++++-----
 system/database.php           |  8 +++-
 system/functions.php          |  4 +-
 system/locale/de/install.php  |  4 ++
 system/locale/en/install.php  |  4 ++
 system/locale/pl/install.php  |  4 ++
 11 files changed, 98 insertions(+), 53 deletions(-)

diff --git a/install/includes/database.php b/install/includes/database.php
index a7100690..e7e1575d 100644
--- a/install/includes/database.php
+++ b/install/includes/database.php
@@ -5,8 +5,20 @@ require(SYSTEM . 'libs/pot/OTS.php');
 $ots = POT::getInstance();
 require(SYSTEM . 'database.php');
 
-if($db->hasTable('accounts'))
-	define('USE_ACCOUNT_NAME', $db->hasColumn('accounts', 'name'));
-else
+if(!isset($db)) {
+	$database_error = $locale['step_database_error_mysql_connect'] . '<br/>' .
+			$locale['step_database_error_mysql_connect_2'] .
+			'<ul>' .
+				'<li>' . $locale['step_database_error_mysql_connect_3'] . '</li>' .
+				'<li>' . $locale['step_database_error_mysql_connect_4'] . '</li>' .
+			'</ul>' . '<br/>' . $error;
+}
+else {
+	if($db->hasTable('accounts'))
+		define('USE_ACCOUNT_NAME', $db->hasColumn('accounts', 'name'));
+}
+
+if(!defined('USE_ACCOUNT_NAME')) {
 	define('USE_ACCOUNT_NAME', false);
+}
 ?>
\ No newline at end of file
diff --git a/install/index.php b/install/index.php
index a408e6a8..06eb6d63 100644
--- a/install/index.php
+++ b/install/index.php
@@ -1,6 +1,8 @@
 <?php
 require('../common.php');
 
+define('MYAAC_INSTALL', true);
+
 // includes
 require(SYSTEM . 'functions.php');
 require(BASE . 'install/includes/functions.php');
diff --git a/install/steps/5-database.php b/install/steps/5-database.php
index 479503b8..fac8b263 100644
--- a/install/steps/5-database.php
+++ b/install/steps/5-database.php
@@ -47,46 +47,51 @@ if(!$error) {
 		success($locale['step_database_importing']);
 		require(BASE . 'install/includes/database.php');
 		
-		echo $twig->render('install.installer.html.twig', array(
-			'url' => 'tools/5-database.php',
-			'message' => $locale['loading_spinner']
-		));
-
-		if(!$error) {
-			if(!Validator::email($_SESSION['var_mail_admin'])) {
-				error($locale['step_config_mail_admin_error']);
-				$error = true;
-			}
-			if(!Validator::email($_SESSION['var_mail_address'])) {
-				error($locale['step_config_mail_address_error']);
-				$error = true;
-			}
+		if(isset($database_error)) { // we failed connect to the database
+			error($database_error);
+		}
+		else {
+			echo $twig->render('install.installer.html.twig', array(
+				'url' => 'tools/5-database.php',
+				'message' => $locale['loading_spinner']
+			));
 			
-			$content .= '$config[\'client_download\'] = \'http://tibia-clients.com/clients/download/\'. $config[\'client\'] . \'/exe/windows\';';
-			$content .= PHP_EOL;
-			$content .= '$config[\'client_download_linux\'] = \'http://tibia-clients.com/clients/download/\'. $config[\'client\'] . \'/tar/linux\';';
-			$content .= PHP_EOL;
-			$content .= '$config[\'session_prefix\'] = \'myaac_' . generateRandomString(8, true, false, true, false) . '_\';';
-			$content .= PHP_EOL;
-			$content .= '$config[\'cache_prefix\'] = \'myaac_' . generateRandomString(8, true, false, true, false) . '_\';';
-
-			$saved = true;
 			if(!$error) {
-				$saved = file_put_contents(BASE . 'config.local.php', $content);
-			}
-			
-			if($saved) {
-				if(!$error) {
-					$_SESSION['saved'] = true;
+				if(!Validator::email($_SESSION['var_mail_admin'])) {
+					error($locale['step_config_mail_admin_error']);
+					$error = true;
+				}
+				if(!Validator::email($_SESSION['var_mail_address'])) {
+					error($locale['step_config_mail_address_error']);
+					$error = true;
 				}
-			}
-			else {
-				$_SESSION['config_content'] = $content;
-				unset($_SESSION['saved']);
 				
-				$locale['step_database_error_file'] = str_replace('$FILE$', '<b>' . BASE . 'config.local.php</b>', $locale['step_database_error_file']);
-				warning($locale['step_database_error_file'] . '<br/>
-					<textarea cols="70" rows="10">' . $content . '</textarea>');
+				$content .= '$config[\'client_download\'] = \'http://tibia-clients.com/clients/download/\'. $config[\'client\'] . \'/exe/windows\';';
+				$content .= PHP_EOL;
+				$content .= '$config[\'client_download_linux\'] = \'http://tibia-clients.com/clients/download/\'. $config[\'client\'] . \'/tar/linux\';';
+				$content .= PHP_EOL;
+				$content .= '$config[\'session_prefix\'] = \'myaac_' . generateRandomString(8, true, false, true, false) . '_\';';
+				$content .= PHP_EOL;
+				$content .= '$config[\'cache_prefix\'] = \'myaac_' . generateRandomString(8, true, false, true, false) . '_\';';
+
+				$saved = true;
+				if(!$error) {
+					$saved = file_put_contents(BASE . 'config.local.php', $content);
+				}
+				
+				if($saved) {
+					if(!$error) {
+						$_SESSION['saved'] = true;
+					}
+				}
+				else {
+					$_SESSION['config_content'] = $content;
+					unset($_SESSION['saved']);
+					
+					$locale['step_database_error_file'] = str_replace('$FILE$', '<b>' . BASE . 'config.local.php</b>', $locale['step_database_error_file']);
+					warning($locale['step_database_error_file'] . '<br/>
+						<textarea cols="70" rows="10">' . $content . '</textarea>');
+				}
 			}
 		}
 	}
diff --git a/install/steps/6-admin.php b/install/steps/6-admin.php
index b3d46db4..46c58288 100644
--- a/install/steps/6-admin.php
+++ b/install/steps/6-admin.php
@@ -5,6 +5,10 @@ require(BASE . 'install/includes/config.php');
 if(!$error) {
 	require(BASE . 'install/includes/database.php');
 	
+	if(isset($database_error)) { // we failed connect to the database
+		error($database_error);
+	}
+
 	echo $twig->render('install.admin.html.twig', array(
 		'locale' => $locale,
 		'session' => $_SESSION,
diff --git a/install/template/style.css b/install/template/style.css
index fb8743c3..c6d1ae66 100644
--- a/install/template/style.css
+++ b/install/template/style.css
@@ -107,6 +107,8 @@ img {
 		background-position: 5px 6px;
 		border-style: solid;
 		border-width: 1px;
+		line-height: 1.6em;
+		margin-bottom: 10px;
 	}
 	.error {
 		background-color: #FDD9D9;
diff --git a/install/tools/5-database.php b/install/tools/5-database.php
index 251f5331..603a3981 100644
--- a/install/tools/5-database.php
+++ b/install/tools/5-database.php
@@ -14,29 +14,35 @@ ob_end_flush();
 
 if(!$error) {
 	require(BASE . 'install/includes/database.php');
+	if(isset($database_error)) { // we failed connect to the database
+		error($database_error);
+		return;
+	}
 }
 
 if(!$db->hasTable('accounts')) {
 	$locale['step_database_error_table'] = str_replace('$TABLE$', 'accounts', $locale['step_database_error_table']);
 	error($locale['step_database_error_table']);
-	$error = true;
+	return;
 }
-else if(!$db->hasTable('players')) {
+
+if(!$db->hasTable('players')) {
 	$locale['step_database_error_table'] = str_replace('$TABLE$', 'players', $locale['step_database_error_table']);
 	error($locale['step_database_error_table']);
-	$error = true;
+	return;
 }
-else if(!$db->hasTable('guilds')) {
+
+if(!$db->hasTable('guilds')) {
 	$locale['step_database_error_table'] = str_replace('$TABLE$', 'guilds', $locale['step_database_error_table']);
 	error($locale['step_database_error_table']);
-	$error = true;
+	return;
 }
 
 if($db->hasTable(TABLE_PREFIX . 'account_actions')) {
 	$locale['step_database_error_table_exist'] = str_replace('$TABLE$', TABLE_PREFIX . 'account_actions', $locale['step_database_error_table_exist']);
 	warning($locale['step_database_error_table_exist']);
 }
-else if(!$error) {
+else {
 	// import schema
 	try {
 		$db->query(file_get_contents(BASE . 'install/includes/schema.sql'));
@@ -47,14 +53,10 @@ else if(!$error) {
 	}
 	catch(PDOException $error_) {
 		error($locale['step_database_error_schema'] . ' ' . $error_);
-		$error = true;
+		return;
 	}
 }
 
-if($error) {
-	return;
-}
-
 if($db->hasColumn('accounts', 'key')) {
 	if(query("ALTER TABLE `accounts` MODIFY `key` VARCHAR(64) NOT NULL DEFAULT '';"))
 		success($locale['step_database_modifying_field'] . ' accounts.key...');
diff --git a/system/database.php b/system/database.php
index ad5e0f2f..5f7ccfdc 100644
--- a/system/database.php
+++ b/system/database.php
@@ -89,11 +89,18 @@ defined('MYAAC') or die('Direct access not allowed!');
 				'log' => $config['database_log']
 			)
 		);
+
+		$db = POT::getInstance()->getDBHandle();
 	}
 	catch(PDOException $error) {
 		if(isset($cache) && $cache->enabled()) {
 			$cache->delete('config_lua');
 		}
+		
+		if(defined('MYAAC_INSTALL')) {
+			return; // installer will take care of this
+		}
+
 		die('ERROR: Cannot connect to MySQL database.<br/>' .
 			'Possible reasons:' .
 			'<ul>' .
@@ -102,5 +109,4 @@ defined('MYAAC') or die('Direct access not allowed!');
 			'</ul>' . $error);
 
 	}
-	$db = POT::getInstance()->getDBHandle();
 ?>
\ No newline at end of file
diff --git a/system/functions.php b/system/functions.php
index 7dc4d9e7..53ebe1f5 100644
--- a/system/functions.php
+++ b/system/functions.php
@@ -12,9 +12,9 @@ defined('MYAAC') or die('Direct access not allowed!');
 function message($message, $type, $return)
 {
 	if($return)
-		return '<p class="' . $type . '">' . $message . '</p>';
+		return '<div class="' . $type . '">' . $message . '</div>';
 	
-	echo '<p class="' . $type . '">' . $message . '</p>';
+	echo '<div class="' . $type . '">' . $message . '</div>';
 	return true;
 }
 function success($message, $return = false) {
diff --git a/system/locale/de/install.php b/system/locale/de/install.php
index 0f971681..4a88042f 100644
--- a/system/locale/de/install.php
+++ b/system/locale/de/install.php
@@ -70,6 +70,10 @@ $locale['step_database_error_database_empty'] = 'Der Datenbanktyp kann nicht aus
 $locale['step_database_error_only_mysql'] = 'Dieser AAC unterstützt nur MySQL. Aus Ihrer Konfigurationsdatei scheint Ihr OTS die Datenbank $DATABASE_TYPE$ zu verwenden. Bitte ändern Sie Ihre Datenbank in MySQL und folgen Sie dann der Installation erneut.';
 $locale['step_database_error_table'] = 'Die Tabelle $TABLE$ existiert nicht. Bitte importieren Sie zuerst Ihr OTS-Datenbankschema.';
 $locale['step_database_error_table_exist'] = 'Die Tabelle $TABLE$ existiert bereits. Scheint, dass AAC bereits installiert ist. Das Importieren des MySQL-Schemas wird übersprungen..';
+$locale['step_database_error_mysql_connect'] = 'Verbindung zur MySQL-Datenbank nicht möglich.';
+$locale['step_database_error_mysql_connect_2'] = 'Mögliche Gründe:';
+$locale['step_database_error_mysql_connect_3'] = 'MySQL ist nicht richtig konfiguriert in <i>config.lua</i>.';
+$locale['step_database_error_mysql_connect_4'] = 'MySQL-Server läuft nicht.';
 $locale['step_database_error_schema'] = 'Fehler beim Importieren des Schemas:';
 $locale['step_database_success_schema'] = '$PREFIX$ Tabellen wurden erfolgreich installiert.';
 $locale['step_database_error_file'] = '$FILE$ konnte nicht geöffnet werden. Bitte kopieren Sie diesen Inhalt und fügen Sie ihn dort ein:';
diff --git a/system/locale/en/install.php b/system/locale/en/install.php
index 60279315..13410415 100644
--- a/system/locale/en/install.php
+++ b/system/locale/en/install.php
@@ -70,6 +70,10 @@ $locale['step_database_error_database_empty'] = 'Cannot determine database type
 $locale['step_database_error_only_mysql'] = 'This AAC supports only MySQL. From your config file it seems that your OTS is using: $DATABASE_TYPE$ database. Please change your database to MySQL and then follow the installation again.';
 $locale['step_database_error_table'] = 'Table $TABLE$ doesn\'t exist. Please import your OTS database schema first.';
 $locale['step_database_error_table_exist'] = 'Table $TABLE$ already exist. Seems AAC is already installed. Skipping importing MySQL schema..';
+$locale['step_database_error_mysql_connect'] = 'Cannot connect to the MySQL database.';
+$locale['step_database_error_mysql_connect_2'] = 'Possible reasons:';
+$locale['step_database_error_mysql_connect_3'] = 'MySQL is not configured propertly in <i>config.lua</i>.';
+$locale['step_database_error_mysql_connect_4'] = 'MySQL server is not running.';
 $locale['step_database_error_schema'] = 'Error while importing schema:';
 $locale['step_database_success_schema'] = 'Successfully installed $PREFIX$ tables.';
 $locale['step_database_error_file'] = '$FILE$ couldn\'t be opened. Please copy this content and paste there:';
diff --git a/system/locale/pl/install.php b/system/locale/pl/install.php
index 1e179860..6fdae6dd 100644
--- a/system/locale/pl/install.php
+++ b/system/locale/pl/install.php
@@ -65,6 +65,10 @@ $locale['step_database_error_database_empty'] = 'Nie można wykryć typu bazy da
 $locale['step_database_error_only_mysql'] = 'Ten AAC wspiera tylko bazy danych MySQL. Z Twojego pliku config wynika, że Twój serwera używa bazy: $DATABASE_TYPE$. Proszę zmienić typ bazy na MySQL i ponownie przystąpić do instalacji.';
 $locale['step_database_error_table'] = 'Tabela $TABLE$ nie istnieje. Proszę najpierw zaimportować schemat bazy danych serwera OTS.';
 $locale['step_database_error_table_exist'] = 'Tabela $TABLE$ już istnieje. Wygląda na to, że AAC został już zainstalowany. Schemat MySQL nie zostanie zaimportowany..';
+$locale['step_database_error_mysql_connect'] = 'Nie udało się połączyć z bazą danych MySQL.';
+$locale['step_database_error_mysql_connect_2'] = 'Możliwe przyczyny:';
+$locale['step_database_error_mysql_connect_3'] = 'MySQL nie jest poprawnie skonfigurowane w <i>config.lua</i>.';
+$locale['step_database_error_mysql_connect_4'] = 'Serwer MySQL nie jest uruchomiony.';
 $locale['step_database_error_schema'] = 'Błąd podczas importowania struktury bazy danych:';
 $locale['step_database_success_schema'] = 'Pomyślnie zainstalowano tabele $PREFIX$.';
 $locale['step_database_error_file'] = '$FILE$ nie mógł zostać otwarty. Proszę skopiować zawartość pola tekstowego i wkleić do tego pliku:';