autodiscover.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. require_once 'inc/vars.inc.php';
  3. require_once 'inc/functions.inc.php';
  4. $config = array(
  5. 'useEASforOutlook' => 'yes',
  6. 'autodiscoverType' => 'activesync',
  7. 'imap' => array(
  8. 'server' => $mailcow_hostname,
  9. 'port' => '993',
  10. 'ssl' => 'on',
  11. ),
  12. 'smtp' => array(
  13. 'server' => $mailcow_hostname,
  14. 'port' => '465',
  15. 'ssl' => 'on'
  16. ),
  17. 'activesync' => array(
  18. 'url' => 'https://'.$mailcow_hostname.'/Microsoft-Server-ActiveSync'
  19. )
  20. );
  21. if(file_exists('inc/vars.local.inc.php')) {
  22. include_once 'inc/vars.local.inc.php';
  23. }
  24. /* ---------- DO NOT MODIFY ANYTHING BEYOND THIS LINE. IGNORE AT YOUR OWN RISK. ---------- */
  25. error_reporting(0);
  26. if ($config['useEASforOutlook'] == 'no') {
  27. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Outlook')) {
  28. $config['autodiscoverType'] = 'imap';
  29. }
  30. }
  31. $dsn = "$database_type:host=$database_host;dbname=$database_name";
  32. $opt = [
  33. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  34. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  35. PDO::ATTR_EMULATE_PREPARES => false,
  36. ];
  37. $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
  38. $login_user = strtolower(trim($_SERVER['PHP_AUTH_USER']));
  39. $as = check_login($login_user, $_SERVER['PHP_AUTH_PW']);
  40. if (!isset($_SERVER['PHP_AUTH_USER']) OR $as !== "user") {
  41. header('WWW-Authenticate: Basic realm=""');
  42. header('HTTP/1.0 401 Unauthorized');
  43. exit;
  44. } else {
  45. if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
  46. if ($as === "user") {
  47. header("Content-Type: application/xml");
  48. echo '<?xml version="1.0" encoding="utf-8" ?><Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">';
  49. $data = trim(file_get_contents("php://input"));
  50. if(!$data) {
  51. list($usec, $sec) = explode(' ', microtime());
  52. echo '<Response>';
  53. echo '<Error Time="' . date('H:i:s', $sec) . substr($usec, 0, strlen($usec) - 2) . '" Id="2477272013">';
  54. echo '<ErrorCode>600</ErrorCode><Message>Invalid Request</Message><DebugData /></Error>';
  55. echo '</Response>';
  56. echo '</Autodiscover>';
  57. exit(0);
  58. }
  59. $discover = new SimpleXMLElement($data);
  60. $email = $discover->Request->EMailAddress;
  61. if ($config['autodiscoverType'] == 'imap') {
  62. ?>
  63. <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
  64. <Account>
  65. <AccountType>email</AccountType>
  66. <Action>settings</Action>
  67. <Protocol>
  68. <Type>IMAP</Type>
  69. <Server><?php echo $config['imap']['server']; ?></Server>
  70. <Port><?php echo $config['imap']['port']; ?></Port>
  71. <DomainRequired>off</DomainRequired>
  72. <LoginName><?php echo $email; ?></LoginName>
  73. <SPA>off</SPA>
  74. <SSL><?php echo $config['imap']['ssl']; ?></SSL>
  75. <AuthRequired>on</AuthRequired>
  76. </Protocol>
  77. <Protocol>
  78. <Type>SMTP</Type>
  79. <Server><?php echo $config['smtp']['server']; ?></Server>
  80. <Port><?php echo $config['smtp']['port']; ?></Port>
  81. <DomainRequired>off</DomainRequired>
  82. <LoginName><?php echo $email; ?></LoginName>
  83. <SPA>off</SPA>
  84. <SSL><?php echo $config['smtp']['ssl']; ?></SSL>
  85. <AuthRequired>on</AuthRequired>
  86. <UsePOPAuth>on</UsePOPAuth>
  87. <SMTPLast>off</SMTPLast>
  88. </Protocol>
  89. </Account>
  90. </Response>
  91. <?php
  92. }
  93. else if ($config['autodiscoverType'] == 'activesync') {
  94. $username = trim($email);
  95. try {
  96. $stmt = $pdo->prepare("SELECT `name` FROM `mailbox` WHERE `username`= :username");
  97. $stmt->execute(array(':username' => $username));
  98. $MailboxData = $stmt->fetch(PDO::FETCH_ASSOC);
  99. }
  100. catch(PDOException $e) {
  101. die("Failed to determine name from SQL");
  102. }
  103. if (!empty($MailboxData['name'])) {
  104. $displayname = utf8_encode($MailboxData['name']);
  105. }
  106. else {
  107. $displayname = $email;
  108. }
  109. ?>
  110. <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006">
  111. <Culture>en:en</Culture>
  112. <User>
  113. <DisplayName><?php echo $displayname; ?></DisplayName>
  114. <EMailAddress><?php echo $email; ?></EMailAddress>
  115. </User>
  116. <Action>
  117. <Settings>
  118. <Server>
  119. <Type>MobileSync</Type>
  120. <Url><?php echo $config['activesync']['url']; ?></Url>
  121. <Name><?php echo $config['activesync']['url']; ?></Name>
  122. </Server>
  123. </Settings>
  124. </Action>
  125. </Response>
  126. <?php
  127. }
  128. ?>
  129. </Autodiscover>
  130. <?php
  131. }
  132. }
  133. }
  134. ?>