<?php
// ======================================================================== //
//                                                                          //
//   KooBI- Webportalsystem                                                 //
//   SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS                     //
//                                                                          //
//   ====================================================================   //
//                                                                          //   
//   dream4 | webservices - Am Michelsgrund 7 - 69469 Weinheim - Germany    //    
//   tel. +49(0) 6201 - 980442 / fax. +49(0) 6201 - 64809                   //
//                                                                          //
// ======================================================================== //
$sname = simpletitle($lang['pwlost']);
if(isset($_REQUEST['code']) && $_REQUEST['code']!=""){
	$ok = 1;
	if(strlen($_REQUEST['code']) <> 6){
		$errorcode = "error_wrongcode";
		$ok = 0;
	}
	if($ok == 1){
		$sql =& new dbc("SELECT passtemp,uid FROM ".PREFIX."_user WHERE passtemp='".md5($_REQUEST['code'])."'");
		$num = $sql->numrows();
		$row = $sql->fetchrow();
		if($num < 1){
			$errorcode = "error_wrongcode";
			$ok = 0;
		}
	}
	
	if($ok == 0){
		$EOUT = msg("error_once", $errorcode , str_replace("__URL__", "index.php", $lang['redirect']), "index.php");
		$NOOUT = 1;
	}else{
		$sql =& new dbc("UPDATE ".PREFIX."_user SET pass='".$row->passtemp."' WHERE passtemp='".md5($_REQUEST['code'])."'");
		setcookie("kuid", $row->uid, time()+(365*24*3600));
		setcookie("kpass",$row->passtemp,time()+(365*24*3600));
		$EOUT = msg("thankyou", "pwchangeok" , str_replace("__URL__", "index.php", $lang['redirect']), "index.php");
		$NOOUT = 1;
	}
	$sname = $lang['pwlost'];
	
	} else {
	
	$tmpl->assign('msg', $lang['pwlost_t']);
	$tmpl->assign('t_email', $lang['t_email']);
	$tmpl->assign('pwlostbutt', $lang['pwlostbutt']);
	
	
	$ok = 1;
	if($_REQUEST['send']==1){
		if(!empty($_POST['email'])){
			$tmpl->assign('email', iform($_POST['email'],"65",""));
		}
		
		$sql =& new dbc("SELECT email,uname FROM ".PREFIX."_user WHERE email='".iform($_POST['email'],"65","")."'");
		$num = $sql->numrows();
		$row = $sql->fetchrow();
		if($num < 1){
			$error .= emsg("pwlostwrongmail");
			$ok = 0;
		}
		
		if($ok==0){
			$tmpl->assign('title_error', $lang['error']);
			$tmpl->assign('iserror', 1);
			$tmpl->assign('error', $error);
		}
		
		
		// MAIL SENDEN UND TEMPORÄR IN DATENBANK SPEICHERN
		if($ok==1){
			$pass = makepass();
			
			$pass_temp = md5($pass);
			$message .= str_replace("__USER__", $row->uname ,$lang['pwlost_emailtext']);
			$message .= "\n\n";
			$message .= $lang['pwlost_emailtext2']."\n\n";
			$message .= $lang['pwlost_emailsubject2']." ".$pass."\n\n";
			$message .= "http://" . $_SERVER['HTTP_HOST'].str_replace("/index.php","",$_SERVER['PHP_SELF'])."/index.php?p=pwlost&code=".$pass."";
			
			$body = str_replace("\n","\n\n", $message);
			
			@mail($row->email, $lang['pwlost_emailsubject'], stripslashes($body),
			"From: $pref[sendemail]\r\n"
			."Reply-To: $pref[sendemail]\r\n"
			."X-Mailer: PHP/" . phpversion());
			
			$sql = new dbc("UPDATE ".PREFIX."_user SET passtemp='".$pass_temp."' WHERE email='".iform($_POST['email'],"65","")."'");
			
			$EOUT = msg("pwlost", "pwlost_okt", str_replace("__URL__", "index.php", $lang['redirect']), "index.php");
			$NOOUT = 1;
			$sname = $lang['pwlost_okt'];
		}
	}
	
	$tmpl->assign("content", parsetrue("container/".container("changepw"), $lang['pwlost'], $tmpl->fetch("user/pwlost.tpl")));
	
}
?>