<?php
session_start();
require_once('lib/gettext/gettext.inc');
include_once("inc/config.inc.php");
include_once("inc/func_geo.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");
require_once ("classes/phpmailer/class.smtp.php");
include_once("classes/ezSQL/shared/ez_sql_core.php");
include_once("classes/ezSQL/mysqli/ez_sql_mysqli.php");
include_once("inc/func_sql.inc.php");
Archivelog(@$_SESSION["code_log"]);

// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);

$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 = @$_POST["pass_code"];
	$pass_email = strtolower(@$_POST["pass_email"]);
	$pass_verif = @$_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)
			$db = open_database(DBUSER,DBPASS,DBNAME,DBHOST);
			$rech = "SELECT * FROM `" .TBPW. "` WHERE `pw_code` = '" .$db->escape($pass_code). "' AND `pw_actif` = '1'";
			$row = $db->get_row($rech,ARRAY_A);
			if ($row) // Il ne peut y avoir qu'un seul résultat
			{
				$validcode = true;
				
				extract ($row);
				
				// Contrôle de l'adresse email
				$rech2 ="";

				switch ($pw_level)
				{
					// Client
					case 1 :
						$rech2 = "SELECT cli_email FROM `" .TBCLI. "` WHERE `cli_code` = '" .$db->escape($pass_code). "'";
						break;
					// Opérateur
					case 2 :
						$rech2 = "SELECT ope_email FROM `" .TBOPE. "` WHERE `ope_code` = '" .$db->escape($pass_code). "'";
						break;
					// Technicien
					case 3 :
						$rech2 = "SELECT tec_email FROM `" .TBTEC. "` WHERE `tec_code` = '" .$db->escape($pass_code). "'";
						break;
					// Agence
					case 4 :
						$rech2 = "SELECT age_email FROM `" .TBAGE. "` WHERE `age_code` = '" .$db->escape($pass_code). "'";
						break;
					// Centre de gestion
					case 5 :
						$rech2 = "SELECT cge_email FROM `" .TBCG. "` WHERE `cge_code` = '" .$db->escape($pass_code). "'";
						break;
					default :
						break;
				}
				
				if ($rech2 <> "")
				{
					$row2 = $db->get_row($rech2,ARRAY_A);
					if ($row2)
					{
						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;
					}
				}
			}
			$db->disconnect();
		}
	}
} 

if (($validsaisie == true) AND ($validcode == true)  AND ($validemail == true) AND ($validverif == true))
{
	// Récupération des informations centre de gestion
	$db = open_database(DBUSER,DBPASS,DBNAME,DBHOST);
	$rech = "SELECT * FROM `" .TBCG. "`";
	$row = $db->get_row($rech,ARRAY_A);
	if ($row) 
		extract ($row);
	$db->disconnect();
	
	$sujet = _("Renouvellement de votre mot de passe");

	/*
	$mail = new SMIMailer;
	$mail->init_PHPM();
	$mail->SetLanguage("fr", $root . REP . "/classes/phpmailer/language/");
	$mail->IsHTML(TRUE);
	*/

	$mail = new PHPMailer();
	$mail->CharSet = 'UTF-8';
	$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_Id($cge_ville,TRUE)." ".Retourneville_Id($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.= _("L'administrateur du site");
	
	$mail->Subject = $sujet;
	$mail->Body = $txtcorps;
	
	$sent = $mail->Send();
	
	$emailsent = true;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>SMI : <?PHP echo _("Mot de passe perdu ?"); ?></title>

<?PHP include_once("header.inc.php"); ?>

<script type="text/javascript">
<!--        
function init_all() {
	init_Body();
	Focus_first();
}

$(document).ready(function(){
	init_all();
});
//-->
</script>
</head>

<body>
<table width="100%" height="300" border="0" cellspacing="0" cellpadding="0">
<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="<?PHP echo _("logo SMI"); ?>"></p>
        <p><img src="img/login.png" alt="<?PHP echo _("connexion"); ?>></p>
        <p class="annot_std_it"><?PHP echo _("Toutes les connexions sont archivées"); ?></p>
		</div>
		<table width="500" border="0" align="center">
		<tr bgcolor="#FF0000">
			<td colspan="2">
				<div align="center" class="titre_std_gras_jaune_frouge"><strong><?PHP echo _("Mot de passe perdu ?"); ?></strong></div>
			</td>
		</tr>
		<?php
		if ($emailsent == false)
		{
		?>
		<tr>
			<td colspan="2">
				<div align="center" class="annot_petit_it">
				  <p><?PHP echo _("Merci de saisir les informations suivantes").'<br />'._("Une fois que vous aurez validé, un email de confirmation vous sera expédié"); ?></p>
				</div>
			</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td class="rubfrm" id="f_code" width="134" align="right"><?PHP echo _("Code Utilisateur"); ?></td>
			<td width="222">
				<input name="pass_code" type="text" id="pass_code" maxlength="20">
			</td>
		</tr>
		<tr>
			<td class="rubfrm" id="f_email" align="right"><?PHP echo _("Votre email"); ?></td>
			<td>
				<input name="pass_email" type="text" id="pass_email" size="50" maxlength="50">
			</td>
		</tr>
		<tr>
			<td class="rubfrm" id="f_verif" align="right"><?PHP echo _("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_std" id="code" value="<?PHP echo _("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_std" id="RAZ" value="<?PHP echo _("Réinitialiser"); ?>">
		<input type="submit" class="bton_std" name="submit" id="submit" value="<?PHP echo _("Valider"); ?>">
		<input name="annul" type="button" class="bton_std" id="annul" value="<?PHP echo _("Retour accueil"); ?>" onclick="location.href='index.php'">
		</p>
		</form>

		<?php 
		if ($validsaisie == false)
		{
			if (@$_POST["submit"] <> "") 
			{
		?>
				<p align="center"><span class="titre_std_gras_jaune_frouge"><?PHP echo _("Erreur de saisie"); ?></span></p>
		<?php
			}
		}
		else
		{
			if (($validcode == false) OR ($validemail == false))
			{
				if (@$_POST["submit"] <> "") 
				{
		?>
					<p align="center"><span class="titre_std_gras_jaune_frouge"><?PHP echo _("Problème code utilisateur et/ou email"); ?></span></p>
		<?php
				}
			}
	
			if ($validverif == false)
			{
				if (@$_POST["submit"] <> "") 
				{
		?>
					<p align="center"><span class="titre_std_gras_jaune_frouge"><?PHP echo _("Mauvais code de validation"); ?></span></p>
		<?php
				}
			}
		} 
		?>
	<?PHP
	}
	else
	{
		unset($_SESSION['captcha_keystring']);
		unset($_POST["submit"]);
	
		$validsaisie = True;
		$validcode = False;
		$validemail = False;
		$validverif = False;

		pop_ret_auto(_("Email de confirmation expédié"), 'ok', 'index.php');
	}
	?>
	</td>
</tr>
</table>
</body>
</html>