add: installer

This commit is contained in:
kemasama 2022-05-22 16:46:00 +09:00
parent c90405522a
commit 817ac32b2b
5 changed files with 440 additions and 1 deletions

View File

@ -1,6 +1,5 @@
<?php <?php
ini_set("display_erros", "on");
require_once __DIR__ . '/provide/autoload.php'; require_once __DIR__ . '/provide/autoload.php';
$boot = new BootLoader(__DIR__); $boot = new BootLoader(__DIR__);

7
install.php Normal file
View File

@ -0,0 +1,7 @@
<?php
require_once __DIR__ . '/provide/autoload.php';
$boot = new BootLoader(__DIR__);
$boot->install();

View File

@ -159,4 +159,104 @@ class BootLoader
$this->pdo = $db->getDB(); $this->pdo = $db->getDB();
$this->config = $config; $this->config = $config;
} }
/**
* Install Timecard
*/
public function install()
{
try {
$step = "welcome";
$configPath = $this->root . '/config.php';
if (!file_exists($configPath))
{
$from = @file_get_contents($this->root . "/config.sample.php");
@file_put_contents($configPath, $from);
}
$install = new Installer($configPath);
$install->open();
$fStep = filter_input(INPUT_POST, "step");
switch ($fStep)
{
case "welcome":
$step = "mysql";
break;
case "mysql":
$hostname = filter_input(INPUT_POST, "hostname");
$dbname = filter_input(INPUT_POST, "dbname");
$username = filter_input(INPUT_POST, "username");
$password = filter_input(INPUT_POST, "password");
$install->readToBuffer();
$install->setKeyBuffer("hostname", $hostname);
$install->setKeyBuffer("dbname", $dbname);
$install->setKeyBuffer("username", $username);
$install->setKeyBuffer("password", $password);
$install->writeBuffer();
$step = "link";
break;
case "link":
$url = filter_input(INPUT_POST, "url");
$install->readToBuffer();
$install->setKeyBuffer("canonical", $url);
$install->writeBuffer();
$step = "account";
break;
case "account":
$this->InitPDO();
$this->cards = new Timecard($this->pdo);
$this->cards->InitTables();
$user = new User($this->pdo);
$username = filter_input(INPUT_POST, "username");
if (!$user->hasCreate())
{
throw new \RuntimeException("ユーザーを作成できませんでした。");
}
$install->readToBuffer();
$install->setKeyBuffer("admin_users", "[" . $username . "]", false);
$install->writeBuffer();
$step = "done";
break;
case "done":
@unlink($this->root . "/install.php");
header("Location: index.php");
exit;
break;
default:
break;
}
$install->close();
$this->model->viewModel("install", [
"step" => $step
]);
} catch (\RuntimeException $e)
{
http_response_code(401);
$result = $this->model->viewModel("error", [
"exception" => $e,
"message" => $e->getMessage(),
"config" => $this->config,
]);
if (!$result)
{
echo $e->getMessage();
exit;
}
}
}
} }

View File

@ -0,0 +1,150 @@
<?php
class Installer
{
public function __construct($file)
{
$this->filePath = $file;
}
protected $filePath;
protected $fp;
public function open()
{
$this->fp = fopen($this->filePath, "r+");
return $this->fp;
}
public function close()
{
fclose($this->fp);
}
public function read()
{
return fgets($this->fp, 4096);
}
public function write($value)
{
fwrite($this->fp, $value);
}
protected $buffer = [];
public function dumpBuffer()
{
return $this->buffer;
}
public function readToBuffer()
{
$line = false;
while (($line = $this->read()) !== false) {
$line = trim($line);
array_push($this->buffer, $line);
}
}
public function writeBuffer()
{
ftruncate($this->fp,0);
fseek($this->fp, 0, SEEK_SET);
for ($i = 0; $i < count($this->buffer); $i++)
{
$this->write($this->buffer[$i] . PHP_EOL);
}
}
public function setKeyBuffer($key, $value, $formal = true)
{
$key = $this->format($key);
if ($formal)
{
$value = $this->format($value);
}
for ($i = 0; $i < count($this->buffer); $i++)
{
$line = $this->buffer[$i];
$line = trim($line);
$args = explode("=>", $line);
if (count($args) != 2)
{
continue;
}
$args[0] = trim($args[0]);
$args[1] = trim($args[1]);
if ($args[0] == $key)
{
$args[1] = $value;
$newLine = $args[0] . " => " . $args[1];
$last = mb_substr($line, -1);
if ($last == ",")
{
$newLine .= ",";
}
$this->buffer[$i] = $newLine;
break;
}
}
}
public function rewrite($key, $value)
{
$line = false;
$key = $this->format($key);
$value = $this->format($value);
$args = [$key, $value];
$buffer = [];
while (($line = $this->read()) !== false) {
$line = trim($line);
$args = explode("=>", $line);
if (count($args) != 2)
{
array_push($buffer, $line);
continue;
}
$args[0] = trim($args[0]);
$args[1] = trim($args[1]);
if ($args[0] == $key)
{
$args[1] = $value;
$newLine = $args[0] . " => " . $args[1];
$last = mb_substr($line, -1);
if ($last == ",")
{
$newLine .= ",";
}
} else {
$newLine = $line;
}
echo $newLine . PHP_EOL;
array_push($buffer, $newLine);
}
ftruncate($this->fp,0);
fseek($this->fp, 0, SEEK_SET);
for ($i = 0; $i < count($buffer); $i++)
{
$this->write($buffer[$i] . "\n");
}
}
public function format($value)
{
return "\"" . $value . "\"";
}
}

