63 lines
1.4 KiB
PHP
63 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Model;
|
|
|
|
use App\Entity\WireguardRequest;
|
|
|
|
class RequestsSqlite
|
|
{
|
|
private $db;
|
|
|
|
public function __construct()
|
|
{
|
|
$db_existed = false;
|
|
if(file_exists( 'Requests.db')) $db_existed = true;
|
|
$this->db = new \SQLite3('Requests.db');
|
|
|
|
if(!$db_existed) $this->initializeDatabase();
|
|
|
|
return $this;
|
|
}
|
|
|
|
private function initializeDatabase() {
|
|
$query = '
|
|
CREATE TABLE requests
|
|
(
|
|
ID INT PRIMARY KEY NOT NULL,
|
|
Email TEXT NOT NULL,
|
|
AuthToken TEXT NOT NULL,
|
|
ValidUntil TEXT NOT NULL,
|
|
PublicKey TEXT
|
|
)
|
|
';
|
|
|
|
return $this->query($query);
|
|
}
|
|
|
|
private function query($query) {
|
|
|
|
$ret = $this->db->exec($query);
|
|
if(!$ret) {
|
|
throw new \Exception($this->db->lastErrorMsg());
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public function addRequest(WireguardRequest $wireguardRequest) {
|
|
|
|
$query = '
|
|
INSERT INTO requests (AuthToken, ValidUntil, Email) VALUES
|
|
(
|
|
'.$wireguardRequest->getAuthToken().',
|
|
'.$wireguardRequest->getValidUntil().',
|
|
'.$wireguardRequest->getEmail().'
|
|
);
|
|
';
|
|
|
|
return $this->query($query);
|
|
}
|
|
|
|
// TODO implement update validate ect
|
|
}
|