Session.php 3.8 KB

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