Config.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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)) 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')) exit('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. public static function exists() {
  32. return file_exists(LYCHEE_CONFIG_FILE);
  33. }
  34. public static function get() {
  35. require(LYCHEE_CONFIG_FILE);
  36. return(array(
  37. 'host' => $dbHost,
  38. 'user' => $dbUser,
  39. 'password' => $dbPassword,
  40. 'name' => $dbName,
  41. 'prefix' => $dbTablePrefix
  42. ));
  43. }
  44. }
  45. ?>