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

Version 0.9.7

Copyright (C) 2006-2007  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();
include_once("inc/config.inc.php");
include_once("inc/func.inc.php");
include_once("inc/func_txt.inc.php");
include_once("inc/func_log.inc.php");
require_once ("classes/phpmailer/class.phpmailer.php");
Archivelog(@$_SESSION["code_log"]);

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

$validsaisie = True;
$validcode = False;
$validemail = False;
$validverif = False;
$emailsent = False;

if (@$_POST["submit"] <> "")
{
	$pass_code = strip_tags(trim(@$_POST["pass_code"]));
	$pass_email = strtolower(strip_tags(trim(@$_POST["pass_email"])));
	$pass_verif = strip_tags(trim(@$_POST["pass_verif"]));
	
	if ($pass_code == "")
		$validsaisie = false;
	if ($pass_email == "")
		$validsaisie = false;
	if ($pass_verif == "")
		$validsaisie = false;
	
	if ($validsaisie == true)
	{
		if ($validcode == false) 
		{	
			// Contrôle du code (et vérification si actif)
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "SELECT * FROM `" .TBPW. "` WHERE `pw_code` = '" .$pass_code. "' 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
			{
				$validcode = true;
				
				if ($row = mysql_fetch_array($rs))
				{
					extract ($row);
					
					// Contrôle de l'adresse email
					$rech2 ="";
	
					switch ($pw_level)
					{
						// Client
						case 1 :
							$rech2 = "SELECT * FROM `" .TBCLI. "` WHERE `cli_code` = '" .$pass_code. "'";
							break;
						// Opérateur
						case 2 :
							$rech2 = "SELECT * FROM `" .TBOPE. "` WHERE `ope_code` = '" .$pass_code. "'";
							break;
						// Technicien
						case 3 :
							$rech2 = "SELECT * FROM `" .TBTEC. "` WHERE `tec_code` = '" .$pass_code. "'";
							break;
						// Agence
						case 4 :
							$rech2 = "SELECT * FROM `" .TBAGE. "` WHERE `age_code` = '" .$pass_code. "'";
							break;
						// Centre de gestion
						case 5 :
							$rech2 = "SELECT * FROM `" .TBCG. "` WHERE `cge_code` = '" .$pass_code. "'";
							break;
						default :
							break;
					}
					
					if ($rech2 <> "")
					{
						$rs2 = mysql_query($rech2) or die(mysql_error());
						if (mysql_num_rows($rs2) == 1) // Il ne peut y avoir qu'un seul résultat
						{
							if ($row2 = mysql_fetch_array($rs2))
							{
								extract ($row2);
								$email = "";
	
								switch ($pw_level)
								{
									// Client
									case 1 :
										$email = $cli_email;
										break;
									// Opérateur
									case 2 :
										$email = $ope_email;
										break;
									// Technicien
									case 3 :
										$email = $tec_email;
										break;
									// Agence
									case 4 :
										$email = $age_email;
										break;
									// Centre de gestion
									case 5 :
										$email = $cge_email;
										break;
									default :
										break;
								}
								if ($pass_email == $email)
									$validemail = true;
	
								// Contrôle du code captcha
								
								if($_SESSION['captcha_keystring'] ==  $pass_verif)
									$validverif = true;
							}
							mysql_free_result($rs2);
						}
					}
				}
			}
			mysql_free_result($rs);
			mysql_close($conn);
		}
	}
} 

if (($validsaisie == true) AND ($validcode == true)  AND ($validemail == true) AND ($validverif == true))
{
	// Récupération des informations 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);
	}
	mysql_free_result($rs);
	mysql_close($conn);
	
	$sujet = "Renouvellement de votre mot de passe";
	
	$mail = new PHPMailer();
	$mail->Host = $smtp_nom;
	$mail->Port = $smtp_port;
	$mail->Helo = "localhost.localdomain";
	if (trim($smtp_login)<>"")
	{
		$mail->Username = $smtp_login;
		$mail->Password = $smtp_mdp;
		$mail->SMTPAuth = true;
	}
	else
	{
		$mail->Username = "";
		$mail->Password = "";
		$mail->SMTPAuth = false;
	}
	$mail->IsHTML(TRUE);
	$mail->Mailer = "smtp";
	
	// Entête de l'email
	$mail->Priority = 1;
	
	//Expédié par
	$mail->From = $admin_mail;
	$mail->FromName = $admin_name;
	$mail->Sender = $admin_mail;
	
	//Réponse automatique à...
	$mail->AddReplyTo($admin_mail, $admin_name);
	
	// Destinataire
	$mail->AddAddress($pass_email);
	
	//Copie du mail à l'administrateur
	if (($admin_mail <> "") AND ($admin_mail <> $pass_email))
		$mail->AddBCC($admin_mail,$admin_name);
	
	// Corps de l'email
	$txtcorps = "<strong>".$cge_nom."</strong><br>";
	$txtcorps.= $cge_adr1."<br>";
	if ($cge_adr2 <> "")
		$txtcorps.= $cge_adr2."<br>";
	$txtcorps.= Retournecp_openId($cge_ville,TRUE)." ".Retourneville_openId($cge_ville,TRUE)."<br><br>";
	$txtcorps.= "Bonjour,<br><br>";
	$txtcorps.="Nous venons de recevoir une demande de renouvellement de votre mot de passe depuis notre site.<br><br>";
	$txtcorps.="Si vous êtes bien à l'origine de cette demande et si vous souhaitez changer de mot de passe, merci de cliquer sur le ";
	$txtcorps.="<a href=\"".$rep_appli."/newpass.php?code=".$pass_code."&pw=".$pw_pass."\">lien suivant</a>.<br><br>";
	$txtcorps.="Dès validation, vous recevrez votre nouveau mot de passe.<br><br>";
	$txtcorps.="Merci de votre demande.<br><br>";
	$txtcorps.="Le Webmaster";
	
	$mail->Subject = $sujet;
	$mail->Body = $txtcorps;
	
	$sent = $mail->Send();
	
	$emailsent = true;
}
?>
<!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 : Demande mot de passe</title>

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

