ringfinger/backend/scripts/setup.php

82 lines
2.0 KiB
PHP
Raw Normal View History

2020-08-19 23:45:39 +02:00
<?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
{
2020-08-23 12:37:39 +02:00
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/\';
2020-08-19 23:45:39 +02:00
}
';
2020-08-25 11:20:51 +02:00
echo 'Let\'s setup the database. Please enter the informations for your database...' . PHP_EOL;
$hostname = getUserInput('MySQL host address: ');
2020-08-19 23:45:39 +02:00
$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
);
2020-08-25 11:20:51 +02:00
require 'backend/classes/core/Autoloader.php';
if (!is_dir(Autoloader::PATH_CACHE)) {
mkdir(Autoloader::PATH_CACHE);
}
2020-08-19 23:45:39 +02:00
$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);
2020-08-25 11:20:51 +02:00
$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(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;