<?php
	session_start();
	if(isset($_POST['sah'])) {
		$userID = $_SESSION['marry_userID'];
		$userPW = $_SESSION['marry_userPW'];
		$dbp = "userDB/";
		$filename = $dbp . $userID . ".txt";
		$sah = $_POST['sah'];
		
		if($sah == "sah") {
			//read userfile
			$file = fopen($filename,"r");
				$readed = fread($file,filesize($filename));
				fclose($file);
			$rearray = explode("\r\n",$readed);
			$with = $rearray[5];
			$target_file = $dbp . $with . ".txt";
			
			if($rearray[2] != "married") {
				//read targetfile
				$file1 = fopen($target_file,"r");
					$readed1 = fread($file1,filesize($target_file));
					fclose($file1);
				$rearray_target = explode("\r\n",$readed1);
				
				if ($rearray[1] != $rearray_target[1]) {	
					//do process
					$content =
						$rearray[0] . "\r\n" .
						$rearray[1] . "\r\n" .
						"married\r\n" .
						$with . "\r\n" .
						"\r\n" .
						"\r\n" .
						"\r\n"
						;
							
					$opn = fopen($filename,"w");
					$write = $content;
					fputs($opn,$write);
					fclose($opn);
							
					$content_target =
						$rearray_target[0] . "\r\n" .
						$rearray_target[1] . "\r\n" .
						"married\r\n" .
						$userID . "\r\n" .
						"\r\n" .
						"\r\n" .
						"\r\n"
						;
							
					$opn1 = fopen($target_file,"w");
					$write1 = $content_target;
					fputs($opn1,$write1);
					fclose($opn1);
					
					echo "Congratulation on married";
				}
				else {
					echo "You are trying to accept equal gender marry not allowed.";
				}
			}
			else {
				echo "cannot accept marry twice";
			}
		}
		else {
			echo "intruder reject";
		}
	}
	else {
		echo "intruder reject";
	}
?>

<meta name="viewport" content="width=device-width, initial-scale=1.0">