<?php
	session_start();
	
	$userID = $_SESSION['marry_userID'];
	$userPW = $_SESSION['marry_userPW'];
	$dbp = "userDB/";
	$filename = $dbp . $userID . ".txt";
	$newgender = $_POST['gender'];
	
	$file = fopen($filename,"r");
		$readed = fread($file,filesize($filename));
		fclose($file);
	$rearray = explode("\r\n",$readed);
	$hashf = $rearray[0];
	
	if(password_verify($userPW, $hashf)) {
		if($rearray[2] != "married") {
			if($rearray[1] != $newgender) {
				if($rearray[6] != "requesting") {
					$content = 
						$rearray[0] . "\r\n" .
						$newgender . "\r\n" .
						$rearray[2] . "\r\n" .
						$rearray[3] . "\r\n" .
						$rearray[4] . "\r\n" .
						$rearray[5] . "\r\n" .
						$rearray[6] . "\r\n" .
						$rearray[7];
			
					$opn = fopen($filename,"w");
					$write = $content;
					fputs($opn,$write);
					fclose($opn);
					
					echo "change gender success!";
				}
				else {
					echo "You may cancel your request before change gender! otherwise you will marry equal gender not allowed";
				}
			}
			else {
				echo "You already have that gender!";
			}
		}
		else {
			echo "Please divorce before gender change otherwise you marry equal gender not allowed.";
		}
	}
	else {
		header("Location: wrong-pass.html");
	}
?>