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