<?php
function iptocountry($ip) {
    $numbers = preg_split( "/\./", $ip);
    include("ip_files/".$numbers[0].".php");
    $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
    foreach($ranges as $key => $value){
        if($key<=$code){
            if($ranges[$key][0]>=$code){$country=$ranges[$key][1];break;}
        }
    }
    return $country;
}

function generateFormToken($form) {
    	$token = md5(uniqid(microtime(), true));
    	$_SESSION[$form.'_token'] = $token;
    	return $token;
}

function stripcleantohtml($s){
// cleaning input for everything which doesn't include any html
	return htmlentities(trim(strip_tags(stripslashes($s))), ENT_NOQUOTES, "UTF-8");
}

//if word from note is in array list
function matchNotes($needles, $noteS)
{
    foreach($needles as $needle){
        if (strpos($noteS, $needle) !== false) {
            return true;
        }
    }
    return false;
}

function count_digit($number) {
  return strlen($number);
}


function verifyFormToken($form) {

    // check if a session is started and a token is transmitted, if not return an error
	if(!isset($_SESSION[$form.'_token'])) {
		return false;
    }

	// check if the form is sent with token in it
	if(!isset($_POST['token'])) {
		return false;
    }

	// compare the tokens against each other if they are still the same
	if ($_SESSION[$form.'_token'] !== $_POST['token']) {
		return false;
    }
	
	return true;
}


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('formlog.log', 'a')) {

			fputs($handle, $logging);  // write the Data to file
			fclose($handle);
            // close the file
            $to = 'ewa@webmastersdesktop.com';
        	$subject = 'Martin the Roofer Form attempted';
        	$header = 'From: websitepba@gmail.com';
        	mail($to, $subject, $logging, $header);
            $url = '../contact.php';
							header("location: $url");
							exit();

		} else {  // if first method is not working, for example because of wrong file permissions, email the data

        	$to = 'ewa@webmastersdesktop.com';
        	$subject = 'Martin the Roofer Form attempted';
        	$header = 'From: websitepba@gmail.com';
        	mail($to, $subject, $logging, $header);
             $url = '../contact.php';
							header("location: $url");
							exit();

	}
}


//not allowed words
$needles = array('urgency','market','marketing','advertise','advertising','advertisement','token','req-name','req-email','typeOfChange','urgency','URL-main','addURLS', 'curText', 'newText', 'save-stuff','(',')','{','}','<','>',"'",'"','.','$','dollar','dollars','dolar','dolars','price');



?>
