<?php
/* lostpasswd.php
 * - Lost password
 * Copyright (c) 2004 Stéphane Salès
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

	include("includes/config.inc.php");
	include(WEB_ROOT."includes/database.inc.php");
	include(WEB_ROOT."includes/functions.inc.php");
	include(WEB_ROOT."includes/lang.inc.php");
	include(WEB_ROOT."includes/session.inc.php");

	function isEmail($login) {
		if( empty($login) ) {
			$GLOBALS["error_detected"] = ("login vide");
		} else {
			$req = "SELECT email_adh
				FROM ".PREFIX_DB."adherents
				WHERE login_adh=".txt_sqls($login);
			$result = &$GLOBALS["DB"]->Execute($req);

			if ($result->EOF) {
				$GLOBALS["error_detected"] = ("login inexistant");
				dblog(("Login inexistant envoyé via le formulaire de récupération du mot de passe")." \"" . $login ."\"");
			}else{
				$email=$result->fields[0];
				if( empty($email) ) {
					$GLOBALS["error_detected"] = ("Ce compte ne contient pas d'adresse email, veuillez contacter un administrateur");
					dblog(("Demande d'envoi de mot de passe mais email vide. Login :")." \"" . $login . "\"");
				}else
				return $email;
			}
		}
	}

	if( isset($_POST["login"]) ) {
		$login_adh=$_POST['login'];
		$email_adh=isEmail($login_adh);

		//send the password
		if(	$email_adh!="" )
		{
			$req = "SELECT mdp_adh from ".PREFIX_DB."adherents where login_adh=".txt_sqls($login_adh);
			$result = &$DB->Execute($req);
			if (!$result->EOF)
				$mdp_adh = $result->fields[0];
			$mail_subject = ("Vos identifiants Galette");
			$mail_text =  ("Bonjour,")."\n";
			$mail_text .= "\n";
			$mail_text .= ("Quelqu'un (sûrement vous) a demandé que l'on vous renvoie votre mot de passe.")."\n";
			$mail_text .= "\n";
			$mail_text .= ("Veuillez vous identifier à cette adresse :")."\n";
			$mail_text .= "http://".$_SERVER["SERVER_NAME"].dirname($_SERVER["REQUEST_URI"])."\n";
			$mail_text .= "\n";
			$mail_text .= ("Identifiant :")." ".custom_html_entity_decode($login_adh, ENT_QUOTES)."\n";
			$mail_text .= ("Mot de passe :")." ".custom_html_entity_decode($mdp_adh, ENT_QUOTES)."\n";
			$mail_text .= "\n";
			$mail_text .= ("A très bientôt !")."\n";
			$mail_text .= "\n";
			$mail_text .= ("(ce mail est un envoi automatique)")."\n";
			$mail_headers = "From: ".PREF_EMAIL_NOM." <".PREF_EMAIL.">\n";
			if(  mail($email_adh,$mail_subject,$mail_text, $mail_headers) ) {
				dblog(("Mot de passe envoyé. Login :")." \"" . $login_adh . "\"");
				$warning_detected = ("Mot de passe envoyé. Login :")." \"" . $login_adh . "\"";
				$password_sent = true;
			}else{
				dblog(("Un problème est survenu dans l'envoi du mot de passe pour le compte :")." \"" . $login_adh . "\"");
				$warning_detected = ("Un problème est survenu dans l'envoi du mot de passe pour le compte :")." \"" . $login_adh . "\"";
			}
		}
	}
?>
<!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" xml:lang="fr-FR" lang="fr-FR"><head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type" />
<title>L’A.M.A.P. des Jardins de Virgile - Produits</title>
        <link rel="stylesheet" type="text/css" href="/galette/style.css" />
        <link rel="icon" type="image/png" href="/galette/icon-mini.png" />
        <meta name="Author" content="GARETTE Emmanuel" />
        <meta name="Keywords" content="AMAP, Dijon, Les bourroches, Le jardin de Virgile, A.M.A.P." />
        <meta name="Description" content="AMAP Dijon le Jardin de Virgile." />
</head>
<body>
        <h1>Partie privée de l'<acronym title="Association pour le Maintien de l'Agriculture Paysanne">A.M.A.P.</acronym> <br />les Jardins de Virgile</h1>
<p id="soustitre">::&nbsp;L’<acronym title="Association pour le Maintien de l'Agriculture Paysanne">A.M.A.P.</acronym> des Jardins de Virgile des bourroches, Dijon en Côte d’Or&nbsp;::</p>
<div id="body">
<div id="content">
	<h2>Mot de passe perdu</h2>
	<form action="lostpasswd.php" method="post">
	<fieldset>
	<legend><?php echo ("Récupération de mot de passe"); ?></legend>

<?php if( isset($error_detected) ) { ?>
<div id="errorbox" style="width: 300px">
	<p style="color: red";><?php echo "Erreur : "; ?><?php echo $error_detected; ?></p>
</div>
<?php } ?>

<?php if( isset($warning_detected) ) { ?>
<div>
	<p style="color: red";><?php echo "Avertissement : "; ?><?php echo $warning_detected; ?></p>
</div>
<?php } ?>

<?php if( !isset($password_sent) ) { ?>
		<p><label><?php echo ("Identifiant :"); ?></label><input type="text" name="login" /></p>
		<p><input type="submit" name="lostpasswd" value="<?php echo ("Envoyez-moi mon mot de passe"); ?>" />
<?php } ?>
	</fieldset>
	</form>
	<br />
<?php	echo '<p><a href="index.php" class="close">Retour à l' . "'" . 'espace privée</a></p>'; ?>

</div>
<p id="pieddepage">
::&nbsp;Mise en page <a href="http://www.gnunux.info">GARETTE Emmanuel</a>&nbsp;::
</p>
</div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />


</body>
</html>
