Database.php 4.0 KB

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