75 lines
2.2 KiB
PHP
75 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace main;
|
|
|
|
use SQLite3;
|
|
|
|
|
|
class Database
|
|
{
|
|
private $db;
|
|
|
|
|
|
function __construct($filename, $flags = SQLITE3_OPEN_READWRITE)
|
|
{
|
|
$this->db = new SQLite3($filename, $flags);
|
|
}
|
|
|
|
function close(): void
|
|
{
|
|
$this->db->close();
|
|
}
|
|
|
|
|
|
function install(): void
|
|
{
|
|
$this->db->exec("CREATE TABLE users(uuid text primary key not null, email text not null, password text not null);");
|
|
// TODO: Do email verification stuff: `current_email` and `email_is_verified` and stuff
|
|
$this->db->exec("CREATE TABLE trackings(user_uuid text not null, person_name text not null, is_deceased int not null)");
|
|
}
|
|
|
|
function add_user($uuid, $email, $password): bool
|
|
{
|
|
$stmt = $this->db->prepare("INSERT INTO users (uuid, email, password) VALUES (:uuid, :email, :password);");
|
|
$stmt->bindValue(":uuid", $uuid);
|
|
$stmt->bindValue(":email", $email);
|
|
$stmt->bindValue(":password", password_hash($password, PASSWORD_DEFAULT));
|
|
return $stmt->execute() !== false;
|
|
}
|
|
|
|
function delete_user($uuid): bool
|
|
{
|
|
$stmt = $this->db->prepare("DELETE FROM users WHERE uuid=:uuid;");
|
|
$stmt->bindValue(":uuid", $uuid);
|
|
return $stmt->execute() !== false;
|
|
}
|
|
|
|
function get_user_by_email($email): array|null
|
|
{
|
|
$stmt = $this->db->prepare("SELECT uuid, password FROM users WHERE email=:email;");
|
|
$stmt->bindValue(":email", $email);
|
|
$result = $stmt->execute();
|
|
|
|
if ($result->numColumns() === 0) {
|
|
return null;
|
|
}
|
|
return $stmt->execute()->fetchArray(SQLITE3_ASSOC);
|
|
}
|
|
|
|
function set_user_email($uuid, $email): bool
|
|
{
|
|
$stmt = $this->db->prepare("UPDATE users SET email=:email WHERE uuid=:uuid;");
|
|
$stmt->bindValue(":uuid", $uuid);
|
|
$stmt->bindValue(":email", $email);
|
|
return $stmt->execute() !== false;
|
|
}
|
|
|
|
function set_user_password($uuid, $password): bool
|
|
{
|
|
$stmt = $this->db->prepare("UPDATE users SET password=:password WHERE uuid=:uuid;");
|
|
$stmt->bindValue(":uuid", $uuid);
|
|
$stmt->bindValue(":password", $password);
|
|
return $stmt->execute() !== false;
|
|
}
|
|
}
|