Database.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. '020601', #2.6.1
  37. '020602' #2.6.2
  38. );
  39. # For each update
  40. foreach ($updates as $update) {
  41. if (isset($version)&&$update<=$version) continue;
  42. # Load update
  43. include(__DIR__ . '/../database/update_' . $update . '.php');
  44. }
  45. return true;
  46. }
  47. static function createConfig($host = 'localhost', $user, $password, $name = 'lychee', $prefix = '') {
  48. # Check dependencies
  49. Module::dependencies(isset($host, $user, $password, $name));
  50. $database = new mysqli($host, $user, $password);
  51. if ($database->connect_errno) return 'Warning: Connection failed!';
  52. # Check if database exists
  53. if (!$database->select_db($name)) {
  54. # Database doesn't exist
  55. # Check if user can create a database
  56. $result = $database->query('CREATE DATABASE lychee_dbcheck');
  57. if (!$result) return 'Warning: Creation failed!';
  58. else $database->query('DROP DATABASE lychee_dbcheck');
  59. }
  60. # Escape data
  61. $host = mysqli_real_escape_string($database, $host);
  62. $user = mysqli_real_escape_string($database, $user);
  63. $password = mysqli_real_escape_string($database, $password);
  64. $name = mysqli_real_escape_string($database, $name);
  65. $prefix = mysqli_real_escape_string($database, $prefix);
  66. # Save config.php
  67. $config = "<?php
  68. ###
  69. # @name Configuration
  70. # @author Tobias Reich
  71. # @copyright 2014 Tobias Reich
  72. ###
  73. if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  74. # Database configuration
  75. \$dbHost = '$host'; # Host of the database
  76. \$dbUser = '$user'; # Username of the database
  77. \$dbPassword = '$password'; # Password of the database
  78. \$dbName = '$name'; # Database name
  79. \$dbTablePrefix = '$prefix'; # Table prefix
  80. ?>";
  81. # Save file
  82. if (file_put_contents(LYCHEE_CONFIG_FILE, $config)===false) return 'Warning: Could not create file!';
  83. return true;
  84. }
  85. static function createDatabase($database, $name = 'lychee') {
  86. # Check dependencies
  87. Module::dependencies(isset($database, $name));
  88. # Create database
  89. $result = $database->query("CREATE DATABASE IF NOT EXISTS $name;");
  90. $database->select_db($name);
  91. if (!$database->select_db($name)||!$result) return false;
  92. return true;
  93. }
  94. static function createTables($database) {
  95. # Check dependencies
  96. Module::dependencies(isset($database));
  97. # Create log
  98. if (!$database->query('SELECT * FROM lychee_log LIMIT 0;')) {
  99. # Read file
  100. $file = __DIR__ . '/../database/log_table.sql';
  101. $query = @file_get_contents($file);
  102. # Create table
  103. if (!isset($query)||$query===false) return false;
  104. if (!$database->query($query)) return false;
  105. }
  106. # Create settings
  107. if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) {
  108. # Read file
  109. $file = __DIR__ . '/../database/settings_table.sql';
  110. $query = @file_get_contents($file);
  111. # Create table
  112. if (!isset($query)||$query===false) {
  113. Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_settings');
  114. return false;
  115. }
  116. if (!$database->query($query)) {
  117. Log::error($database, __METHOD__, __LINE__, $database->error);
  118. return false;
  119. }
  120. # Read file
  121. $file = __DIR__ . '/../database/settings_content.sql';
  122. $query = @file_get_contents($file);
  123. # Add content
  124. if (!isset($query)||$query===false) {
  125. Log::error($database, __METHOD__, __LINE__, 'Could not load content-query for lychee_settings');
  126. return false;
  127. }
  128. if (!$database->query($query)) {
  129. Log::error($database, __METHOD__, __LINE__, $database->error);
  130. return false;
  131. }
  132. }
  133. # Create albums
  134. if (!$database->query('SELECT * FROM lychee_albums LIMIT 0;')) {
  135. # Read file
  136. $file = __DIR__ . '/../database/albums_table.sql';
  137. $query = @file_get_contents($file);
  138. # Create table
  139. if (!isset($query)||$query===false) {
  140. Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_albums');
  141. return false;
  142. }
  143. if (!$database->query($query)) {
  144. Log::error($database, __METHOD__, __LINE__, $database->error);
  145. return false;
  146. }
  147. }
  148. # Create photos
  149. if (!$database->query('SELECT * FROM lychee_photos LIMIT 0;')) {
  150. # Read file
  151. $file = __DIR__ . '/../database/photos_table.sql';
  152. $query = @file_get_contents($file);
  153. # Create table
  154. if (!isset($query)||$query===false) {
  155. Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_photos');
  156. return false;
  157. }
  158. if (!$database->query($query)) {
  159. Log::error($database, __METHOD__, __LINE__, $database->error);
  160. return false;
  161. }
  162. }
  163. return true;
  164. }
  165. static function prepare($database, $query, $data) {
  166. # Check dependencies
  167. Module::dependencies(isset($database, $query, $data));
  168. foreach ($data as $value) {
  169. # Escape
  170. $value = mysqli_real_escape_string($database, $value);
  171. # Replace
  172. $query = preg_replace('/\?/', $value, $query, 1);
  173. }
  174. # Add semicolon at the end
  175. $query .= ';';
  176. return $query;
  177. }
  178. }
  179. ?>