autodiscover.php 6.0 KB

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