Database.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 user can create a database
  51. $result = $database->query('CREATE DATABASE lychee_dbcheck');
  52. if (!$result) return 'Warning: Creation failed!';
  53. else $database->query('DROP DATABASE lychee_dbcheck');
  54. # Save config.php
  55. $config = "<?php
  56. ###
  57. # @name Configuration
  58. # @author Tobias Reich
  59. # @copyright 2014 Tobias Reich
  60. ###
  61. if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  62. # Database configuration
  63. \$dbHost = '$host'; # Host of the database
  64. \$dbUser = '$user'; # Username of the database
  65. \$dbPassword = '$password'; # Password of the database
  66. \$dbName = '$name'; # Database name
  67. ?>";
  68. # Save file
  69. if (file_put_contents(LYCHEE_CONFIG_FILE, $config)===false) return 'Warning: Could not create file!';
  70. return true;
  71. }
  72. static function createDatabase($database, $name = 'lychee') {
  73. # Check dependencies
  74. Module::dependencies(isset($database, $name));
  75. # Create database
  76. $result = $database->query("CREATE DATABASE IF NOT EXISTS $name;");
  77. $database->select_db($name);
  78. if (!$database->select_db($name)||!$result) return false;
  79. return true;
  80. }
  81. static function createTables($database) {
  82. # Check dependencies
  83. Module::dependencies(isset($database));
  84. # Create log
  85. if (!$database->query('SELECT * FROM lychee_log LIMIT 0;')) {
  86. # Read file
  87. $file = __DIR__ . '/../database/log_table.sql';
  88. $query = @file_get_contents($file);
  89. # Create table
  90. if (!isset($query)||$query===false) return false;
  91. if (!$database->query($query)) return false;
  92. }
  93. # Create settings
  94. if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) {
  95. # Read file
  96. $file = __DIR__ . '/../database/settings_table.sql';
  97. $query = @file_get_contents($file);
  98. # Create table
  99. if (!isset($query)||$query===false) {
  100. Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_settings');
  101. return false;
  102. }
  103. if (!$database->query($query)) {
  104. Log::error($database, __METHOD__, __LINE__, $database->error);
  105. return false;
  106. }
  107. # Read file
  108. $file = __DIR__ . '/../database/settings_content.sql';
  109. $query = @file_get_contents($file);
  110. # Add content
  111. if (!isset($query)||$query===false) {
  112. Log::error($database, __METHOD__, __LINE__, 'Could not load content-query for lychee_settings');
  113. return false;
  114. }
  115. if (!$database->query($query)) {
  116. Log::error($database, __METHOD__, __LINE__, $database->error);
  117. return false;
  118. }
  119. }
  120. # Create albums
  121. if (!$database->query('SELECT * FROM lychee_albums LIMIT 0;')) {
  122. # Read file
  123. $file = __DIR__ . '/../database/albums_table.sql';
  124. $query = @file_get_contents($file);
  125. # Create table
  126. if (!isset($query)||$query===false) {
  127. Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_albums');
  128. return false;
  129. }
  130. if (!$database->query($query)) {
  131. Log::error($database, __METHOD__, __LINE__, $database->error);
  132. return false;
  133. }
  134. }
  135. # Create photos
  136. if (!$database->query('SELECT * FROM lychee_photos LIMIT 0;')) {
  137. # Read file
  138. $file = __DIR__ . '/../database/photos_table.sql';
  139. $query = @file_get_contents($file);
  140. # Create table
  141. if (!isset($query)||$query===false) {
  142. Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_photos');
  143. return false;
  144. }
  145. if (!$database->query($query)) {
  146. Log::error($database, __METHOD__, __LINE__, $database->error);
  147. return false;
  148. }
  149. }
  150. return true;
  151. }
  152. }
  153. ?>