<?php
include ('includes/bootstrap.inc.php');
$op 	= Data::getGet ( 'op' );
$e		= Data::getGet ( 'e' );
$msg 	= '';

switch ( $op ) {
	case 'check' :
		$user_array = Connection::entity_load( 'users', array(), array (
				'email' => Data::getPost( 'username' ),
				'password' => md5( Data::getPost( 'password' ) ) 
				) );
		
		
		// load uid into $_SESSION
		if( is_array( $user_array ) && ! empty( $user_array ) ) {
			$_SESSION['uid'] 			= intval( $user_array[0]['uid'] );
			$_SESSION['rid'] 			= intval( $user_array[0]['rid'] );
			$_SESSION['session_id'] 	= session_id();
			$_SESSION['user_array']		= $user_array[0];
			
			
			
			// create (load) a user object to save 1st access and login timestamp 
			$ou = new User ( $user_array [0] ['uid'] );

			if( $ou->access == 0 ) {
				$ou->access = time();
			}
			$ou->login 		= time();
			$ou->save();
			unset( $ou );
			
			
			
			// delete all message stored into session variable
			Message::reset();
			
			// save a watchdog on database :)
			Watchdog::write( Watchdog::TYPE_USER, 'user login: ' . $_SESSION ['user_array'] ['email'], $_SESSION ['uid'], Watchdog::WATCHDOG_NOTICE );
			
			// redirect to a landing page
			header('Location: ' . _LANDING_PAGE);
		}else{
			// error in authenticating. Save a log on database and redirect to login page w/error
			Watchdog::write( Watchdog::TYPE_USER, 'user login error: ' . Data::getPost ( 'username', 'anonymous' ), 0, Watchdog::WATCHDOG_NOTICE );
			header('Location: login.php?e=noauth');
		}
		
		break;
	
	case 'logout' :
		Watchdog::write( Watchdog::TYPE_USER, 'user logout: ' . $_SESSION ['user_array'] ['email'], $_SESSION['uid'], Watchdog::WATCHDOG_NOTICE );
		
		// session destroy
		$_SESSION = array ();

		// redirect to main page
		header('Location: index.php');
		break;
	default :
		/**
		 * Creates a new template for the user's profile.
		 * Fills it with user login form.
		 */
		
		$oBS = new Bootstrap();
		
		$inputEmail = $oBS->addInput('email', 
										'username', 
										_DEFAULT_USERNAME, 
										true, 
										_('username'),
										'myname@example.com'
										);
		
		$inputPassword = $oBS->addInput('password', 
										'password', 
										_DEFAULT_PASSWORD, 
										true, 
										_('password'),
										'',
										_('password')
										);
		
		
		
		$inputSubmit = $oBS->addSubmit( 'login', _("log in") );
		
		
		/**
		 * Loads and redefine layout template, settings its title and content.
		 */
		$template = _DEFAULT_THEME_PATH . _DEFAULT_THEME . "/login.tpl";
		$layout = new Template ( $template );
		
		
		$layout->set ( "login-form-title", _("Log In") );
		$layout->set ( "input-username", $inputEmail );
		$layout->set ( "input-password", $inputPassword );
		$layout->set ( "input-submit", $inputSubmit );
		
		
		/*
		 * access denied
		 */
		if($e == 'noauth'){
			$msg = $oBS->message( _('Access denied. Wrong username or password') );
		}
}



	/**
	 * Outputs the page with the user's profile.
	 */
	$layout->set ( "application-name", _APPLICATION_NAME);
	$layout->set ( "message-box", $msg );

	echo template_common_header();
	echo $layout->output ();
	echo template_common_footer();
?>
