autodiscover.php 4.7 KB

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