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; } }