| Matthias Andreas Benkard | 9dabdab | 2024-03-06 13:12:09 +0100 | [diff] [blame] | 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("FOOTER: " . $e . PHP_EOL); |
| 21 | http_response_code(501); |
| 22 | exit; |
| 23 | } |
| 24 | |
| 25 | if (!function_exists('getallheaders')) { |
| 26 | function getallheaders() { |
| 27 | if (!is_array($_SERVER)) { |
| 28 | return array(); |
| 29 | } |
| 30 | $headers = array(); |
| 31 | foreach ($_SERVER as $name => $value) { |
| 32 | if (substr($name, 0, 5) == 'HTTP_') { |
| 33 | $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; |
| 34 | } |
| 35 | } |
| 36 | return $headers; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Read headers |
| 41 | $headers = getallheaders(); |
| 42 | // Get Domain |
| 43 | $domain = $headers['Domain']; |
| 44 | // Get Username |
| 45 | $username = $headers['Username']; |
| 46 | // Get From |
| 47 | $from = $headers['From']; |
| 48 | // define empty footer |
| 49 | $empty_footer = json_encode(array( |
| 50 | 'html' => '', |
| 51 | 'plain' => '', |
| 52 | 'skip_replies' => 0, |
| 53 | 'vars' => array() |
| 54 | )); |
| 55 | |
| 56 | error_log("FOOTER: checking for domain " . $domain . ", user " . $username . " and address " . $from . PHP_EOL); |
| 57 | |
| 58 | try { |
| 59 | // try get $target_domain if $domain is an alias_domain |
| 60 | $stmt = $pdo->prepare("SELECT `target_domain` FROM `alias_domain` |
| 61 | WHERE `alias_domain` = :alias_domain"); |
| 62 | $stmt->execute(array( |
| 63 | ':alias_domain' => $domain |
| 64 | )); |
| 65 | $alias_domain = $stmt->fetch(PDO::FETCH_ASSOC); |
| 66 | if (!$alias_domain) { |
| 67 | $target_domain = $domain; |
| 68 | } else { |
| 69 | $target_domain = $alias_domain['target_domain']; |
| 70 | } |
| 71 | |
| 72 | // get footer associated with the domain |
| 73 | $stmt = $pdo->prepare("SELECT `plain`, `html`, `mbox_exclude`, `alias_domain_exclude`, `skip_replies` FROM `domain_wide_footer` |
| 74 | WHERE `domain` = :domain"); |
| 75 | $stmt->execute(array( |
| 76 | ':domain' => $target_domain |
| 77 | )); |
| 78 | $footer = $stmt->fetch(PDO::FETCH_ASSOC); |
| 79 | |
| 80 | // check if the sender is excluded |
| 81 | if (in_array($from, json_decode($footer['mbox_exclude']))){ |
| 82 | $footer = false; |
| 83 | } |
| 84 | if (in_array($domain, json_decode($footer['alias_domain_exclude']))){ |
| 85 | $footer = false; |
| 86 | } |
| 87 | if (empty($footer)){ |
| 88 | echo $empty_footer; |
| 89 | exit; |
| 90 | } |
| 91 | error_log("FOOTER: " . json_encode($footer) . PHP_EOL); |
| 92 | |
| 93 | // footer will be applied |
| 94 | // get custom mailbox attributes to insert into the footer |
| 95 | $stmt = $pdo->prepare("SELECT `custom_attributes` FROM `mailbox` WHERE `username` = :username"); |
| 96 | $stmt->execute(array( |
| 97 | ':username' => $username |
| 98 | )); |
| 99 | $custom_attributes = $stmt->fetch(PDO::FETCH_ASSOC)['custom_attributes']; |
| 100 | if (empty($custom_attributes)){ |
| 101 | $custom_attributes = (object)array(); |
| 102 | } |
| 103 | } |
| 104 | catch (Exception $e) { |
| 105 | error_log("FOOTER: " . $e->getMessage() . PHP_EOL); |
| 106 | http_response_code(502); |
| 107 | exit; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | // return footer |
| 112 | $footer["vars"] = $custom_attributes; |
| 113 | echo json_encode($footer); |