autodiscover.php 5.9 KB

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