You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
179 lines
5.4 KiB
179 lines
5.4 KiB
<?php
|
|
|
|
namespace IQBall\Core\Gateway;
|
|
|
|
use IQBall\Core\Connection;
|
|
use IQBall\Core\Data\Account;
|
|
use IQBall\Core\Data\User;
|
|
use PDO;
|
|
|
|
class AccountGateway {
|
|
private Connection $con;
|
|
|
|
/**
|
|
* @param Connection $con
|
|
*/
|
|
public function __construct(Connection $con) {
|
|
$this->con = $con;
|
|
}
|
|
|
|
public function insertAccount(string $name, string $email, string $token, string $hash, string $profilePicture): int {
|
|
$this->con->exec("INSERT INTO Account(username, hash, email, token,profilePicture) VALUES (:username,:hash,:email,:token,:profilePic)", [
|
|
':username' => [$name, PDO::PARAM_STR],
|
|
':hash' => [$hash, PDO::PARAM_STR],
|
|
':email' => [$email, PDO::PARAM_STR],
|
|
':token' => [$token, PDO::PARAM_STR],
|
|
':profilePic' => [$profilePicture, PDO::PARAM_STR],
|
|
]);
|
|
return intval($this->con->lastInsertId());
|
|
}
|
|
|
|
/**
|
|
* @param string $email
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
private function getRowsFromMail(string $email): ?array {
|
|
return $this->con->fetch("SELECT * FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]])[0] ?? null;
|
|
}
|
|
|
|
/**
|
|
* @param int $id
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
private function getRowsFromId(int $id): ?array {
|
|
return $this->con->fetch("SELECT * FROM Account WHERE id = :id", [':id' => [$id, PDO::PARAM_INT]])[0] ?? null;
|
|
}
|
|
|
|
/**
|
|
* @param string $email
|
|
* @return string|null the hashed user's password, or null if the given mail does not exist
|
|
*/
|
|
public function getHash(string $email): ?string {
|
|
$results = $this->getRowsFromMail($email);
|
|
if ($results == null) {
|
|
return null;
|
|
}
|
|
return $results['hash'];
|
|
}
|
|
|
|
/**
|
|
* @param string $email
|
|
* @return bool true if the given email exists in the database
|
|
*/
|
|
public function exists(string $email): bool {
|
|
return $this->getRowsFromMail($email) != null;
|
|
}
|
|
|
|
/**
|
|
* @param string $email
|
|
* @return Account|null
|
|
*/
|
|
public function getAccountFromMail(string $email): ?Account {
|
|
$acc = $this->getRowsFromMail($email);
|
|
if (empty($acc)) {
|
|
return null;
|
|
}
|
|
|
|
return new Account($acc["token"], new User($email, $acc["username"], $acc["id"], $acc["profilePicture"]));
|
|
}
|
|
|
|
/**
|
|
* @param int $id
|
|
* @return Account|null
|
|
*/
|
|
public function getAccountFromId(int $id): ?Account {
|
|
$acc = $this->getRowsFromId($id);
|
|
if (empty($acc)) {
|
|
return null;
|
|
}
|
|
|
|
return new Account($acc["token"], new User($acc["email"], $acc["username"], $id, $acc["profilePicture"]));
|
|
}
|
|
|
|
/**
|
|
* @param string $token get an account from given token
|
|
* @return Account|null
|
|
*/
|
|
public function getAccountFromToken(string $token): ?Account {
|
|
$acc = $this->con->fetch("SELECT * FROM Account WHERE token = :token", [':token' => [$token, PDO::PARAM_STR]])[0] ?? null;
|
|
if (empty($acc)) {
|
|
return null;
|
|
}
|
|
|
|
return new Account($acc["token"], new User($acc["email"], $acc["username"], $acc["id"], $acc["profilePicture"]));
|
|
}
|
|
|
|
public function updateProfile(string $name, string $email, int $id) : void {
|
|
$this->con->exec("
|
|
UPDATE Account
|
|
SET email = :email AND username = :username
|
|
WHERE id = :id
|
|
", [
|
|
':username' => [$name, PDO::PARAM_STR],
|
|
':email' => [$email, PDO::PARAM_STR],
|
|
':id' => [$id, PDO::PARAM_INT]
|
|
]);
|
|
}
|
|
|
|
public function nameIsDifferent(int $id, string $name) : bool {
|
|
$nameExist = $this->con->fetch("SELECT username FROM Account WHERE id = :id", [':id' => [$id, PDO::PARAM_STR]]) ?? null;
|
|
|
|
if (!empty($nameExist)) {
|
|
if ($name != $nameExist[0]["username"]) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function emailIsDifferent(int $id, string $email) : bool {
|
|
$emailExist = $this->con->fetch("SELECT email FROM Account WHERE id = :id", [':id' => [$id, PDO::PARAM_STR]]) ?? null;
|
|
|
|
if (!empty($emailExist)) {
|
|
if ($email != $emailExist[0]["email"]) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function emailExist(string $email) {
|
|
$res = $this->con->fetch("SELECT email FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]) ?? null;
|
|
return $res != null;
|
|
}
|
|
|
|
public function changeName(int $id, string $newName) {
|
|
$this->con->exec("
|
|
UPDATE Account
|
|
SET username = :username
|
|
WHERE id = :id
|
|
", [
|
|
':username' => [$newName, PDO::PARAM_STR],
|
|
':id' => [$id, PDO::PARAM_STR]
|
|
]);
|
|
}
|
|
|
|
public function changeEmail(int $id, string $newEmail) {
|
|
$this->con->exec("
|
|
UPDATE Account
|
|
SET email = :email
|
|
WHERE id = :id
|
|
", [
|
|
':email' => [$newEmail, PDO::PARAM_STR],
|
|
':id' => [$id, PDO::PARAM_STR]
|
|
]);
|
|
}
|
|
|
|
public function changePicture(int $id, string $newLien) {
|
|
error_log($newLien);
|
|
$this->con->exec("
|
|
UPDATE Account
|
|
SET profilePicture = :lien
|
|
WHERE id = :id
|
|
", [
|
|
':lien' => [$newLien, PDO::PARAM_STR],
|
|
':id' => [$id, PDO::PARAM_STR]
|
|
]);
|
|
}
|
|
}
|