lychee/php/modules/Database.php

189 lines
4.3 KiB
PHP
Raw Normal View History

2014-04-04 19:59:39 +00:00
<?php
###
# @name Database Module
# @author Tobias Reich
# @copyright 2014 by Tobias Reich
###
if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
class Database extends Module {
static function connect($host = 'localhost', $user, $password, $name = 'lychee') {
# Check dependencies
Module::dependencies(isset($host, $user, $password, $name));
2014-04-04 19:59:39 +00:00
$database = new mysqli($host, $user, $password);
# Check connection
if ($database->connect_errno) exit('Error: ' . $database->connect_error);
# Avoid sql injection on older MySQL versions by using GBK
2014-04-04 19:59:39 +00:00
if ($database->server_version<50500) $database->set_charset('GBK');
else $database->set_charset("utf8");
2014-04-04 19:59:39 +00:00
# Check database
if (!$database->select_db($name))
if (!Database::createDatabase($database, $name)) exit('Error: Could not create database!');
# Check tables
2014-05-06 19:22:56 +00:00
if (!$database->query('SELECT * FROM lychee_photos, lychee_albums, lychee_settings, lychee_log LIMIT 0;'))
2014-04-04 19:59:39 +00:00
if (!Database::createTables($database)) exit('Error: Could not create tables!');
return $database;
}
static function update($database, $dbName, $version = 0) {
2014-04-04 19:59:39 +00:00
# Check dependencies
Module::dependencies(isset($database, $dbName));
2014-04-05 16:30:24 +00:00
# List of updates
$updates = array(
'020100', #2.1
'020101', #2.1.1
'020200', #2.2
'020500' #2.5
2014-04-05 16:30:24 +00:00
);
# For each update
foreach ($updates as $update) {
if (isset($version)&&$update<=$version) continue;
# Load update
2014-04-09 18:26:25 +00:00
include(__DIR__ . '/../database/update_' . $update . '.php');
2014-04-05 16:30:24 +00:00
}
return true;
}
static function createConfig($host = 'localhost', $user, $password, $name = 'lychee') {
# Check dependencies
Module::dependencies(isset($host, $user, $password, $name));
2014-04-04 19:59:39 +00:00
$database = new mysqli($host, $user, $password);
if ($database->connect_errno) return 'Warning: Connection failed!';
else {
$config = "<?php
###
2014-04-09 18:26:25 +00:00
# @name Configuration
2014-04-04 19:59:39 +00:00
# @author Tobias Reich
# @copyright 2014 Tobias Reich
###
if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
# Database configuration
\$dbHost = '$host'; # Host of the database
\$dbUser = '$user'; # Username of the database
\$dbPassword = '$password'; # Password of the database
\$dbName = '$name'; # Database name
?>";
# Save file
2014-04-13 12:08:18 +00:00
if (file_put_contents(LYCHEE_CONFIG_FILE, $config)===false) return 'Warning: Could not create file!';
2014-04-04 19:59:39 +00:00
return true;
}
}
static function createDatabase($database, $name = 'lychee') {
# Check dependencies
Module::dependencies(isset($database, $name));
2014-04-04 19:59:39 +00:00
2014-04-05 16:30:24 +00:00
# Create database
2014-04-04 19:59:39 +00:00
$result = $database->query("CREATE DATABASE IF NOT EXISTS $name;");
$database->select_db($name);
if (!$database->select_db($name)||!$result) return false;
return true;
}
static function createTables($database) {
# Check dependencies
Module::dependencies(isset($database));
2014-04-04 19:59:39 +00:00
2014-05-06 19:22:56 +00:00
# Create log
if (!$database->query('SELECT * FROM lychee_log LIMIT 0;')) {
# Read file
$file = __DIR__ . '/../database/log_table.sql';
$query = @file_get_contents($file);
# Create table
if (!isset($query)||$query===false) return false;
if (!$database->query($query)) return false;
}
2014-04-04 19:59:39 +00:00
# Create settings
if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) {
2014-04-05 16:30:24 +00:00
# Read file
2014-04-09 18:26:25 +00:00
$file = __DIR__ . '/../database/settings_table.sql';
$query = @file_get_contents($file);
2014-04-04 19:59:39 +00:00
2014-04-05 16:30:24 +00:00
# Create table
if (!isset($query)||$query===false) return false;
2014-04-04 19:59:39 +00:00
if (!$database->query($query)) return false;
2014-04-05 16:30:24 +00:00
# Read file
2014-04-09 18:26:25 +00:00
$file = __DIR__ . '/../database/settings_content.sql';
$query = @file_get_contents($file);
2014-04-04 19:59:39 +00:00
2014-04-05 16:30:24 +00:00
# Add content
if (!isset($query)||$query===false) return false;
2014-04-04 19:59:39 +00:00
if (!$database->query($query)) return false;
}
# Create albums
if (!$database->query('SELECT * FROM lychee_albums LIMIT 0;')) {
2014-04-05 16:30:24 +00:00
# Read file
2014-04-09 18:26:25 +00:00
$file = __DIR__ . '/../database/albums_table.sql';
$query = @file_get_contents($file);
2014-04-04 19:59:39 +00:00
2014-04-05 16:30:24 +00:00
# Create table
if (!isset($query)||$query===false) return false;
2014-04-04 19:59:39 +00:00
if (!$database->query($query)) return false;
}
# Create photos
if (!$database->query('SELECT * FROM lychee_photos LIMIT 0;')) {
2014-04-05 16:30:24 +00:00
# Read file
2014-04-09 18:26:25 +00:00
$file = __DIR__ . '/../database/photos_table.sql';
$query = @file_get_contents($file);
2014-04-04 19:59:39 +00:00
2014-04-05 16:30:24 +00:00
# Create table
if (!isset($query)||$query===false) return false;
2014-04-04 19:59:39 +00:00
if (!$database->query($query)) return false;
}
return true;
}
}
?>