]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - install/index.php
fix installation error on wrong credentials (#1175 and #1114)
[github/wallabag/wallabag.git] / install / index.php
index 699572839a5c4a4a0ad6b98cb939a5e74eb0af6b..77e50864015b4bbb7b0092eebd7ae013ab1f073b 100755 (executable)
@@ -89,9 +89,11 @@ else if (isset($_POST['install'])) {
             try {
 
             if ($_POST['db_engine'] == 'mysql') {
-                if ($_POST['mysql_server'] != "") {$server = $_POST['mysql_server'];}
+                if ($_POST['mysql_server'] != "") {$server = $_POST['mysql_server'];} // if server and database are filled
                 if ($_POST['mysql_database'] != "") {$database = $_POST['mysql_database'];}
 
+                $sql_structure = file_get_contents('install/mysql.sql');
+
                 if (isset($_POST['mysql_utf8_mb4'])) {
                     //with UTF8-MB4
                     $db_path = 'mysql:host=' . $server . ';dbname=' . $database . ';charset=utf8mb4';
@@ -100,6 +102,7 @@ else if (isset($_POST['install'])) {
                     ));
                     $content = str_replace("define ('MYSQL_USE_UTF8MB4', FALSE);", "define ('MYSQL_USE_UTF8MB4', TRUE);", $content);
                 } else { // regular UTF8
+                    $sql_structure = str_replace(" DEFAULT CHARSET=utf8mb4", "", $sql_structure); // replace the UTF8-MB4 occurences inside the mysql.sql file
                     $db_path = 'mysql:host=' . $server . ';dbname=' . $database;
                     $handle = new PDO($db_path, $_POST['mysql_user'], $_POST['mysql_password']);
                 }
@@ -117,7 +120,6 @@ else if (isset($_POST['install'])) {
                 $moreQueries[] = "INSERT INTO `tags` (`id`, `value`) VALUES (1, 'opensource');";
                 $moreQueries[] = "INSERT INTO `tags_entries` (`id`, `entry_id`, `tag_id`) VALUES (1, 2, 1);";
 
-                $sql_structure = file_get_contents('install/mysql.sql');
             }
             else if ($_POST['db_engine'] == 'postgres') {
                 if ($_POST['pg_server'] != "") {$server = $_POST['pg_server'];}
@@ -142,16 +144,43 @@ else if (isset($_POST['install'])) {
                 $sql_structure = file_get_contents('install/postgres.sql');
             }
             // create database structure
-            $query = $handle->exec($sql_structure);
-
-            $usertest = executeQuery($handle,"SELECT * from users WHERE username = ?", array($username));
-            if (!empty($usertest)) {
-                $continue = false;
-                $errors[] = "An user already exists with this username in database.";
-            }
+            if (!$handle) {
+               $continue = false;
+                $errors[] = "Couldn't connect to your database server. Please check credentials.";
+            } else {
+                   $query = $handle->exec($sql_structure);
+
+                   $usertest = executeQuery($handle,"SELECT * from users WHERE username = ?", array($username));
+                   if (!empty($usertest)) {
+                       $continue = false;
+                       $errors[] = "An user already exists with this username in database.";
+                   }
+        }
 
         } catch (PDOException $e) {
-            $errors[] = $e->getMessage();
+               /* Error codes :
+               /       7 : PostgreSQL issues
+               /       1045 : Access denied to the user : user doesn't exists                     
+               /       1044 : Access denied to the user : user doesn't have the rights to connect to this database
+               /       2005 : Unknown database server
+                       */
+               switch ($e->getCode()) {
+                       case 7:
+                               $errors[] = "PostgreSQL has encountered an issue : " . $e->getMessage();
+                               break;
+                       case 1045:
+                               $errors[] = "The password for this user is incorrect, or this user doesn't exist.";
+                               break;
+                       case 1044:
+                               $errors[] = "The user isn't allowed to use this database.";
+                               break;
+                       case 2005:
+                               $errors[] = "The server can't be reached.";
+                               break;
+                       default:
+                               $errors[] = "A error happened while connecting to your database : " . $e->getMessage();
+                               break;
+               }
             $continue = false;
         }
         }
@@ -645,8 +674,8 @@ php composer.phar install</code></pre>
                                  <p>All fields have to be filled.</p>
                               </div>
                               <ul id="mysql_infos">
-                                 <li><label for="mysql_server">Server</label> <input type="text" placeholder="localhost" id="mysql_server" name="mysql_server" /></li>
-                                 <li><label for="mysql_database">Existing database</label> <input type="text" placeholder="wallabag" id="mysql_database" name="mysql_database" /></li>
+                                 <li><label for="mysql_server">Server</label> <input type="text" placeholder="eg : localhost" id="mysql_server" name="mysql_server" /></li>
+                                 <li><label for="mysql_database">Existing database</label> <input type="text" placeholder="eg : wallabag" id="mysql_database" name="mysql_database" /></li>
                                  <li><label for="mysql_user">User</label> <input type="text" placeholder="user" id="mysql_user" name="mysql_user" /></li>
                                  <li><label for="mysql_password">Password</label> <input type="password" placeholder="p4ssw0rd" id="mysql_password" name="mysql_password" /></li>
                                  <li><label for="mysql_utf8_mb4">Use UTF-8 MB4</label> <input id="mysql_utf8_mb4" type="checkbox" name="mysql_utf8_mb4">
@@ -666,8 +695,8 @@ php composer.phar install</code></pre>
                                     <p>All fields have to be filled.</p>
                                  </div>
                                  <ul id="pg_infos">
-                                    <li><label for="pg_server">Server</label> <input type="text" placeholder="localhost" id="pg_server" name="pg_server" /></li>
-                                    <li><label for="pg_database">Existing database</label> <input type="text" placeholder="wallabag" id="pg_database" name="pg_database" /></li>
+                                    <li><label for="pg_server">Server</label> <input type="text" placeholder="eg : localhost" id="pg_server" name="pg_server" /></li>
+                                    <li><label for="pg_database">Existing database</label> <input type="text" placeholder="eg : wallabag" id="pg_database" name="pg_database" /></li>
                                     <li><label for="pg_user">User</label> <input type="text" placeholder="user" id="pg_user" name="pg_user" /></li>
                                     <li><label for="pg_password">Password</label> <input type="password" placeholder="p4ssw0rd" id="pg_password" name="pg_password" /></li>
                                  </ul>