123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\Types\DatabaseObjects;
- use app\Exceptions\SessionNotFoundException;
- use app\Interfaces\IDatabaseObject;
- use app\Hajeebtok;
- use app\Logger;
- use Hajeebtok\Types\Exceptions\SecurityFaultException;
- use Random\RandomException;
- class Session implements IDatabaseObject
- {
- public private(set) ?string $token;
- public private(set) ?int $account_id;
- public private(set) ?int $date_authenticated;
- /**
- * @throws SecurityFaultException
- */
- public function __construct(?string $token = null, ?int $account_id = null, ?int $date_authenticated = null)
- {
- try {
- $this->token = bin2hex(random_bytes(32));
- } catch (RandomException $e) {
- throw new SecurityFaultException("yo the server caught fire and maybe my house burnt down im soRRY GANG (my ears burn).");
- }
- $this->account_id = $account_id;
- $this->date_authenticated = $date_authenticated;
- }
- /**
- * Creates the table for the object type in the database.
- */
- public static function CreateTable(): void
- {
- throw new SecurityFaultException("Attempt to create table on session object.");
- }
- /**
- * Drops the table for the object type from the database.
- */
- public static function DropTable(): void
- {
- throw new SecurityFaultException("Attempt to drop table on session object.");
- }
-
- /**
- * Saves the object to the database.
- */
- public function Save(): string
- {
- Hajeebtok::$Database->Query("INSERT INTO sessions (token, account_id) VALUES (:token, :account_id);", [
- "token" => $this->token,
- "account_id" => $this->account_id
- ]);
-
- $token = Hajeebtok::$Database->Row("SELECT token FROM sessions WHERE account_id = :account_id", ["account_id" => $this->account_id]);
- Logger::Debug("Saved session token ($token) for account id ($this->account_id).");
- return $this->token;
- }
- /**
- * Deletes the object from the database.
- */
- public function Delete()
- {
- if(empty($this->token)) throw new SessionNotFoundException(0, 404);
- Hajeebtok::$Database->Query("DELETE FROM session WHERE token = :token", ["token" => $this->token]);
- }
- public function DeleteMany() {
- if(empty($this->account_id)) throw new SessionNotFoundException(0, 404);
- Hajeebtok::$Database->Query("DELETE FROM session WHERE account_id = :account_id", ["account_id" => $this->account_id]);
- }
- /**
- * Loads the object from the database.
- */
- public function Load()
- {
- if (empty($this->token)) throw new SessionNotFoundException($this->token, 404);
- $data = Hajeebtok::$Database->Row("SELECT * FROM sessions WHERE token = :token", ["token" => $this->token]);
- if (empty($data)) throw new SessionNotFoundException($this->token, 404);
- $this->token = $data["token"];
- $this->account_id = $data["account_id"];
- $this->date_authenticated = strtotime($data["date_authenticated"]);
- }
- public function LoadMany(): array {
- if(empty($this->account_id)) throw new SessionNotFoundException(0, 404);
- $data = Hajeebtok::$Database->Query("SELECT * FROM sessions WHERE account_id = :account_id", ["account_id" => $this->account_id]);
- if(empty($data)) throw new SessionNotFoundException($this->account_id, 404);
- return $data;
- }
- }
|