<?php
/**		
	* This file is the entry point of phpAduc. 		
	*   	
	* This file switch to appropriate module 	
	*   	
	* PHP version > 7.3 	
	*   	
	* LICENSE: This source file is subject to version 3.01 of the PHP license   	
	* that is available through the world-wide-web at the following URI: 		
	* http://www.php.net/license/3_01.txt.  If you did not receive a copy of  		
	* the PHP License and are unable to obtain it through the web, please  		
	* send a note to license@php.net so we can mail you a copy immediately. 	
	*  		
	* @category  Main  		
	* @package   phpAduc  	
	* @author    Serge NOEL <serge.noel@easylinux.fr>  	
	* @copyright 2016-2020 Easylinux  	
	* @license   http://opensource.org/licenses/gpl-license.php GNU Public License  	
	* @version   GIT: 2.0  	
	* @link      ../tests/Documentation Tests/Documentation.odt  	
	*/
// load composer's classes
require_once 'vendor/autoload.php';
// Site's configuration
require_once 'src/config/config.php';
// load local classes
require_once 'src/class/autoload.php';

/* Smarty initialization */
$smarty = new Smarty();
// Define template's forlder
$smarty->setTemplateDir('src/templates');
$smarty->setConfigDir('src/templates/configs');
// Define compiled templates folder target
$smarty->setCompileDir('templates_c');
$smarty->setCacheDir('src/templates_c/cache');

$smarty->assign('Title',"easyCloud");
echo $smarty->display("main.smarty");


die();


















// read task wanted
$Action=filter_input(INPUT_POST,'Action',FILTER_SANITIZE_STRING);

// Read token (in aduc's cookie) 
if( isset($_COOKIE['aduc']) )
{ // got one
  if( $Action == ""){ // No Action specified -> display main page
    $Action="main";
  }
  // Compare actual time with TimeOut in redis values -> know if token is still valid
  $redis = new Redis();
  $redis->connect($Cfg['redishost'], 6379);
  // Read variables designed by the token
  $aUser = json_decode($redis->get($_COOKIE['aduc']));
  if( time() > strtotime($aUser->TimeOut))
  {
    // Token is not valid anymore, force login
    $redis->unlink($_COOKIE['aduc']);
    setcookie("aduc","",time() - 3600);
    $Action = "Login";
  }

} else {
  // No token available
  if( $Action != "tryLogin" ){
    // Action is not tryLogin, we don't have a token -> force login
    $Action='Login';
  }
}



switch($Action)
{
	case 'tryLogin':
		$Usr=new userClass();
		$sLogin=filter_input(INPUT_POST,'sLogin',FILTER_SANITIZE_STRING);
		$sPasswd=filter_input(INPUT_POST,'sPassword',FILTER_SANITIZE_STRING);
		if($Usr->isValid($Cfg, $sLogin,$sPasswd))
		{ // User type good credentials

      // Get a new token
      $TOKEN = uniqid("aduc_",true);
      // put it in a cookie
      setcookie("aduc",$TOKEN,time()+$Cfg['delay']);
      // Save session on redis server
      $User = ["Login" => $sLogin, "Password" => $sPasswd, "Start" => date('Y-m-d H:i:s',time()), "TimeOut" => date('Y-m-d H:i:s',time()+3600)];
      $redis = new Redis();
      $redis->connect($Cfg['redishost'], 6379);
      $redis->set($TOKEN,json_encode($User));
      // load main page
      $template='main.smarty';
			$component='main';    
		}
		else
		{  // login fail -> display login page
			$template='login.smarty';
			$component="";
			$smarty->assign('error','Compte ou mot de passe invalide !');
			$smarty->assign('Title',"easyCloud");
		}
		break;

	case 'Logout':
    $redis = new Redis();
    $redis->connect($Cfg['redishost'], 6379);
    $redis->unlink($_COOKIE['aduc']);
    setcookie("aduc","",time() - 3600);
    error_log("unset cookie");

	case 'Login':
		$template='login.smarty';
		$component="";
		break;

	default:
		$component=strtolower($Action);
		$template="$component.smarty";
		break;
}

if($component!="")
{
	require_once("src/components/$component.php");
}

$smarty->assign('Title',"easyCloud");
echo $smarty->display($template);