183
template/install.php Normal file
View File

@ -0,0 +1,183 @@
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>インストール | タイムカード</title>
<link rel="canonical" href="<?php echo $args["canonical"]; ?>" />
<link rel="stylesheet" href="<?php echo $args["canonical"]; ?>css/bootstrap.min.css" />
<?php foreach ($args["config"]["heads"] as $key => $val): ?>
<?php echo $val; ?>
<?php endforeach; ?>
</head>
<body>
<div class="container">
<h2>タイムカード</h2>
</div>
<div class="container mt-3">
<?php if($args["step"] == "welcome"): ?>
<h2>Welcome to Installer</h2>
<p>
インストーラーを実行するには、ボタンをクリックしてください。
</p>
<form class="form" method="POST">
<button class="btn btn-success" name="step" value="welcome">インストール</button>
</form>
<?php elseif($args["step"] == "done"): ?>
<h2>インストール完了</h2>
<p>
インストール作業が完了しました。<br />
インストーラーを削除するには、ボタンをクリックしてください。
</p>
<form class="form" method="POST">
<button class="btn btn-danger" name="step" value="done">インストーラー削除</button>
</form>
<?php elseif($args["step"] == "mysql"): ?>
<h2>MYSQL設定</h2>
<form class="form" method="POST">
<div class="form-group row">
<div class="col-md-2">
<label for="hostname" class="form-label">ホスト名</label>
</div>
<div class="col-md-7">
<input class="form-control" type="text" name="hostname" id="hostname" required />
</div>
<div class="col-md-3 form-text">
ホスト名を入力してください。
</div>
</div>
<div class="form-group row">
<div class="col-md-2">
<label for="dbname" class="form-label">データベース名</label>
</div>
<div class="col-md-7">
<input class="form-control" type="text" name="dbname" id="dbname" required />
</div>
<div class="col-md-3 form-text">
データベース名を入力してください。
</div>
</div>
<div class="form-group row">
<div class="col-md-2">
<label for="username" class="form-label">ユーザー名</label>
</div>
<div class="col-md-7">
<input class="form-control" type="text" name="username" id="username" required />
</div>
<div class="col-md-3 form-text">
ユーザー名を入力してください。
</div>
</div>
<div class="form-group row">
<div class="col-md-2">
<label for="password" class="form-label">パスワード</label>
</div>
<div class="col-md-7">
<input class="form-control" type="password" name="password" id="password" required />
</div>
<div class="col-md-3 form-text">
パスワードを入力してください。
</div>
</div>
<input type="hidden" name="step" value="mysql" />
<div class="form-group row">
<div class="col-md-2">
</div>
<div class="col-md-7">
<button class="btn btn-primary" type="submit">設定</button>
</div>
<div class="col-md-3 form-text">
</div>
</div>
</form>
<?php elseif($args["step"] == "link"): ?>
<h2>リンク設定</h2>
<form class="form" method="POST">
<div class="form-group row">
<div class="col-md-2">
<label for="url" class="form-label">ウェブサイトURL</label>
</div>
<div class="col-md-7">
<input class="form-control" type="url" name="url" id="url" required />
</div>
<div class="col-md-3 form-text">
ウェブサイトURLを入力してください。
</div>
</div>
<input type="hidden" name="step" value="link" />
<div class="form-group row">
<div class="col-md-2">
</div>
<div class="col-md-7">
<button class="btn btn-primary" type="submit">設定</button>
</div>
<div class="col-md-3 form-text">
</div>
</div>
</form>
<?php elseif($args["step"] == "account"): ?>
<h2>管理者アカウント作成</h2>
<form class="form" method="POST">
<div class="form-group row">
<div class="col-md-2">
<label for="username" class="form-label">ユーザー名</label>
</div>
<div class="col-md-7">
<input type="text" value="" class="form-control" name="username" id="username" />
</div>
<div class="col-md-3 form-text">
ユーザー名を入力してください。
</div>
</div>
<div class="form-group row">
<div class="col-md-2">
<label for="password" class="form-label">パスワード</label>
</div>
<div class="col-md-7">
<input type="password" value="" class="form-control" name="password" id="password" />
</div>
<div class="col-md-3 form-text">
パスワードを入力してください。
</div>
</div>
<input type="hidden" name="mode" value="create" />
<input type="hidden" name="step" value="account" />
<div class="form-group row">
<div class="col-md-2">
</div>
<div class="col-md-7">
<button class="btn btn-primary" type="submit">作成</button>
</div>
<div class="col-md-3 form-text">
</div>
</div>
</form>
<?php endif; ?>
</div>
<div class="container">
<footer class="footer">
<div class="container text-center">
<p class="text-muted">
Copyright &copy; 2022 <a href="https://devras.net">DevRas</a> All Rights Reserved.
</p>
<p class="text-muted">
TimeCard v1.0.0
<a href="https://github.com/kemasama/Timecard">github</a>
</p>
</div>
</footer>
</div>
</body>
</html>