<?php
define('_VALID', true);
require 'include/config.php';
require 'classes/filter.class.php';
require 'classes/validation.class.php';
require 'include/function_smarty.php';

if ( isset($_POST['submit_lost']) ) {
    $filter     = new VFilter();
    $valid      = new VValidation();
    $email      = $filter->get('email');
    
    if ( $email == '' ) {
        $errors[]   = 'Please enter your email which you first registered with!';
    } elseif ( !$valid->email($email) ) {
        $errors[]   = 'Email is not a valid email address!';
    } elseif ( !$valid->emailExists($email) ) {
        $errors[]   = 'There is no user with this email!';
    } else {
        require 'classes/random.class.php';
		require 'classes/email.class.php';
		
        $passwd     = VRandom::generate(8);
		$password	= md5($passwd);
        $sql        = "SELECT username FROM signup WHERE email = '" .mysql_real_escape_string($email). "' LIMIT 1";
        $rs         = $conn->execute($sql);
        $username   = $rs->fields['username'];
        
        $sql        = "UPDATE signup SET pwd = '" .mysql_real_escape_string($password). "'
                       WHERE username = '" .mysql_real_escape_string($username). "' LIMIT 1";
        $conn->execute($sql);

        $smarty->assign('receiver_name', $username);
        $smarty->assign('password', $passwd);
        
        $sql        = "SELECT * FROM emailinfo WHERE email_id = 'recover_password' LIMIT 1";
        $rs         = $conn->execute($sql);
        $subject    = str_replace('{$site_name}', $config['site_name'], $rs->fields['email_subject']);
        $email_path = $config['BASE_DIR']. '/templates/' .$rs->fields['email_path'];
        $body       = $smarty->fetch($email_path);
        
        $mail           = new VMail();
        $mail->set();
        $mail->Subject  = $subject;
        $mail->AltBody  = $body;
        $mail->Body     = nl2br($body);
        $mail->AddAddress($email);
        $mail->Send();
        
        $messages[]     = 'Your username and password have been emailed to you!';        
    }
}

$smarty->assign('errors',$errors);
$smarty->assign('messages',$messages);
$smarty->assign('menu', 'home');
$smarty->assign('self_title', $seo['lost_title']);
$smarty->assign('self_description', $seo['lost_desc']);
$smarty->assign('self_keywords', $seo['lost_keywords']);
$smarty->display('header.tpl');
$smarty->display('errors.tpl');
$smarty->display('messages.tpl');
$smarty->display('lost.tpl');
$smarty->display('footer.tpl');
$smarty->gzip_encode();
?>
