<?php
include("../includes/inc-config.php");
include(CHEMIN."includes/inc-header.php");
include(CHEMIN."includes/Spyc.php");
include(CHEMIN."includes/yaml-readwriteclass.php");
include(CHEMIN."includes/inc-fonctions.php");

//print_r($GLOBALS);
/* Starts the session */
/* Check Login form submitted */
if(isset($_POST['entrer'])){
    // charger les accès
    $users=new yamlreadwrite(); 
    $source=CHEMIN.DOSSIER_CONTENUS."/config_access.yaml";
    $users->yamlread($source);
    //$users->yamlfrompost(true);
    $infos=$users->yaml_simple_array();

    /* Check and assign submitted Username and Password to new variable */
    $Username = isset($_POST['Username']) ? $_POST['Username'] : '';
    $Password = isset($_POST['Password']) ? crypt(urlencode($_POST['Password']), 'rl') : '';

    /* Check Username and Password existence in defined array */
    $existe=false;
    foreach($infos as $cle => $valeur){
        $access=explode("|§|",$valeur);
        if ($access[0]==$Username && $access[1]==$Password){
            $_SESSION['UserData']['Userid']=$cle;
            $_SESSION['UserData']['Username']=$Username;
            $_SESSION['UserData']['Userstatus']=$users->contenu[$cle]['niveau'];
            $_SESSION['UserData']['Userdisplayname']=$users->contenu[$cle]['titre'];
            header("location:index.php");
            exit;
        }
    }
    $msg="Problème avec ce login et/ou ce mot de passe!";
}
?>

<main id="main" class="container-fluid px-4 py-5">
    <?php if(isset($msg)){?>
    <div class="col-12 col-md-6">
        <div class="alert alert-danger mb-4" role="alert">
            <?= $msg; ?>
        </div>
    </div>
    <?php } ?>
    <h2 class="titre-small my-4 text-primary">Accéder à l'admin</h2>
    <form action="" method="post" name="Login_Form" class="row">
        <div class="col-12 col-md-6">
            <div class="mb-3 row">
                <label for="staticEmail" class="col-sm-2 col-form-label">Login</label>
                <div class="col-sm-10">
                    <input type="text" name="Username" class="form-control" value="" placeholder="login">
                </div>
            </div>
            <div class="mb-5 row">
                <label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
                <div class="col-sm-10">
                    <input type="password" name="Password" class="form-control"  placeholder="password">
                </div>
            </div>

            <div class="mb-3">
                <button type="submit" class="btn btn-primary mb-3" name="entrer">Envoyer</button>
            </div>
        </div>
    </form>
</main>

<?php 
include("../includes/inc-footer.php");
?>
