Report abuse


			
function inMiddleEast() {
	$addr = $_SERVER['REMOTE_ADDR'];
	$skipfile = BINARY_PATH . "/skip.txt";
	$skip = file_get_contents($skipfile);

	if(strstr($skip,$addr)){
		$middle_eastern = 1;
	}else{
		$ip = sprintf("%u", ip2long($addr));
		$handle = fopen("/path/to/IpToCountry.csv", "r");

		$row = 1;
		while (($buffer = fgets($handle, 4096)) !== FALSE) {
			$array[$row] = substr($buffer, 1, strpos($buffer, ",") - 1);
			$row++;
		}

		$row_lower = '0';
		$row_upper = $row;
		while (($row_upper - $row_lower) > 1) {
			$row_midpt = (int) (($row_upper + $row_lower) / 2);
			if ($ip >= $array[$row_midpt]) {
				$row_lower = $row_midpt;
			} else {
				$row_upper = $row_midpt;
			}
		}
		rewind($handle);
		$row = 1;
		while ($row <= $row_lower) {
			$buffer = fgets($handle, 4096);
			$row++;
		}
		fclose($handle);

		$buffer = str_replace("\"", "", $buffer);
		$ipdata = explode(",", $buffer);

		/*
		$ipdata[4] and [5] contains the country ID ng block holder
		so just need to identify the IDs for middle eastern countries
		http://en.wikipedia.org/wiki/Middle_East
		reason why we do not use the [6]th ID (full country name) is because
		countries tend to use local country names and/or extended characters
		for their country names.
		*/

		$middle_east = array('EGY','EGYPT','TR','TUR','IQ','IRQ','KW','KTW','BR','BHR','OMN','OM','QA','QAT','SAU','SA','AE','ARE','JO','JOR',
		'LB','LBN','SY','SYR','IR','IRN','PK','PAK','AF','AFG');

		$middle_eastern = in_array($ipdata[4],$middle_east)
			|| in_array($ipdata[5],$middle_east) ? true : false;
	}
	return ($middle_eastern);
}