controller.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. require_once './imap_client.php';
  3. function render_error($status, $msg) {
  4. @http_response_code($status);
  5. die("{'result': 'error', 'error': '$msg'}");
  6. }
  7. class DisplayEmailsController {
  8. public static function matches() {
  9. return !isset($_GET['action']) && !empty($_SERVER['QUERY_STRING'] ?? '');
  10. }
  11. public static function invoke(ImapClient $imapClient, array $config) {
  12. $address = $_SERVER['QUERY_STRING'] ?? '';
  13. $user = User::parseDomain($address, $config['blocked_usernames']);
  14. if ($user->isInvalid($config['domains'])) {
  15. RedirectToRandomAddressController::invoke($imapClient, $config);
  16. return;
  17. }
  18. $emails = $imapClient->get_emails($user);
  19. DisplayEmailsController::render($emails, $config, $user);
  20. }
  21. public static function render($emails, $config, $user) {
  22. // variables that have to be defined here for frontend template: $emails, $config
  23. require "frontend.template.php";
  24. }
  25. }
  26. class RedirectToAddressController {
  27. public static function matches() {
  28. return ($_GET['action'] ?? null) === "redirect"
  29. && isset($_POST['username'])
  30. && isset($_POST['domain']);
  31. }
  32. public static function invoke(ImapClient $imapClient, array $config) {
  33. $user = User::parseUsernameAndDomain($_POST['username'], $_POST['domain'], $config['blocked_usernames']);
  34. RedirectToAddressController::render($user->username . "@" . $user->domain);
  35. }
  36. public static function render($address) {
  37. header("location: ?$address");
  38. }
  39. }
  40. class RedirectToRandomAddressController {
  41. public static function matches() {
  42. return ($_GET['action'] ?? null) === 'random';
  43. }
  44. public static function invoke(ImapClient $imapClient, array $config) {
  45. $address = User::get_random_address($config{'domains'});
  46. RedirectToAddressController::render($address);
  47. }
  48. }
  49. class HasNewMessagesControllerJson {
  50. public static function matches() {
  51. return ($_GET['action'] ?? null) === "has_new_messages"
  52. && isset($_GET['email_ids'])
  53. && isset($_GET['address']);
  54. }
  55. public static function invoke(ImapClient $imapClient, array $config) {
  56. $email_ids = $_GET['email_ids'];
  57. $address = $_GET['address'];
  58. $user = User::parseDomain($address, $config['blocked_usernames']);
  59. if ($user->isInvalid($config['domains'])) {
  60. render_error(400, "invalid email address");
  61. }
  62. $emails = $imapClient->get_emails($user);
  63. $knownMailIds = explode('|', $email_ids);
  64. $newMailIds = array_map(function ($mail) {
  65. return $mail->id;
  66. }, $emails);
  67. $onlyNewMailIds = array_diff($newMailIds, $knownMailIds);
  68. HasNewMessagesControllerJson::render(count($onlyNewMailIds));
  69. }
  70. public static function render($counter) {
  71. header('Content-Type: application/json');
  72. print json_encode($counter);
  73. }
  74. }
  75. class DownloadEmailController {
  76. public static function matches() {
  77. return ($_GET['action'] ?? null) === "download_email"
  78. && isset($_GET['email_id'])
  79. && isset($_GET['address']);
  80. }
  81. public static function invoke(ImapClient $imapClient, array $config) {
  82. $email_id = $_GET['email_id'];
  83. $address = $_GET['address'];
  84. $user = User::parseDomain($address, $config['blocked_usernames']);
  85. if ($user->isInvalid($config['domains'])) {
  86. RedirectToRandomAddressController::invoke($imapClient, $config);
  87. return;
  88. }
  89. $download_email_id = filter_var($email_id, FILTER_SANITIZE_NUMBER_INT);
  90. $full_email = $imapClient->load_one_email_fully($download_email_id, $user);
  91. if ($full_email !== null) {
  92. $filename = $user->address . "-" . $download_email_id . ".eml";
  93. DownloadEmailController::renderDownloadEmailAsRfc822($full_email, $filename);
  94. } else {
  95. render_error(404, 'download error: invalid username/mailid combination');
  96. }
  97. }
  98. public static function renderDownloadEmailAsRfc822($full_email, $filename) {
  99. header("Content-Type: message/rfc822; charset=utf-8");
  100. header("Content-Disposition: attachment; filename=\"$filename\"");
  101. print $full_email;
  102. }
  103. }
  104. class DeleteEmailController {
  105. public static function matches() {
  106. return ($_GET['action'] ?? null) === "delete_email"
  107. && isset($_GET['email_id'])
  108. && isset($_GET['address']);
  109. }
  110. public static function invoke(ImapClient $imapClient, array $config) {
  111. $email_id = $_GET['email_id'];
  112. $address = $_GET['address'];
  113. $user = User::parseDomain($address, $config['blocked_usernames']);
  114. if ($user->isInvalid($config['domains'])) {
  115. RedirectToRandomAddressController::invoke($imapClient, $config);
  116. return;
  117. }
  118. $delete_email_id = filter_var($email_id, FILTER_SANITIZE_NUMBER_INT);
  119. if ($imapClient->delete_email($delete_email_id, $user)) {
  120. RedirectToAddressController::render($address);
  121. } else {
  122. render_error(404, 'delete error: invalid username/mailid combination');
  123. }
  124. }
  125. }