<?php
$page = "index";
include "script.php";




//CAS
require_once("../phpCAS-1.3.6/CAS.php");


// Initialize phpCAS
phpCAS::client(CAS_VERSION_2_0, "cas.insa-toulouse.fr", 443, 'cas', true);

// For production use set the CA certificate that is the issuer of the cert
// on the CAS server and uncomment the line below
//phpCAS::setCasServerCACert($cas_server_ca_cert_path);
phpCAS::setNoCasServerValidation();

// force CAS authentication
phpCAS::forceAuthentication();

// at this step, the user has been authenticated by the CAS server
// and the user's login name can be read with phpCAS::getUser().
$pseudo = phpCAS::getUser();

if(!isset($_SESSION['id'])) {
    $req = $db->prepare("SELECT id FROM admin WHERE pseudo = ?");
    $req->execute(array($pseudo));
    $user_exist = $req->rowcount();
    if ($user_exist == 1) {
            while ($user_login = $req->fetch()) {
        	$_SESSION['id'] = $user_login['id'];
        	header('Refresh:0');
        }
    } else {
        $error = "Identifiant invalide !";
    }
}

if(isset($_POST['login'])) {
    session_start();
    $_SESSION = array();
    session_destroy();
    header('Refresh:0');
}


?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Admin / index</title>
</head>
<body>
    <main>
    <?php
    if(isset($_SESSION['id'])) {
        echo "Connecté sous l'identifiant : ".$user['pseudo'];
        echo "<br><br>Accès : ";
        switch ($user['perm']) {
        case 0:
            echo "<font color='green'>GDA</font>";
            break;
        case 1:
            echo "<font color='#3498DB'>ville</font>";
            break;
        case 2:
            echo "<font color='orange'>bureau</font>";
            break;
        case 3:
            echo "<font color='red'>Admin</font>";
            break;
        }
        echo "<br> Si l'onglet que vous recherchez n'est pas disponible, demandez avec amour et volupté une élévation d'acces à un respo web.";
    
    } else {
    ?>
	<form method="POST">
		<input type="submit" name="login" value="Se connecter" class="submit">
	</form>
	<?php
    }
	if(isset($error)) {
		echo "<font color='red'>".$error."</font>";
	}
	?>


    </main>
</body>
</html>