Session.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\Types\DatabaseObjects;
  3. use app\Exceptions\SessionNotFoundException;
  4. use app\Interfaces\IDatabaseObject;
  5. use app\Hajeebtok;
  6. use app\Logger;
  7. use Hajeebtok\Types\Exceptions\SecurityFaultException;
  8. use Random\RandomException;
  9. class Session implements IDatabaseObject
  10. {
  11. public private(set) ?string $token;
  12. public private(set) ?int $account_id;
  13. public private(set) ?int $date_authenticated;
  14. /**
  15. * @throws SecurityFaultException
  16. */
  17. public function __construct(?string $token = null, ?int $account_id = null, ?int $date_authenticated = null)
  18. {
  19. try {
  20. $this->token = bin2hex(random_bytes(32));
  21. } catch (RandomException $e) {
  22. throw new SecurityFaultException("yo the server caught fire and maybe my house burnt down im soRRY GANG (my ears burn).");
  23. }
  24. $this->account_id = $account_id;
  25. $this->date_authenticated = $date_authenticated;
  26. }
  27. /**
  28. * Creates the table for the object type in the database.
  29. */
  30. public static function CreateTable(): void
  31. {
  32. throw new SecurityFaultException("Attempt to create table on session object.");
  33. }
  34. /**
  35. * Drops the table for the object type from the database.
  36. */
  37. public static function DropTable(): void
  38. {
  39. throw new SecurityFaultException("Attempt to drop table on session object.");
  40. }
  41. /**
  42. * Saves the object to the database.
  43. */
  44. public function Save(): string
  45. {
  46. Hajeebtok::$Database->Query("INSERT INTO sessions (token, account_id) VALUES (:token, :account_id);", [
  47. "token" => $this->token,
  48. "account_id" => $this->account_id
  49. ]);
  50. $token = Hajeebtok::$Database->Row("SELECT token FROM sessions WHERE account_id = :account_id", ["account_id" => $this->account_id]);
  51. Logger::Debug("Saved session token ($token) for account id ($this->account_id).");
  52. return $this->token;
  53. }
  54. /**
  55. * Deletes the object from the database.
  56. */
  57. public function Delete()
  58. {
  59. if(empty($this->token)) throw new SessionNotFoundException(0, 404);
  60. Hajeebtok::$Database->Query("DELETE FROM session WHERE token = :token", ["token" => $this->token]);
  61. }
  62. public function DeleteMany() {
  63. if(empty($this->account_id)) throw new SessionNotFoundException(0, 404);
  64. Hajeebtok::$Database->Query("DELETE FROM session WHERE account_id = :account_id", ["account_id" => $this->account_id]);
  65. }
  66. /**
  67. * Loads the object from the database.
  68. */
  69. public function Load()
  70. {
  71. if (empty($this->token)) throw new SessionNotFoundException($this->token, 404);
  72. $data = Hajeebtok::$Database->Row("SELECT * FROM sessions WHERE token = :token", ["token" => $this->token]);
  73. if (empty($data)) throw new SessionNotFoundException($this->token, 404);
  74. $this->token = $data["token"];
  75. $this->account_id = $data["account_id"];
  76. $this->date_authenticated = strtotime($data["date_authenticated"]);
  77. }
  78. public function LoadMany(): array {
  79. if(empty($this->account_id)) throw new SessionNotFoundException(0, 404);
  80. $data = Hajeebtok::$Database->Query("SELECT * FROM sessions WHERE account_id = :account_id", ["account_id" => $this->account_id]);
  81. if(empty($data)) throw new SessionNotFoundException($this->account_id, 404);
  82. return $data;
  83. }
  84. }