db.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @name DB Module
  4. * @author Philipp Maurer
  5. * @author Tobias Reich
  6. * @copyright 2014 by Philipp Maurer, Tobias Reich
  7. */
  8. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  9. function dbConnect() {
  10. global $dbUser, $dbPassword, $dbHost, $dbName;
  11. $database = new mysqli($dbHost, $dbUser, $dbPassword);
  12. if ($database->connect_errno) exit('Error: ' . $database->connect_error);
  13. // Avoid sql injection on older MySQL versions
  14. if ($database->server_version<50500) $database->set_charset('GBK');
  15. if (!$database->select_db($dbName))
  16. if (!dbCreate($dbName, $database)) exit('Error: Could not create database!');
  17. dbCheck($database);
  18. return $database;
  19. }
  20. function dbCheck($database) {
  21. if (!$database->query("SELECT * FROM lychee_photos, lychee_albums, lychee_settings LIMIT 1;"))
  22. if (!dbCreateTables($database)) exit('Error: Could not create tables!');
  23. }
  24. function dbCreateConfig($dbHost = 'localhost', $dbUser, $dbPassword, $dbName = 'lychee') {
  25. $dbPassword = urldecode($dbPassword);
  26. $database = new mysqli($dbHost, $dbUser, $dbPassword);
  27. if ($database->connect_errno) return "Warning: Connection failed!";
  28. else {
  29. $config = "<?php
  30. /**
  31. * @name Config
  32. * @author Philipp Maurer
  33. * @author Tobias Reich
  34. * @copyright 2014 by Philipp Maurer, Tobias Reich
  35. */
  36. if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  37. // Config version
  38. \$configVersion = '2.1';
  39. // Database configurations
  40. \$dbHost = '$dbHost'; //Host of the Database
  41. \$dbUser = '$dbUser'; //Username of the database
  42. \$dbPassword = '$dbPassword'; //Password of the Database
  43. \$dbName = '$dbName'; //Database name
  44. ?>";
  45. if (file_put_contents("../data/config.php", $config)===false) return "Warning: Could not create file!";
  46. else {
  47. $_SESSION['login'] = true;
  48. return true;
  49. }
  50. }
  51. }
  52. function dbCreate($dbName, $database) {
  53. $result = $database->query("CREATE DATABASE IF NOT EXISTS $dbName;");
  54. $database->select_db($dbName);
  55. if (!$database->select_db($dbName)||!$result) return false;
  56. return true;
  57. }
  58. function dbCreateTables($database) {
  59. if (!$database->query("SELECT * FROM lychee_settings LIMIT 1;")) {
  60. $query = "
  61. CREATE TABLE `lychee_settings` (
  62. `key` varchar(50) NOT NULL DEFAULT '',
  63. `value` varchar(50) DEFAULT ''
  64. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  65. ";
  66. if (!$database->query($query)) return false;
  67. $query = "
  68. INSERT INTO `lychee_settings` (`key`, `value`)
  69. VALUES
  70. ('username',''),
  71. ('password',''),
  72. ('thumbQuality','90'),
  73. ('checkForUpdates','1'),
  74. ('sorting','ORDER BY id DESC'),
  75. ('importFilename','1');
  76. ";
  77. if (!$database->query($query)) return false;
  78. }
  79. if (!$database->query("SELECT * FROM lychee_albums LIMIT 1;")) {
  80. $query = "
  81. CREATE TABLE `lychee_albums` (
  82. `id` int(11) NOT NULL AUTO_INCREMENT,
  83. `title` varchar(50) NOT NULL,
  84. `description` varchar(1000) DEFAULT '',
  85. `sysdate` varchar(10) NOT NULL,
  86. `public` tinyint(1) DEFAULT '0',
  87. `password` varchar(100) DEFAULT '',
  88. PRIMARY KEY (`id`)
  89. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  90. ";
  91. if (!$database->query($query)) return false;
  92. }
  93. if (!$database->query("SELECT * FROM lychee_photos LIMIT 1;")) {
  94. $query = "
  95. CREATE TABLE `lychee_photos` (
  96. `id` bigint(14) NOT NULL,
  97. `title` varchar(50) NOT NULL,
  98. `description` varchar(1000) NOT NULL DEFAULT '',
  99. `url` varchar(100) NOT NULL,
  100. `tags` varchar(1000) NOT NULL DEFAULT '',
  101. `public` tinyint(1) NOT NULL,
  102. `type` varchar(10) NOT NULL,
  103. `width` int(11) NOT NULL,
  104. `height` int(11) NOT NULL,
  105. `size` varchar(20) NOT NULL,
  106. `sysdate` varchar(10) NOT NULL,
  107. `systime` varchar(8) NOT NULL,
  108. `iso` varchar(15) NOT NULL,
  109. `aperture` varchar(20) NOT NULL,
  110. `make` varchar(20) NOT NULL,
  111. `model` varchar(50) NOT NULL,
  112. `shutter` varchar(30) NOT NULL,
  113. `focal` varchar(20) NOT NULL,
  114. `takedate` varchar(20) NOT NULL,
  115. `taketime` varchar(8) NOT NULL,
  116. `star` tinyint(1) NOT NULL,
  117. `thumbUrl` varchar(50) NOT NULL,
  118. `album` varchar(30) NOT NULL DEFAULT '0',
  119. `import_name` varchar(100) DEFAULT '',
  120. PRIMARY KEY (`id`)
  121. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  122. ";
  123. if (!$database->query($query)) return false;
  124. }
  125. return true;
  126. }
  127. function dbClose() {
  128. global $database;
  129. if (!$database->close()) exit("Error: Closing the connection failed!");
  130. return true;
  131. }
  132. ?>