Database.php 3.5 KB

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