format files

pull/4/head
remrem 2 years ago
parent 7bcccec826
commit 1f55f39a77

@ -1,9 +1,12 @@
<?php
namespace Config;
use PDO;
use PDOStatement;
class Connection extends PDO{
class Connection extends PDO
{
private PDOStatement $stmt;
public function __construct(string $dsn, string $username, string $password)

@ -1,14 +1,19 @@
<?php
namespace Config;
use PDOException;
require_once __DIR__ . "/connection.php";
class DatabaseCon{
class DatabaseCon
{
private string $dsn;
private string $login;
private string $password;
public function __construct(){
public function __construct()
{
if (getenv("SMDB_HOST") == null || getenv("SMDB_DATABASE") == null || getenv("SMDB_USER") == null || getenv("SMDB_PASSWORD") == null) {
throw new PDOException("ENV variables not found");
}
@ -17,7 +22,8 @@ class DatabaseCon{
$this->password = getenv("SMDB_PASSWORD");
}
public function connect(): int|Connection {
public function connect(): int|Connection
{
try {
$connection = new Connection($this->dsn, $this->login, $this->password);
} catch (PDOException $e) {

@ -1,25 +1,31 @@
<?php
namespace Config;
use Config\Connection;
use Config\DatabaseCon;
use PDOException;
class DatabaseInit {
class DatabaseInit
{
private Connection $con;
public function __construct() {
public function __construct()
{
if (getenv("IS_DB_INIT") === false) {
#try {
try {
$this->con = (new DatabaseCon)->connect();
#} catch(PDOException $e) {
# throw new PDOException($e->getMessage(), $e->getCode(), $e);
$this->createUserTable();
$this->createFileTable();
} catch (PDOException $e) {
throw new PDOException($e->getMessage(), $e->getCode(), $e);
}
putenv("IS_DB_INIT=true");
}
}
private function createUserTable() {
private function createUserTable()
{
$query = 'CREATE TABLE IF NOT EXISTS user (
id UUID PRIMARY KEY,
email VARCHAR(100) UNIQUE,
@ -30,7 +36,8 @@ class DatabaseInit {
$this->con->executeQuery($query);
}
private function createFileTable() {
private function createFileTable()
{
$query = 'CREATE TABLE IF NOT EXISTS file (
id UUID PRIMARY KEY,
user_id UUID REFERENCES `user`(`id`) ON DELETE CASCADE,

@ -1,14 +1,18 @@
<?php
namespace Gateway;
use Config\DatabaseCon;
use Config\Connection;
use PDOException;
use PDO;
class FileGateway {
class FileGateway
{
private Connection $con;
public function __construct() {
public function __construct()
{
try {
$this->con = (new DatabaseCon)->connect();
} catch (PDOException $e) {
@ -16,7 +20,8 @@ class FileGateway {
}
}
public function createFile(string $filename, string $user_uuid, string $category, string $creation_date) {
public function createFile(string $filename, string $user_uuid, string $category, string $creation_date)
{
$query = "INSERT INTO file VALUES(UUID(), :user_uuid, :filename, :category, :creation_date ,CURDATE());";
try {
$this->con->executeQuery($query, array(
@ -33,7 +38,8 @@ class FileGateway {
}
// Delete User: (1:OK, 2:Unauthorize, 3:No User)
public function deleteFile(string $file_uuid) : int {
public function deleteFile(string $file_uuid): int
{
$query = "DELETE FROM file WHERE id=:file_uuid;";
try {
$this->con->executeQuery($query, array(
@ -46,7 +52,8 @@ class FileGateway {
return 0;
}
public function getFilename(string $file_uuid, string $user_uuid) {
public function getFilename(string $file_uuid, string $user_uuid)
{
$query = "SELECT filename FROM file WHERE user_id=:user_uuid and id=:file_uuid;";
try {
$this->con->executeQuery($query, array(
@ -62,7 +69,8 @@ class FileGateway {
return $results[0]['filename'];
}
public function listFiles(string $user_uuid) {
public function listFiles(string $user_uuid)
{
$query = "SELECT f.id, f.filename, f.category, f.creation_date FROM file f, user u WHERE f.user_id=u.id and u.id=:user_uuid;";
try {
$this->con->executeQuery($query, array(

@ -1,16 +1,20 @@
<?php
namespace Gateway;
use Config\DatabaseCon;
use Config\Connection;
use PDOException;
use PDO;
use Config\Token;
class UserGateway {
class UserGateway
{
private Connection $con;
private Token $token;
public function __construct() {
public function __construct()
{
$this->token = new Token;
try {
$this->con = (new DatabaseCon)->connect();
@ -19,7 +23,8 @@ class UserGateway {
}
}
public function createUser(string $email, string $hash, string $username) {
public function createUser(string $email, string $hash, string $username)
{
$query = "INSERT INTO user VALUES(UUID(), :email, :hash, :username, CURDATE()) RETURNING id;";
try {
$this->con->executeQuery($query, array(
@ -36,7 +41,8 @@ class UserGateway {
}
// Delete User: (1:OK, 2:Unauthorize, 3:No User)
public function deleteUser(string $uuid) : int {
public function deleteUser(string $uuid): int
{
$query = "DELETE FROM user WHERE id=:uuid RETURNING row_count();";
try {
$this->con->executeQuery($query, array(
@ -52,7 +58,8 @@ class UserGateway {
}
// Login User (get token)
public function login(string $email, string $hash) {
public function login(string $email, string $hash)
{
$query = "SELECT hash, id FROM user WHERE email=:email;";
try {
@ -69,7 +76,8 @@ class UserGateway {
return json_encode($this->token->getNewJsonToken($results[0]['id']));
}
public function getInfo(string $uuid) {
public function getInfo(string $uuid)
{
$query = "SELECT email, username FROM user WHERE id=:uuid;";
try {
$this->con->executeQuery($query, array(
@ -84,7 +92,8 @@ class UserGateway {
return ["email" => $results[0]['email'], "username" => $results[0]['username']];
}
public function updateMail(string $uuid, string $new_email) {
public function updateMail(string $uuid, string $new_email)
{
$query = "UPDATE user SET email=:new_email WHERE id=:uuid;";
try {
$this->con->executeQuery($query, array(
@ -98,7 +107,8 @@ class UserGateway {
return 0;
}
public function updateUsername(string $uuid, string $new_username) {
public function updateUsername(string $uuid, string $new_username)
{
$query = "UPDATE user SET username=:new_username WHERE id=:uuid;";
try {
$this->con->executeQuery($query, array(

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
require_once "gateway/user_gateway.php";
require_once "gateway/file_gateway.php";

@ -1,11 +1,13 @@
<?php
namespace Config;
use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
class Token {
class Token
{
private string $key = 'passwd';
// Need to be in a config file
private string $path_to_key = "../sym_keyfile.key";
@ -18,7 +20,8 @@ class Token {
}
// Return json containing JWT with uuid and exp
public function getNewJsonToken(string $uuid) :array {
public function getNewJsonToken(string $uuid): array
{
$payload = [
'uuid' => $uuid,
'exp' => strtotime("+2month", time())
@ -28,7 +31,8 @@ class Token {
}
// Verify the JWT authenticity
public function verifyToken(string $jwt) :bool {
public function verifyToken(string $jwt): bool
{
try {
JWT::decode($jwt, new Key($this->key, 'HS256'));
} catch (Exception $e) {
@ -39,7 +43,8 @@ class Token {
// Get uuid from JWT
// Missing error handling on bad JWT
public function getUuidFromToken(string $jwt) :string {
public function getUuidFromToken(string $jwt): string
{
$decoded = (array) JWT::decode($jwt, new Key($this->key, 'HS256'));
return $decoded['uuid'];
}

Loading…
Cancel
Save