Database.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. ###
  3. # @name Database Module
  4. # @author Tobias Reich
  5. # @copyright 2014 by Tobias Reich
  6. ###
  7. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  8. class Database extends Module {
  9. static function connect($host = 'localhost', $user, $password, $name = 'lychee') {
  10. # Check dependencies
  11. Module::dependencies(isset($host, $user, $password, $name));
  12. $database = new mysqli($host, $user, $password);
  13. # Check connection
  14. if ($database->connect_errno) exit('Error: ' . $database->connect_error);
  15. # Avoid sql injection on older MySQL versions by using GBK
  16. if ($database->server_version<50500) $database->set_charset('GBK');
  17. else $database->set_charset("utf8");
  18. # Set unicode
  19. $database->query('SET NAMES utf8;');
  20. # Check database
  21. if (!$database->select_db($name))
  22. if (!Database::createDatabase($database, $name)) exit('Error: Could not create database!');
  23. # Check tables
  24. if (!$database->query('SELECT * FROM lychee_photos, lychee_albums, lychee_settings, lychee_log LIMIT 0;'))
  25. if (!Database::createTables($database)) exit('Error: Could not create tables!');
  26. return $database;
  27. }
  28. static function update($database, $dbName, $version = 0) {
  29. # Check dependencies
  30. Module::dependencies(isset($database, $dbName));
  31. # List of updates
  32. $updates = array(
  33. '020100', #2.1
  34. '020101', #2.1.1
  35. '020200', #2.2
  36. '020500', #2.5
  37. '020505', #2.5.5
  38. '020601' #2.6.1
  39. );
  40. # For each update
  41. foreach ($updates as $update) {
  42. if (isset($version)&&$update<=$version) continue;
  43. # Load update
  44. include(__DIR__ . '/../database/update_' . $update . '.php');
  45. }
  46. return true;
  47. }
  48. static function createConfig($host = 'localhost', $user, $password, $name = 'lychee') {
  49. # Check dependencies
  50. Module::dependencies(isset($host, $user, $password, $name));
  51. $database = new mysqli($host, $user, $password);
  52. if ($database->connect_errno) return 'Warning: Connection failed!';
  53. # Check if database exists
  54. if (!$database->select_db($name)) {
  55. # Database doesn't exist
  56. # Check if user can create a database
  57. $result = $database->query('CREATE DATABASE lychee_dbcheck');
  58. if (!$result) return 'Warning: Creation failed!';
  59. else $database->query('DROP DATABASE lychee_dbcheck');
  60. }
  61. # Save config.php
  62. $config = "<?php
  63. ###
  64. # @name Configuration
  65. # @author Tobias Reich
  66. # @copyright 2014 Tobias Reich
  67. ###
  68. if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  69. # Database configuration
  70. \$dbHost = '$host'; # Host of the database
  71. \$dbUser = '$user'; # Username of the database
  72. \$dbPassword = '$password'; # Password of the database
  73. \$dbName = '$name'; # Database name
  74. ?>";
  75. # Save file
  76. if (file_put_contents(LYCHEE_CONFIG_FILE, $config)===false) return 'Warning: Could not create file!';
  77. return true;
  78. }
  79. static function createDatabase($database, $name = 'lychee') {
  80. # Check dependencies
  81. Module::dependencies(isset($database, $name));
  82. # Create database
  83. $result = $database->query("CREATE DATABASE IF NOT EXISTS $name;");
  84. $database->select_db($name);
  85. if (!$database->select_db($name)||!$result) return false;
  86. return true;
  87. }
  88. static function createTables($database) {
  89. # Check dependencies
  90. Module::dependencies(isset($database));
  91. # Create log
  92. if (!$database->query('SELECT * FROM lychee_log LIMIT 0;')) {
  93. # Read file
  94. $file = __DIR__ . '/../database/log_table.sql';
  95. $query = @file_get_contents($file);
  96. # Create table
  97. if (!isset($query)||$query===false) return false;
  98. if (!$database->query($query)) return false;
  99. }
  100. # Create settings
  101. if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) {
  102. # Read file
  103. $file = __DIR__ . '/../database/settings_table.sql';
  104. $query = @file_get_contents($file);
  105. # Create table
  106. if (!isset($query)||$query===false) {
  107. Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_settings');
  108. return false;
  109. }
  110. if (!$database->query($query)) {
  111. Log::error($database, __METHOD__, __LINE__, $database->error);
  112. return false;
  113. }
  114. # Read file
  115. $file = __DIR__ . '/../database/settings_content.sql';
  116. $query = @file_get_contents($file);
  117. # Add content
  118. if (!isset($query)||$query===false) {
  119. Log::error($database, __METHOD__, __LINE__, 'Could not load content-query for lychee_settings');
  120. return false;
  121. }
  122. if (!$database->query($query)) {
  123. Log::error($database, __METHOD__, __LINE__, $database->error);
  124. return false;
  125. }
  126. }
  127. # Create albums
  128. if (!$database->query('SELECT * FROM lychee_albums LIMIT 0;')) {
  129. # Read file
  130. $file = __DIR__ . '/../database/albums_table.sql';
  131. $query = @file_get_contents($file);
  132. # Create table
  133. if (!isset($query)||$query===false) {
  134. Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_albums');
  135. return false;
  136. }
  137. if (!$database->query($query)) {
  138. Log::error($database, __METHOD__, __LINE__, $database->error);
  139. return false;
  140. }
  141. }
  142. # Create photos
  143. if (!$database->query('SELECT * FROM lychee_photos LIMIT 0;')) {
  144. # Read file
  145. $file = __DIR__ . '/../database/photos_table.sql';
  146. $query = @file_get_contents($file);
  147. # Create table
  148. if (!isset($query)||$query===false) {
  149. Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_photos');
  150. return false;
  151. }
  152. if (!$database->query($query)) {
  153. Log::error($database, __METHOD__, __LINE__, $database->error);
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. }
  160. ?>