Account.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\Types\DatabaseObjects;
  3. use app\Exceptions\AccountNotFoundException;
  4. use app\Interfaces\IDatabaseObject;
  5. use app\Hajeebtok;
  6. use app\Logger;
  7. use Hajeebtok\Types\Exceptions\SecurityFaultException;
  8. class Account implements IDatabaseObject
  9. {
  10. public private(set) ?int $id;
  11. public private(set) ?string $username;
  12. public private(set) ?string $password;
  13. public private(set) ?string $picture_hash;
  14. public private(set) ?bool $verified;
  15. public private(set) ?string $bio;
  16. public function __construct(?int $id = null, ?string $username = null, ?string $password=null, ?string $picture_hash=null, ?bool $verified=null, ?string $bio=null)
  17. {
  18. $this->id = $id;
  19. $this->username = $username;
  20. $this->password = $password ? password_hash($password, PASSWORD_DEFAULT) : null;
  21. $this->picture_hash = $picture_hash;
  22. $this->verified = $verified;
  23. $this->bio = $bio;
  24. }
  25. /**
  26. * Creates the table for the object type in the database.
  27. */
  28. public static function CreateTable(): void
  29. {
  30. throw new SecurityFaultException("Attempt to create table on user object.");
  31. }
  32. /**
  33. * Drops the table for the object type from the database.
  34. */
  35. public static function DropTable(): void
  36. {
  37. throw new SecurityFaultException("Attempt to drop table on user object.");
  38. }
  39. /**
  40. * Saves the object to the database.
  41. */
  42. public function Save(): int
  43. {
  44. Hajeebtok::$Database->Query("INSERT INTO accounts (username, password, picture_hash, verified, bio) VALUES (:username, :password, :picture_hash, :verified, :bio)", [
  45. "username" => $this->username,
  46. "password" => $this->password,
  47. "picture_hash" => $this->picture_hash,
  48. "verified" => $this->verified,
  49. "bio" => $this->bio
  50. ]);
  51. $id = Hajeebtok::$Database->LastInsertId();
  52. Logger::Debug("Saved account id ($id).");
  53. return $id;
  54. }
  55. /**
  56. * Deletes the object from the database.
  57. */
  58. public function Delete()
  59. {
  60. Hajeebtok::$Database->Query("DELETE FROM accounts WHERE id = :id", ["id" => $this->id]);
  61. }
  62. /**
  63. * Loads the object from the database.
  64. */
  65. public function Load()
  66. {
  67. if (empty($this->id)) { // search by username
  68. $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE username = :username", ["username" => $this->username]);
  69. } else {
  70. // search by id
  71. $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE id = :id", ["id" => $this->id]);
  72. }
  73. if (empty($data)) throw new AccountNotFoundException($this->id, 404);
  74. $this->id = $data["id"];
  75. $this->username = $data["username"];
  76. $this->password = $data["password"];
  77. $this->picture_hash = $data["picture_hash"];
  78. $this->verified = $data["verified"];
  79. $this->bio = $data["bio"];
  80. }
  81. public function Update() {
  82. Hajeebtok::$Database->Query("UPDATE accounts SET username = :username, password = :password, picture_hash = :picture_hash, verified = :verified, bio = :bio WHERE id = :id", [
  83. "id" => $this->id,
  84. "username" => $this->username,
  85. "password" => $this->password,
  86. "picture_hash" => $this->picture_hash,
  87. "verified" => $this->verified,
  88. "bio" => $this->bio
  89. ]);
  90. Logger::Debug("Updated account id ($this->id).");;
  91. }
  92. public function LoadMany(): array {
  93. if(empty($this->username)) throw new AccountNotFoundException(0, 404);
  94. $data = Hajeebtok::$Database->Query("SELECT * FROM accounts WHERE username LIKE :username", ["username" => "%$this->username%"]);
  95. if(empty($data)) throw new AccountNotFoundException(0, 404);
  96. return $data;
  97. }
  98. public function Exists(): bool {
  99. if ($this->id === null) { // search by username
  100. $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE username = :username", ["username" => $this->username]);
  101. } else { // search by id
  102. $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE id = :id", ["id" => $this->id]);
  103. }
  104. return !empty($data);
  105. }
  106. }