bcc.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. // File size is limited by Nginx site to 10M
  3. // To speed things up, we do not include prerequisites
  4. header('Content-Type: text/plain');
  5. require_once "vars.inc.php";
  6. // Do not show errors, we log to using error_log
  7. ini_set('error_reporting', 0);
  8. // Init database
  9. //$dsn = $database_type . ':host=' . $database_host . ';dbname=' . $database_name;
  10. $dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
  11. $opt = [
  12. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  13. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  14. PDO::ATTR_EMULATE_PREPARES => false,
  15. ];
  16. try {
  17. $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
  18. }
  19. catch (PDOException $e) {
  20. error_log("BCC MAP SQL ERROR: " . $e . PHP_EOL);
  21. http_response_code(501);
  22. exit;
  23. }
  24. function parse_email($email) {
  25. if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
  26. $a = strrpos($email, '@');
  27. return array('local' => substr($email, 0, $a), 'domain' => substr(substr($email, $a), 1));
  28. }
  29. if (!function_exists('getallheaders')) {
  30. function getallheaders() {
  31. if (!is_array($_SERVER)) {
  32. return array();
  33. }
  34. $headers = array();
  35. foreach ($_SERVER as $name => $value) {
  36. if (substr($name, 0, 5) == 'HTTP_') {
  37. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  38. }
  39. }
  40. return $headers;
  41. }
  42. }
  43. // Read headers
  44. $headers = getallheaders();
  45. // Get rcpt
  46. $rcpt = $headers['Rcpt'];
  47. // Get from
  48. $from = $headers['From'];
  49. // Remove tags
  50. $rcpt = preg_replace('/^(.*?)\+.*(@.*)$/', '$1$2', $rcpt);
  51. $from = preg_replace('/^(.*?)\+.*(@.*)$/', '$1$2', $from);
  52. try {
  53. if (!empty($rcpt)) {
  54. $stmt = $pdo->prepare("SELECT `bcc_dest` FROM `bcc_maps` WHERE `type` = 'rcpt' AND `local_dest` = :local_dest AND `active` = '1'");
  55. $stmt->execute(array(
  56. ':local_dest' => $rcpt
  57. ));
  58. $bcc_dest = $stmt->fetch(PDO::FETCH_ASSOC)['bcc_dest'];
  59. if (!empty($bcc_dest) && filter_var($bcc_dest, FILTER_VALIDATE_EMAIL)) {
  60. error_log("BCC MAP: returning ". $bcc_dest . " for " . $rcpt . PHP_EOL);
  61. http_response_code(201);
  62. echo trim($bcc_dest);
  63. exit;
  64. }
  65. }
  66. if (!empty($from)) {
  67. $stmt = $pdo->prepare("SELECT `bcc_dest` FROM `bcc_maps` WHERE `type` = 'sender' AND `local_dest` = :local_dest AND `active` = '1'");
  68. $stmt->execute(array(
  69. ':local_dest' => $from
  70. ));
  71. $bcc_dest = $stmt->fetch(PDO::FETCH_ASSOC)['bcc_dest'];
  72. if (!empty($bcc_dest) && filter_var($bcc_dest, FILTER_VALIDATE_EMAIL)) {
  73. error_log("BCC MAP: returning ". $bcc_dest . " for " . $from . PHP_EOL);
  74. http_response_code(201);
  75. echo trim($bcc_dest);
  76. exit;
  77. }
  78. }
  79. }
  80. catch (PDOException $e) {
  81. error_log("BCC MAP SQL ERROR: " . $e->getMessage() . PHP_EOL);
  82. http_response_code(502);
  83. exit;
  84. }