db.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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', $version) {
  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 Tobias Reich
  31. */
  32. if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  33. // Config version
  34. \$configVersion = '';
  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. $_SESSION['login'] = true;
  43. return true;
  44. }
  45. }
  46. function dbCreate($dbName, $database) {
  47. $result = $database->query("CREATE DATABASE IF NOT EXISTS $dbName;");
  48. $database->select_db($dbName);
  49. if (!$database->select_db($dbName)||!$result) return false;
  50. return true;
  51. }
  52. function dbCreateTables($database) {
  53. if (!$database->query('SELECT * FROM lychee_settings LIMIT 0;')) {
  54. $query = "
  55. CREATE TABLE `lychee_settings` (
  56. `key` varchar(50) NOT NULL DEFAULT '',
  57. `value` varchar(50) DEFAULT ''
  58. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  59. ";
  60. if (!$database->query($query)) return false;
  61. $query = "
  62. INSERT INTO `lychee_settings` (`key`, `value`)
  63. VALUES
  64. ('username',''),
  65. ('password',''),
  66. ('thumbQuality','90'),
  67. ('checkForUpdates','1'),
  68. ('sorting','ORDER BY id DESC'),
  69. ('dropboxKey','');
  70. ";
  71. if (!$database->query($query)) return false;
  72. }
  73. if (!$database->query('SELECT * FROM lychee_albums LIMIT 0;')) {
  74. $query = "
  75. CREATE TABLE `lychee_albums` (
  76. `id` int(11) NOT NULL AUTO_INCREMENT,
  77. `title` varchar(50) NOT NULL,
  78. `description` varchar(1000) DEFAULT '',
  79. `sysdate` varchar(10) NOT NULL,
  80. `public` tinyint(1) DEFAULT '0',
  81. `password` varchar(100) DEFAULT '',
  82. PRIMARY KEY (`id`)
  83. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  84. ";
  85. if (!$database->query($query)) return false;
  86. }
  87. if (!$database->query('SELECT * FROM lychee_photos LIMIT 0;')) {
  88. $query = "
  89. CREATE TABLE `lychee_photos` (
  90. `id` bigint(14) NOT NULL,
  91. `title` varchar(50) NOT NULL,
  92. `description` varchar(1000) NOT NULL DEFAULT '',
  93. `url` varchar(100) NOT NULL,
  94. `tags` varchar(1000) NOT NULL DEFAULT '',
  95. `public` tinyint(1) NOT NULL,
  96. `type` varchar(10) NOT NULL,
  97. `width` int(11) NOT NULL,
  98. `height` int(11) NOT NULL,
  99. `size` varchar(20) NOT NULL,
  100. `sysdate` varchar(10) NOT NULL,
  101. `systime` varchar(8) NOT NULL,
  102. `iso` varchar(15) NOT NULL,
  103. `aperture` varchar(20) NOT NULL,
  104. `make` varchar(20) NOT NULL,
  105. `model` varchar(50) NOT NULL,
  106. `shutter` varchar(30) NOT NULL,
  107. `focal` varchar(20) NOT NULL,
  108. `takedate` varchar(20) NOT NULL,
  109. `taketime` varchar(8) NOT NULL,
  110. `star` tinyint(1) NOT NULL,
  111. `thumbUrl` varchar(50) NOT NULL,
  112. `album` varchar(30) NOT NULL DEFAULT '0',
  113. `import_name` varchar(100) DEFAULT '',
  114. PRIMARY KEY (`id`)
  115. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  116. ";
  117. if (!$database->query($query)) return false;
  118. }
  119. return true;
  120. }
  121. function dbClose() {
  122. global $database;
  123. if (!$database->close()) exit('Error: Closing the connection failed!');
  124. return true;
  125. }
  126. ?>