<?php
	error_reporting(0);
	//error_reporting(E_ALL);

	$username = $_POST['username'];
	$filename = "userDB/" . $username . ".txt";
	$tpass = $_POST['PW'];
	$tval = $_POST['manyvalue'];
	$target = $_POST['userAccount'];
	$filetarget = "../CShop/userDB/" . $target . ".txt";
	
	$filepw = fopen($filename,"r");
		$readedpw = fread($filepw,filesize($filename));
		fclose($filepw);
	$pwarray = explode("\r\n",$readedpw);
	$hashf = $pwarray[0];
	
	$verify = password_verify($tpass, $hashf);
	
	if($verify == true) {
		if(file_exists($filetarget)) {
			if($tval >= 1) {
				if($pwarray[1] >= $tval) {
					if($pwarray[1] >= 1) {
						//subtract your
						$subt = (int)$pwarray[1] - (int)$tval;
						$content = $pwarray[0] . "\r\n" . $subt . "\r\n" . $_SERVER['REMOTE_ADDR'] . "\r\n" . $target;
					
						$opn = fopen($filename,"w");
						$write = $content;
						fputs($opn,$write);
						fclose($opn);
						
						//add their
						$file1 = fopen($filetarget,"r");
						$readed1 = fread($file1,filesize($filetarget));
						fclose($file1);
						$rearray1 = explode("\r\n",$readed1);
						$b1 = $rearray1[1];
						
						$addn = (int)$b1 + (int)$tval;
						$content1 = $rearray1[0] . "\r\n" . $addn . "\r\n" . $rearray1[2] . "\r\n" . $rearray1[3];
					
						$opn1 = fopen($filetarget,"w");
						$write1 = $content1;
						fputs($opn1,$write1);
						fclose($opn1);
						
						//say done
						echo "CShop TopUp MBC. " . $tval . " Succes! to " . $target;
					}
					else {
					echo "Please refill your money before transfer more";
					}
				}
				else {
					echo "Your money value must be greater than your Trasnfer value";
				}
			}
			else {
				echo "Don't steal their money!";
			}
		}
		else {
			echo "Target don't exist";
		}
	}
	else {
		echo "Password wrong!";
	}
?>