autodiscover.php 5.1 KB

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