ContactTracker.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\Types\DatabaseObjects;
  3. use app\Exceptions\AccountNotFoundException;
  4. use app\Exceptions\ContactTrackerNotFoundException;
  5. use app\Interfaces\IDatabaseObject;
  6. use app\Hajeebtok;
  7. use app\Logger;
  8. use app\Exceptions\SecurityFaultException;
  9. class ContactTracker implements IDatabaseObject
  10. {
  11. public private(set) ?int $id;
  12. public private(set) ?string $name;
  13. public private(set) ?string $email;
  14. public private(set) ?string $message;
  15. public private(set) ?string $ip;
  16. public private(set) ?int $date_sent;
  17. public function __construct(?int $id = null, ?string $name = null, ?string $email = null, ?string $message = null, ?string $ip = null, ?int $date_sent = null)
  18. {
  19. $this->id = $id;
  20. $this->name = $name;
  21. $this->email = $email;
  22. $this->message = $message;
  23. $this->ip = $ip;
  24. $this->date_sent = $date_sent;
  25. }
  26. /**
  27. * Creates the table for the object type in the database.
  28. */
  29. public static function CreateTable(): void
  30. {
  31. throw new SecurityFaultException("Attempt to create table on contact tracker object.");
  32. }
  33. /**
  34. * Drops the table for the object type from the database.
  35. */
  36. public static function DropTable(): void
  37. {
  38. throw new SecurityFaultException("Attempt to drop table on contact tracker object.");
  39. }
  40. /**
  41. * Saves the object to the database.
  42. */
  43. public function Save(): int
  44. {
  45. Hajeebtok::$Database->Query("INSERT INTO contact_tracking (name, email, message, ip) VALUES (:name, :email, :message, :ip)", [
  46. "name" => $this->name,
  47. "email" => $this->email,
  48. "message" => $this->message,
  49. "ip" => $this->ip
  50. ]);
  51. $id = Hajeebtok::$Database->LastInsertId();
  52. Logger::Debug("Saved contact 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 contact_tracking WHERE id = :id", ["id" => $this->id]);
  61. }
  62. /**
  63. * Loads the object from the database.
  64. */
  65. public function Load(): void
  66. {
  67. if(empty($this->id)) { // search via ip
  68. $data = Hajeebtok::$Database->Row("SELECT * FROM contact_tracking WHERE ip = :ip", ["ip" => $this->ip]);
  69. if (empty($data)) throw new ContactTrackerNotFoundException($this->ip, 404);
  70. } else {
  71. $data = Hajeebtok::$Database->Row("SELECT * FROM contact_tracking WHERE id = :id", ["id" => $this->id]);
  72. if (empty($data)) throw new ContactTrackerNotFoundException($this->id, 404);
  73. }
  74. $this->id = $data["id"];
  75. $this->name = $data["name"];
  76. $this->email = $data["email"];
  77. $this->message = $data["message"];
  78. $this->ip = $data["ip"];
  79. $this->date_sent = strtotime($data["date_sent"]);
  80. }
  81. public function LoadMany(): array {
  82. if(!empty($this->email) && empty($this->name) && empty($this->ip)) {
  83. $query = "SELECT * FROM contact_tracking WHERE email = :email";
  84. $array = ["email" => $this->email];
  85. } else if (empty($this->email) && !empty($this->name) && empty($this->ip)) {
  86. $query = "SELECT * FROM contact_tracking WHERE name = :name";
  87. $array = ["name" => $this->name];
  88. } else if (empty($this->email) && empty($this->name) && !empty($this->ip)) {
  89. $query = "SELECT * FROM contact_tracking WHERE ip = :ip";
  90. $array = ["ip" => $this->ip];
  91. } else {
  92. throw new ContactTrackerNotFoundException(0, 404);
  93. }
  94. $data = Hajeebtok::$Database->Query($query, $array);
  95. if(empty($data)) throw new ContactTrackerNotFoundException(0, 404);
  96. return $data;
  97. }
  98. public function Exists(): bool {
  99. if(empty($this->ip)) throw new ContactTrackerNotFoundException(0, 400);
  100. $data = Hajeebtok::$Database->Query("SELECT * FROM contact_tracking WHERE ip = :ip", ["ip" => $this->ip]);
  101. return !empty($data);
  102. }
  103. }