autodiscover.php 4.7 KB

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