Skip to content

Instantly share code, notes, and snippets.

@msaladna
Created August 9, 2019 17:23
Show Gist options
  • Select an option

  • Save msaladna/b6afcac9d94ea3f14e82a0851f8aff32 to your computer and use it in GitHub Desktop.

Select an option

Save msaladna/b6afcac9d94ea3f14e82a0851f8aff32 to your computer and use it in GitHub Desktop.
<?php
namespace App\Models\MailConfig;
class Outlook extends \App\Models\MailConfig {
public static function handle()
{
/*** Begin Configuration ***/
// ActiveSync URL.
$_CONFIG['MobileSync']['Url'] = null;//"https://activesync.domain.com/Microsoft-Server-ActiveSync";
$email = null;
$domain = $_SERVER['HTTP_HOST'];
if (substr($domain, 0, 4) == 'www.') {
$domain = substr($domain, 4);
}
if (!strncmp($domain, "autodiscover.", strlen("autodiscover."))) {
$domain = substr($domain, strlen("autodiscover."));
}
// see also: http://technet.microsoft.com/en-us/library/cc511507.aspx
$request = file_get_contents("php://input");
if (preg_match("/\<EMailAddress\>(.*?)\<\/EMailAddress\>/", $request, $tmp)) {
$email = $tmp[1];
}
if (false !== strpos($request, "/outlook/")) {
$domain = substr($email, strpos($email, "@") + 1);
}
$server = self::lookup($domain);
// IMAP configuration settings.
$_CONFIG['IMAP']['Server'] = $server;
$_CONFIG['IMAP']['Port'] = "993";
$_CONFIG['IMAP']['SSL'] = "on";
$_CONFIG['IMAP']['SPA'] = "off";
$_CONFIG['IMAP']['AuthRequired'] = "on";
$_CONFIG['IMAP']['LoginName'] = $email;
// SMTP configuration settings.
$_CONFIG['SMTP']['Server'] = $server;
$_CONFIG['SMTP']['Port'] = "587";
$_CONFIG['SMTP']['SSL'] = 'on';
$_CONFIG['SMTP']['Encryption'] = 'TLS';
$_CONFIG['SMTP']['SPA'] = "off";
$_CONFIG['SMTP']['AuthRequired'] = "on";
$_CONFIG['SMTP']['LoginName'] = $email;
/*** End Configuration ***/
// XML document heading.
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.
$schema = $schema[1] ?? "";
if (false !== strpos($schema, "mobilesync/")) {
// Mobile device.
?>
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
<Response xmlns="<?php echo $schema; ?>">
<Culture>en:en</Culture>
<User>
<DisplayName><?php echo $email; ?></DisplayName>
<EMailAddress><?php echo $email; ?></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 (false !== strpos($schema, "outlook")) {
?>
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
<Response xmlns="<?php echo $schema; ?>">
<Account>
<AccountType>email</AccountType>
<Action>settings</Action>
<?php
// Loop through each configured protocol.
foreach($_CONFIG as $protocol => $settings) {
// Skip ActiveSync protocol.
if ($protocol == "MobileSync") continue;
?>
<Protocol>
<Type><?php echo $protocol; ?></Type>
<?php
// Loop through each setting for this protocol.
foreach($settings as $setting => $value) {
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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment