<?php
$siteCode = str_replace('www.', '', strtolower($_SERVER['HTTP_HOST']));
$siteCode = explode('.', $siteCode);
$siteCode = mysqli_real_escape_string($mysqli, $siteCode[0]);
if ($result = $mysqli->query("SELECT Website.*
FROM Website WHERE Website.AffiliateCode = '" . $siteCode . "' AND Website.Live = 1
AND (Website.StartDate > NOW() OR Website.EndDate > NOW() OR Website.EndDate IS NULL) LIMIT 1;"))
{
    if($result->num_rows == 0)
    {
        print '<!DOCTYPE html><html lang="en"><head><title>' . $motoquoteName . '</title>
        <link href="/css/style.css" rel="stylesheet" media="screen" />
        <link href="/css/style_print.css" rel="stylesheet" media="print" />
        </head><body>';
        print '<div id="content" class="content"><p style="padding: 20px;">Sorry, we could not find the ' . $motoquoteName . ' website.</p></div>';
        print '</body></html>';
        exit;
    }
    else
    {
	    while ($row = $result->fetch_object())
	    {
		    $_SESSION['websiteId'] = $row->Id;
		    $_SESSION['websiteName'] = $row->Name;
		    $_SESSION['websiteEmail'] = $row->ContactEmail;
		    $_SESSION['websiteCopyEmail'] = $row->CopyEmail;
		    $_SESSION['websiteCopyMarketEmail'] = $row->CopyMarketEmail;
		    $_SESSION['websiteFooter'] = stripslashes($row->Footer);
		    $_SESSION['websiteHomepageURL'] = $row->HomepageURL;
            $motoquoteUrl = 'http://' . $row->AffiliateCode . '.' . $motoquoteDomain;
            $motoquoteSubdomain = $row->AffiliateCode;
            $quotationDayLimit = $row->QuotationDayLimit;
            $enableBeatThisPrice = $row->EnableBeatThisPrice;
            $enableBeatThisPriceQuestions = $row->EnableBeatThisPriceQuestions;
            $enableMarineInsurance = $row->EnableMarineInsurance;
            $enableProfileNotification = $row->EnableProfileNotification;
            $enableSailingSchedule = $row->EnableSailingSchedule;
            $extendedRegistration = $row->ExtendedRegistration;
            $automaticActivation = $row->AutomaticActivation;
            $priceRoundUp = $row->PriceRoundUp;
		    $websiteAddress = $row->Address;
		    $websitePhone = $row->Phone;
		    $websiteEmail = $row->Email;
		    $TwitterURL = $row->TwitterURL;
		    $FacebookURL = $row->FacebookURL;
		    $LinkedInURL = $row->LinkedInURL;
		    $GooglePlusURL = $row->GooglePlusURL;
		    $YouTubeURL = $row->YouTubeURL;
		    $PinterestURL = $row->PinterestURL;
		    $InstagramURL = $row->InstagramURL;
		    $DateFormatUS = $row->DateFormatUS;
		    $LimitedPowerOfAttorneyText = stripslashes($row->LimitedPowerOfAttorneyText);
		    $ShippersLetterOfInstructionText = stripslashes($row->ShippersLetterOfInstructionText);
	    }
    }
}
else
{
	print $mysqli->error;
	exit;
}
$result->close();
if(isset($_SESSION['userId']) && isset($_SESSION['websiteId']))
{
    if ($result = $mysqli->query("SELECT User.Id FROM User WHERE User.Id = '" . $_SESSION['userId'] . "' AND (User.WebsiteId = '" . $_SESSION['websiteId'] . "' OR User.UserLevelId > ' . $user . ') AND User.Disabled = 0 LIMIT 1;"))
    {
        if($result->num_rows == 0)
        {
            header("Location: /logout");
            exit;
        }
    }
    else
    {
	    print $mysqli->error;
	    exit;
    }
    $result->close();
}
?>