ringfinger/backend/scripts/setup.php
2020-09-10 21:59:00 +02:00

82 lines
2.0 KiB
PHP

<?php
function getUserInput(string $label): string
{
echo $label;
return str_replace("\n", '', fgets(STDIN));
}
const TEMPLATE_SETTINGS = '<?php
/*
* This file was auto generated on :DATETIME
*/
declare(strict_types=1);
final class Setting
{
public const MYSQL_HOST = \':HOST\';
public const MYSQL_USER = \':USER\';
public const MYSQL_PASSWORD = \':PASSWORD\';
public const MYSQL_DATABASE = \':DATABASE\';
public const PATH_ROOT = \':ROOT_PATH/\';
public const PATH_QR_CODES = self::PATH_ROOT . \'backend/qr/\';
public const PATH_TMP = self::PATH_ROOT . \'backend/tmp/\';
}
';
echo 'Let\'s setup the database. Please enter the informations for your database...' . PHP_EOL;
$hostname = getUserInput('MySQL host address: ');
$username = getUserInput('MySQL user: ');
$password = getUserInput('MySQL password (shown!): ');
$database = getUserInput('MySQL database: ');
$settings = str_replace(
[':HOST', ':USER', ':PASSWORD', ':DATABASE', ':ROOT_PATH', ':DATETIME'],
[$hostname, $username, $password, $database, getcwd(), (new DateTime())->format('Y-m-d H:i:s')],
TEMPLATE_SETTINGS
);
require 'backend/classes/core/Autoloader.php';
if (!is_dir(Autoloader::PATH_CACHE)) {
mkdir(Autoloader::PATH_CACHE);
}
$file = fopen(getcwd() . '/backend/classes/Setting.php', 'w');
fwrite($file, $settings);
fclose($file);
Autoloader::BuildCache();
$autoloader = new Autoloader();
$file = fopen('backend/scripts/setup.sql', 'r');
$setupSql = fread($file, filesize('backend/scripts/setup.sql'));
fclose($file);
$db = new MySqlDatabase($hostname, $username, $password, $database);
$db->startTransaction();
$db->Query(str_replace('$DB_NAME', $database, $setupSql));
echo 'Let\'s create an admin account...' . PHP_EOL;
$user = new User(null, $db);
$user->setUsername(getUserInput('Username: '));
$user->setPassword(Password::GetHash(getUserInput('Password: ')));
$user->setEmail(getUserInput('Email: '));
$user->setJabberAddress(getUserInput('Jabber address: '));
$user->setAdmin(true);
$user->Save();
$db->commit();
echo 'Ringfinger setup has successfully finished.' . PHP_EOL;