<?php
//+-----------------------------------------------------+//
//+ Copyright Floopo.com
//+ login.php
//+ Main web loggin in
//+-----------------------------------------------------+//

defined('BASEPATH') or die('You can not directly access this file');//check its within this framework

//____DEFAULT TEMPLATE_____________________________
$inner_template = TEMPLATES.'tpl_index.htm';


//____Mysql: Connect to dbase______________________
$dbase = new QuickMysql(DB_NAME, DB_USER, DB_PASS, DB_HOST);

//____Login if Submitted___________________________
if (isset($_POST['login']) && $_POST['user_name'] != '' && $_POST['password'] != '') {
    $user_name = Sanatize(mysql_real_escape_string($_POST['user_name']));
    $password = md5(Sanatize(mysql_real_escape_string($_POST['password'])));

//check if user is valid
$result = $dbase->SelectRecord("SELECT user_group, account_status FROM member_profile WHERE user_name = '$user_name' AND password = '$password'");

if(!empty($result)){
	
	    //some user details
        $user_group = $result['user_group'];
        $account_status = $result['account_status'];
        
        
        //If user is active, store sessions
        $new_salt = RandomCode(15);
        if ($account_status == 'active') {
		    $dbase->UpdateRecord("UPDATE member_profile SET salt = '$new_salt' WHERE user_name = '$user_name'");
            $_SESSION["env_user_name"] = $user_name;
            $_SESSION["env_slat"] = $new_salt;
            $_SESSION["user_group"] = $user_group;
            $env_loggedin = 1;
            $env_user_name = $_SESSION["env_user_name"];
            @header('Location: '.$env_base_url.'/'.$user_name); //redirect to home
        }
        
        //maybe this user has not yet completed registration
        if ($account_status == 'new') {
			$_SESSION["notice"] = 1;
            @header('Location: '.$env_base_url.'/notice'); //notice codes can be added in com_notice
        }

        //maybe this user is suspended
        if ($account_status == 'suspended') {
			$_SESSION["notice"] = 5;
            @header('Location: '.$env_base_url.'/notice'); //notice codes can be added in com_notice
        }

    }else{
        //The login is incorrect
		    $_SESSION["notice"] = 2;
            @header('Location: '.$env_base_url.'/notice'); //notice codes can be added in com_notice
}
}else{
        //No login details provided
		    $_SESSION["notice"] = 3;
            @header('Location: '.$env_base_url.'/notice'); //notice codes can be added in com_notice
}


//____Logout___________________________
if (isset($_GET['logout']) && $_GET['logout'] == 1) {
    @session_destroy();
     header('Location: '.$env_base_url.'/home');
}




//____Display any output_________________________
$TBS = new clsTinyButStrong;
$TBS->NoErr = true;
$TBS->LoadTemplate(TPL_WRAPPER);
$TBS->MergeField('config', $config);
$TBS->Render = TBS_OUTPUT;
$TBS->Show();
@mysql_close();

/*-----------------------------------------------------

Types
-----
1 = activation of new user





-------------------------------------------------------*/ ?>