<?php

	//Security
	if( ! defined( 'IS_INIT' ) )
	{
		die( 'Unauthorized Access');
	}
	
	global $Handler;
	
	if( isset( $_GET['logout'] ) && isset( $_SESSION['UserID'] ) )
	{
            $Handler->WriteLog( 'Log Out' , 'Success' , $_SESSION['UserID'] , null );
            $_SESSION['LoggedIn'] = false;
            $_SESSION['AccountLevel'] = GUEST;
            unset( $_SESSION['UserID'] );
            unset( $_SESSION['PassWord'] );
            header( 'Location:' . MAIN_URL );
	}
	
	if( LOGGED_IN == false && ACC_LEVEL == GUEST )
	{		
		if( isset( $_POST['username'] ) && isset( $_POST['password'] ) )
		{
			if( strlen( $_POST['username'] ) > 2 && strlen( $_POST['username'] ) < 15 )
			{
				if( strlen( $_POST['password'] ) > 4 && strlen( $_POST['password'] ) < 20 )
				{
					$ret = $Handler->user->Authenticate( $_POST['username'] , $_POST['password'] );
					if( $ret == 1 )
					{
						$_SESSION['LoggedIn'] = true;
						$_SESSION['UserID'] = $_POST['username'];
						$_SESSION['PassWord'] = md5( $_POST['password'] . SALT );
						$_SESSION['AccountLevel'] = USER;
						$Handler->database->free();
                                                $Handler->WriteLog( 'Login' , 'Success' , $_SESSION['UserID'] , null );
						header( 'Location:' . MAIN_URL );
					}
					elseif( $ret == 2 ) 
					{
                                            $Handler->WriteLog( 'Login' , 'Failed' , $_POST['username'] , 'Banned' );
                                            $Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_BANNED' ) );
                                            $Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
                                            $Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=login" );
                                            $Handler->theme->Parse( 'error.html' );
					}
					else
					{
                                            $Handler->WriteLog( 'Login' , 'Failed' , $_POST['username'] , $_POST['password'] );
                                            $Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_BAD_INFO' ) );
                                            $Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
                                            $Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=login" );
                                            $Handler->theme->Parse( 'error.html' );			
					}
				}
				else
				{
					$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_PASS_LENGTH' ) );
					$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
					$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=login" );
					$Handler->theme->Parse( 'error.html' );					
				}
			} 
			else
			{
				$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_USER_LENGTH' ) );
				$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
				$Handler->theme->SetValue( 'RED_URL' , MAIN_URL . "?view=login" );
				$Handler->theme->Parse( 'error.html' );	
			}
		} 
		else
		{
			$Handler->theme->parse( 'login.html' );
		}
	}
	else
	{
		$Handler->theme->SetValue( 'ERROR' , $Handler->theme->GetValue( 'ERROR_LOGGED_IN' ) );
		$Handler->theme->SetValue( 'GO_BACK' , $Handler->theme->GetValue( 'GO_BACK' ) );
		$Handler->theme->SetValue( 'RED_URL' , MAIN_URL );
		$Handler->theme->Parse( 'error.html' );
	}

?>