<?php
include_once "../tools/inc.php";
$page = include("../common.php");
include("tools/function.string.php");
include("tools/function.sql.php");
$table="users".$config["table_prefix"];

$message_list=array(
        1 => "{rs:MSG_FORGOT_SENT}",
);

$message=new Message();
$error=new Message("error");
if (isset($_r)) $message->add($message_list[$_r]);
if (isset($save)) {
	$r=check_array($r);
	$sql="select * from $table where login='".$r["login"]."'";
	$rs=$db->select($sql);
	if (isset($rs[0]["id"])) {
		$settings=load_settings($rs[0]["id"]);
		$message=$db->get_val("pages".$config["table_prefix"], "description", "where name='forgot_password'");
                $f=array(
			"%name%",
			"%login%",
			"%password%",
			"%url%",
		);
                $v=array(
			$rs[0]["name"],
			$rs[0]["login"],
			$rs[0]["password"],
			"http://".$HTTP_HOST.$PATH,
		);
		$array=array(
			"login"         => $settings["autoresponder"]["smtp"]["login"],
			"password"      => $settings["autoresponder"]["smtp"]["password"],
			"host"          => $settings["autoresponder"]["smtp"]["host"],
			"port"          => $settings["autoresponder"]["smtp"]["port"],
			"from_name"     => $settings["mail_from_name"],
			"from"          => ($settings["mail_from"] ? $settings["mail_from"] : $rs[0]['mail']),
			"reply_to"      => "",
			"to_name"       => $rs[0]["name"],
			"to"            => $rs[0]["mail"],
			"subject"       => $db->get_val("pages".$config["table_prefix"], "subject", "where name='forgot_password'"),
			"message"       => str_replace($f, $v, $message),
			"charset"       => $settings["encoding"],
			"content_type"  => "text/plain",
		);
		include("tools/class.mime.php");
		include("tools/class.smtp.php");
		$smtp=new SMTP($array);
		if ($smtp->send_mail()) {
			$link->add("_r", 1);
			$db->close();
			header("Location: ".$link->get());
			exit;
		} else $error->add("{rs:ERROR_NOT_SENT}");
	} else $error->add("{rs:ERROR_LOGIN}");
	$r=html_string($r, true);
	$page->assign($r);
}

$page->section("FORGOT");
$db->close();
$page->assign(array(
	"TITLE"			=> "{rs:TITLE_FORGOT}",
	"ERROR"			=> ($error->count ? $error->get() : ""),
	"MESSAGE"		=> ($message->count ? $message->get() : ""),
	"LINK"			=> $link->get(),
	"IMG"			=> "forgot.gif",
	"WINDOW_TITLE"		=> "{rs:TITLE_FORGOT}",
));
print $page->evaluate();
?>