Browse Source

Better function naming in db.php

Tobias Reich 11 years ago
parent
commit
f671077c26
3 changed files with 29 additions and 25 deletions
  1. 1 1
      assets/js/modules/settings.js
  2. 2 2
      php/api.php
  3. 26 22
      php/modules/db.php

+ 1 - 1
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) {

+ 2 - 2
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!';

+ 26 - 22
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 = "<?php
@@ -84,7 +78,7 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
 
 }
 
-function createDatabase($dbName, $database) {
+function dbCreate($dbName, $database) {
 
 	$result = $database->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;
+
+}
+
 ?>