<?PHP
//================================================================================
// PLEASE DO NOT REMOVE THIS HEADER!!!
//
// COPYRIGHT NOTICE
// This script is licensed under the GPL
// 
// Copyright 2007-2008 Alias 454 Studios and Brandon Keep (c) All rights reserved.
// Created 11/18/2007   
// Brandon Keep,    http://www.openautoclassifieds.com
//                  http://www.alias454studios.com/scripts/
//
// Last Modified 05-05-2008 by
// Brandon Keep,    http://alias454studios.com
//================================================================================
// This software IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//================================================================================

require_once './Includes/configs/functions.php';
require_once './Includes/libs/Smarty.class.php';
include_once "./Includes/language/$site_lang.php";

$smarty = new Smarty;

$smarty->assign("manufacturerslist",getSearchList("make"));
$smarty->assign("modelslist",getSearchList("model"));
$smarty->assign("typeslist",getSearchList("vehicle_type"));

if (isset($_GET["logoff"]))
	logoff();
	
//Check if single user mode 1 or multi-user mode 0
if ($site_mode == 0) {
	$smarty->assign("site_mode",1);
}
//Check if single user mode 1 or multi-user mode 0
if ($site_mode == 1) {
	$smarty->assign("single_mode",1);
}

if (isset($_SESSION["admin"])) {
    session_write_close();
	header("Location: admin/admin.php");
} elseif (isset($_SESSION["user"])) {
	session_write_close();
    header("Location: member.php");
}

$user = "";
$pass = "";

if (isset($_POST["user"])) {
	if (validateUsername($_POST["user"])) {
	
		if (verifyLogin($_POST["user"],$_POST["pass"])) {
			$_SESSION["user"] = $_POST["user"];

			//get md5 password hashes with salt and then set as session password 
			$salt = 's+(_a*';
			$md5pass = md5($_POST["pass"].$salt);
        	$_SESSION["pass"] = "$md5pass";
			lastActive($_SESSION["user"]);
			$_SESSION["logged_in"] = 1;

			//set persistant cookie for remember me function
			if (isset($_POST["remember"])) {
	   			setcookie("user", $_SESSION["user"], time() + (60*60*24*30));
	   			setcookie("pass", "$md5pass", time() + (60*60*24*30));
			}

			if (checkIfAdmin($_SESSION["user"],$_SESSION["pass"])) {
				$_SESSION["admin"] = 1;
				header("Location: admin/admin.php");
			} elseif (checkIfSeller($_SESSION["user"],$_SESSION["pass"])) {
				$_SESSION["seller"] = 1;
				header("Location: member.php"); 
			} else {
				header("Location: member.php");
			}

		} else {
			//there was an error with your login
			$smarty->assign("formremember",$_POST["remember"]);

			if ($_POST["user"] == "") {
				$smarty->assign("user_error",$_POST["user"]);
				$smarty->assign("error_message",BLANK_USERNAME);
			} else {
				$smarty->assign("error_message",LOGIN_ERROR);
			}
		}
	}
}

$smarty->assign("login",1);
$smarty->display('login.tpl'); 
?>