db.php 4.3 KB

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