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
|
|
|
|
|
|
|
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-01-24 21:14:20 +00:00
|
|
|
if (!Database::createDatabase($connection, $name)) return 'Warning: Creation failed!';
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|
|
|
|
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-01-24 21:14:20 +00:00
|
|
|
if (file_put_contents(LYCHEE_CONFIG_FILE, $config)===false) return 'Warning: Could not create file!';
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function exists() {
|
|
|
|
|
|
|
|
return file_exists(LYCHEE_CONFIG_FILE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|