Database.php 5.0 KB

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