<?php
// $Id: lostpass.php,v 1.1 2004/07/11 10:27:17 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS                                                   //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: The Xoops Project
// Author Website : http://www.xoops.org
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //

$xoopsOption['pagetype']   = 'user';
$xoopsOption['page_style'] = 8;

include_once("mainfile.php");

if ( !isset($email) || $email == "" ) {
	redirect_header("user.php", 2, _US_SORRYNOTFOUND);
	exit();
	}

$getuser = XoopsUser::getAllUsers(array("email='".$myts->oopsAddSlashesGPC($email)."'"), true);

if ( empty($getuser) ) {
	redirect_header("user.php", 2, _US_SORRYNOTFOUND);
	exit();
	} else {
		$areyou = substr($getuser[0]->getVar("pass"), 0, 5);
		if ( isset($code) && $areyou == $code ) {
			$newpass     = makepass();
			$xoopsMailer =& getMailer();
			$xoopsMailer->useMail();
			$xoopsMailer->setTemplate("lostpass2.tpl");
			$xoopsMailer->assign("SITENAME", $meta['title']);
			$xoopsMailer->assign("ADMINMAIL", $xoopsConfig['adminmail']);
			$xoopsMailer->assign("SITEURL", XOOPS_URL."/");
			$xoopsMailer->assign("IP", _REMOTE_ADDR);
			$xoopsMailer->assign("NEWPWD", $newpass);
			$xoopsMailer->setToUsers($getuser[0]->getVar("uid"));
			$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
			$xoopsMailer->setFromName($meta['title']);
			$xoopsMailer->setSubject(sprintf(_US_NEWPWDREQ, XOOPS_URL));
			if ( !$xoopsMailer->send() ) {
				echo $xoopsMailer->getErrors();
			}

// Next step: add the new password to the database
$cryptpass = md5($newpass);
$query     = "UPDATE ".$db->prefix("users")." SET pass='$cryptpass' WHERE uid=".$getuser[0]->getVar("uid")."";

if ( !$db->query($query) ) {
	include_once("header.php");
	echo _US_MAILPWDNG;
	include_once("footer.php");
	exit();
}

redirect_header("user.php", 3, sprintf(_US_PWDMAILED, $getuser[0]->getVar("uname")));
exit();

// If no Code, send it
} else {
	$xoopsMailer =& getMailer();
	$xoopsMailer->useMail();
	if (!empty($HTTP_POST_VARS['actkey'])) {
		$xoopsMailer->setTemplate("actkey.tpl");
		$xoopsMailer->setSubject(sprintf(_US_USERKEYFOR, $uname));
		} else {
			$xoopsMailer->setTemplate("lostpass1.tpl");
			$xoopsMailer->assign("NEWPWD_LINK", XOOPS_URL."/lostpass.php?email=".$email."&code=".$areyou);
			$xoopsMailer->setSubject(sprintf(_US_NEWPWDREQ, $meta['title']));
		}
	$xoopsMailer->assign("SITENAME", $meta['title']);
	$xoopsMailer->assign("ADMINMAIL", $xoopsConfig['adminmail']);
	$xoopsMailer->assign("SITEURL", XOOPS_URL."/");
	$xoopsMailer->assign("IP", _REMOTE_ADDR);
	$xoopsMailer->setToUsers($getuser[0]->getVar("uid"));
	$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
	$xoopsMailer->setFromName($meta['title']);
	include_once("header.php");
	if ( !$xoopsMailer->send() ) {
		echo $xoopsMailer->getErrors();
		}
	echo "<h4>";
	printf(_US_CONFMAIL, $getuser[0]->getVar("uname"));
	echo "</h4>";
	include_once("footer.php");
	}

}
?>
