diff --git a/index.php b/index.php index c97202d..3a28c87 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,5 @@ install(); + diff --git a/provide/classes/BootLoader.php b/provide/classes/BootLoader.php index 169698e..2dcfa0c 100644 --- a/provide/classes/BootLoader.php +++ b/provide/classes/BootLoader.php @@ -159,4 +159,104 @@ class BootLoader $this->pdo = $db->getDB(); $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; + } + } + } } diff --git a/provide/classes/Installer.php b/provide/classes/Installer.php new file mode 100644 index 0000000..feab8bc --- /dev/null +++ b/provide/classes/Installer.php @@ -0,0 +1,150 @@ +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 . "\""; + } +} diff --git a/template/install.php b/template/install.php new file mode 100644 index 0000000..1bca751 --- /dev/null +++ b/template/install.php @@ -0,0 +1,183 @@ + + +
+ + ++ インストーラーを実行するには、ボタンをクリックしてください。 +
+ + +
+ インストール作業が完了しました。
+ インストーラーを削除するには、ボタンをクリックしてください。
+