Config.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Lychee\Modules;
  3. final class Config extends Module {
  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. ###
  20. # @name Configuration
  21. # @author Tobias Reich
  22. # @copyright 2015 Tobias Reich
  23. ###
  24. if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  25. # Database configuration
  26. \$dbHost = '$host'; # Host of the database
  27. \$dbUser = '$user'; # Username of the database
  28. \$dbPassword = '$password'; # Password of the database
  29. \$dbName = '$name'; # Database name
  30. \$dbTablePrefix = '$prefix'; # Table prefix
  31. ?>";
  32. # Save file
  33. if (file_put_contents(LYCHEE_CONFIG_FILE, $config)===false) return 'Warning: Could not create file!';
  34. return true;
  35. }
  36. public static function exists() {
  37. return file_exists(LYCHEE_CONFIG_FILE);
  38. }
  39. public static function get() {
  40. require(LYCHEE_CONFIG_FILE);
  41. return(array(
  42. 'host' => $dbHost,
  43. 'user' => $dbUser,
  44. 'password' => $dbPassword,
  45. 'name' => $dbName,
  46. 'prefix' => $dbTablePrefix
  47. ));
  48. }
  49. }
  50. ?>