59 lines
1.4 KiB
PHP
Executable File
59 lines
1.4 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"
|
|
]);
|
|
}
|
|
|
|
$input = json_decode(file_get_contents("php://input"), true);
|
|
|
|
if (!isset($input["username"]) || !isset($input["password"]) || !isset($input["password_repeat"])) {
|
|
$api->answer(400, [
|
|
"error" => "missing_fields",
|
|
"error_text" => "Some fields are missing"
|
|
]);
|
|
}
|
|
|
|
$username = $input["username"];
|
|
$password = $input["password"];
|
|
$password_repeat = $input["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); |