format files

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

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

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

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

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

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

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

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

Loading…
Cancel
Save