Database.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. if (!isset($host, $user, $password, $name)) return false;
  11. $database = new mysqli($host, $user, $password);
  12. # Check connection
  13. if ($database->connect_errno) exit('Error: ' . $database->connect_error);
  14. # Avoid sql injection on older MySQL versions by using GBK
  15. if ($database->server_version<50500) $database->set_charset('GBK');
  16. else $database->set_charset("utf8");
  17. # Check database
  18. if (!$database->select_db($name))
  19. if (!Database::createDatabase($database, $name)) exit('Error: Could not create database!');
  20. # Check tables
  21. if (!$database->query('SELECT * FROM lychee_photos, lychee_albums, lychee_settings LIMIT 0;'))
  22. if (!Database::createTables($database)) exit('Error: Could not create tables!');
  23. return $database;
  24. }
  25. static function update($database, $dbName, $version) {
  26. if (!isset($database)) return false;
  27. # List of updates
  28. $updates = array(
  29. '020100', #2.1
  30. '020101', #2.1.1
  31. '020200', #2.2
  32. '020500' #2.5
  33. );
  34. # For each update
  35. foreach ($updates as $update) {
  36. if (isset($version)&&$update<=$version) continue;
  37. # Load update
  38. include(__DIR__ . '/../database/update_' . $update . '.php');
  39. }
  40. return true;
  41. }
  42. static function createConfig($host = 'localhost', $user, $password, $name = 'lychee') {
  43. if (!isset($host, $user, $password, $name)) return false;
  44. $database = new mysqli($host, $user, $password);
  45. if ($database->connect_errno) return 'Warning: Connection failed!';
  46. else {
  47. $config = "<?php
  48. ###
  49. # @name Configuration
  50. # @author Tobias Reich
  51. # @copyright 2014 Tobias Reich
  52. ###
  53. if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  54. # Database configuration
  55. \$dbHost = '$host'; # Host of the database
  56. \$dbUser = '$user'; # Username of the database
  57. \$dbPassword = '$password'; # Password of the database
  58. \$dbName = '$name'; # Database name
  59. ?>";
  60. # Save file
  61. if (file_put_contents(LYCHEE_CONFIG_FILE, $config)===false) return 'Warning: Could not create file!';
  62. return true;
  63. }
  64. }
  65. static function createDatabase($database, $name = 'lychee') {
  66. if (!isset($database, $name)) return false;
  67. # Create database
  68. $result = $database->query("CREATE DATABASE IF NOT EXISTS $name;");
  69. $database->select_db($name);
  70. if (!$database->select_db($name)||!$result) return false;
  71. return true;
  72. }
  73. static function createTables($database) {
  74. if (!isset($database)) return false;
  75. # Create settings
  76. if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) {
  77. # Read file
  78. $file = __DIR__ . '/../database/settings_table.sql';
  79. $query = @file_get_contents($file);
  80. # Create table
  81. if (!isset($query)||$query===false) return false;
  82. if (!$database->query($query)) return false;
  83. # Read file
  84. $file = __DIR__ . '/../database/settings_content.sql';
  85. $query = @file_get_contents($file);
  86. # Add content
  87. if (!isset($query)||$query===false) return false;
  88. if (!$database->query($query)) return false;
  89. }
  90. # Create albums
  91. if (!$database->query('SELECT * FROM lychee_albums LIMIT 0;')) {
  92. # Read file
  93. $file = __DIR__ . '/../database/albums_table.sql';
  94. $query = @file_get_contents($file);
  95. # Create table
  96. if (!isset($query)||$query===false) return false;
  97. if (!$database->query($query)) return false;
  98. }
  99. # Create photos
  100. if (!$database->query('SELECT * FROM lychee_photos LIMIT 0;')) {
  101. # Read file
  102. $file = __DIR__ . '/../database/photos_table.sql';
  103. $query = @file_get_contents($file);
  104. # Create table
  105. if (!isset($query)||$query===false) return false;
  106. if (!$database->query($query)) return false;
  107. }
  108. return true;
  109. }
  110. }
  111. ?>