Location: PHPKode > projects > Ads Exchange Server > estigi_0.1.1_RC2/ips/iptools.php
<?php

//This PHP contribution is written by Luc Balemans <hide@address.com> with really smal modifications by me

//This connects you to the Mysql database
function db_Connect($db_Specs){

	$bdConn = mysql_connect($db_Specs['host'], $db_Specs['user'], $db_Specs['psw'])
	or die("Could not connect : " . mysql_error());

	mysql_select_db($db_Specs['db_Name']) or die("Could not select database");

	return $bdConn;
}

//This reads the file that comes from WebNet 777 and creats a new one that we can use
//This PHP contribution is written by Luc Balemans <hide@address.com>. Modified a bit by me
function create_File($file_Name, $new_File){

	$handle1 = gzopen($file_Name, 'r');

	if($handle1){

		if(file_exists($new_File)){
			unlink($new_File);
		}

		$handle2 = fopen($new_File,"w");

		chmod($new_File, 0777);

		$tofind = array("\"", "\n");
		$toreplace   = array("", "");

		while (!gzeof($handle1)) {
		  $buffer = gzgets($handle1, 4096);
		  $pos = strpos($buffer, '#');
		  if ($pos === false) {
		    $buffer = str_replace($tofind, $toreplace, $buffer);
		    if ($buffer != "") {
		      $data = explode(",", $buffer);
		      $line = "\"" . $data[0] . "\",\"" . $data[1] . "\",\"" . $data[4] . "\",\"" . $data[2] . "\"\r\n";
		      fwrite($handle2, $line);
		      }
		    }
		}
		return 0;
	}
		else{
			
			return 1;
		}
}

//This PHP contribution is written by Luc Balemans <hide@address.com>. Modified a bit by me
function load_Into_DB($db_Specs, $file_Name){

	if(file_exists($file_Name)){

		$bdConn = db_Connect($db_Specs);

		//Empty database
		$q = "TRUNCATE TABLE ".$db_Specs['ips_Name']."";
		$q_Empty_Database = mysql_query($q) or die("Unable to Empty Database: " . mysql_error());

		$q = "LOAD DATA INFILE '".$file_Name."' INTO TABLE ".$db_Specs['ips_Name']." FIELDS TERMINATED BY ',' ENCLOSED BY '" . "\"' LINES TERMINATED BY '\\r\\n'"; //'\\r\\n'";

		$q_Upload_Data = mysql_query($q) or die("Unable to Upload Data: " . mysql_error());

		$affected = mysql_affected_rows();

		//Close the connection and exit
		mysql_close($bdConn);

		return $affected;
	}
		else{
			return -1;
		}
}

//This is used get ip specs, you can include this entire file or just copy paste this function where ever you need to use it. Just remember the credits ;-)
//This is basically all you will be calling from your application, the rest is used to upload the ips database
function get_Ip_Info($ip,
							$db_Specs,
							$connect = 0 //If you are already connected, leave this in 0, otherwise call it with 1
							){

	if($connect != 0){
		$bdConn = db_Connect($db_Specs);
	}

	//Convert ip address
	$new_Ip = explode(".", $ip);
	$decimal =  $new_Ip[3];
	$decimal += $new_Ip[2] * 256;
	$decimal += $new_Ip[1] * 256 * 256;
	$decimal += $new_Ip[0] * 256 * 256 * 256;
	// 1.2.3.4 = 4 + (3 * 256) + (2 * 256 * 256) + (1 * 256 * 256 * 256)
	// #  is 4 + 768 + 13,1072 + 16,777,216 = 16,909,060

	$q = "
		SELECT ".$db_Specs['ips_Name'].".*,
				 ".$db_Specs['countries_Name'].".country, ".$db_Specs['countries_Name'].".cntry
		FROM ".$db_Specs['ips_Name']."
		INNER JOIN ".$db_Specs['countries_Name']." ON ".$db_Specs['countries_Name'].".ctry = ".$db_Specs['ips_Name'].".ctry
		WHERE ip_From <= ".$decimal."	AND ip_To >= ".$decimal."
		";

	$q_Ip_Data = mysql_query($q) or die("Unable to Get Data: " . mysql_error());

	if(mysql_affected_rows() > 0){
		$row = mysql_fetch_array($q_Ip_Data, MYSQL_ASSOC);
			$ip_Data = array(
					"lower_Bound" => $row['ip_From'],
					"upper_Bound" => $row['ip_To'],
					"ctry"        => $row['ctry'],
					"registry"    => $row['registry'],
					"cntry"       => $row['cntry'],
					"country"     => $row['country'],
					"decimal"     => $decimal
			);
	}
		else{
			$ip_Data = array(
					"lower_Bound" => 0,
					"upper_Bound" => 0,
					"ctry"        => 0,
					"registry"    => 0,
					"cntry"       => 0,
					"country"     => 0,
					"decimal"     => 0
			);
		}

	//Close the connection and exit
	if($connect != 0){
		mysql_close($bdConn);
	}

	return $ip_Data;
}

?>
Return current item: Ads Exchange Server