Merge pull request #45 from PhoenixPeca/master

Improved Autodiscover
master
André Peters 2017-02-11 13:19:08 +01:00 committed by GitHub
commit fdcf739140
1 changed files with 106 additions and 90 deletions

View File

@ -1,6 +1,4 @@
<?php <?php
header("Content-Type: application/xml");
require_once "inc/vars.inc.php";
$config = array( $config = array(
'useEASforOutlook' => 'yes', 'useEASforOutlook' => 'yes',
'autodiscoverType' => 'activesync', 'autodiscoverType' => 'activesync',
@ -15,22 +13,43 @@ $config = array(
'ssl' => 'on' 'ssl' => 'on'
), ),
'activesync' => array( 'activesync' => array(
'url' => 'https://' . $mailcow_hostname . '/Microsoft-Server-ActiveSync' 'url' => 'https://'.$mailcow_hostname.'/Microsoft-Server-ActiveSync'
) )
); );
// If useEASforOutlook == no, the autodiscoverType option will be replaced to imap.
/* ---------- DO NOT MODIFY ANYTHING BEYOND THIS LINE. IGNORE AT YOUR OWN RISK. ---------- */
if ($config['useEASforOutlook'] == 'no') { if ($config['useEASforOutlook'] == 'no') {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Outlook')) { if (strpos($_SERVER['HTTP_USER_AGENT'], 'Outlook')) {
$config['autodiscoverType'] = 'imap'; $config['autodiscoverType'] = 'imap';
} }
} }
// Workaround for short open tags require_once 'inc/vars.inc.php';
echo '<?xml version="1.0" encoding="utf-8" ?>'; include_once 'inc/vars.local.inc.php';
?> require_once 'inc/functions.inc.php';
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
<?php $dsn = "$database_type:host=$database_host;dbname=$database_name";
$data = trim(file_get_contents("php://input")); $opt = [
if(!$data) { PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $database_user, $database_pass, $opt);
$login_user = strtolower(trim($_SERVER['PHP_AUTH_USER']));
$as = check_login($login_user, $_SERVER['PHP_AUTH_PW']);
if (!isset($_SERVER['PHP_AUTH_USER']) OR $as !== "user") {
header('WWW-Authenticate: Basic realm=""');
header('HTTP/1.0 401 Unauthorized');
exit;
} else {
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
if ($as === "user") {
header("Content-Type: application/xml");
echo '<?xml version="1.0" encoding="utf-8" ?><Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">';
$data = trim(file_get_contents("php://input"));
if(!$data) {
list($usec, $sec) = explode(' ', microtime()); list($usec, $sec) = explode(' ', microtime());
echo '<Response>'; echo '<Response>';
echo '<Error Time="' . date('H:i:s', $sec) . substr($usec, 0, strlen($usec) - 2) . '" Id="2477272013">'; echo '<Error Time="' . date('H:i:s', $sec) . substr($usec, 0, strlen($usec) - 2) . '" Id="2477272013">';
@ -38,14 +57,13 @@ if(!$data) {
echo '</Response>'; echo '</Response>';
echo '</Autodiscover>'; echo '</Autodiscover>';
exit(0); exit(0);
} }
$discover = new SimpleXMLElement($data);
$email = $discover->Request->EMailAddress;
$discover = new SimpleXMLElement($data); if ($config['autodiscoverType'] == 'imap') {
$email = $discover->Request->EMailAddress; ?>
<Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
if ($config['autodiscoverType'] == 'imap') {
?>
<Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
<Account> <Account>
<AccountType>email</AccountType> <AccountType>email</AccountType>
<Action>settings</Action> <Action>settings</Action>
@ -72,17 +90,10 @@ if ($config['autodiscoverType'] == 'imap') {
<SMTPLast>off</SMTPLast> <SMTPLast>off</SMTPLast>
</Protocol> </Protocol>
</Account> </Account>
</Response> </Response>
<?php <?php
} }
else if ($config['autodiscoverType'] == 'activesync') { else if ($config['autodiscoverType'] == 'activesync') {
$dsn = "$database_type:host=$database_host;dbname=$database_name";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $database_user, $database_pass, $opt);
$username = trim($email); $username = trim($email);
try { try {
$stmt = $pdo->prepare("SELECT `name` FROM `mailbox` WHERE `username`= :username"); $stmt = $pdo->prepare("SELECT `name` FROM `mailbox` WHERE `username`= :username");
@ -98,8 +109,8 @@ else if ($config['autodiscoverType'] == 'activesync') {
else { else {
$displayname = $email; $displayname = $email;
} }
?> ?>
<Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006"> <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006">
<Culture>en:en</Culture> <Culture>en:en</Culture>
<User> <User>
<DisplayName><?php echo $displayname; ?></DisplayName> <DisplayName><?php echo $displayname; ?></DisplayName>
@ -114,8 +125,13 @@ else if ($config['autodiscoverType'] == 'activesync') {
</Server> </Server>
</Settings> </Settings>
</Action> </Action>
</Response> </Response>
<?php
}
?>
</Autodiscover>
<?php <?php
}
}
} }
?> ?>
</Autodiscover>