db.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. $_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. ";
  70. if (!$database->query($query)) return false;
  71. }
  72. if (!$database->query('SELECT * FROM lychee_albums LIMIT 0;')) {
  73. $query = "
  74. CREATE TABLE `lychee_albums` (
  75. `id` int(11) NOT NULL AUTO_INCREMENT,
  76. `title` varchar(50) NOT NULL,
  77. `description` varchar(1000) DEFAULT '',
  78. `sysdate` varchar(10) NOT NULL,
  79. `public` tinyint(1) DEFAULT '0',
  80. `password` varchar(100) DEFAULT '',
  81. PRIMARY KEY (`id`)
  82. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  83. ";
  84. if (!$database->query($query)) return false;
  85. }
  86. if (!$database->query('SELECT * FROM lychee_photos LIMIT 0;')) {
  87. $query = "
  88. CREATE TABLE `lychee_photos` (
  89. `id` bigint(14) NOT NULL,
  90. `title` varchar(50) NOT NULL,
  91. `description` varchar(1000) NOT NULL DEFAULT '',
  92. `url` varchar(100) NOT NULL,
  93. `tags` varchar(1000) NOT NULL DEFAULT '',
  94. `public` tinyint(1) NOT NULL,
  95. `type` varchar(10) NOT NULL,
  96. `width` int(11) NOT NULL,
  97. `height` int(11) NOT NULL,
  98. `size` varchar(20) NOT NULL,
  99. `sysdate` varchar(10) NOT NULL,
  100. `systime` varchar(8) NOT NULL,
  101. `iso` varchar(15) NOT NULL,
  102. `aperture` varchar(20) NOT NULL,
  103. `make` varchar(20) NOT NULL,
  104. `model` varchar(50) NOT NULL,
  105. `shutter` varchar(30) NOT NULL,
  106. `focal` varchar(20) NOT NULL,
  107. `takedate` varchar(20) NOT NULL,
  108. `taketime` varchar(8) NOT NULL,
  109. `star` tinyint(1) NOT NULL,
  110. `thumbUrl` varchar(50) NOT NULL,
  111. `album` varchar(30) NOT NULL DEFAULT '0',
  112. `import_name` varchar(100) DEFAULT '',
  113. PRIMARY KEY (`id`)
  114. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  115. ";
  116. if (!$database->query($query)) return false;
  117. }
  118. return true;
  119. }
  120. function dbClose() {
  121. global $database;
  122. if (!$database->close()) exit('Error: Closing the connection failed!');
  123. return true;
  124. }
  125. ?>