autodiscover.php 4.0 KB

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