<?php
// $Id: login.php,v 1.7 2008-04-02 23:17:37 fgiva Exp $
include("../dyn/session.inc");
// * custom *
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'];
}}

$ville1="";
if (isset ($_GET['ville']))
  if ($ville1==""){
    $ville1=$_GET['ville'];
}
// classe(s) utilisée(s)
//include_once "DB.php";
require_once "DB.php";   
include ("../dyn/connexion.php");
$DEBUG=0;
$db=& DB :: connect($dsn, $db_option);
if (DB :: isError($db)) {
    die($db->getMessage());
}else{
    if($DEBUG==1)
    echo "La base ".$dsn['database']." est connectée.<br>";
    // variables collectivites
    $res = $db->query($sql_collectivite);
    if (DB :: isError($res))
       die($res->getMessage()."erreur ".$sql_collectivite);
    else{
        $k=0;
        while ($row=& $res->fetchRow()){
              $ville=$row[0];
              $logo=$row[1];
    }}
    $res->free();
     // * custom *
    include("../dyn/cg.css");
    if(file_exists("../dyn/custom/cg.css")) {
        include("../dyn/custom/cg.css");
    }
    //*
    echo "<html>";
    // * custom *
    if(file_exists("../dyn/custom/lien.inc")) {
       include("../dyn/custom/lien.inc");
    }
    else {
      include("../dyn/lien.inc");
    }
    //*
    echo"<head>";
    include("../dyn/entete.inc");
    echo "</head>";
    //
    echo "<body  class='login'>";
    echo "<div id='entetelogin'>";
    echo "<table>";
    echo "<tr><td>";
    if (!isset($_GET['step'])){
       $step=0;
    }else{
       $step=$_GET['step'];
    }
    if ( !isset($_GET['dec']) )
       $dec=0 ;
    else
       $dec= $_GET['dec'];
    //
    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 = $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;
            }
            //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
            else 
            {
              $_SESSION['profil'] = $profil;
              $_SESSION['nom'] = $nom;
              $_SESSION['login'] = $login;
//              $_SESSION['service'] = $service;
//              $_SESSION['libservice'] = $libservice;
              $step=1;
              echo "<div id='msgloginok'>";
              echo "Bienvenue ".ucwords($nom)." (Profil : ".$profil.")<br>" ;
              echo "Vous travaillez sur la collectivit&eacute; ".ucwords($ville); //." Service : ".$service." ".$libservice;
              echo "<div>";
              $step=2;
            }
            $res->free();
         }
    }
    echo "</td>" ;
    echo "</table>";
    echo "</div>";

    /* Identification */
    if ($step==0)
    {
         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 "<td colspan='3'><b>I D E N T I F I E Z - V O U S </b><br>Accès à ".$ville."</td></tr>";
       echo "<tr>";
       echo "<td>LOGIN : </td>";
 //      echo "<td><INPUT type='text' name='login' value='demo'  size=25 class='champFormulaire'></td><td> </td></tr>";
       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' value='demo' size=25 class='champFormulaire' ></td><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){
      include ("../dyn/menu.inc");
      $recherche="";

           include ("../scr/recherche.php");

    }
    $db->disconnect();
    if ($DEBUG == 1)
        echo "La base ".$dsn['database']." est déconnectée.<br>";
}
echo "</body></html>";
?>