<?php

class utilisateur
{
  function list_utilisateur()
  {
    $data = array();

    $q = "SELECT * FROM utilisateur ORDER BY user_nom ASC, user_prenom ASC";
    $r = mysql_query($q);

    if ($r)
    {
      if ($t = mysql_num_rows($r))
      {
        while ($n = mysql_fetch_assoc($r))
          $data[] = $n;
      }
    }
    else
      die("Erreur mecanique " . mysql_errno() . " : " . mysql_error());

    return $data;
  }

  function get_utilisateur($id_user)
  {
    $q = "SELECT * FROM utilisateur WHERE id_user = '" . $id_user . "'";
    $r = mysql_query($q);

    if ($r)
    {
      $n = mysql_fetch_assoc($r);
      return $n;
    }
    else
      die("Erreur mecanique " . mysql_errno() . " : " . mysql_error());

    return false;
  }

  function edit_utilisateur($id_user, $id_zone, $user_nom, $user_prenom, $user_coords, $user_tel1, $user_tel2, $user_login, $user_pass, $user_cat)
  {
		if (empty($id_user) && empty($user_pass)) // Generation aleatoire du mot de passe
		{
			$chars = "aaabcdeeefghiiiklnoooprtuuuvwxyz23456789";
			for ($i = 0; $i < 7; $i++)
				$user_pass .= substr($chars, rand() % strlen($chars), 1);
		}

    if ($id_user)
      $q = "UPDATE utilisateur SET id_zone = '" . $id_zone . "' , user_nom = '" . sqldata(strtoupper($user_nom)) . "' , user_prenom = '" . sqldata(ucwords(strtolower($user_prenom))) . "' , user_coords = '" . sqldata($user_coords) . "' , user_tel1 = '" . sqldata($user_tel1) . "' , user_tel2 = '" . sqldata($user_tel2) . "' , user_login = '" . sqldata($user_login) . "'" . (!empty($user_pass) ? " , user_pass = '" . md5($user_pass) . "'" : "") . ", user_cat = '" . $user_cat . "' WHERE id_user = '" . $id_user . "'";
    else
      $q = "INSERT INTO utilisateur (id_zone, user_nom, user_prenom, user_coords, user_tel1, user_tel2, user_login" . (!empty($user_pass) ? ", user_pass" : "") . ", user_cat) VALUES ('" . $id_zone . "' , '" . sqldata(strtoupper($user_nom)) . "' , '" . sqldata(ucwords(strtolower($user_prenom))) . "' , '" . sqldata($user_coords) . "' , '" . sqldata($user_tel1) . "' , '" . sqldata($user_tel2) . "' , '" . sqldata($user_login) . "'" . (!empty($user_pass) ? ", '" . md5($user_pass) . "'" : "") . ", '" . $user_cat . "')";
    $r = mysql_query($q);

    if ($r)
      return true;
    else
      die("Erreur mecanique " . mysql_errno() . " : " . mysql_error());

    return false;
  }

	function edit_password($id_user, $user_pass)
	{
		settype($id_user, "integer");
		$q = "UPDATE utilisateur SET user_pass = '" . md5($user_pass) . "' WHERE id_user = " . $id_user;
    $r = mysql_query($q);

    if ($r)
			return true;
    else
      die("Erreur mecanique " . mysql_errno() . " : " . mysql_error());
	}

  function del_utilisateur($id_user)
  {
    $q = "DELETE FROM utilisateur WHERE id_user = '" . $id_user . "'";
    $r = mysql_query($q);

    if ($r)
    {
      $q = "OPTIMIZE TABLE utilisateur";
      $r = mysql_query($q);

      return true;
    }
    else
      die("Erreur mecanique " . mysql_errno() . " : " . mysql_error());

    return false;
  }

	function check_login($username, $password)
	{
		if (defined("ADMIN_ACCOUNT") && ADMIN_ACCOUNT === true && $username == "administrateur" && md5($password) == "<inscrire ici le mot de passe chiffre MD5>")
		{
			$q = "SELECT * FROM utilisateur WHERE id_user = '" . ADMIN_USER . "'";
			$infoAdmin = queryOne($q);
			$_SESSION["Sid_user"] = $infoAdmin["id_user"];
			$_SESSION["Suser_nom"] = $infoAdmin["user_nom"];
			$_SESSION["Suser_prenom"] = $infoAdmin["user_prenom"];
			$_SESSION["Suser_login"] = $infoAdmin["user_login"];
			$_SESSION["Suser_cat"] = $infoAdmin["user_cat"];
			$_SESSION["Sadmin"] = 1;
			return true;
		}

		$q = "SELECT * FROM utilisateur WHERE user_login = '" . sqldata($username) . "' && user_pass = '" . md5($password) . "'";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				$n = mysql_fetch_assoc($r);
				$_SESSION["Sid_user"] = $n["id_user"];
				$_SESSION["Suser_nom"] = $n["user_nom"];
				$_SESSION["Suser_prenom"] = $n["user_prenom"];
				$_SESSION["Suser_login"] = $n["user_login"];
				$_SESSION["Suser_cat"] = $n["user_cat"];
				if ($_SESSION["Sid_user"] == ADMIN_USER)
					$_SESSION["Sadmin"] = 1;
				return true;
			}
		}
		else
		  die("Erreur mecanique " . mysql_errno() . " : " . mysql_error());

		return false;
	}
}

?>