<?php
	session_start();
	error_reporting(0);
	
	$target = $_POST['tID'];
	$userID = $_SESSION['marry_userID'];
	$userPW = $_SESSION['marry_userPW'];
	$dbp = "userDB/";
	$filename = $dbp . $userID . ".txt";
	$target_file = $dbp . $target . ".txt";
	
	//read userfile
	$file = fopen($filename,"r");
		$readed = fread($file,filesize($filename));
		fclose($file);
	$rearray = explode("\r\n",$readed);
	
	if(password_verify($userPW, $rearray[0])) {
		if(file_exists($dbp . $target . ".txt")) {
			//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]) {
				if($rearray[6] != "requesting") {
					if($rearray_target[4] != "requested") {
						//do process
						$content =
							$rearray[0] . "\r\n" .
							$rearray[1] . "\r\n" .
							$rearray[2] . "\r\n" .
							$rearray[3] . "\r\n" .
							$rearray[4] . "\r\n" .
							$rearray[5] . "\r\n" .
							"requesting\r\n" .
							$target;
						
						$opn = fopen($filename,"w");
						$write = $content;
						fputs($opn,$write);
						fclose($opn);
						
						$content_target =
							$rearray_target[0] . "\r\n" .
							$rearray_target[1] . "\r\n" .
							$rearray_target[2] . "\r\n" .
							$rearray_target[3] . "\r\n" .
							"requested\r\n" .
							$userID . "\r\n" .
							$rearray_target[6] . "\r\n" .
							$rearray_target[7];
						
						$opn1 = fopen($target_file,"w");
						$write1 = $content_target;
						fputs($opn1,$write1);
						fclose($opn1);
						
						echo "succesfully request!";
					}
					else {
						echo "Target already have a request!";
					}
				}
				else {
					echo "You have requested already! don't forget it!";
				}
			}
			else {
				echo "you may not marry equal gender";
			}
		}
		else {
			echo "such target only in your mind, target dont exist";
		}
	}
	else {
		echo "intruder reject";
	}
?>

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