<?php

// For other supported protocols and more protocol settings, see:

// http://technet.microsoft.com/en-us/library/cc511507.aspx

// Get contents of request made to Autodiscover.

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

preg_match("/\<EMailAddress\>(.*?)\<\/EMailAddress\>/", $request, $email_address);

preg_match("/\<EMailAddress\>(.*?)@(.*?)\<\/EMailAddress\>/", $request, $email_name);

/*** Begin Configuration ***/

// ActiveSync URL.

$_CONFIG['MobileSync']['Url'] = https://webmail.domain.com/Microsoft-Server-ActiveSync;

// IMAP configuration settings.

$_CONFIG['IMAP']['Server'] = "webmail.domain.com";

$_CONFIG['IMAP']['Port'] = "993";

$_CONFIG['IMAP']['SSL'] = "on";

$_CONFIG['IMAP']['SPA'] = "off";

$_CONFIG['IMAP']['AuthRequired'] = "on";

$_CONFIG['IMAP']['DomainRequired'] = "off";

$_CONFIG['IMAP']['LoginName'] = $email_name[1];

// SMTP configuration settings.

$_CONFIG['SMTP']['Server'] = "webmail.domain.com";

$_CONFIG['SMTP']['Port'] = "465";

$_CONFIG['SMTP']['SSL'] = "on";

$_CONFIG['SMTP']['SPA'] = "off";

$_CONFIG['SMTP']['AuthRequired'] = "on";

$_CONFIG['SMTP']['DomainRequired'] = "off";

$_CONFIG['SMTP']['LoginName'] = $email_name[1];

/*** End Configuration ***/

// XML document heading.

header("Content-Type: text/xml");

echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

// Get the schema from the request.

preg_match("/\<AcceptableResponseSchema\>(.*?)\<\/AcceptableResponseSchema\>/", $request, $schema);

// Determine the type of device requesting Autodiscover.

if (preg_match("/\/mobilesync\//", $schema[1]))

{

        // Mobile device.

        ?>

                <Autodiscover xmlns=http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006>

                        <Response xmlns="<?php echo $schema[1]; ?>">

<Culture>de:de</Culture>

                                <User>

<DisplayName><?php echo $email_address[1]; ?></DisplayName>

<EMailAddress><?php echo $email_address[1]; ?></EMailAddress>

                                </User>

                                <Action>

<Settings>

<Server>

<Type>MobileSync</Type>

               <Url><?php echo $_CONFIG['MobileSync']['Url']; ?></Url>

<Name><?php echo $_CONFIG['MobileSync']['Url']; ?></Name>

</Server>

                           </Settings>

                                </Action>

                        </Response>

                </Autodiscover>

        <?php

}

else if (preg_match("/\/outlook\//", $schema[1]))

{

        // MUA (mail client).

        ?>

                <Autodiscover xmlns=http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006>

                        <Response xmlns="<?php echo $schema[1]; ?>">

                                <Account>

       <AccountType>email</AccountType>

<Action>settings</Action>

                        <?php

                                        // Loop through each configured protocol.

while(list($protocol, $settings) = each($_CONFIG))

                                        {

// Skip ActiveSync protocol.

  if ($protocol == "MobileSync") continue;

                                        ?>

<Protocol>

<Type><?php echo $protocol; ?></Type>

                       <?php

// Loop through each setting for this protocol.

while(list($setting, $value) = each($settings))

{

echo "\t\t\t\t\t\t\t<$setting>$value</$setting>\n";

}

                                        ?>

        </Protocol>

<?php

                                        }

                                ?>

</Account>

                        </Response>

                </Autodiscover>

        <?php

}

else

{

        // Unknown.

        list($usec, $sec) = explode(' ', microtime());

        ?>

                <Autodiscover xmlns=http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006>

                        <Response>

                                <Error Time="<?php echo date('H:i:s', $sec) . substr($usec, 0, strlen($usec) - 2); ?>" Id="2477272013">

<ErrorCode>600</ErrorCode>

                      <Message>Invalid Request</Message>

<DebugData />

                                </Error>

                        </Response>

                </Autodiscover>

        <?php

}

?>

And the mailsetup.php for iOS devices:

<?php if (isset($_REQUEST['name'])):?>

<?php

$username = strtolower($_REQUEST['name']);

$conf = file_get_contents('/var/www/html/mailsetup.mobileconfig');

$conf = str_replace('%EMAIL%', $username . '@domain.com', $conf);

$conf = str_replace('%USERNAME%', $username, $conf);

$timestamp = microtime(true);

$filehandle = fopen("/var/www/html/apple/$timestamp", "w");

fwrite($filehandle, $conf);

fclose($filehandle);

$cmdline = "/usr/bin/openssl smime -sign -in /var/www/html/apple/$timestamp -out /var/www/html/apple/$timestamp.signed -signer /etc/pki/dovecot/dovecot.cer -inkey /etc/pki/dovecot/private/dovecot.pem -certfile /etc/pki/dovecot/intermediate.cer -nodetach -outform der";

$output = exec($cmdline);

header('Content-type: application/x-apple-aspen-config; charset=utf-8');

header('Content-Disposition: attachment; filename="domain.mobileconfig"');

header('Content-Transfer-Encoding: binary');

ob_clean();

flush();

readfile("/var/www/html/apple/$timestamp.signed");

flush();

unlink("/var/www/html/apple/$timestamp");

unlink("/var/www/html/apple/$timestamp.signed");

?>

<?php else: ?>

<html>

<head>

<title>domain.com ActiveSync Configuration for Apple iOS</title>

<meta name="viewport" content="width=device-width; initial-scale=1; user-scalable=no" />

</head>

<body>

<form method="post" action="mailsetup.php">

<p style="text-align: center">

<b>Benutzer-Name:</b>

<br>&nbsp;<br>

<input type="text" name="name" style="height: 30px; width: 200px;"/>

<br>&nbsp;<br>

<input type="submit" value="Ok" style="width: 80px; height: 30px;"/>

</p>

</form>

</body>

</html>

<?php endif ?>
