Location: PHPKode > scripts > GG-Tracker GPS and GSM Tracking > gg-tracker-gps-and-gsm-tracking/celldb_org.php
<?php

//**********************************************************************************************
//***** Extension made for integration with CellDB.org                                    ******
//***** Please check http://www.celldb.org/aboutapi.php for more information              ******
//*****                                                                                   ******
//***** Extended $status information                                                      ******
//***** B = Cell contributed to CellDB.org                                                ******
//***** R = Resolved through CellDB.org                                                   ******
//***** T = Resolved through CellDB.org and tower was found                               ******
//***** F = Lookup through CellDB.org failed                                              ******
//**********************************************************************************************

//Test variables go here
//include ("config.php");
//include ("config_check.php");
//$status = "Z";
//$mcc = 204;
//$mnc = 20;
//$lac = 30703;
//$cell = 40053;
//$lat = 51.9253;
//$lon = 4.36152;
//$timestamp = time(); 

/* If the internal MySQL database can't give a clue about the location we
   can always to try find the location by using CellDB.org if we are allowed
   to use that external database 
*/

if ($status == "Z") {
	//Reset status
	$status = "F";
	
	//Write a logfile as well
	$timestamp = gmdate("D, d M Y H:i:s");
	$handle = fopen("celldb_org_read.log", "w");
	fwrite ($handle, "Results from reading at CellDB.org at $timestamp \n\n");
	
	//Build URL for CellDB.org
	$cdurl = "http://celldb.org/api/?method=celldb.getcell";
	$cdurl .= "&username=".$cduser;
	$cdurl .= "&hash=".$cdhash;
	$cdurl .= "&mcc=".$mcc;
	$cdurl .= "&mnc=".$mnc;	
	$cdurl .= "&lac=".$lac;
	$cdurl .= "&cellid=".$cell;
	
	//Read information from CellDB.org database, errors are handled at that site.
	$xml = simplexml_load_file ($cdurl);
	fwrite ($handle, "URL used to get data:\n".$cdurl."\n\n");
	fwrite ($handle, "Received data:\n");
	
	//Clean all variables properly
	$tower = False;
	$received = False;
	
	//Check all cells received from CellDB.org
	foreach ($xml -> cell as $xcell) {
		
		//Assign to proper variables, built array and log
		$curr_lat = floatval($xcell -> latitude);
		$curr_lon = floatval($xcell -> longitude);
		$curr_type = $xcell -> type;
		$all_lat[] = $curr_lat;
		$all_lon[] = $curr_lon;
		fwrite ($handle, "Latitude: ".$curr_lat." | Longitude: ".$curr_lon." | Type: ".$curr_type."\n");
								
		//Check for towers
		if ($curr_type == "tower") {
			$lat_t = $lat_t + $curr_lat;
			$lon_t = $lon_t + $curr_lon;
			$tower = True;
			$tower_count++;
		}
		//Check for recieved cells
		if ($curr_type == "received") {
			$lat_r = $lat_r + $curr_lat;
			$lon_r = $lon_r + $curr_lon;
			$received = True;
			$received_count++;			
		}
	}
	
	//Check data quality from CellDB.org using variance
	//http://en.wikipedia.org/wiki/Variance
	$lat_var = variance ($all_lat);
	$lon_var = variance ($all_lon);
	
	//If variance is okay (0.2 is just a figure, but it needs to small for high quality data), 
	//accept cell and change status
	
	//WARNING: DATA IS SWAPPED LAT <=> LON DUE TO POSSIBLE BUG IN CELLDB.ORG!!!
	if (($lat_var<0.2) && ($lon_var<0.2)) {
		if ($tower == True) {
			$lon = round($lat_t / $tower_count,5);
			$lat = round($lon_t / $tower_count,4);
			$status = "T";
			$nwname = "CellDB";
		}
		if ((!$tower) && ($received)) {
			$lon = round($lat_r / $received_count,5);		
			$lat = round($lon_r / $received_count,4);
			$status = "R";
			$nwname = "CellDB";
		}
	}			
	else {
		//Reset the data to zero values
		unset ($lat, $lon);
		$speed = 0;
		$course = 0;
		$time = "";
		$date = "";
	}	
	
	//Write rest of log file
	fwrite ($handle, "\nCalculated data:\n");
	fwrite ($handle, "Mean Latitude: ".$lat."\n");	
	fwrite ($handle, "Mean Longitude: ".$lon."\n");
	fwrite ($handle, "Variance Latitude: ".$lat_var."\n");
	fwrite ($handle, "Variance Longitude: ".$lon_var."\n");
	fwrite ($handle, "Cellcount: ".($tower_count+$received_count)."\n");	
	fwrite ($handle, "Status: ".$status."\n");
	fclose ($handle);
}

/* If a GPS is connected and we are allowed to store data in the central CellDB.org
   database then we will contribute. Sharing is what it's all about :)
*/

if ($status == "A") {
	//Build URL for CellDB.org
	//WARNING: DATA IS SWAPPED LAT <=> LON DUE TO POSSIBLE BUG IN CELLDB.ORG!!!	
	$cdurl = "http://celldb.org/api/?method=celldb.addcell";
	$cdurl .= "&username=".$cduser;
	$cdurl .= "&hash=".$cdhash;
	$cdurl .= "&mcc=".$mcc;
	$cdurl .= "&mnc=".$mnc;	
	$cdurl .= "&lac=".$lac;
	$cdurl .= "&cellid=".$cell;
	$cdurl .= "&latitude=".$lon;
	$cdurl .= "&longitude=".$lat;
	$cdurl .= "&timestamp=".$timestamp;
	$cdurl .= "&type=received&amp;signalstrength=0&amp;enduserid=ggtracker";
	
	//Write information into CellDB.org database, errors are handled at that site.
	$lines = file ($cdurl);
	
	//Store result from CellDB.org in a log file
	$timestamp = gmdate("D, d M Y H:i:s");
	$handle = fopen("celldb_org_write.log", "w");
	fwrite ($handle, "Results from writing to CellDB.org at $timestamp \n\n");
	foreach ($lines as $line_num => $line) {
    	fwrite($handle, $line);
	}	
	fclose ($handle);
	
	//Change status because we contributed
	$status = "B";
}

?>
Return current item: GG-Tracker GPS and GSM Tracking