<?php
//get raw POST data so we can extract the email address
$postdata = $_POST;
$data = file_get_contents("php://input");
preg_match("/\<EMailAddress\>(.*?)\<\/EMailAddress\>/", $data, $matches);
$host = getDomain();

$imapServer = "imap.nanosistemi.com";
$smtpServer = "smtp.nanosistemi.com";

if ($host == "resnovaspa.com"){
        $imapServer = $smtpServer = "resnova-mail.nanosistemi.com";
}
if ($host == "samsic.it"){
        $imapServer = $smtpServer = "resnova-mail.nanosistemi.com";
}


$logfilep = date('m-d-Y_hia').'.txt';
$logfile = fopen($logfilep, 'w');
fwrite($logfile, "\n");
fwrite($logfile, $data);
fwrite($logfile, "\n");
fwrite($logfile, "\n");
fwrite($logfile, $matches[1]);
fwrite($logfile, "\n");
fwrite($logfile, "\n");
fclose($logfile);

if (count($_GET) != 0) {

	echo "sonoqua";
	
}


if (count($_GET) == 0) {

	//set Content-Type
	header("Content-Type: application/xml");
	echo '<?xml version="1.0" encoding="utf-8" ?>'; ?>
	
	<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
		<Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
	
	
	      		<User><DisplayName><?php echo $matches[1]; ?></DisplayName></User>
	
			<Account>
	          		<AccountType>email</AccountType>
				<Action>settings</Action>
				<ServiceHome>https://nanosistemi.com</ServiceHome>
	
			        <Protocol>
	               			<Type>IMAP</Type>
					<Server><?php echo $imapServer; ?></Server>
	        			<Port>993</Port>
	               			<DomainRequired>off</DomainRequired>
	               			<LoginName><?php echo $matches[1]; ?></LoginName>
	               			<SPA>on</SPA>
		        		<SSL>on</SSL>
	        			<AuthRequired>on</AuthRequired>
	           		</Protocol>
		        	<Protocol>
	        			<Type>SMTP</Type>
	               			<Server><?php echo $smtpServer; ?></Server>
	               			<Port>465</Port>
	               			<DomainRequired>off</DomainRequired>
		        		<LoginName><?php echo $matches['1']; ?></LoginName>
	        			<SPA>on</SPA>
	               			<Encryption>SSL</Encryption>
	               			<AuthRequired>on</AuthRequired>
	               			<UsePOPAuth>off</UsePOPAuth>
		        		<SMTPLast>off</SMTPLast>
	          		</Protocol>
			</Account>
		</Response>
	</Autodiscover>
<?php } 

function getDomain()
{
    // a list of decimal-separated TLDs
        static $doubleTlds = array(
                'co.uk',
        );

        $fulldomain = $_SERVER['HTTP_HOST'];
        $fulldomain = strtolower($fulldomain);

        // explode on the decimals
        $parts = explode( '.', $fulldomain );

        // grab the TLD
        $tld = array_pop( $parts );

        // grab the hostname
        $host = array_pop( $parts ) . '.' . $tld;

        // have we collected a double TLD?
        if ( ! empty( $parts ) && in_array( $host, $doubleTlds ) )
        {
            $host = array_pop( $parts ) . '.' . $host;
        }
        if(empty($host)){
                return 'nanosistemi.com';
        }
        return $host;
}
?>
