<?php
/*
 **************************************************************************************************
 *    Copyright (c) 2010-2012 Just Another Video script
 *
 * 	  This file is part of Just Another Video Script.
 *    Version 1.4.3
 *    info@justanothervideoscript.com
 **************************************************************************************************
 *    Come to website for support or buying additional plugins/modules.
 *    http://justanothervideoscript.com/
 **************************************************************************************************
 *    By using this software you agree that you have read and acknowledged our End-User License
 *    Agreement available at http://justanothervideoscript.com/license and can be bound by it.
 **********************************************************************************
 */
session_start() ;
require_once '../includes/loginfunctions.inc' ;
$db = db_connect() ;
$date = gmdate("'Y-m-d'") ;
$user = new User($db) ;

if (isset($_GET['logout'])) {
	if (quote_smart($_GET['logout']) == 1) {
		$user->_logout() ;
		leavePage(3) ;
	}
}
$remember = true ;
if (isset($_POST['remember'])) {
	$remember = quote_smart($_POST['remember']) ;
}
$isLogged = -1 ;
$isLogged = $user->_checkLogin(quote_smart($_POST['user']), quote_smart($_POST['pass']), $remember) ;
leavePage($isLogged) ;

function leavePage($logged) {
	global $db ;
	$sitefolder = getSetting("sitefolder", $db) ;
	$sitepath = "http://".$_SERVER['SERVER_NAME'].$sitefolder ;
	$path = "http://".$_SERVER['SERVER_NAME'] ;
	switch ($logged) {
		case '0':
			if (getSetting("accountpageon", $db) == 1) {
				header('Location: '.$sitepath.'account') ;
			} else {
				$referrer = $_SESSION['session_url'] ;
				$referURL = !strpos($referrer, $sitepath) ? $referrer : $sitepath ;
				header('Location: '.$path.$referURL) ;
			}
		break ;
		case '1':
				$_SESSION['session_loginerr'] = "Wrong User name and/or password." ;
		break ;
		case '2':
				$_SESSION['session_loginerr'] = "You have not activated your account yet, please check your e-mail and activate your account." ;
		break ;
		case '3':
				header('Location: '.$sitepath) ;
		break ;
	}
}
?>
