Changes to syntax

master
andryyy 2017-06-25 20:17:31 +02:00
parent 170e030dd6
commit 468b74c860
1 changed files with 100 additions and 90 deletions

View File

@ -1,102 +1,112 @@
<?php <?php
require_once 'inc/vars.inc.php'; require_once 'inc/vars.inc.php';
require_once 'inc/functions.inc.php'; require_once 'inc/functions.inc.php';
if (file_exists('inc/vars.local.inc.php')) { if(file_exists('inc/vars.local.inc.php')) {
include_once 'inc/vars.local.inc.php'; include_once 'inc/vars.local.inc.php';
} }
error_reporting(0); error_reporting(0);
$data = trim(file_get_contents("php://input")); $data = trim(file_get_contents("php://input"));
if ($autodiscover_config['useEASforOutlook'] == 'yes' && // Desktop client needs IMAP, unless it's Outlook 2013 or higher on Windows
preg_match('/(Outlook|Office).+(1[5-9]\.)/', $_SERVER['HTTP_USER_AGENT']) && if (strpos($data, 'autodiscover/outlook/responseschema')) { // desktop client
strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') === false) { $autodiscover_config['autodiscoverType'] = 'imap';
$autodiscover_config['autodiscoverType'] = 'activesync'; if ($autodiscover_config['useEASforOutlook'] == 'yes' &&
} // Office for macOS does not support EAS
else { strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') === false &&
$autodiscover_config['autodiscoverType'] = 'imap'; // Outlook 2013 (version 15) or higher
preg_match('/(Outlook|Office).+1[5-9]\./', $_SERVER['HTTP_USER_AGENT'])) {
$autodiscover_config['autodiscoverType'] = 'activesync';
}
} }
$dsn = "$database_type:host=$database_host;dbname=$database_name"; $dsn = $database_type . ":host=" . $database_host . ";dbname=" . $database_name;
$opt = [ $opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_EMULATE_PREPARES => false,
]; ];
$pdo = new PDO($dsn, $database_user, $database_pass, $opt); $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
$login_user = strtolower(trim($_SERVER['PHP_AUTH_USER'])); $login_user = strtolower(trim($_SERVER['PHP_AUTH_USER']));
$as = check_login($login_user, $_SERVER['PHP_AUTH_PW']); $login_role = check_login($login_user, $_SERVER['PHP_AUTH_PW']);
if (!isset($_SERVER['PHP_AUTH_USER']) OR $as !== "user") { if (!isset($_SERVER['PHP_AUTH_USER']) OR $login_role !== "user") {
header('WWW-Authenticate: Basic realm=""'); header('WWW-Authenticate: Basic realm=""');
header('HTTP/1.0 401 Unauthorized'); header('HTTP/1.0 401 Unauthorized');
exit; exit(0);
} }
else { else {
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) { if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
if ($as === "user") { if ($login_role === "user") {
header("Content-Type: application/xml"); header("Content-Type: application/xml");
echo '<?xml version="1.0" encoding="utf-8" ?><Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">'; echo '<?xml version="1.0" encoding="utf-8" ?>' . PHP_EOL;
?>
if (!$data) { <Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
<?php
if(!$data) {
list($usec, $sec) = explode(' ', microtime()); list($usec, $sec) = explode(' ', microtime());
echo '<Response>'; ?>
echo '<Error Time="' . date('H:i:s', $sec) . substr($usec, 0, strlen($usec) - 2) . '" Id="2477272013">'; <Response>
echo '<ErrorCode>600</ErrorCode><Message>Invalid Request</Message><DebugData /></Error>'; <Error Time="<?=date('H:i:s', $sec) . substr($usec, 0, strlen($usec) - 2);?>" Id="2477272013">
echo '</Response>'; <ErrorCode>600</ErrorCode>
echo '</Autodiscover>'; <Message>Invalid Request</Message>
<DebugData />
</Error>
</Response>
</Autodiscover>
<?php
exit(0); exit(0);
} }
$discover = new SimpleXMLElement($data); $discover = new SimpleXMLElement($data);
$email = $discover->Request->EMailAddress; $email = $discover->Request->EMailAddress;
if ($autodiscover_config['autodiscoverType'] == 'imap') { if ($autodiscover_config['autodiscoverType'] == 'imap') {
?> ?>
<Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a"> <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
<User> <User>
<DisplayName><?php echo $displayname; ?></DisplayName> <DisplayName><?=$displayname;?></DisplayName>
</User> </User>
<Account> <Account>
<AccountType>email</AccountType> <AccountType>email</AccountType>
<Action>settings</Action> <Action>settings</Action>
<Protocol> <Protocol>
<Type>IMAP</Type> <Type>IMAP</Type>
<Server><?php echo $autodiscover_config['imap']['server']; ?></Server> <Server><?=$autodiscover_config['imap']['server'];?></Server>
<Port><?php echo $autodiscover_config['imap']['port']; ?></Port> <Port><?=$autodiscover_config['imap']['port'];?></Port>
<DomainRequired>off</DomainRequired> <DomainRequired>off</DomainRequired>
<LoginName><?php echo $email; ?></LoginName> <LoginName><?=$email;?></LoginName>
<SPA>off</SPA> <SPA>off</SPA>
<SSL><?php echo $autodiscover_config['imap']['ssl']; ?></SSL> <SSL><?=$autodiscover_config['imap']['ssl'];?></SSL>
<AuthRequired>on</AuthRequired> <AuthRequired>on</AuthRequired>
</Protocol> </Protocol>
<Protocol> <Protocol>
<Type>SMTP</Type> <Type>SMTP</Type>
<Server><?php echo $autodiscover_config['smtp']['server']; ?></Server> <Server><?=$autodiscover_config['smtp']['server'];?></Server>
<Port><?php echo $autodiscover_config['smtp']['port']; ?></Port> <Port><?=$autodiscover_config['smtp']['port'];?></Port>
<DomainRequired>off</DomainRequired> <DomainRequired>off</DomainRequired>
<LoginName><?php echo $email; ?></LoginName> <LoginName><?=$email;?></LoginName>
<SPA>off</SPA> <SPA>off</SPA>
<SSL><?php echo $autodiscover_config['smtp']['ssl']; ?></SSL> <SSL><?=$autodiscover_config['smtp']['ssl'];?></SSL>
<AuthRequired>on</AuthRequired> <AuthRequired>on</AuthRequired>
<UsePOPAuth>on</UsePOPAuth> <UsePOPAuth>on</UsePOPAuth>
<SMTPLast>off</SMTPLast> <SMTPLast>off</SMTPLast>
</Protocol> </Protocol>
<Protocol> <Protocol>
<Type>CalDAV</Type> <Type>CalDAV</Type>
<Server>https://<?php echo $mailcow_hostname; ?>/SOGo/dav/<?php echo $email; ?>/Calendar</Server> <Server>https://<?=$mailcow_hostname;?>/SOGo/dav/<?=$email;?>/Calendar</Server>
<DomainRequired>off</DomainRequired> <DomainRequired>off</DomainRequired>
<LoginName><?php echo $email; ?></LoginName> <LoginName><?=$email;?></LoginName>
</Protocol> </Protocol>
<Protocol> <Protocol>
<Type>CardDAV</Type> <Type>CardDAV</Type>
<Server>https://<?php echo $mailcow_hostname; ?>/SOGo/dav/<?php echo $email; ?>/Contacts</Server> <Server>https://<?=$mailcow_hostname;?>/SOGo/dav/<?=$email;?>/Contacts</Server>
<DomainRequired>off</DomainRequired> <DomainRequired>off</DomainRequired>
<LoginName><?php echo $email; ?></LoginName> <LoginName><?=$email;?></LoginName>
</Protocol> </Protocol>
</Account> </Account>
</Response> </Response>
<?php <?php
} }
else if ($autodiscover_config['autodiscoverType'] == 'activesync') { else if ($autodiscover_config['autodiscoverType'] == 'activesync') {
$username = trim($email); $username = trim($email);
@ -114,29 +124,29 @@ else {
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><?=$displayname;?></DisplayName>
<EMailAddress><?php echo $email; ?></EMailAddress> <EMailAddress><?=$email;?></EMailAddress>
</User> </User>
<Action> <Action>
<Settings> <Settings>
<Server> <Server>
<Type>MobileSync</Type> <Type>MobileSync</Type>
<Url><?php echo $autodiscover_config['activesync']['url']; ?></Url> <Url><?=$autodiscover_config['activesync']['url'];?></Url>
<Name><?php echo $autodiscover_config['activesync']['url']; ?></Name> <Name><?=$autodiscover_config['activesync']['url'];?></Name>
</Server> </Server>
</Settings> </Settings>
</Action> </Action>
</Response> </Response>
<?php <?php
} }
?> ?>
</Autodiscover> </Autodiscover>
<?php <?php
} }
} }
} }
?> ?>