57 lines
1.3 KiB
PHP
Executable File
57 lines
1.3 KiB
PHP
Executable File
<?php
|
|
// Load Main Libary
|
|
require "../api.php";
|
|
|
|
$api = new gbAPI(false);
|
|
|
|
// Check Request body
|
|
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
|
|
$api->answer(400, [
|
|
"error" => "false_request_method",
|
|
"error_text" => "Only POST allowed"
|
|
]);
|
|
}
|
|
|
|
if (!isset($_POST["username"]) || !isset($_POST["password"]) || !isset($_POST["password_repeat"])) {
|
|
$api->answer(400, [
|
|
"error" => "missing_fields",
|
|
"error_text" => "Some fields are missing"
|
|
]);
|
|
}
|
|
|
|
$username = $_POST["username"];
|
|
$password = $_POST["password"];
|
|
$password_repeat = $_POST["password_repeat"];
|
|
|
|
// Check Passwords
|
|
if ($password !== $password_repeat) {
|
|
$api->answer(400, [
|
|
"error" => "passwords_dont_match",
|
|
"error_text" => "The passwords does not match."
|
|
]);
|
|
}
|
|
|
|
// Hash Password
|
|
$hash = password_hash($password);
|
|
|
|
|
|
|
|
// CREATE DATABASE STRUCTURE
|
|
$sql_file = file_get_contents(APP_PATH . "db_structure.sql");
|
|
|
|
$db_path = APP_PATH . "database.db";
|
|
|
|
$db = new PDO("sqlite:" . $db_path);
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
$db = exec($sql_file);
|
|
|
|
|
|
|
|
// INSERT USER
|
|
$command = "INSERT INTO users (username, password, owner) VALUES (:username, :password, :owner)";
|
|
|
|
$stmt = $db->prepare($command);
|
|
$stmt->execute([":username" => $username, ":password" => $hash, ":owner" => 1]);
|
|
|
|
$api->answer(200); |