Account.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 app\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 private(set) ?int $social_credit;
  17. public function __construct(?int $id = null, ?string $username = null, ?string $password=null, ?string $picture_hash=null, ?bool $verified=null, ?string $bio=null, ?int $social_credit=null)
  18. {
  19. $this->id = $id;
  20. $this->username = $username;
  21. $this->password = $password ? password_hash($password, PASSWORD_DEFAULT) : null;
  22. $this->picture_hash = $picture_hash;
  23. $this->verified = $verified;
  24. $this->bio = $bio;
  25. $this->social_credit = $social_credit;
  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 user 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 user object.");
  40. }
  41. /**
  42. * Saves the object to the database.
  43. */
  44. public function Save(): int
  45. {
  46. Hajeebtok::$Database->Query("INSERT INTO accounts (username, password, picture_hash, verified, bio, social_credit) VALUES (:username, :password, :picture_hash, :verified, :bio, :social_credit)", [
  47. "username" => $this->username,
  48. "password" => $this->password,
  49. "picture_hash" => $this->picture_hash,
  50. "verified" => $this->verified,
  51. "bio" => $this->bio,
  52. "social_credit" => $this->social_credit
  53. ]);
  54. $id = Hajeebtok::$Database->LastInsertId();
  55. Logger::Debug("Saved account id ($id).");
  56. return $id;
  57. }
  58. /**
  59. * Deletes the object from the database.
  60. */
  61. public function Delete()
  62. {
  63. Hajeebtok::$Database->Query("DELETE FROM accounts WHERE id = :id", ["id" => $this->id]);
  64. }
  65. /**
  66. * Loads the object from the database.
  67. */
  68. public function Load()
  69. {
  70. if (empty($this->id)) { // search by username
  71. $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE username = :username", ["username" => $this->username]);
  72. } else {
  73. // search by id
  74. $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE id = :id", ["id" => $this->id]);
  75. }
  76. if (empty($data)) throw new AccountNotFoundException($this->id, 404);
  77. $this->id = $data["id"];
  78. $this->username = $data["username"];
  79. $this->password = $data["password"];
  80. $this->picture_hash = $data["picture_hash"];
  81. $this->verified = $data["verified"] === 1;
  82. $this->bio = $data["bio"];
  83. $this->social_credit = $data["social_credit"];
  84. }
  85. public function Update() {
  86. Hajeebtok::$Database->Query("UPDATE accounts SET username = :username, password = :password, picture_hash = :picture_hash, verified = :verified, bio = :bio, social_credit = :social_credit WHERE id = :id", [
  87. "id" => $this->id,
  88. "username" => $this->username,
  89. "password" => $this->password,
  90. "picture_hash" => $this->picture_hash,
  91. "verified" => $this->verified,
  92. "bio" => $this->bio,
  93. "social_credit" => $this->social_credit
  94. ]);
  95. Logger::Debug("Updated account id ($this->id).");;
  96. }
  97. public function LoadMany(): array {
  98. if(empty($this->username)) throw new AccountNotFoundException(0, 404);
  99. $data = Hajeebtok::$Database->Query("SELECT * FROM accounts WHERE username LIKE :username", ["username" => "%$this->username%"]);
  100. if(empty($data)) throw new AccountNotFoundException(0, 404);
  101. return $data;
  102. }
  103. public function Exists(): bool {
  104. if ($this->id === null) { // search by username
  105. $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE username = :username", ["username" => $this->username]);
  106. } else { // search by id
  107. $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE id = :id", ["id" => $this->id]);
  108. }
  109. return !empty($data);
  110. }
  111. }