Database.php 5.3 KB

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