<?php
define('DEBUG', true);
ob_start();
$ASUrl = 'https://intranet.stegbauer.info';
$server = 'intranet.stegbauer.info';

$data = file_get_contents("php://input");

function value_in($element_name, $xml, $content_only = true) {
	if ($xml) {
		$found = preg_match('#<'.$element_name.'(?:\s+[^>]+)?>(.*?)</'.$element_name.'>#s', $xml, $matches1);
		if ($found) {
			if ($content_only) {
				return $matches1[1];  //ignore the enclosing tags
			} else {
				return $matches1[0];  //return the full pattern match
			}
		}
	}
	return false;
}

preg_match("/\<EMailAddress\>(.*?)\<\/EMailAddress\>/", $data, $matches);
$schemaforclient = value_in('AcceptableResponseSchema', $data);

if ($schemaforclient == "http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006") {
	echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
	echo '<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">' . "\n";
	echo '  <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">' . "\n";
	echo '  <Culture>en:en</Culture>' . "\n";
	echo '    <User>' . "\n";
	echo '      <DisplayName>' . $matches[1] . '</DisplayName>' . "\n";
	echo '      <EMailAddress>' . $matches[1] . '</EMailAddress>' . "\n";
	echo '    </User>' . "\n";
	echo '    <Action>' . "\n";
	echo '      <Settings>' . "\n";
	echo '        <Server>' . "\n";
	echo '          <Type>MobileSync</Type>' . "\n";
	echo '          <Url>' . $ASUrl . '</Url>' . "\n";
	echo '          <Name>' . $ASUrl . '</Name>' . "\n";
	echo '        </Server>' . "\n";
	echo '      </Settings>' . "\n";
	echo '    </Action>' . "\n";
	echo '  </Response>' . "\n";
	echo '</Autodiscover>' . "\n";
} else if ($schemaforclient == "http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006a") {
	echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
	echo '<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">' . "\n";
	echo '  <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">' . "\n";
	echo '    <User>' . "\n";
	echo '      <DisplayName>' . $matches[1] . '</DisplayName>' . "\n";
	echo '    </User>' . "\n";
	echo '    <AccountType>email</AccountType>' . "\n";
	echo '    <Action>settings</Action>' . "\n";
	echo '    <Protocol>' . "\n";
	echo '      <Type>IMAP</Type>' . "\n";
	echo '      <Server>' . $server . '</Server>' . "\n";
	echo '      <Port>993</Port>' . "\n";
	echo '      <LoginName>' . $matches[1] . '</LoginName>' . "\n";
	echo '      <DomainRequired>off</DomainRequired>' . "\n";
	echo '      <SPA>off</SPA>' . "\n";
	echo '      <SSL>on</SSL>' . "\n";
	echo '      <AuthRequired>on</AuthRequired>' . "\n";
	echo '    </Protocol>' . "\n";
	echo '    <Protocol>' . "\n";
	echo '      <Type>SMTP</Type>' . "\n";
	echo '      <Server>' . $server . '</Server>' . "\n";
	echo '      <Port>587</Port>' . "\n";
	echo '      <LoginName>' . $matches[1] . '</LoginName>' . "\n";
	echo '      <DomainRequired>off</DomainRequired>' . "\n";
	echo '      <SPA>off</SPA>' . "\n";
	echo '      <SSL>on</SSL>' . "\n";
	echo '      <AuthRequired>on</AuthRequired>' . "\n";
	echo '      <UsePOPAuth>off</UsePOPAuth>' . "\n";
	echo '      <SMTPLast>off</SMTPLast>' . "\n";
	echo '    </Protocol>' . "\n";
	echo '  </Response>' . "\n";
	echo '</Autodiscover>' . "\n";
} else {
	echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
	echo '<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">' . "\n";
	echo '  <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">' . "\n";
	echo '    <Error Time="' . date('G:i:s.u') . '" Id="4130155072">' . "\n";
	echo '      <ErrorCode>600</ErrorCode>' . "\n";
	echo '      <Message>Invalid Request</Message>' . "\n";
	echo '      <DebugData />' . "\n";
	echo '    </Error>' . "\n";
	echo '  </Response>' . "\n";
	echo '</Autodiscover>' . "\n";
}

if (DEBUG) {
	$base = '/var/www/autoconfig.stegbauer.info/autodiscover-debug';
	$timestamp = date('Y-m-d_G:i:s.u');
	$ip = $_SERVER['REMOTE_ADDR'];
	file_put_contents($base . '/' . $ip . '_' . $timestamp . '_request.log', $data);
	file_put_contents($base . '/' . $ip . '_' . $timestamp . '_response.log', ob_get_contents());
}

