<?php
ini_set('session.cookie_domain', '.wowdoyou.com');
session_start();
include("includes/site_info.php");
include_once('lib/lib.Functions.php');
include_once __INCLUDE_CLASS_PATH."/class.ItemManager.php";
$option_manager = new OptionManager();
$mail=$option_manager->GetValue('suppmail');

	switch ($_REQUEST["Submit"]) {
	case "FrmReset"; 
		FrmReset();
		break;
	case "FrmNewPass"; 
		FrmNewPass();
		break;
	default; 
		echo "";
		break;
}

 function FrmReset() {
 global $CONST_URL, $link, $CONST_LINK_ROOT, $mail, $CONST_COMPANY;

 $error = 0;
 $vcode=$_REQUEST['vcode'];
 $resetMail=$_REQUEST['resetMail'];
 
 $return['vcodeo']=$vcode;
 $return['vcode']=md5($vcode);
 $return['resetMail']=$resetMail;
 
 if (md5($vcode)!=$_SESSION['image_random_value']) {
 $error = 1;
 $return = array_merge_recursive($return,  array( 'error' => "Invalid Verification Code"));
 }
 
$query="SELECT mem_username, mem_userid, mem_email FROM members WHERE (mem_email = '$resetMail' or mem_username = '$resetMail')";
$result=mysql_query($query,$link) or die(mysql_error());
$retval=mysql_num_rows($result);

if ($retval < 1) {
 $error = 1;
 $return = array_merge_recursive($return,  array( 'error' => "Invalid Username or Email Address"));
} 

if ($error == 0) {

$chars = explode(',','a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9'); 
$random=''; 
for($i=0; $i<8;$i++)  { 
  $random.=$chars[rand(0,count($chars)-1)]; 
} 

	$sql_array = mysql_fetch_object($result);

$pass=md5($random);
$rsQue=mysql_query("REPLACE into password_reset set mem_confirm='$pass', mem_email='$sql_array->mem_email', mem_userid='$sql_array->mem_userid'");

	$confirm_url = "$CONST_LINK_ROOT/reset.php?email=".urlencode($sql_array->mem_email)."&id=".$random;
	$data['Url'] = $CONST_URL;
    $data['Username'] = $sql_array->mem_username;
	$data['Link'] = $confirm_url;
    $data['SupportEmail'] = $mail;
    $data['CompanyName'] = $CONST_COMPANY;
	$m_template = 'resend_passconfirm';
    list($type,$message) = getTemplateByName($m_template,$data,getDefaultLanguage($sql_array->mem_userid));
	//$return ['message'] = $message;
	$return ['message'] = send_mail ("$sql_array->mem_email", "Wauhaha <noreply@wauhaha.com>", "Wauhaha Password Reset Confirmation", "$message",$type,"ON");
}

 echo json_encode($return);
 exit;
}

 function FrmNewPass() {
 global $CONST_URL, $link, $CONST_LINK_ROOT, $mail, $CONST_COMPANY;

 $error = 0;
 $npass=$_REQUEST['npass'];
 $cpass=$_REQUEST['cpass'];
 $resetMail=$_REQUEST['resetMail'];
 $reg_confirm_id=$_REQUEST['reg_confirm_id'];
 
 $return['npass']=$npass;
 $return['cpass']=$cpass;
 $return['resetMail']=$resetMail;
 $return['reg_confirm_id']=$reg_confirm_id;
 
 if ($npass!=$cpass) {
 $error = 1;
 $return = array_merge_recursive($return,  array( 'error' => "Invalid Confirm Password"));
 }

$query_pass="SELECT mem_userid FROM password_reset WHERE mem_email = '$resetMail' and mem_confirm ='".md5($reg_confirm_id)."'";
$result_pass=mysql_query($query_pass,$link) or die(mysql_error());
$retval_pass=mysql_num_rows($result_pass);

if ($retval_pass < 1) {
 $error = 1;
 $return = array_merge_recursive($return,  array( 'error' => "Invalid Verification Code or Email Address"));
} 


$query_mail="SELECT mem_username, mem_userid FROM members WHERE mem_email = '$resetMail'";
$result_mail=mysql_query($query_mail,$link) or die(mysql_error());
$retval_mail=mysql_num_rows($result_mail);

if ($retval_mail < 1) {
 $error = 1;
 $return = array_merge_recursive($return,  array( 'error' => "Invalid Email Address"));
} 

if ($error == 0) {

$sql_array = mysql_fetch_object($result_mail);

$pass=md5($npass);
$rsQue=mysql_query("UPDATE members set mem_password='$pass' where mem_email='$resetMail' and mem_userid='$sql_array->mem_userid'");
$rsQue2=mysql_query("DELETE FROM password_reset WHERE mem_email = '$resetMail' and mem_confirm ='".md5($reg_confirm_id)."'");
if (!$rsQue || !$rsQue2) {
 $error = 1;
 $return = array_merge_recursive($return,  array( 'error' => "Database Error"));
}

    $data['Username'] = $sql_array->mem_username;
    $data['SupportEmail'] = $mail;
    $data['CompanyName'] = $CONST_COMPANY;
	$m_template = 'reset_success';
    list($type,$message) = getTemplateByName($m_template,$data,getDefaultLanguage($sql_array->mem_userid));
	//$return ['message'] = $message;
	$return ['message'] = send_mail ("$resetMail", "Wauhaha <noreply@wauhaha.com>", "Wauhaha Password Change", "$message",$type,"ON");

$return['username']=$sql_array->mem_username;
$return['password']=$npass;
}

 echo json_encode($return);
 exit;
}

?>
