Database.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, $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. );
  33. # For each update
  34. foreach ($updates as $update) {
  35. if (isset($version)&&$update<=$version) continue;
  36. # Load update
  37. include(__DIR__ . '/../database/update_' . $update . '.php');
  38. }
  39. return true;
  40. }
  41. static function createConfig($host = 'localhost', $user, $password, $name = 'lychee') {
  42. if (!isset($host, $user, $password, $name)) return false;
  43. $database = new mysqli($host, $user, $password);
  44. if ($database->connect_errno) return 'Warning: Connection failed!';
  45. else {
  46. $config = "<?php
  47. ###
  48. # @name Configuration
  49. # @author Tobias Reich
  50. # @copyright 2014 Tobias Reich
  51. ###
  52. if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  53. # Database configuration
  54. \$dbHost = '$host'; # Host of the database
  55. \$dbUser = '$user'; # Username of the database
  56. \$dbPassword = '$password'; # Password of the database
  57. \$dbName = '$name'; # Database name
  58. ?>";
  59. # Save file
  60. if (file_put_contents(__DIR__ . '/../../data/config.php', $config)===false) return 'Warning: Could not create file!';
  61. return true;
  62. }
  63. }
  64. static function createDatabase($database, $name = 'lychee') {
  65. if (!isset($database, $name)) return false;
  66. # Create database
  67. $result = $database->query("CREATE DATABASE IF NOT EXISTS $name;");
  68. $database->select_db($name);
  69. if (!$database->select_db($name)||!$result) return false;
  70. return true;
  71. }
  72. static function createTables($database) {
  73. if (!isset($database)) return false;
  74. # Create settings
  75. if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) {
  76. # Read file
  77. $file = __DIR__ . '/../database/settings_table.sql';
  78. $query = file_get_contents($file);
  79. # Create table
  80. if (!$database->query($query)) return false;
  81. # Read file
  82. $file = __DIR__ . '/../database/settings_content.sql';
  83. $query = file_get_contents($file);
  84. # Add content
  85. if (!$database->query($query)) return false;
  86. }
  87. # Create albums
  88. if (!$database->query('SELECT * FROM lychee_albums LIMIT 0;')) {
  89. # Read file
  90. $file = __DIR__ . '/../database/albums_table.sql';
  91. $query = file_get_contents($file);
  92. # Create table
  93. if (!$database->query($query)) return false;
  94. }
  95. # Create photos
  96. if (!$database->query('SELECT * FROM lychee_photos LIMIT 0;')) {
  97. # Read file
  98. $file = __DIR__ . '/../database/photos_table.sql';
  99. $query = file_get_contents($file);
  100. # Create table
  101. if (!$database->query($query)) return false;
  102. }
  103. return true;
  104. }
  105. }
  106. ?>