db.php 4.3 KB

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