]> git.wh0rd.org - tt-rss.git/commitdiff
installer: use PDO, improve wording for some notices
authorAndrew Dolgov <noreply@fakecake.org>
Sun, 10 Dec 2017 06:20:52 +0000 (09:20 +0300)
committerAndrew Dolgov <noreply@fakecake.org>
Sun, 10 Dec 2017 06:20:52 +0000 (09:20 +0300)
PDO wrapper: allow working with blank DB_HOST

classes/db.php
include/sanity_check.php
install/index.php

index 3f3c12680751c39703ff99e15fe54841405af40a..b6320944b549ff3be1d58a876060af1ae69f98c9 100644 (file)
@@ -52,8 +52,9 @@ class Db
        private function pdo_connect() {
 
                $db_port = defined('DB_PORT') && DB_PORT ? ';port=' . DB_PORT : '';
+               $db_host = defined('DB_HOST') && DB_HOST ? ';host=' . DB_HOST : '';
 
-               $this->pdo = new PDO(DB_TYPE . ':dbname=' . DB_NAME . ';host=' . DB_HOST . $db_port,
+               $this->pdo = new PDO(DB_TYPE . ':dbname=' . DB_NAME . $db_host . $db_port,
                        DB_USER,
                        DB_PASS);
 
index dddc557f2500986ab5f206556534bad615fd34fd..94578b4040c495c604bd52eb7093b6c5e4d4e490 100755 (executable)
                        }
 
                        if (!class_exists("PDO")) {
-                               array_push($errors, "PHP support for PDO (Portable Data Objects) is required but was not found.");
+                               array_push($errors, "PHP support for PDO is required but was not found.");
                        }
 
                        if (!function_exists("mb_strlen")) {
index 46a0fdecdfa6d3f146c130cd2b182e71c0f55cb6..f1547982c4534285ddbb7beeaf6dd2e00f3aaf7e 100755 (executable)
                        array_push($errors, "PHP support for JSON is required, but was not found.");
                }
 
-               if ($db_type == "mysql" && !function_exists("mysqli_connect")) {
-                       array_push($errors, "PHP support for MySQL is required for configured $db_type in config.php.");
-               }
-
-               if ($db_type == "pgsql" && !function_exists("pg_connect")) {
-                       array_push($errors, "PHP support for PostgreSQL is required for configured $db_type in config.php");
+               if (!class_exists("PDO")) {
+                       array_push($errors, "PHP support for PDO is required but was not found.");
                }
 
                if (!function_exists("mb_strlen")) {
                print "<div class=\"alert alert-info\">$msg</div>";
        }
 
-       function db_connect($host, $user, $pass, $db, $type, $port = false) {
-               if ($type == "pgsql") {
-
-                       $string = "dbname=$db user=$user";
+       function pdo_connect($host, $user, $pass, $db, $type, $port = false) {
 
-                       if ($pass) {
-                               $string .= " password=$pass";
-                       }
+               $db_port = $port ? ';port=' . $port : '';
+               $db_host = $host ? ';host=' . $host : '';
 
-                       if ($host) {
-                               $string .= " host=$host";
-                       }
+               try {
+                       $pdo = new PDO($type . ':dbname=' . $db . $db_host . $db_port,
+                               $user,
+                               $pass);
 
-                       if ($port) {
-                               $string = "$string port=" . $port;
-                       }
-
-                       $link = pg_connect($string);
-
-                       return $link;
-
-               } else if ($type == "mysql") {
-                       if ($port)
-                               return mysqli_connect($host, $user, $pass, $db, $port);
-                       else
-                               return mysqli_connect($host, $user, $pass, $db);
-               }
+                       return $pdo;
+               } catch (Exception $e) {
+                   print "<div class='alert alert-danger'>" . $e->getMessage() . "</div>";
+                   return null;
+        }
        }
 
        function make_config($DB_TYPE, $DB_HOST, $DB_USER, $DB_NAME, $DB_PASS,
                return $rv;
        }
 
-       function db_query($link, $query, $type, $die_on_error = true) {
-               if ($type == "pgsql") {
-                       $result = pg_query($link, $query);
-                       if (!$result) {
-                               $query = htmlspecialchars($query); // just in case
-                               if ($die_on_error) {
-                                       die("Query <i>$query</i> failed [$result]: " . ($link ? pg_last_error($link) : "No connection"));
-                               }
-                       }
-                       return $result;
-               } else if ($type == "mysql") {
-
-                       $result = mysqli_query($link, $query);
-
-                       if (!$result) {
-                               $query = htmlspecialchars($query);
-                               if ($die_on_error) {
-                                       die("Query <i>$query</i> failed: " . ($link ? mysqli_error($link) : "No connection"));
-                               }
-                       }
-                       return $result;
-               }
-       }
-
        function is_server_https() {
                return (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off')) || $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https';
        }
                        array_push($notices, "PHP support for Internationalization Functions is required to handle Internationalized Domain Names.");
                }
 
+        if ($DB_TYPE == "mysql" && !function_exists("mysqli_connect")) {
+            array_push($notices, "PHP extension for MySQL (mysqli) is missing. This may prevent legacy plugins from working.");
+        }
+
+        if ($DB_TYPE == "pgsql" && !function_exists("pg_connect")) {
+                       array_push($notices, "PHP extension for PostgreSQL is missing. This may prevent legacy plugins from working.");
+        }
+
                if (count($notices) > 0) {
                        print_notice("Configuration check succeeded with minor problems:");
 
        <h2>Checking database</h2>
 
        <?php
-               $link = db_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_TYPE, $DB_PORT);
+               $pdo = pdo_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_TYPE, $DB_PORT);
 
-               if (!$link) {
+               if (!$pdo) {
                        print_error("Unable to connect to database using specified parameters.");
                        exit;
                }
                        <p>Before you can start using tt-rss, database needs to be initialized. Click on the button below to do that now.</p>
 
                        <?php
-                               $result = @db_query($link, "SELECT true FROM ttrss_feeds", $DB_TYPE, false);
+                               $res = $pdo->query("SELECT true FROM ttrss_feeds");
 
-                               if ($result) {
-                                       print_error("Existing tt-rss tables will be removed from the database. If you would like to keep your data, skip database initialization.");
+                               if ($res && $res->fetch()) {
+                                       print_error("Some tt-rss data already exists in this database. If you continue with database initialization your current data will be lost.");
                                        $need_confirm = true;
                                } else {
                                        $need_confirm = false;
 
                } else if ($op == 'installschema' || $op == 'skipschema') {
 
-                       $link = db_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_TYPE, $DB_PORT);
+                       $pdo = pdo_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_TYPE, $DB_PORT);
 
-                       if (!$link) {
+                       if (!$pdo) {
                                print_error("Unable to connect to database using specified parameters.");
                                exit;
                        }
 
                                print "<h2>Initializing database...</h2>";
 
-                               $lines = explode(";", preg_replace("/[\r\n]/", "", file_get_contents("../schema/ttrss_schema_".basename($DB_TYPE).".sql")));
+                               $lines = explode(";", preg_replace("/[\r\n]/", "",
+                    file_get_contents("../schema/ttrss_schema_".basename($DB_TYPE).".sql")));
 
                                foreach ($lines as $line) {
                                        if (strpos($line, "--") !== 0 && $line) {
-                                               db_query($link, $line, $DB_TYPE);
+                                               $res = $pdo->query($line);
+
+                                               if (!$res) {
+                                                       print_notice("Query: $line");
+                                                       print_error("Error: " . implode(", ", $this->pdo->errorInfo()));
+                        }
                                        }
                                }