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

Version 0.9.9

Copyright (C) 2006-2009  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

Web : http://smitst.homelinux.com
****************************************************************************************/

session_start();
if (!isset($_SESSION["lng"])) $_SESSION["lng"]="fr";
include_once("lng/".$_SESSION["lng"].".inc.php"); // Paramètres de language
include_once("inc/config.inc.php");
include_once("inc/fic_age.inc.php");
include_once("inc/fic_cli.inc.php");
include_once("inc/fic_do.inc.php");
include_once("inc/fic_hum.inc.php");
include_once("inc/fic_ope.inc.php");
include_once("inc/fic_prm.inc.php");
include_once("inc/fic_st.inc.php");
include_once("inc/fic_tec.inc.php");
include_once("inc/fic_txt.inc.php");
include_once("inc/func.inc.php");
include_once("inc/func_date.inc.php");
include_once("inc/func_geo.inc.php");
include_once("inc/func_log.inc.php");
include_once("inc/func_txt.inc.php");
include_once("inc/func_txtcm.inc.php");
include_once("inc/func_mail.inc.php");
include_once("classes/phpmailer/class.phpmailer.php");
include_once("inc/mail.inc.php");
Archivelog(@$_SESSION["code_log"]);

$log_ip = $_SERVER['REMOTE_ADDR']; // Récupération adresse IP
$recup_ok = true;
$newpass = false;
$msg ="Probl&egrave;me lors de la r&eacute;initialisation de votre mot de passe.<br><br>Impossible de vous affecter un nouveau mot de passe.";

// Variables Formulaire
$code = strip_tags(trim(@$_GET["code"]));
$pass = strip_tags(trim(@$_GET["pw"]));

if (($code == "") OR ($pass == ""))
	$recup_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);

			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			switch ($pw_level)
			{
				// Centre de gestion
				case 5 :
					// MAJ de la fiche centre de gestion
					$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;
						}
					}
					break;
				// Agence
				case 4 :
					// MAJ de la fiche Agence
					$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;
						}
					}
					break;
				// Technicien
				case 3 :
					// MAJ de la fiche Technicien
					$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;
						}
					}
					break;
				// Opérateur
				case 2 :
					// MAJ de la fiche Opérateur
					$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;
						}
					}
					break;
				// Client
				case 1 :
					// MAJ de la fiche Client
					$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;
						}
					}
					break;

				default :
					break;
			}

			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);
	
				switch ($pw_level)
				{
					// Centre de gestion
					case 5 :
						// 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 :
						// 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 :
						// 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 :
						// 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 :
						// Envoi de l'email vers le client
						envoi_mail("","Mise à jour mot de passe","cli","mdp","xxx","cli",$code,$nouvmdp,"",TRUE);
						break;
					default :
						break;
				}
				$msg = "Mot de passe réinitialisé avec succès !<br><br>Vous allez bientôt recevoir un email avec vos nouveaux codes.";
			}
			else
				mysql_close($conn);
		}
	}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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="css/smi.css" rel="stylesheet" type="text/css">

</head>
<body>

<table width="100%" height="300" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
	<td valign="top">
		<div align="center">
		<p><img src="img/logo.gif" alt="logo"></p>
		<p><img src="img/login.png" alt="connexion"></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"><?PHP echo $txt_lnk_ret_idx; ?></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>
</body>
</html>