Config.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Lychee\Modules;
  3. final class Config {
  4. public static function create($host, $user, $password, $name = 'lychee', $prefix = '') {
  5. // Open a new connection to the MySQL server
  6. $connection = Database::connect($host, $user, $password);
  7. // Check if the connection was successful
  8. if ($connection===false) return 'Warning: Connection failed!';
  9. // Check if user can create the database before saving the configuration
  10. if (Database::createDatabase($connection, $name)===false) return 'Warning: Creation failed!';
  11. // Escape data
  12. $host = mysqli_real_escape_string($connection, $host);
  13. $user = mysqli_real_escape_string($connection, $user);
  14. $password = mysqli_real_escape_string($connection, $password);
  15. $name = mysqli_real_escape_string($connection, $name);
  16. $prefix = mysqli_real_escape_string($connection, $prefix);
  17. // Save config.php
  18. $config = "<?php
  19. if(!defined('LYCHEE')) Response::error('Direct access is not allowed!');
  20. // Database configuration
  21. \$dbHost = '$host'; // Host of the database
  22. \$dbUser = '$user'; // Username of the database
  23. \$dbPassword = '$password'; // Password of the database
  24. \$dbName = '$name'; // Database name
  25. \$dbTablePrefix = '$prefix'; // Table prefix
  26. ?>";
  27. // Save file
  28. if (file_put_contents(LYCHEE_CONFIG_FILE, $config)===false) return 'Warning: Could not create file!';
  29. return true;
  30. }
  31. /**
  32. * @return boolean Returns true when the config exists.
  33. */
  34. public static function exists() {
  35. return file_exists(LYCHEE_CONFIG_FILE);
  36. }
  37. /**
  38. * @return array Returns the config.
  39. */
  40. public static function get() {
  41. require(LYCHEE_CONFIG_FILE);
  42. return(array(
  43. 'host' => $dbHost,
  44. 'user' => $dbUser,
  45. 'password' => $dbPassword,
  46. 'name' => $dbName,
  47. 'prefix' => $dbTablePrefix
  48. ));
  49. }
  50. }
  51. ?>