<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Copyright (C) 2006  Sylvain FATOME

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; 
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr
****************************************************************************************/

session_start(); 
?>
<?php if ((@$HTTP_SESSION_VARS["etat_log"] <> "connecte") or ((@$HTTP_SESSION_VARS["niv_log"] <> 9) AND (@$HTTP_SESSION_VARS["niv_log"] <> 5))) header("Location: ../index.php") ?>
<?php include ("../inc/config.inc.php") ?>
<?php include ("../inc/func.inc.php") ?>
<?php include ("../inc/func_date.inc.php") ?>
<?php include ("../inc/func_log.inc.php") ?>
<?php include ("../inc/fic.inc.php") ?>
<?php require_once ("../classes/xpm/smtp.php"); ?>
<?php include ("../inc/mail.inc.php") ?>
<?PHP
	Archivelog(@$HTTP_SESSION_VARS["code_log"]);
?>
<?php
	 	// Variables Formulaire
		$categorie = $_GET["cat"];
		
		$titre = strip_tags(trim(@$_POST["titre"]));

		$code = strip_tags(trim(@$_POST["code"]));

		$log_ip = $_SERVER['REMOTE_ADDR']; // Récupération adresse IP

		$retourmnu = FALSE;

		$nouvmdp = cremdp(FALSE); // Création d'un nouveau de passe (envoyé)
		$nouvmdpcrypte = md5($nouvmdp); // Cryptage du nouveau mot de passe (stocké)

		if ($categorie == "siege")
		{
			if (@$_POST["init"] == "Réinitialiser")
			{
				// MAJ de la fiche centre de gestion
			
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rech = "SELECT * FROM " .TBCG;
				$rs = mysql_query($rech) or die(mysql_error());
				if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
				{
					if ($row = mysql_fetch_array($rs)) 
					{
						extract($row);
						$upd = "UPDATE `" .TBCG. "` SET `cge_pass`='".$nouvmdpcrypte."'  WHERE `cge_id` = '".$cge_id."'";
						$rs2 = mysql_query($upd) or die(mysql_error());
						$msg = "Mot de passe réinitialisé avec succès !";
						$retourmnu = TRUE;
					}
				}
				
				if ($retourmnu == TRUE)
				{
					// Modification du compte de connexion
						
					$rech = "UPDATE `" .TBPW.  "` SET `pw_pass`='".$nouvmdpcrypte."',`pw_lastip`='".$log_ip."'  WHERE `pw_code` = '".$cge_code."'";
					$rs = mysql_query($rech) or die(mysql_error());
				}

				mysql_close($conn);

				if ($retourmnu == TRUE)
				{
					// Envoi de l'email vers le siège
					envoi_mail("Mise à jour mot de passe","cdg","mdp","xxx","cdg",$code,$nouvmdp,"",TRUE);
				}
			}
		}

		if ($categorie == "age")
		{
			if (@$_POST["init"] == "Réinitialiser")
			{
				// MAJ de la fiche Agence
			
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rech = "SELECT * FROM " .TBAGE. " WHERE `age_code` = '".$code."'";
				$rs = mysql_query($rech) or die(mysql_error());
				if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
				{
					if ($row = mysql_fetch_array($rs)) 
					{
						extract($row);
						$upd = "UPDATE `" .TBAGE. "` SET `age_pass`='".$nouvmdpcrypte."'  WHERE `age_id` = '".$age_id."'";
						$rs2 = mysql_query($upd) or die(mysql_error());
						$msg = "Mot de passe réinitialisé avec succès !";
						$retourmnu = TRUE;
					}
				}
				
				if ($retourmnu == TRUE)
				{
					// Modification du compte de connexion
						
					$log_ip = $_SERVER['REMOTE_ADDR']; // Récupération adresse IP
					$rech = "UPDATE `" .TBPW.  "` SET `pw_pass`='".$nouvmdpcrypte."',`pw_lastip`='".$log_ip."'  WHERE `pw_code` = '".$code."'";
					$rs = mysql_query($rech) or die(mysql_error());
				}

				mysql_close($conn);

				if ($retourmnu == TRUE)
				{
					// Envoi de l'email vers l'agence
					envoi_mail("Mise à jour mot de passe","age","mdp","xxx","age",$code,$nouvmdp,"",TRUE);
				}
			}

			if ((@$_POST["init"] == "Désactiver") OR (@$_POST["init"] == "Réactiver"))
			{
				if (@$_POST["init"] == "Désactiver")
					$actif = 0;
				if (@$_POST["init"] == "Réactiver")
					$actif = 1;

				// MAJ du compte de connexion
			
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rech = "SELECT * FROM " .TBPW. " WHERE `pw_code` = '".$code."'";

				$rech = "UPDATE `" .TBPW.  "` SET `pw_actif`='".$actif."',`pw_lastip`='".$log_ip."'  WHERE `pw_code` = '".$code."'";
				$rs = mysql_query($rech) or die(mysql_error());

				mysql_close($conn);

				if (@$_POST["init"] == "Désactiver")
					$msg = "Mot de passe désactivé avec succès !";
				if (@$_POST["init"] == "Réactiver")
					$msg = "Mot de passe réactivé avec succès !";
				$retourmnu = TRUE;
			}

		}

		if ($categorie == "tec")
		{
			if (@$_POST["init"] == "Réinitialiser")
			{
				// MAJ de la fiche Technicien
			
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rech = "SELECT * FROM " .TBTEC. " WHERE `tec_code` = '".$code."'";
				$rs = mysql_query($rech) or die(mysql_error());
				if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
				{
					if ($row = mysql_fetch_array($rs)) 
					{
						extract($row);
						$upd = "UPDATE `" .TBTEC. "` SET `tec_pass`='".$nouvmdpcrypte."'  WHERE `tec_id` = '".$tec_id."'";
						$rs2 = mysql_query($upd) or die(mysql_error());
						$msg = "Mot de passe réinitialisé avec succès !";
						$retourmnu = TRUE;
					}
				}
				
				if ($retourmnu == TRUE)
				{
					// Modification du compte de connexion
						
					$rech = "UPDATE `" .TBPW.  "` SET `pw_pass`='".$nouvmdpcrypte."',`pw_lastip`='".$log_ip."'  WHERE `pw_code` = '".$code."'";
					$rs = mysql_query($rech) or die(mysql_error());
				}

				mysql_close($conn);

				if ($retourmnu == TRUE)
				{
					// Envoi de l'email vers le technicien
					envoi_mail("Mise à jour mot de passe","tec","mdp","xxx","tec",$code,$nouvmdp,"",TRUE);
				}
			}

			if ((@$_POST["init"] == "Désactiver") OR (@$_POST["init"] == "Réactiver"))
			{
				if (@$_POST["init"] == "Désactiver")
					$actif = 0;
				if (@$_POST["init"] == "Réactiver")
					$actif = 1;

				// MAJ du compte de connexion
			
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rech = "SELECT * FROM " .TBPW. " WHERE `pw_code` = '".$code."'";

				$rech = "UPDATE `" .TBPW.  "` SET `pw_actif`='".$actif."',`pw_lastip`='".$log_ip."'  WHERE `pw_code` = '".$code."'";
				$rs = mysql_query($rech) or die(mysql_error());

				mysql_close($conn);

				if (@$_POST["init"] == "Désactiver")
					$msg = "Mot de passe désactivé avec succès !";
				if (@$_POST["init"] == "Réactiver")
					$msg = "Mot de passe réactivé avec succès !";
				$retourmnu = TRUE;
			}

		}

		if ($categorie == "cli")
		{
			if (@$_POST["init"] == "Réinitialiser")
			{
				// MAJ de la fiche Client
			
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rech = "SELECT * FROM " .TBCLI. " WHERE `cli_code` = '".$code."'";
				$rs = mysql_query($rech) or die(mysql_error());
				if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
				{
					if ($row = mysql_fetch_array($rs)) 
					{
						extract($row);
						$upd = "UPDATE `" .TBCLI. "` SET `cli_pass`='".$nouvmdpcrypte."'  WHERE `cli_id` = '".$cli_id."'";
						$rs2 = mysql_query($upd) or die(mysql_error());
						$msg = "Mot de passe réinitialisé avec succès !";
						$retourmnu = TRUE;
					}
				}
				
				if ($retourmnu == TRUE)
				{
					// Modification du compte de connexion
						
					$rech = "UPDATE `" .TBPW.  "` SET `pw_pass`='".$nouvmdpcrypte."',`pw_lastip`='".$log_ip."'  WHERE `pw_code` = '".$code."'";
					$rs = mysql_query($rech) or die(mysql_error());
				}

				mysql_close($conn);

				if ($retourmnu == TRUE)
				{
					// Envoi de l'email vers le client
					envoi_mail("Mise à jour mot de passe","cli","mdp","xxx","cli",$code,$nouvmdp,"",TRUE);
				}
			}

			if ((@$_POST["init"] == "Désactiver") OR (@$_POST["init"] == "Réactiver"))
			{
				if (@$_POST["init"] == "Désactiver")
					$actif = 0;
				if (@$_POST["init"] == "Réactiver")
					$actif = 1;

				// MAJ du compte de connexion
			
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rech = "SELECT * FROM " .TBPW. " WHERE `pw_code` = '".$code."'";

				$rech = "UPDATE `" .TBPW.  "` SET `pw_actif`='".$actif."',`pw_lastip`='".$log_ip."'  WHERE `pw_code` = '".$code."'";
				$rs = mysql_query($rech) or die(mysql_error());

				mysql_close($conn);

				if (@$_POST["init"] == "Désactiver")
					$msg = "Mot de passe désactivé avec succès !";
				if (@$_POST["init"] == "Réactiver")
					$msg = "Mot de passe réactivé avec succès !";
				$retourmnu = TRUE;
			}

		}

		if ($categorie == "ope")
		{
			if (@$_POST["init"] == "Réinitialiser")
			{
				// MAJ de la fiche Opérateur
			
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rech = "SELECT * FROM " .TBOPE. " WHERE `ope_code` = '".$code."'";
				$rs = mysql_query($rech) or die(mysql_error());
				if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
				{
					if ($row = mysql_fetch_array($rs)) 
					{
						extract($row);
						$upd = "UPDATE `" .TBOPE. "` SET `ope_pass`='".$nouvmdpcrypte."'  WHERE `ope_id` = '".$ope_id."'";
						$rs2 = mysql_query($upd) or die(mysql_error());
						$msg = "Mot de passe réinitialisé avec succès !";
						$retourmnu = TRUE;
					}
				}
				
				if ($retourmnu == TRUE)
				{
					// Modification du compte de connexion
						
					$rech = "UPDATE `" .TBPW.  "` SET `pw_pass`='".$nouvmdpcrypte."',`pw_lastip`='".$log_ip."'  WHERE `pw_code` = '".$code."'";
					$rs = mysql_query($rech) or die(mysql_error());
				}

				mysql_close($conn);

				if ($retourmnu == TRUE)
				{
					// Envoi de l'email vers l'opérateur
					envoi_mail("Mise à jour mot de passe","ope","mdp","xxx","ope",$code,$nouvmdp,"",TRUE);
				}
			}

			if ((@$_POST["init"] == "Désactiver") OR (@$_POST["init"] == "Réactiver"))
			{
				if (@$_POST["init"] == "Désactiver")
					$actif = 0;
				if (@$_POST["init"] == "Réactiver")
					$actif = 1;

				// MAJ du compte de connexion
			
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rech = "SELECT * FROM " .TBPW. " WHERE `pw_code` = '".$code."'";

				$rech = "UPDATE `" .TBPW.  "` SET `pw_actif`='".$actif."',`pw_lastip`='".$log_ip."'  WHERE `pw_code` = '".$code."'";
				$rs = mysql_query($rech) or die(mysql_error());

				mysql_close($conn);

				if (@$_POST["init"] == "Désactiver")
					$msg = "Mot de passe désactivé avec succès !";
				if (@$_POST["init"] == "Réactiver")
					$msg = "Mot de passe réactivé avec succès !";
				$retourmnu = TRUE;
			}

		}

?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI - ESPACE PRIVE : Mot de passe</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../inc/smi.css" rel="stylesheet" type="text/css">

<!-- Feuille de style & Script pour le menu -->
<link href="../inc/menu.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="../coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour affichages -->
<script language="JavaScript" src="../inc/aff.js"></script>

</head>
<body>

<?php 
	include_once ("../inc/entete.php"); 
?>


<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<p align="center">&nbsp;</p>
<p align="center" class="annot_gras_it"><?PHP echo $msg ?></p>

	<table width="100%"  border="0">
	  <tr>
		<td width="3%">&nbsp;</td>
		<td width="44%"><div align="right"></div></td>
		<td width="7%">&nbsp;</td>
		<td width="43%"><a href="index.php"><em><img src="../img/index.gif" width="48" height="48" border="0" align="absmiddle"> </em><span class="annot_gras_it">Revenir &agrave; l'accueil</span> </a></td>
		<td width="3%">&nbsp;</td>
	  </tr>
	</table>
	<script type="text/javascript">
	window.setTimeout("location=('index.php');",2000)
	</script>

<?php 
	include_once ("pied.php")
?>

