<?php

namespace FOPRouter;

class Login {
  
  

  
  

    // --- Statische Methoden ---
  
  public static function logoutURL($params = [] ) { 
    
    
    // --- BLOCK: main ---
    
		
		return self::logoutURL2($params);
	
    
  }

  public static function loginFormURL($login_error = "" , $params = [] ) { 
    
    
    // --- BLOCK: main ---
    
		$params['login_error'] = $login_error;
		return self::loginFormURL2($params);
	
    
  }

  public static function loginURL($params = [] ) { 
    
    
    // --- BLOCK: main ---
    
		
		return self::loginURL2($params);
	
    
  }

  public static function logoutURL2($params) { 
    
    
    // --- BLOCK: main ---
    
		// Default-URL - kann durch rewrite-Regeln abgeändert werden
		
		if (!isset($url)) {
			$url = 'FOP/Login/logout';
			$isFirst = true;
		}
		foreach ($params AS $k => $v) {
			if (!is_null($v)) {
				$url .= ($isFirst ? '?' : '&') . urlencode($k) . '=' . \FOP\Util::urlencode($v);
				$isFirst = false;
			}
		}
		if (\FOP\Configuration::getFOPUtilEnableRewrite() == 'true' || \FOP\Configuration::getFOPUtilEnableRewrite() == 1) {
			return \FOP\Util::getCurrentBaseurl() . $url;
		} else {
			return \FOP\Util::getCurrentBaseurl() . 'fop-router.php?q=' . urlencode($url);
		}
	
    
  }

  public static function loginFormURL2($params) { 
    
    
    // --- BLOCK: main ---
    
		// Default-URL - kann durch rewrite-Regeln abgeändert werden
		
		if (!isset($url)) {
			$url = 'FOP/Login/loginForm';
			$isFirst = true;
		}
		foreach ($params AS $k => $v) {
			if (!is_null($v)) {
				$url .= ($isFirst ? '?' : '&') . urlencode($k) . '=' . \FOP\Util::urlencode($v);
				$isFirst = false;
			}
		}
		if (\FOP\Configuration::getFOPUtilEnableRewrite() == 'true' || \FOP\Configuration::getFOPUtilEnableRewrite() == 1) {
			return \FOP\Util::getCurrentBaseurl() . $url;
		} else {
			return \FOP\Util::getCurrentBaseurl() . 'fop-router.php?q=' . urlencode($url);
		}
	
    
  }

  public static function route($q) { 
    
    
    // --- BLOCK: main ---
    
	if (($q == 'logout')) {
		try {
			$result = \FOP\Login::logoutAction();
		} catch(\FOP\AccessDeniedException $e){
			\FOP\Login::render($e->getMessage() ?: 'accessdenied');
			die();
		}
		if (isset($result)) {
			header('Content-Type: application/json'); 
			die(json_encode($result));
		} else {
			die('');
		}
	}if (($q == 'login')) {
		try {
			$result = \FOP\Login::loginAction();
		} catch(\FOP\AccessDeniedException $e){
			\FOP\Login::render($e->getMessage() ?: 'accessdenied');
			die();
		}
		if (isset($result)) {
			header('Content-Type: application/json'); 
			die(json_encode($result));
		} else {
			die('');
		}
	}if (($q == 'loginForm') ) {
		try {
			$result = \FOP\Login::loginFormAction(isset($_GET['login_error']) ? $_GET['login_error'] : "");
		} catch(\FOP\AccessDeniedException $e){
			\FOP\Login::render($e->getMessage() ?: 'accessdenied');
			die();
		}
		if (isset($result)) {
			header('Content-Type: application/json'); 
			die(json_encode($result));
		} else {
			die('');
		}
	}

	header('HTTP/1.0 404 Not found');
	\FOP\Login::render('Die angeforderte Funktion wurde nicht gefunden.');
	die();
    
  }

  public static function loginURL2($params) { 
    
    
    // --- BLOCK: main ---
    
		// Default-URL - kann durch rewrite-Regeln abgeändert werden
		
		if (!isset($url)) {
			$url = 'FOP/Login/login';
			$isFirst = true;
		}
		foreach ($params AS $k => $v) {
			if (!is_null($v)) {
				$url .= ($isFirst ? '?' : '&') . urlencode($k) . '=' . \FOP\Util::urlencode($v);
				$isFirst = false;
			}
		}
		if (\FOP\Configuration::getFOPUtilEnableRewrite() == 'true' || \FOP\Configuration::getFOPUtilEnableRewrite() == 1) {
			return \FOP\Util::getCurrentBaseurl() . $url;
		} else {
			return \FOP\Util::getCurrentBaseurl() . 'fop-router.php?q=' . urlencode($url);
		}
	
    
  }

  public static function logoutAccess() { 
    
    
    // --- BLOCK: access ---
    if(!(((\FOP\User::getIsLoggedIn())))) return false;
    // --- BLOCK: main ---
    return true;
    
  }

  public static function loginFormAccess($login_error = "" ) { 
    
    
    // --- BLOCK: access ---
    if(!(((\FOP\User::getIsGuest())))) return false;
    // --- BLOCK: main ---
    return true;
    
  }

  public static function loginAccess() { 
    
    
    // --- BLOCK: access ---
    if(!(((\FOP\User::getIsGuest())))) return false;
    // --- BLOCK: main ---
    return true;
    
  }


  
  

  
  
}



