Database.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. # Save config.php
  61. $config = "<?php
  62. ###
  63. # @name Configuration
  64. # @author Tobias Reich
  65. # @copyright 2014 Tobias Reich
  66. ###
  67. if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  68. # Database configuration
  69. \$dbHost = '$host'; # Host of the database
  70. \$dbUser = '$user'; # Username of the database
  71. \$dbPassword = '$password'; # Password of the database
  72. \$dbName = '$name'; # Database name
  73. \$dbTablePrefix = '$prefix'; # Table prefix
  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. static function prepare($database, $query, $data) {
  160. # Check dependencies
  161. Module::dependencies(isset($database, $query, $data));
  162. foreach ($data as $value) {
  163. # Escape
  164. $value = mysqli_real_escape_string($database, $value);
  165. # Replace
  166. $query = preg_replace('/\?/', $value, $query, 1);
  167. }
  168. # Add semicolon at the end
  169. $query .= ';';
  170. return $query;
  171. }
  172. }
  173. ?>