<?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 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 include("classes/xpm/smtp.php"); ?>
<?php include ("inc/mail.inc.php") ?>
<?PHP
	Archivelog(@$HTTP_SESSION_VARS["code_log"]);

	$log_ip = $_SERVER['REMOTE_ADDR']; // Récupération adresse IP
?>
<?php
	$recup_ok = true;
	$newpass = false;
	$msg ="Problème lors de la rénitialisation de votre mot de passe.<br><br>Impossible de vous affecter u nouveau mot de passe.";

	// Variables Formulaire
	$code = strip_tags(trim(@$_GET["code"]));
	$pass = strip_tags(trim(@$_GET["pw"]));
	
	if (($code == "") OR ($pass == ""))
		$reup_ok = false;
		
	if ($recup_ok == true)
	{
		$nouvmdp = cremdp(FALSE); // Création d'un nouveau de passe (envoyé)
		$nouvmdpcrypte = md5($nouvmdp); // Cryptage du nouveau mot de passe (stocké)

		// Récupération du compte

		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "SELECT * FROM `" .TBPW. "` WHERE `pw_code` = '" .$code. "' AND `pw_pass` = '".$pass."' AND `pw_actif` = '1'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 1) // Il ne peut y avoir qu'un seul résultat
		{
			if ($row = mysql_fetch_array($rs))
			{
				extract ($row);

				switch ($pw_level)
				{
					// Centre de gestion
					case 5 :
						// 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());
								$newpass = true;
							}
						}

						if ($newpass == 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 ($newpass == TRUE)
						{
							// Envoi de l'email vers le siège
							envoi_mail("Mise à jour mot de passe","cdg","mdp","xxx","cdg",$code,$nouvmdp,"",TRUE);
						}

						break;
					// Agence
					case 4 :
						// 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());
								$newpass = TRUE;
							}
						}
						
						if ($newpass == 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 ($newpass == TRUE)
						{
							// Envoi de l'email vers l'agence
							envoi_mail("Mise à jour mot de passe","age","mdp","xxx","age",$code,$nouvmdp,"",TRUE);
						}
						break;
					// Technicien
					case 3 :
						// 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());
								$newpass = TRUE;
							}
						}
						
						if ($newpass == 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 ($newpass == TRUE)
						{
							// Envoi de l'email vers le technicien
							envoi_mail("Mise à jour mot de passe","tec","mdp","xxx","tec",$code,$nouvmdp,"",TRUE);
						}
						break;
					// Opérateur
					case 2 :
						// 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());
								$newpass = TRUE;
							}
						}
						
						if ($newpass == 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 ($newpass == TRUE)
						{
							// Envoi de l'email vers l'opérateur
							envoi_mail("Mise à jour mot de passe","ope","mdp","xxx","ope",$code,$nouvmdp,"",TRUE);
						}
						break;
					// Client
					case 1 :
						// 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());
								$newpass = TRUE;
							}
						}
						
						if ($newpass == 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 ($newpass == TRUE)
						{
							// Envoi de l'email vers le client
							envoi_mail("Mise à jour mot de passe","cli","mdp","xxx","cli",$code,$nouvmdp,"",TRUE);
						}
						break;

					Default :
						break;
				}
			if ($newpass == true);
				$msg = "Mot de passe réinitialisé avec succès !<br><br>Vous allez bientôt recevoir un email avec vos nouveaux codes.";
			}
		}
	}
?>

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

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

</head>
<body>

<table width="100%" height="300" border="0" cellspacing="0" cellpadding="0">
<tr>
	<td valign="top">
		<div align="center">
		<p><img src="img/logo.gif" alt="logo" width="195" height="36"></p>
		<p><img src="img/login.gif" width="64" height="64"></p>
		<p class="annot_std_it">Toutes les connexions sont archiv&eacute;es</p>
		</div>
		<table width="600" border="0" align="center">
		<tr bgcolor="#FF0000">
			<td colspan="2">
				<div align="center" class="titre_std_gras_jaune_frouge"><strong>Nouveau mot de passe</strong></div>
			</td>
		</tr>
		<tr>
			<td align="center">
				<p class="annot_gras_it"><?PHP echo $msg ?></p>
				<p align="center"><a href="index.php">Retour &agrave; l'accueil</a></p>
				<p align="center" class="annot_petit_it">(Retour automatique dans 5 secondes)</p>
		
				<script type="text/javascript">
				window.setTimeout("location=('index.php');",5000)
				</script>
			</td>
		</tr>
		</table>
	</td>
</tr>
</table>
