footer.php 3.2 KB

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