<?php

// display code errors and log errors

ini_set('display_errors', 1); 

ini_set('log_errors', 1); 

ini_set('error_log', '_cargo_errors_log.txt'); 

error_reporting(E_ALL);



function encodeString($string)

{

	$string = utf8_encode($string);

	$string = htmlspecialchars($string);

	return $string;

}



// start session

session_start();

date_default_timezone_set('Europe/London');



// installation config

$apiVersion = '1.0';

$motoquoteName = 'MotoQuote';

$motoquoteDomain = 'motoquote.net';

$motoquoteUrl = 'http://www.motoquote.net';

$errorNotifyEmail = 'motoquote@elliff.co.uk';



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

$siteCode = explode('.', $siteCode);



if(isset($siteCode[0]) && isset($_REQUEST['username']) && isset($_REQUEST['password']) && !empty($_REQUEST['username']) && !empty($_REQUEST['password']))

{

    // database connection settings

$databaseServer = "localhost";
$databaseUser = "motoquot_2d52";
$databasePassword = "W.=Dy3OiZanw";
$databaseName = "motoquot_2d52";



    // connect to database

    $mysqli = new mysqli($databaseServer, $databaseUser, $databasePassword, $databaseName) or die("Could not make connection");

    if ($mysqli->connect_error)

    {

	    die("Unable to connect to mysqli: " . $databaseServer . "/" . $databaseUser . "/" . $databaseName . "/" . mysqli_connect_error());

	    exit();

    }

    

    // get the username and password

    $webServiceUsername = mysqli_real_escape_string($mysqli, $_REQUEST['username']);

    $webServicePassword = mysqli_real_escape_string($mysqli, $_REQUEST['password']);

    $siteCode = mysqli_real_escape_string($mysqli, $siteCode[0]);



    if ($result = $mysqli->query("SELECT Website.Id

    FROM Website WHERE Website.AffiliateCode = '" . $siteCode . "' AND Website.Live = 1

    AND (Website.StartDate > NOW() OR Website.EndDate > NOW() OR Website.EndDate IS NULL)

    AND WebServicePassword = '" . $webServicePassword . "' AND WebServiceUsername = '" . $webServiceUsername . "' LIMIT 1;"))

    {

        if($result->num_rows == 0)

        {

            $errorMsg[] = 'The website cannot be found, please check your credentials';

        }

        else

        {

	        while ($row = $result->fetch_object())

	        {

                $websiteId = $row->Id;

	        }

        }

    }

    else

    {

	    print $mysqli->error;

	    exit;

    }

    $result->close();

}

else

{

    $errorMsg[] = 'Username and password required';

}

?>