<?php



function writeLog($where) {

	$ip = $_SERVER["REMOTE_ADDR"]; // Get the IP from superglobal
	$host = gethostbyaddr($ip);    // Try to locate the host of the attack
	$date = date("d M Y");
	
	// create a logging message with php heredoc syntax
	$logging = <<<LOG
		\n
		<< Start of Message >>
		There was a hacking attempt on your form. \n 
		Date of Attack: {$date}
		IP-Adress: {$ip} \n
		Host of Attacker: {$host}
		Point of Attack: {$where}
		<< End of Message >>
LOG;
        
        // open log file
		if($handle = fopen('hacklog.log', 'a')) {
		
			fputs($handle, $logging);  // write the Data to file
			fclose($handle);           // close the file
			
		} else {  // if first method is not working, for example because of wrong file permissions, email the data
		
        	$to = 'ADMIN@gmail.com';  
        	$subject = 'HACK ATTEMPT';
        	$header = 'From: ADMIN@gmail.com';
        	if (mail($to, $subject, $logging, $header)) {
        		echo "Sent notice to admin.";
        	}

	}
}




$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$cellnumber = $_POST['cellnumber'];


$firstname = htmlspecialchars($firstname, ENT_QUOTES, 'UTF-8');
$lastname = htmlspecialchars($lastname, ENT_QUOTES, 'UTF-8');
$cellnumber = htmlspecialchars($cellnumber, ENT_QUOTES, 'UTF-8');

//How to write a secure code for mailing with php:

if ( preg_match( "/[\r\n]/", $firstname ) || preg_match( "/[\r\n]/", $lastname ) || preg_match( "/[\r\n]/", $cellnumber )) { 
    //header("location : http://www.example.com/mail-error.php"); 
	
	writeLog('form1');
	echo "HACK DETECTED!!! Your IP has Been reported.";
} else {
	$to = "blakemeister@earthlink.net";
	$subject = "Please add me to you Text Message Notifications";
	$txt = $firstname ." ".$lastname ." - ".$cellnumber;


	mail($to,$subject,$txt);
	
	echo '
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
		<link rel="stylesheet" href="css/vcda.css">';

	echo '<nav>';
	include '../nav.php';
	echo '</nav>';
	include '../header.php';
	echo '<main class="container">
		<h3 id="big-home">thank you, Your submission has been sent.<h3>
		</main>';
	include '../footer.php';
	echo '<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>';
}

?>
















