<?php
/**
* MiniClick
* Gestionnaire de tableau collaboratif
* Copyright (C) 2006 Guillaume Monet <gm@ibreizh.net>
*
* 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
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
* @package miniclick
* @version 5.0.0
*/

session_start();
include("config.php");
include("connection.php");
$mail = $_POST['mail'];
require(SMARTY_LIBS_DIR.'Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = SMARTY_TEMPLATE_DIR;
$smarty->compile_dir = SMARTY_COMPILE_DIR;
$smarty->config_dir = SMARTY_CONFIG_DIR;
$smarty->cache_dir = SMARTY_CACHE_DIR;

$message = "";

if($mail != ""){
	mysql_select_db("".DB_NAME."");
	$query = "SELECT * FROM USER WHERE Mail='$mail'";
	$users = mysql_query($query);
	if($user = mysql_fetch_object($users)){
		$pass =  generate();
		$to = "".$user->Mail;
		$subject = "[Miniclick] Compte Utilisateur";
		$body = "Vos informations a propos de votre compte utilisateur Miniclick\n\nLogin:".$user->Login."\nPassword:$pass";
		$sql_query = "UPDATE USER SET Passwd = PASSWORD('$pass') WHERE User_Id='".$user->User_Id."'";
		mysql_query($sql_query);
		if (mail($to, $subject, $body)) {
			$message = "<p>Message correctement envoy&eacute;</p>";
		} else {
			$message = "<p>Erreur lors de l'envoi du message</p>";
		}
	}else{
		$message = "Email inconnue,<br /> veuillez contacter l'administrateur";
	}
}else{
	$message = "Email Vide";	
}

$smarty->assign('page',"../includes/forgotten.php?message=".urlencode($message));
$smarty->display('redirect.tpl');

function generate(){
	$chaine = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@!?";
	$nb_caract = 8;
	$pass = "";

	for($u = 1; $u <= $nb_caract; $u++) {
		$nb = strlen($chaine);
		$nb = mt_rand(0,($nb-1));
		$pass.=$chaine[$nb];
	}
	return $pass;
}
?>