Database.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 createConfig($host = 'localhost', $user, $password, $name = 'lychee', $version) {
  25. if (!isset($host, $user, $password, $name, $version)) return false;
  26. $database = new mysqli($host, $user, $password);
  27. if ($database->connect_errno) return 'Warning: Connection failed!';
  28. else {
  29. $config = "<?php
  30. ###
  31. # @name Config
  32. # @author Tobias Reich
  33. # @copyright 2014 Tobias Reich
  34. ###
  35. if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  36. # Config version
  37. \$configVersion = '';
  38. # Database configuration
  39. \$dbHost = '$host'; # Host of the database
  40. \$dbUser = '$user'; # Username of the database
  41. \$dbPassword = '$password'; # Password of the database
  42. \$dbName = '$name'; # Database name
  43. ?>";
  44. # Save file
  45. if (file_put_contents('../data/config.php', $config)===false) return 'Warning: Could not create file!';
  46. return true;
  47. }
  48. }
  49. static function createDatabase($database, $name = 'lychee') {
  50. if (!isset($database, $name)) return false;
  51. # Execute query
  52. $result = $database->query("CREATE DATABASE IF NOT EXISTS $name;");
  53. $database->select_db($name);
  54. if (!$database->select_db($name)||!$result) return false;
  55. return true;
  56. }
  57. static function createTables($database) {
  58. if (!isset($database)) return false;
  59. # Create settings
  60. if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) {
  61. $query = "
  62. CREATE TABLE `lychee_settings` (
  63. `key` varchar(50) NOT NULL DEFAULT '',
  64. `value` varchar(50) DEFAULT ''
  65. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  66. ";
  67. if (!$database->query($query)) return false;
  68. $query = "
  69. INSERT INTO `lychee_settings` (`key`, `value`)
  70. VALUES
  71. ('username',''),
  72. ('password',''),
  73. ('thumbQuality','90'),
  74. ('checkForUpdates','1'),
  75. ('sorting','ORDER BY id DESC'),
  76. ('dropboxKey','');
  77. ";
  78. if (!$database->query($query)) return false;
  79. }
  80. # Create albums
  81. if (!$database->query('SELECT * FROM lychee_albums LIMIT 0;')) {
  82. $query = "
  83. CREATE TABLE `lychee_albums` (
  84. `id` int(11) NOT NULL AUTO_INCREMENT,
  85. `title` varchar(50) NOT NULL,
  86. `description` varchar(1000) DEFAULT '',
  87. `sysdate` varchar(10) NOT NULL,
  88. `public` tinyint(1) NOT NULL DEFAULT '0',
  89. `visible` tinyint(1) NOT NULL DEFAULT '1',
  90. `password` varchar(100) DEFAULT '',
  91. PRIMARY KEY (`id`)
  92. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  93. ";
  94. if (!$database->query($query)) return false;
  95. }
  96. # Create photos
  97. if (!$database->query('SELECT * FROM lychee_photos LIMIT 0;')) {
  98. $query = "
  99. CREATE TABLE `lychee_photos` (
  100. `id` bigint(14) NOT NULL,
  101. `title` varchar(50) NOT NULL,
  102. `description` varchar(1000) NOT NULL DEFAULT '',
  103. `url` varchar(100) NOT NULL,
  104. `tags` varchar(1000) NOT NULL DEFAULT '',
  105. `public` tinyint(1) NOT NULL,
  106. `type` varchar(10) NOT NULL,
  107. `width` int(11) NOT NULL,
  108. `height` int(11) NOT NULL,
  109. `size` varchar(20) NOT NULL,
  110. `sysdate` varchar(10) NOT NULL,
  111. `systime` varchar(8) NOT NULL,
  112. `iso` varchar(15) NOT NULL,
  113. `aperture` varchar(20) NOT NULL,
  114. `make` varchar(20) NOT NULL,
  115. `model` varchar(50) NOT NULL,
  116. `shutter` varchar(30) NOT NULL,
  117. `focal` varchar(20) NOT NULL,
  118. `takedate` varchar(20) NOT NULL,
  119. `taketime` varchar(8) NOT NULL,
  120. `star` tinyint(1) NOT NULL,
  121. `thumbUrl` varchar(50) NOT NULL,
  122. `album` varchar(30) NOT NULL DEFAULT '0',
  123. `import_name` varchar(100) DEFAULT '',
  124. PRIMARY KEY (`id`)
  125. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  126. ";
  127. if (!$database->query($query)) return false;
  128. }
  129. return true;
  130. }
  131. }
  132. ?>