<?php
// $Id: login_utils.php,v 1.1 2008-04-02 14:56:54 fgiva Exp $
/**
 * Fichiers requis
 */
    if (file_exists ("../dyn/session.inc"))
        include ("../dyn/session.inc");
    if (file_exists ("../obj/utils.class.php"))
        include ("../obj/utils.class.php");
    if (file_exists ("../dyn/custom/var.inc"))
        include("../dyn/custom/var.inc");
    else
        include("../dyn/var.inc");

    //$error ="";
    $profil="";
    if (isset ($_GET['coll'])){
       if (!isset ($_SESSION['coll'])){
          $_SESSION['coll']=$_GET['coll'];
    }}
    $ville="";
    if (isset ($_GET['ville']))
       $ville=$_GET['ville'];
    if (!isset($_GET['step'])){
       $step=0;
    }else{
       $step=$_GET['step'];
    }
    if ( !isset($_GET['dec']) )
       $dec=0 ;
    else
       $dec= $_GET['dec'];
    //
    $lib = "";
    $nom = "";
    $login = "";
    $obj = "login";
    $msg="";
    $DEBUG=0;

/**
 * Classe utils ()
 */
    $f = new utils ();
    $f -> headerhtml ();
    $f -> collectivite ();

    if ( $step==1 ){
       $pwd = md5($_POST['pwd']);
       //$pwd = ($_POST['pwd']);
      // traitement contre les injections sql #
       $post_login= $_POST['login'];
       $post_login= str_replace('#','',$post_login);
         if($DEBUG==1)
            echo "La base ".$dsn['database']." est connectée.<br>";
         //verification de la validite du login
         $sql =  "SELECT * "."from utilisateur "."WHERE login like '".$post_login."' ";
         $res = $f -> db -> query ($sql);
         if (DB :: isError($res))
            die($res->getMessage());
         else
         {
            //si on a un utilisateur qui correspond dans la base on recup les infos le concernant
            while ($row=& $res->fetchRow())
            {
               if($row[3]==$pwd){ // injection sql
                   $profil = $row[4];
                   $nom = $row[1];
                   $login=$row[2];
//                   $service=$row[5];
//                   // service
//                   $libservice="";
//                   $sql ="select servicelib from service where service ='".$service."'" ;
//                   $libservice=$db->getOne($sql);
                   $error= 'ok';
               } else
                   $error = "********** mot de passe incorrect, recommencer ...";
            }

            //
            if ($error != 'ok' )
            {
              if($error=='')
                  $error= "********** Login incorrect, recommencez ...";
              echo "<div id='msglogin'>";
              echo "<fieldset class=cadremsg>".$error."</fieldset>";
              echo "<div>";
              $step=0;
            } else {
            //si lutilisateur existe bien on initialise les variables de session qui le concerne et affiche le message de bienvenue une fois connecte
            //ucwords met en maj la premiere lettre de tous les mots de la chaine passee en parametre
              $_SESSION['profil'] = $profil;
              $_SESSION['nom'] = $nom;
              $_SESSION['login'] = $login;
//              $_SESSION['service'] = $service;
//              $_SESSION['libservice'] = $libservice;
              $step=1;
              $msg .= " Bienvenue ".ucwords($nom)." (Profil : ".$profil.")<br>" ;
              $msg .= " Vous travaillez sur la collectivité : ".ucwords (strtolower($f -> collectivite ['ville'])).".<br><br>";
            }
         }
         $res -> free ();
    }
    if ($profil > 0)
    {
        $f -> header (1, "Tableau de bord");
        echo "\n<div id=\"msgloginok\">\n";
    }
    else
    {
        echo "\n<div id=\"content_login\">\n";
    }
    if ($step == 1) {
       echo $msg;
    }
    //*****
    if ($profil > 0)
    {
      if (file_exists ("../dyn/tdb.inc")){
          include ("../dyn/tdb.inc");
      }
    }
    //*****
    /* Identification */
    if ($step==0)
    {
        if ($msg != "")
        {
            echo "<div class=\"erreur_login\">";
            echo $msg;
            echo "</div>";
        }
         if ( $dec==1 ) {
          $_SESSION['profil'] = '';
          $_SESSION['login'] = '';
        }
        $step=1;
       echo "<div id='logo'>";
       // * custom *
       if(file_exists("../dyn/custom/img/logo.png")) {
          echo "<img src='../dyn/custom/img/logo.png'><br><br>";
       }
       else {
          echo "<img src='".$logo."'><br><br>";
       }
       //*
       echo "</div>";
       echo "<div>";
       echo "<FORM action='login.php?step=".$step."'  method=POST  name=f1 >";
       echo "<table class='login' border='0' cellpadding='5'  cellspacing='5'>";
       echo "<tr>";
       echo "<tr><td colspan=\"3\"><b>I D E N T I F I E Z - V O U S </b><br />Accès à ".ucwords (strtolower($f -> collectivite ['ville']))."</td></tr>";
       // mode demo
       if($demo==1){
            echo "<tr><td>LOGIN demo: </td><td><input type=\"text\" name=\"login\" value=\"demo\" size=\"20\" class=\"champFormulaire\" /></td><td>&nbsp;</td></tr>";
            echo "<tr>";
            echo "<td>MOT DE PASSE : </td><td><input type=\"password\" name=\"pwd\" value=\"demo\" size=\"25\" class=\"champFormulaire\" /></td>";
       }else{
            echo "<tr>";
            echo "<td>LOGIN : </td>";
            echo "<td><INPUT type='text' name='login' value=''  size=25 class='champFormulaire'></td><td> </td></tr>";
            echo "<tr><td>MOT DE PASSE :</td>";
            echo "<td><INPUT type='password' name='pwd'  size=25 class='champFormulaire' ></td><td>";
       }
       echo "<input type='submit' value='Valider' style='".$styleBouton."'  border='0'></td></tr>";
       echo "<tr><td colspan='3'><a class='lien' href='../index.php'>";
       // * custom *
       if(file_exists("../dyn/custom/img/ico_collectivite.png")) {
          echo "<img src='../dyn/custom/img/ico_collectivite.png' align='middle' hspace='5' border='0'>";
       }
       else {
         echo "<img src='../img/ico_collectivite.png' align='middle' hspace='5' border='0'>";
       }
       //*
       echo "Choix  Autre Base</a><br><br></td>";
       echo "</tr>";
       echo "</tr>";
       echo "</table>";
       echo "</form>";
    }

    echo "</div>";
/**
 *
 */
    if ($profil > 0)
//    $f -> footer ();
    $f -> deconnexion (); 
    $f -> footerhtml ();
?>