2016-01-24 21:14:20 +00:00
|
|
|
<?php
|
|
|
|
|
2016-01-26 14:31:53 +00:00
|
|
|
namespace Lychee\Modules;
|
2016-01-24 21:14:20 +00:00
|
|
|
|
2016-01-30 20:33:31 +00:00
|
|
|
final class Config {
|
2016-01-24 21:14:20 +00:00
|
|
|
|
2016-02-13 22:35:13 +00:00
|
|
|
/**
|
|
|
|
* Creates the configuration file.
|
|
|
|
* @return true|string Returns true when successful.
|
|
|
|
* Warning: Connection failed!
|
|
|
|
* Warning: Creation failed!
|
|
|
|
* Warning: Could not create file!
|
|
|
|
*/
|
2016-01-24 21:14:20 +00:00
|
|
|
public static function create($host, $user, $password, $name = 'lychee', $prefix = '') {
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Open a new connection to the MySQL server
|
2016-01-24 21:14:20 +00:00
|
|
|
$connection = Database::connect($host, $user, $password);
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Check if the connection was successful
|
2016-01-24 21:14:20 +00:00
|
|
|
if ($connection===false) return 'Warning: Connection failed!';
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Check if user can create the database before saving the configuration
|
2016-02-13 22:33:39 +00:00
|
|
|
if (Database::createDatabase($connection, $name)===false) return 'Warning: Creation failed!';
|
2016-01-24 21:14:20 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Escape data
|
|
|
|
$host = mysqli_real_escape_string($connection, $host);
|
|
|
|
$user = mysqli_real_escape_string($connection, $user);
|
|
|
|
$password = mysqli_real_escape_string($connection, $password);
|
|
|
|
$name = mysqli_real_escape_string($connection, $name);
|
|
|
|
$prefix = mysqli_real_escape_string($connection, $prefix);
|
2016-01-24 21:14:20 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Save config.php
|
2016-01-24 21:14:20 +00:00
|
|
|
$config = "<?php
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Database configuration
|
|
|
|
\$dbHost = '$host'; // Host of the database
|
|
|
|
\$dbUser = '$user'; // Username of the database
|
|
|
|
\$dbPassword = '$password'; // Password of the database
|
|
|
|
\$dbName = '$name'; // Database name
|
|
|
|
\$dbTablePrefix = '$prefix'; // Table prefix
|
2016-01-24 21:14:20 +00:00
|
|
|
|
|
|
|
?>";
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Save file
|
2016-03-24 21:09:21 +00:00
|
|
|
if (@file_put_contents(LYCHEE_CONFIG_FILE, $config)===false) return 'Warning: Could not create file!';
|
2016-01-24 21:14:20 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-08 21:12:03 +00:00
|
|
|
/**
|
|
|
|
* @return boolean Returns true when the config exists.
|
|
|
|
*/
|
2016-01-24 21:14:20 +00:00
|
|
|
public static function exists() {
|
|
|
|
|
|
|
|
return file_exists(LYCHEE_CONFIG_FILE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-08 21:12:03 +00:00
|
|
|
/**
|
|
|
|
* @return array Returns the config.
|
|
|
|
*/
|
2016-01-24 21:14:20 +00:00
|
|
|
public static function get() {
|
|
|
|
|
|
|
|
require(LYCHEE_CONFIG_FILE);
|
|
|
|
|
|
|
|
return(array(
|
2016-01-30 20:43:57 +00:00
|
|
|
'host' => $dbHost,
|
|
|
|
'user' => $dbUser,
|
|
|
|
'password' => $dbPassword,
|
|
|
|
'name' => $dbName,
|
|
|
|
'prefix' => $dbTablePrefix
|
2016-01-24 21:14:20 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-31 14:53:44 +00:00
|
|
|
?>
|