Config.php 2.0 KB

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