<!-- Scripts Javascript divers -->
<script language="JavaScript" src="jscript/divers.js"></script>

</head>

<body onLoad="metFocus();">

<table width="100%" height="300" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
	<td valign="top">
		<?php
		if ($emailsent == false)
		{
		?>
			<form name="pass" action="pass.php" method="post">
		<?php
		}
		?>
		<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>Mot de passe perdu ? </strong></div>
			</td>
		</tr>
		<?php
		if ($emailsent == false)
		{
		?>
		<tr>
			<td colspan="2">
				<div align="center" class="annot_petit_it">
				  <p>Merci de saisir les informations suivantes <br>
				  Une fois que vous aurez valid&eacute;, un email de confirmation vous sera exp&eacute;di&eacute;.</p>
				</div>
			</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr bgcolor="#333333">
			<td class="rubfrm" id="f_code" width="134" align="right">
				Code Utilisateur
			</td>
			<td width="222">
				<input name="pass_code" type="text" id="pass_code" maxlength="20">
			</td>
		</tr>
		<tr bgcolor="#333333">
			<td class="rubfrm" id="f_email" align="right">
				Votre email
			</td>
			<td>
				<input name="pass_email" type="text" id="pass_email" maxlength="20">
			</td>
		</tr>
		<tr bgcolor="#333333">
			<td class="rubfrm" id="f_verif" align="right">
				Code ci-dessous
			</td>
			<td>
				<input name="pass_verif" type="text" id="pass_verif" maxlength="20">
			</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>
				<img src="imggen.php?<?php echo session_name()?>=<?php echo session_id()?>" align="absmiddle">
				<input name="code" type="button" class="bton_fmarr_std" id="code" value="Autre code" onclick="location.href='pass.php'">
			</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;
				
			</td>
		</tr>
	<?PHP
	}
	?>
	</table>
	<?php
	if ($emailsent == false)
	{
	?>
		<p align="center">
		<input name="RAZ" type="reset" class="bton_fmarr_std" id="RAZ" value="R&eacute;initialiser">
		<input type="submit" class="bton_fmarr_std" name="submit" id="submit" value="Valider">
		<input name="annul" type="button" class="bton_fmarr_std" id="annul" value="Retour accueil" onclick="location.href='index.php'">
		</p>
		</form>

		<?php 
		if ($validsaisie == false)
		{
			if (@$_POST["submit"] <> "") 
			{
		?>
				<p align="center"><font color="#FF0000"><font size="-1"><strong>Erreur de saisie </strong></font></font></p>
		<?php
			}
		}
		else
		{
			if (($validcode == false) OR ($validemail == false))
			{
				if (@$_POST["submit"] <> "") 
				{
		?>
					<p align="center"><font color="#FF0000"><font size="-1"><strong>Probl&egrave;me code utilisateur et/ou email </strong></font></font></p>
		<?php
				}
			}
	
			if ($validverif == false)
			{
				if (@$_POST["submit"] <> "") 
				{
		?>
					<p align="center"><font color="#FF0000"><font size="-1"><strong>Mauvais code de validation </strong></font></font></p>
		<?php
				}
			}
		} 
		?>
		<td width="10">&nbsp;</td>
		<td width="250" valign="top">&nbsp;
		
		</td>
	<?PHP
	}
	else
	{
		unset($_SESSION['captcha_keystring']);
		unset($_POST["submit"]);
	
		$validsaisie = True;
		$validcode = False;
		$validemail = False;
		$validverif = False;
	?>
		<p align="center"><font color="#FF0000"><font size="-1"><strong>Email de confirmation expédié</strong></font></font></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>
	<?php
	}
	?>
	</td>
</tr>
</table>
</body>
</html>