autodiscover.php 4.7 KB

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