Update database connect function and update mechanism
Database::connect now takes care of the update-mechanism and uses the current database version instead of the version from the client.
This commit is contained in:
parent
9e76acd9a8
commit
634c3621db
@ -247,9 +247,8 @@ class Admin extends Access {
|
|||||||
|
|
||||||
global $dbName;
|
global $dbName;
|
||||||
|
|
||||||
Module::dependencies(isset($_POST['version']));
|
|
||||||
$session = new Session($this->plugins, $this->settings);
|
$session = new Session($this->plugins, $this->settings);
|
||||||
echo json_encode($session->init($this->database, $dbName, false, $_POST['version']));
|
echo json_encode($session->init($this->database, $dbName, false));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ class Guest extends Access {
|
|||||||
global $dbName;
|
global $dbName;
|
||||||
|
|
||||||
$session = new Session($this->plugins, $this->settings);
|
$session = new Session($this->plugins, $this->settings);
|
||||||
echo json_encode($session->init($this->database, $dbName, true, $_POST['version']));
|
echo json_encode($session->init($this->database, $dbName, true));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,20 @@ if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
|
|
||||||
class Database extends Module {
|
class Database extends Module {
|
||||||
|
|
||||||
|
private static $versions = array(
|
||||||
|
'020100', #2.1
|
||||||
|
'020101', #2.1.1
|
||||||
|
'020200', #2.2
|
||||||
|
'020500', #2.5
|
||||||
|
'020505', #2.5.5
|
||||||
|
'020601', #2.6.1
|
||||||
|
'020602', #2.6.2
|
||||||
|
'020700', #2.7.0
|
||||||
|
'030000', #3.0.0
|
||||||
|
'030001', #3.0.1
|
||||||
|
'030003' #3.0.3
|
||||||
|
);
|
||||||
|
|
||||||
static function connect($host = 'localhost', $user, $password, $name = 'lychee') {
|
static function connect($host = 'localhost', $user, $password, $name = 'lychee') {
|
||||||
|
|
||||||
# Check dependencies
|
# Check dependencies
|
||||||
@ -26,44 +40,34 @@ class Database extends Module {
|
|||||||
# Set unicode
|
# Set unicode
|
||||||
$database->query('SET NAMES utf8;');
|
$database->query('SET NAMES utf8;');
|
||||||
|
|
||||||
# Check database
|
# Create database
|
||||||
if (!$database->select_db($name))
|
if (!self::createDatabase($database, $name)) exit('Error: Could not create database!');
|
||||||
if (!Database::createDatabase($database, $name)) exit('Error: Could not create database!');
|
|
||||||
|
|
||||||
# Check tables
|
# Create tables
|
||||||
$query = Database::prepare($database, 'SELECT * FROM ?, ?, ?, ? LIMIT 0', array(LYCHEE_TABLE_PHOTOS, LYCHEE_TABLE_ALBUMS, LYCHEE_TABLE_SETTINGS, LYCHEE_TABLE_LOG));
|
if (!self::createTables($database)) exit('Error: Could not create tables!');
|
||||||
if (!$database->query($query))
|
|
||||||
if (!Database::createTables($database)) exit('Error: Could not create tables!');
|
# Update database
|
||||||
|
if (!self::update($database, $name)) exit('Error: Could not update database and tables!');
|
||||||
|
|
||||||
return $database;
|
return $database;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static function update($database, $dbName, $version = 0) {
|
private static function update($database, $dbName) {
|
||||||
|
|
||||||
# Check dependencies
|
# Check dependencies
|
||||||
Module::dependencies(isset($database, $dbName));
|
Module::dependencies(isset($database, $dbName));
|
||||||
if (!isset($version)) return true;
|
|
||||||
|
|
||||||
# List of updates
|
# Get current version
|
||||||
$updates = array(
|
$query = self::prepare($database, "SELECT * FROM ? WHERE `key` = 'version'", array(LYCHEE_TABLE_SETTINGS));
|
||||||
'020100', #2.1
|
$results = $database->query($query);
|
||||||
'020101', #2.1.1
|
$current = $results->fetch_object()->value;
|
||||||
'020200', #2.2
|
|
||||||
'020500', #2.5
|
|
||||||
'020505', #2.5.5
|
|
||||||
'020601', #2.6.1
|
|
||||||
'020602', #2.6.2
|
|
||||||
'020700', #2.7.0
|
|
||||||
'030000', #3.0.0
|
|
||||||
'030001', #3.0.1
|
|
||||||
'030003' #3.0.3
|
|
||||||
);
|
|
||||||
|
|
||||||
# For each update
|
# For each update
|
||||||
foreach ($updates as $update) {
|
foreach (self::$versions as $version) {
|
||||||
|
|
||||||
if ($update<=$version) continue;
|
# Only update when newer version available
|
||||||
|
if ($version<=$current) continue;
|
||||||
|
|
||||||
# Load update
|
# Load update
|
||||||
include(__DIR__ . '/../database/update_' . $update . '.php');
|
include(__DIR__ . '/../database/update_' . $update . '.php');
|
||||||
@ -74,7 +78,7 @@ class Database extends Module {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static function createConfig($host = 'localhost', $user, $password, $name = 'lychee', $prefix = '') {
|
public static function createConfig($host = 'localhost', $user, $password, $name = 'lychee', $prefix = '') {
|
||||||
|
|
||||||
# Check dependencies
|
# Check dependencies
|
||||||
Module::dependencies(isset($host, $user, $password, $name));
|
Module::dependencies(isset($host, $user, $password, $name));
|
||||||
@ -83,15 +87,8 @@ class Database extends Module {
|
|||||||
|
|
||||||
if ($database->connect_errno) return 'Warning: Connection failed!';
|
if ($database->connect_errno) return 'Warning: Connection failed!';
|
||||||
|
|
||||||
# Check if database exists
|
# Check if user can create the database before saving the configuration
|
||||||
if (!$database->select_db($name)) {
|
if (!self::createDatabase($database, $name)) return 'Warning: Creation failed!';
|
||||||
|
|
||||||
# Database doesn't exist
|
|
||||||
# Check if user can create the database
|
|
||||||
$result = Database::createDatabase($database, $name);
|
|
||||||
if ($result===false) return 'Warning: Creation failed!';
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
# Escape data
|
# Escape data
|
||||||
$host = mysqli_real_escape_string($database, $host);
|
$host = mysqli_real_escape_string($database, $host);
|
||||||
@ -127,13 +124,16 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static function createDatabase($database, $name = 'lychee') {
|
private static function createDatabase($database, $name = 'lychee') {
|
||||||
|
|
||||||
# Check dependencies
|
# Check dependencies
|
||||||
Module::dependencies(isset($database, $name));
|
Module::dependencies(isset($database, $name));
|
||||||
|
|
||||||
|
# Check if database exists
|
||||||
|
if ($database->select_db($name)) return true;
|
||||||
|
|
||||||
# Create database
|
# Create database
|
||||||
$query = Database::prepare($database, 'CREATE DATABASE IF NOT EXISTS ?', array($name));
|
$query = self::prepare($database, 'CREATE DATABASE IF NOT EXISTS ?', array($name));
|
||||||
$result = $database->query($query);
|
$result = $database->query($query);
|
||||||
|
|
||||||
if (!$database->select_db($name)||!$result) return false;
|
if (!$database->select_db($name)||!$result) return false;
|
||||||
@ -141,13 +141,17 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static function createTables($database) {
|
private static function createTables($database) {
|
||||||
|
|
||||||
# Check dependencies
|
# Check dependencies
|
||||||
Module::dependencies(isset($database));
|
Module::dependencies(isset($database));
|
||||||
|
|
||||||
|
# Check if tables exist
|
||||||
|
$query = self::prepare($database, 'SELECT * FROM ?, ?, ?, ? LIMIT 0', array(LYCHEE_TABLE_PHOTOS, LYCHEE_TABLE_ALBUMS, LYCHEE_TABLE_SETTINGS, LYCHEE_TABLE_LOG));
|
||||||
|
if ($database->query($query)) return true;
|
||||||
|
|
||||||
# Create log
|
# Create log
|
||||||
$exist = Database::prepare($database, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_LOG));
|
$exist = self::prepare($database, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_LOG));
|
||||||
if (!$database->query($exist)) {
|
if (!$database->query($exist)) {
|
||||||
|
|
||||||
# Read file
|
# Read file
|
||||||
@ -157,13 +161,13 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
if (!isset($query)||$query===false) return false;
|
if (!isset($query)||$query===false) return false;
|
||||||
|
|
||||||
# Create table
|
# Create table
|
||||||
$query = Database::prepare($database, $query, array(LYCHEE_TABLE_LOG));
|
$query = self::prepare($database, $query, array(LYCHEE_TABLE_LOG));
|
||||||
if (!$database->query($query)) return false;
|
if (!$database->query($query)) return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create settings
|
# Create settings
|
||||||
$exist = Database::prepare($database, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_SETTINGS));
|
$exist = self::prepare($database, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_SETTINGS));
|
||||||
if (!$database->query($exist)) {
|
if (!$database->query($exist)) {
|
||||||
|
|
||||||
# Read file
|
# Read file
|
||||||
@ -176,7 +180,7 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Create table
|
# Create table
|
||||||
$query = Database::prepare($database, $query, array(LYCHEE_TABLE_SETTINGS));
|
$query = self::prepare($database, $query, array(LYCHEE_TABLE_SETTINGS));
|
||||||
if (!$database->query($query)) {
|
if (!$database->query($query)) {
|
||||||
Log::error($database, __METHOD__, __LINE__, $database->error);
|
Log::error($database, __METHOD__, __LINE__, $database->error);
|
||||||
return false;
|
return false;
|
||||||
@ -192,7 +196,7 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Add content
|
# Add content
|
||||||
$query = Database::prepare($database, $query, array(LYCHEE_TABLE_SETTINGS));
|
$query = self::prepare($database, $query, array(LYCHEE_TABLE_SETTINGS));
|
||||||
if (!$database->query($query)) {
|
if (!$database->query($query)) {
|
||||||
Log::error($database, __METHOD__, __LINE__, $database->error);
|
Log::error($database, __METHOD__, __LINE__, $database->error);
|
||||||
return false;
|
return false;
|
||||||
@ -200,7 +204,7 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
|
|
||||||
# Generate identifier
|
# Generate identifier
|
||||||
$identifier = md5(microtime(true));
|
$identifier = md5(microtime(true));
|
||||||
$query = Database::prepare($database, "UPDATE `?` SET `value` = '?' WHERE `key` = 'identifier' LIMIT 1", array(LYCHEE_TABLE_SETTINGS, $identifier));
|
$query = self::prepare($database, "UPDATE `?` SET `value` = '?' WHERE `key` = 'identifier' LIMIT 1", array(LYCHEE_TABLE_SETTINGS, $identifier));
|
||||||
if (!$database->query($query)) {
|
if (!$database->query($query)) {
|
||||||
Log::error($database, __METHOD__, __LINE__, $database->error);
|
Log::error($database, __METHOD__, __LINE__, $database->error);
|
||||||
return false;
|
return false;
|
||||||
@ -209,7 +213,7 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Create albums
|
# Create albums
|
||||||
$exist = Database::prepare($database, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_ALBUMS));
|
$exist = self::prepare($database, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_ALBUMS));
|
||||||
if (!$database->query($exist)) {
|
if (!$database->query($exist)) {
|
||||||
|
|
||||||
# Read file
|
# Read file
|
||||||
@ -222,7 +226,7 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Create table
|
# Create table
|
||||||
$query = Database::prepare($database, $query, array(LYCHEE_TABLE_ALBUMS));
|
$query = self::prepare($database, $query, array(LYCHEE_TABLE_ALBUMS));
|
||||||
if (!$database->query($query)) {
|
if (!$database->query($query)) {
|
||||||
Log::error($database, __METHOD__, __LINE__, $database->error);
|
Log::error($database, __METHOD__, __LINE__, $database->error);
|
||||||
return false;
|
return false;
|
||||||
@ -231,7 +235,7 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Create photos
|
# Create photos
|
||||||
$exist = Database::prepare($database, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_PHOTOS));
|
$exist = self::prepare($database, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_PHOTOS));
|
||||||
if (!$database->query($exist)) {
|
if (!$database->query($exist)) {
|
||||||
|
|
||||||
# Read file
|
# Read file
|
||||||
@ -244,7 +248,7 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Create table
|
# Create table
|
||||||
$query = Database::prepare($database, $query, array(LYCHEE_TABLE_PHOTOS));
|
$query = self::prepare($database, $query, array(LYCHEE_TABLE_PHOTOS));
|
||||||
if (!$database->query($query)) {
|
if (!$database->query($query)) {
|
||||||
Log::error($database, __METHOD__, __LINE__, $database->error);
|
Log::error($database, __METHOD__, __LINE__, $database->error);
|
||||||
return false;
|
return false;
|
||||||
@ -256,9 +260,9 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static function setVersion($database, $version) {
|
public static function setVersion($database, $version) {
|
||||||
|
|
||||||
$query = Database::prepare($database, "UPDATE ? SET value = '?' WHERE `key` = 'version'", array(LYCHEE_TABLE_SETTINGS, $version));
|
$query = self::prepare($database, "UPDATE ? SET value = '?' WHERE `key` = 'version'", array(LYCHEE_TABLE_SETTINGS, $version));
|
||||||
$result = $database->query($query);
|
$result = $database->query($query);
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
Log::error($database, __METHOD__, __LINE__, 'Could not update database (' . $database->error . ')');
|
Log::error($database, __METHOD__, __LINE__, 'Could not update database (' . $database->error . ')');
|
||||||
@ -267,7 +271,7 @@ if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static function prepare($database, $query, $data) {
|
public static function prepare($database, $query, $data) {
|
||||||
|
|
||||||
# Check dependencies
|
# Check dependencies
|
||||||
Module::dependencies(isset($database, $query, $data));
|
Module::dependencies(isset($database, $query, $data));
|
||||||
|
@ -21,22 +21,14 @@ class Session extends Module {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function init($database, $dbName, $public, $version) {
|
public function init($database, $dbName, $public) {
|
||||||
|
|
||||||
# Check dependencies
|
# Check dependencies
|
||||||
self::dependencies(isset($this->settings, $public, $version));
|
self::dependencies(isset($this->settings, $public));
|
||||||
|
|
||||||
# Call plugins
|
# Call plugins
|
||||||
$this->plugins(__METHOD__, 0, func_get_args());
|
$this->plugins(__METHOD__, 0, func_get_args());
|
||||||
|
|
||||||
# Update
|
|
||||||
if (!isset($this->settings['version'])||$this->settings['version']!==$version) {
|
|
||||||
if (!Database::update($database, $dbName, @$this->settings['version'])) {
|
|
||||||
Log::error($database, __METHOD__, __LINE__, 'Updating the database failed');
|
|
||||||
exit('Error: Updating the database failed!');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Return settings
|
# Return settings
|
||||||
$return['config'] = $this->settings;
|
$return['config'] = $this->settings;
|
||||||
|
|
||||||
|
@ -32,11 +32,7 @@ lychee = {
|
|||||||
|
|
||||||
lychee.init = function() {
|
lychee.init = function() {
|
||||||
|
|
||||||
let params = {
|
api.post('Session::init', {}, function(data) {
|
||||||
version: lychee.version_code
|
|
||||||
}
|
|
||||||
|
|
||||||
api.post('Session::init', params, function(data) {
|
|
||||||
|
|
||||||
// Check status
|
// Check status
|
||||||
// 0 = No configuration
|
// 0 = No configuration
|
||||||
|
Loading…
Reference in New Issue
Block a user