diff --git a/assets/js/modules/settings.js b/assets/js/modules/settings.js index fec2be9..52e51ad 100644 --- a/assets/js/modules/settings.js +++ b/assets/js/modules/settings.js @@ -26,7 +26,7 @@ var settings = { if (dbHost.length<1) dbHost = "localhost"; if (dbName.length<1) dbName = "lychee"; - params = "createConfig&dbName=" + escape(dbName) + "&dbUser=" + escape(dbUser) + "&dbPassword=" + escape(dbPassword) + "&dbHost=" + escape(dbHost); + params = "dbCreateConfig&dbName=" + escape(dbName) + "&dbUser=" + escape(dbUser) + "&dbPassword=" + escape(dbPassword) + "&dbHost=" + escape(dbHost); lychee.api(params, function(data) { if (data!==true) { diff --git a/php/api.php b/php/api.php index 54b5196..350deb0 100755 --- a/php/api.php +++ b/php/api.php @@ -37,8 +37,8 @@ if (!empty($_POST['function'])||!empty($_GET['function'])) { switch ($_POST['function']) { - case 'createConfig': if (isset($_POST['dbHost'])&&isset($_POST['dbUser'])&&isset($_POST['dbPassword'])&&isset($_POST['dbName'])) - echo createConfig($_POST['dbHost'], $_POST['dbUser'], $_POST['dbPassword'], $_POST['dbName']); + case 'dbCreateConfig': if (isset($_POST['dbHost'])&&isset($_POST['dbUser'])&&isset($_POST['dbPassword'])&&isset($_POST['dbName'])) + echo dbCreateConfig($_POST['dbHost'], $_POST['dbUser'], $_POST['dbPassword'], $_POST['dbName']); break; default: echo 'Warning: No configuration!'; diff --git a/php/modules/db.php b/php/modules/db.php index 2453996..5c99f8c 100755 --- a/php/modules/db.php +++ b/php/modules/db.php @@ -15,39 +15,33 @@ function dbConnect() { $database = new mysqli($dbHost, $dbUser, $dbPassword); - if (mysqli_connect_errno()) { - echo mysqli_connect_errno().': '.mysqli_connect_error(); - return false; - } + if ($database->connect_errno) exit('Error: ' . $database->connect_error); + + // Avoid sql injection on older MySQL versions + if ($database->server_version<50500) $database->set_charset('GBK'); if (!$database->select_db($dbName)) - if (!createDatabase($dbName, $database)) exit('Error: Could not create database!'); - if (!$database->query("SELECT * FROM lychee_photos, lychee_albums, lychee_settings LIMIT 1;")) - if (!createTables($database)) exit('Error: Could not create tables!'); - - // Avoid sql injection on older MySQL versions - if ($database->server_version<50500) $database->set_charset('GBK'); + if (!dbCreate($dbName, $database)) exit('Error: Could not create database!'); + + dbCheck($database); return $database; } -function dbClose() { - - global $database; - - if (!$database->close()) exit("Error: Closing the connection failed!"); - - return true; - +function dbCheck($database) { + + if (!$database->query("SELECT * FROM lychee_photos, lychee_albums, lychee_settings LIMIT 1;")) + if (!dbCreateTables($database)) exit('Error: Could not create tables!'); + } -function createConfig($dbHost = 'localhost', $dbUser, $dbPassword, $dbName = 'lychee') { +function dbCreateConfig($dbHost = 'localhost', $dbUser, $dbPassword, $dbName = 'lychee') { $dbPassword = urldecode($dbPassword); $database = new mysqli($dbHost, $dbUser, $dbPassword); - if (mysqli_connect_errno()||$dbUser=="") return "Warning: Connection failed!"; + if ($database->connect_errno) return "Warning: Connection failed!"; else { $config = "query("CREATE DATABASE IF NOT EXISTS $dbName;"); $database->select_db($dbName); @@ -94,7 +88,7 @@ function createDatabase($dbName, $database) { } -function createTables($database) { +function dbCreateTables($database) { if (!$database->query("SELECT * FROM lychee_settings LIMIT 1;")) { @@ -188,4 +182,14 @@ function createTables($database) { } +function dbClose() { + + global $database; + + if (!$database->close()) exit("Error: Closing the connection failed!"); + + return true; + +} + ?>