<?php 

require realpath( __DIR__ . '/../' ) . '/connections/config.php';

define( 'PANEL_DIR', __DIR__ );
define( '_SDIR', __DIR__ . '/server' );


## get current user;
$uid = $_SESSION['uid'] ?? null;
$__user = help::get_user($_SESSION['token'] ?? null, 'token', "AND id = '{$uid}'");


if( !empty(PERMISSION_TYPE) ) {
	
	## Force Login;
	
	if( !$__user ):
	
		header("location: " . help::url( PANEL_DIR ));
		exit;
	
	else:

		$location = null;
		$user_url = help::url( PANEL_DIR );
		
		## Test for deactivation
		if( empty($__user['status']) ):
		
			header("location: {$user_url}/error.php");
			exit;
			
		else:
		
			if( $__user['pin'] != ($_SESSION['pin'] ?? null) ) $location = "pin.php";
					
			elseif( !empty($settings->get('email_otp')) && $__user['otp_hash'] !== ($_SESSION['otp_hash'] ?? null)) $location = "otp_form.php";
			
			if( $location && !in_array(PERMISSION_TYPE, ['LOGIN_SECURITY', NULL]) ):
				header("location: {$user_url}/{$location}");
				exit;
			endif;
			
		endif;
		
	endif;

};

require __DIR__ . '/--functions.php';

