<?php

// NETISSE - PCZ : 2012/10/14
require_once("../inc/confNetisse.php");

$MATORG = "";
if (isset($_POST["mat"]))
    $MATORG = $_POST["mat"];
echo "<li>MAT ORIGINE =" . $MATORG . "</li>";

function checkMatricule($MAT) {
    $LENGTH = strlen($MAT) - 1;
    //echo "<li>LENGTH =".$LENGTH."</li>";
    $MATTEMP = substr($MAT, 1, $LENGTH);
    //echo "<li>MATTEMP = ".$MATTEMP;
    if (substr($MATTEMP, 0, 1) == "0") {
        $MATTEMP = substr($MATTEMP, 1, ($LENGTH - 1));
    }
    if (substr($MATTEMP, 0, 1) == "0") {
        $MATTEMP = substr($MATTEMP, 1, ($LENGTH - 1));
    }
    if (substr($MATTEMP, 0, 1) == "0") {
        $MATTEMP = substr($MATTEMP, 1, ($LENGTH - 1));
    }
    if (substr($MATTEMP, 0, 1) == "0") {
        $MATTEMP = substr($MATTEMP, 1, ($LENGTH - 1));
    }
    if (substr($MATTEMP, 0, 1) == "0") {
        $MATTEMP = substr($MATTEMP, 1, ($LENGTH - 1));
    }
    return $MATTEMP;
}

$MAT = checkMatricule($MATORG);
echo "<li>MAT CHECKED = " . $MAT;
//$MAT = "5986";

$CURLURL = 'http://10.70.131.14/netisseGetInfosFromMat.asp?mat=' . $MAT . '&key=HaQe5etat42cRaT7uMageguhuPupracr';
echo "<li>CURLURL = " . $CURLURL;
$ch = curl_init();
curl_setopt_array(
        $ch, array(
    CURLOPT_URL => $CURLURL,
    CURLOPT_RETURNTRANSFER => true
        )
);
$output = curl_exec($ch);
echo "<li>CURL RESULT = " . $output;

$AUTHURL = "http://ifp10-iucrdevapa.ca-technologies.fr:81/CRIF/auth_service/public/ws/getprofiluser/application_id/BORNE_ORIENT/matricule/" . $MATORG;
echo "<br><br><hr><br><li>HABILITATIONS = " . $AUTHURL . "</li>";

$dom = new DOMDocument();
if (!$dom->load($AUTHURL)) {
    echo "<li>Impossible de charger le fichier XML</li>";
} else {
    echo "<li>XML Ok !</li>";
}

$profilList = $dom->getElementsByTagName("id_profil");
$profilNumber != $profilList->length;
echo "<li>Il y a " . $profilList->length . " id profils : </li>";
foreach ($profilList as $profil) {
    echo "<li>" . $profil->firstChild->nodeValue . "</li>";
}
exit();


$q = "SELECT id_admin, profil, id_agence, login, email, nom, prenom FROM ca_admins WHERE login=? AND password=? AND inactif=? ";
//echo "<li>".$q;
$inactifSql = 0;
$values = array(':login' => $LOGIN, ':password' => $PASSWORD, ':inactif' => $inactifSql);
$stmt = excuteQuery($q, $values);
$result = $stmt['stmt'];
if ($stmt['status']) {
    $result = $stmt['stmt'];
    if ($result->rowCount() == 1) {
        $o = $result->fetchObject();

        // OUVERTURE DE LA SESSION
        session_start();
        // RECUPERATION DES INFORMATIONS DE L'UTILISATEUR CONNECTE
        $_SESSION['auth'] = "ok";
        $_SESSION['id'] = $MAT;
        $_SESSION['agence'] = $o->id_agence;
        $_SESSION['profil'] = $o->profil;
        $_SESSION['nom'] = $o->prenom . " " . $o->nom;
        $_SESSION['email'] = $o->email;
        // MAJ DE LA DATE DE DERNIERE CONNEXION
        $q = "UPDATE ca_admins SET connexion=:connexion WHERE id_admin=:id_admin";
        $v = array(':connexion' => date('Y-m-d H:i:s'), ':id_admin' => intval($MAT));
        $r = excuteQuery($q, $v);
        // AUTH OK : REDIRECTION VERS LE MENU
        header("location:dashboard.php");
    } else {
        // AUTH KO : REDIRECTION VERS LE FORMULAIRE D'ACCUEIL
        header("location:index.php?e=1");
    }
} else {
    wts_die($stmt->error);
}
?>