Database.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. ###
  3. # @name Database Module
  4. # @copyright 2014 by Tobias Reich
  5. ###
  6. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  7. class Database extends Module {
  8. static function connect($host = 'localhost', $user, $password, $name = 'lychee') {
  9. # Check dependencies
  10. Module::dependencies(isset($host, $user, $password, $name));
  11. $database = new mysqli($host, $user, $password);
  12. # Check connection
  13. if ($database->connect_errno) exit('Error: ' . $database->connect_error);
  14. # Avoid sql injection on older MySQL versions by using GBK
  15. if ($database->server_version<50500) @$database->set_charset('GBK');
  16. else @$database->set_charset('utf8');
  17. # Set unicode
  18. $database->query('SET NAMES utf8;');
  19. # Check database
  20. if (!$database->select_db($name))
  21. if (!Database::createDatabase($database, $name)) exit('Error: Could not create database!');
  22. # Check tables
  23. $query = Database::prepare($database, 'SELECT * FROM ?, ?, ?, ? LIMIT 0', array(LYCHEE_TABLE_PHOTOS, LYCHEE_TABLE_ALBUMS, LYCHEE_TABLE_SETTINGS, LYCHEE_TABLE_LOG));
  24. if (!$database->query($query))
  25. if (!Database::createTables($database)) exit('Error: Could not create tables!');
  26. return $database;
  27. }
  28. static function update($database, $dbName, $version = 0) {
  29. # Check dependencies
  30. Module::dependencies(isset($database, $dbName));
  31. if (!isset($version)) return true;
  32. # List of updates
  33. $updates = array(
  34. '020100', #2.1
  35. '020101', #2.1.1
  36. '020200', #2.2
  37. '020500', #2.5
  38. '020505', #2.5.5
  39. '020601', #2.6.1
  40. '020602', #2.6.2
  41. '020700' #2.7.0
  42. );
  43. # For each update
  44. foreach ($updates as $update) {
  45. if ($update<=$version) continue;
  46. # Load update
  47. include(__DIR__ . '/../database/update_' . $update . '.php');
  48. }
  49. return true;
  50. }
  51. static function createConfig($host = 'localhost', $user, $password, $name = 'lychee', $prefix = '') {
  52. # Check dependencies
  53. Module::dependencies(isset($host, $user, $password, $name));
  54. $database = new mysqli($host, $user, $password);
  55. if ($database->connect_errno) return 'Warning: Connection failed!';
  56. # Check if database exists
  57. if (!$database->select_db($name)) {
  58. # Database doesn't exist
  59. # Check if user can create the database
  60. $result = Database::createDatabase($database, $name);
  61. if ($result===false) return 'Warning: Creation failed!';
  62. }
  63. # Escape data
  64. $host = mysqli_real_escape_string($database, $host);
  65. $user = mysqli_real_escape_string($database, $user);
  66. $password = mysqli_real_escape_string($database, $password);
  67. $name = mysqli_real_escape_string($database, $name);
  68. $prefix = mysqli_real_escape_string($database, $prefix);
  69. # Save config.php
  70. $config = "<?php
  71. ###
  72. # @name Configuration
  73. # @author Tobias Reich
  74. # @copyright 2014 Tobias Reich
  75. ###
  76. if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  77. # Database configuration
  78. \$dbHost = '$host'; # Host of the database
  79. \$dbUser = '$user'; # Username of the database
  80. \$dbPassword = '$password'; # Password of the database
  81. \$dbName = '$name'; # Database name
  82. \$dbTablePrefix = '$prefix'; # Table prefix
  83. ?>";
  84. # Save file
  85. if (file_put_contents(LYCHEE_CONFIG_FILE, $config)===false) return 'Warning: Could not create file!';
  86. return true;
  87. }
  88. static function createDatabase($database, $name = 'lychee') {
  89. # Check dependencies
  90. Module::dependencies(isset($database, $name));
  91. # Create database
  92. $query = Database::prepare($database, 'CREATE DATABASE IF NOT EXISTS ?', array($name));
  93. $result = $database->query($query);
  94. if (!$database->select_db($name)||!$result) return false;
  95. return true;
  96. }
  97. static function createTables($database) {
  98. # Check dependencies
  99. Module::dependencies(isset($database));
  100. # Create log
  101. $exist = Database::prepare($database, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_LOG));
  102. if (!$database->query($exist)) {
  103. # Read file
  104. $file = __DIR__ . '/../database/log_table.sql';
  105. $query = @file_get_contents($file);
  106. if (!isset($query)||$query===false) return false;
  107. # Create table
  108. $query = Database::prepare($database, $query, array(LYCHEE_TABLE_LOG));
  109. if (!$database->query($query)) return false;
  110. }
  111. # Create settings
  112. $exist = Database::prepare($database, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_SETTINGS));
  113. if (!$database->query($exist)) {
  114. # Read file
  115. $file = __DIR__ . '/../database/settings_table.sql';
  116. $query = @file_get_contents($file);
  117. if (!isset($query)||$query===false) {
  118. Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_settings');
  119. return false;
  120. }
  121. # Create table
  122. $query = Database::prepare($database, $query, array(LYCHEE_TABLE_SETTINGS));
  123. if (!$database->query($query)) {
  124. Log::error($database, __METHOD__, __LINE__, $database->error);
  125. return false;
  126. }
  127. # Read file
  128. $file = __DIR__ . '/../database/settings_content.sql';
  129. $query = @file_get_contents($file);
  130. if (!isset($query)||$query===false) {
  131. Log::error($database, __METHOD__, __LINE__, 'Could not load content-query for lychee_settings');
  132. return false;
  133. }
  134. # Add content
  135. $query = Database::prepare($database, $query, array(LYCHEE_TABLE_SETTINGS));
  136. if (!$database->query($query)) {
  137. Log::error($database, __METHOD__, __LINE__, $database->error);
  138. return false;
  139. }
  140. }
  141. # Create albums
  142. $exist = Database::prepare($database, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_ALBUMS));
  143. if (!$database->query($exist)) {
  144. # Read file
  145. $file = __DIR__ . '/../database/albums_table.sql';
  146. $query = @file_get_contents($file);
  147. if (!isset($query)||$query===false) {
  148. Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_albums');
  149. return false;
  150. }
  151. # Create table
  152. $query = Database::prepare($database, $query, array(LYCHEE_TABLE_ALBUMS));
  153. if (!$database->query($query)) {
  154. Log::error($database, __METHOD__, __LINE__, $database->error);
  155. return false;
  156. }
  157. }
  158. # Create photos
  159. $exist = Database::prepare($database, 'SELECT * FROM ? LIMIT 0', array(LYCHEE_TABLE_PHOTOS));
  160. if (!$database->query($exist)) {
  161. # Read file
  162. $file = __DIR__ . '/../database/photos_table.sql';
  163. $query = @file_get_contents($file);
  164. if (!isset($query)||$query===false) {
  165. Log::error($database, __METHOD__, __LINE__, 'Could not load query for lychee_photos');
  166. return false;
  167. }
  168. # Create table
  169. $query = Database::prepare($database, $query, array(LYCHEE_TABLE_PHOTOS));
  170. if (!$database->query($query)) {
  171. Log::error($database, __METHOD__, __LINE__, $database->error);
  172. return false;
  173. }
  174. }
  175. return true;
  176. }
  177. static function setVersion($database, $version) {
  178. $query = Database::prepare($database, "UPDATE ? SET value = '?' WHERE `key` = 'version'", array(LYCHEE_TABLE_SETTINGS, $version));
  179. $result = $database->query($query);
  180. if (!$result) {
  181. Log::error($database, __METHOD__, __LINE__, 'Could not update database (' . $database->error . ')');
  182. return false;
  183. }
  184. }
  185. static function prepare($database, $query, $data) {
  186. # Check dependencies
  187. Module::dependencies(isset($database, $query, $data));
  188. # Count the number of placeholders and compare it with the number of arguments
  189. # If it doesn't match, calculate the difference and skip this number of placeholders before starting the replacement
  190. # This avoids problems with placeholders in user-input
  191. # $skip = Number of placeholders which need to be skipped
  192. $skip = 0;
  193. $num = array(
  194. 'placeholder' => substr_count($query, '?'),
  195. 'data' => count($data)
  196. );
  197. if (($num['data']-$num['placeholder'])<0) Log::notice($database, __METHOD__, __LINE__, 'Could not completely prepare query. Query has more placeholders than values.');
  198. foreach ($data as $value) {
  199. # Escape
  200. $value = mysqli_real_escape_string($database, $value);
  201. # Recalculate number of placeholders
  202. $num['placeholder'] = substr_count($query, '?');
  203. # Calculate number of skips
  204. if ($num['placeholder']>$num['data']) $skip = $num['placeholder'] - $num['data'];
  205. if ($skip>0) {
  206. # Need to skip $skip placeholders, because the user input contained placeholders
  207. # Calculate a substring which does not contain the user placeholders
  208. # 1 or -1 is the length of the placeholder (placeholder = ?)
  209. $pos = -1;
  210. for ($i=$skip; $i>0; $i--) $pos = strpos($query, '?', $pos + 1);
  211. $pos++;
  212. $temp = substr($query, 0, $pos); # First part of $query
  213. $query = substr($query, $pos); # Last part of $query
  214. }
  215. # Replace
  216. $query = preg_replace('/\?/', $value, $query, 1);
  217. if ($skip>0) {
  218. # Reassemble the parts of $query
  219. $query = $temp . $query;
  220. }
  221. # Reset skip
  222. $skip = 0;
  223. # Decrease number of data elements
  224. $num['data']--;
  225. }
  226. return $query;
  227. }
  228. }
  229. ?>