Extended Statistics Mod?

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
dcigary
Regular
Posts: 6
Joined: Sat Sep 23, 2006 3:17 am

Extended Statistics Mod?

Post by dcigary »

How difficult would it be to mod the Extended Statistics plug-in to decode (to the best of ability) the location where the IP address is coming from? For example, using the http://hostip.info site:

Code: Select all

http://api.hostip.info/get_html.php?ip=68.3.50.93
returns

Code: Select all

Country: UNITED STATES (US)
City: Phoenix, AZ
I'm by no means a PHP programmer, but I've tinkered and modified Gallery in the past for my purposes. However, in this case, I'm stuck on how I could capture the output from the URL and put it into the output being generated by the script.

We're running Serendipity to follow our pregnancy, and it's interesting to see who's coming in and hitting the site. I know I can't tell exactly who it is, but usually we can figure out by the ISP info on the hostname and knowing what service some family members are on.

Anyhow, any help would be greatly appreciated!

Thanks,

--Gary
azel
Regular
Posts: 265
Joined: Thu Apr 21, 2005 4:28 am
Contact:

Post by azel »

I had a look at my installation of Mint (which collects statistics) at one of the plugins called Geomint (which is a free plugin) that does just what you're looking for using hostip.info:

Code: Select all

 /**************************************************************************
   queryHostIP($ip_adr)
    This is the function which queries HostIP.info and stores the data in our
    local db
  **************************************************************************/
  function queryHostIP($ip_adr) {
    $ip_adr_ip = long2ip($ip_adr);
    $tbl_Prefix = $this->Mint->db['tblPrefix'];
    
    $useCURL = in_array('curl', get_loaded_extensions());
    
    $host     = 'api.hostip.info';
    $gateway  = '/get_html.php';
    $post     =  'ip='.$ip_adr_ip.'&position=true';
    $data = '';
        
    if ($useCURL) {
      //Use cURL
      $handle	 = curl_init();
      curl_setopt($handle, CURLOPT_URL, "http://{$host}{$gateway}");
      curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 4);
      curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
      
      curl_setopt($handle, CURLOPT_POST, true);
      curl_setopt($handle, CURLOPT_POSTFIELDS, $post);
      
      $data = curl_exec($handle);
      if (curl_errno($handle))
      {
        $error = 'Could not connect to Gateway (using cURL): '.curl_error($handle);
      }
      curl_close($handle);
    }
    else
    {
      //Use fopen
      $handle = fopen("http://{$host}{$gateway}?{$post}", 'rb');  // open connection to hostip.info
      
      while (!feof($handle)) {
        $data .= fread($handle, 8192);      // getting the data
      }
      fclose($handle);
    }
        
            
    global $country, $country_abrv, $city, $lat, $long;     // defining global vars. BETTER SOLUTION?

        
    $pos = strpos($data, "City:");
    $tmp_string = substr($data, 0, $pos);
    $tmp_string = trim($tmp_string);
    $tmp_string = substr($tmp_string, 9, strlen($tmp_string));
        
    $country = substr($tmp_string, 0, strpos($tmp_string, "("));
    $country = mysql_escape_string($country);
        
    $country_abrv = substr($tmp_string, strpos($tmp_string, "("), strlen($tmp_string));
    $country_abrv = trim($country_abrv, "()");
        
    $tmp_string = substr($data, $pos, strpos($data, "Latitude:") - $pos);
    $city = substr($tmp_string, 5, strlen($tmp_string));
    $city = trim($city);
    $city = mysql_escape_string($city);
        
    $tmp_string = substr($data, strpos($data, "Latitude:"), strlen($data));
    $lat = substr($tmp_string, 9, strpos($tmp_string, "Longitude:") - 9);
    $lat = trim($lat);
        
    $tmp_string = substr($tmp_string, strpos($tmp_string, "Longitude:"), strlen($tmp_string));
    $long = substr($tmp_string, 10, strlen($tmp_string));
    $long = trim($long);
        
    if ($lat == NULL) {                                     // if no geodata is present, set to zero. MAYBE NULL?
      $lat = 0;
    }
        
    if ($long == NULL) {                                    // same here
      $long = 0;
    }

    $query_hostip = "INSERT INTO {$tbl_Prefix}geo (ip, country, country_abrv, city, latitude, longitude)
                     VALUES ('$ip_adr', '$country', '$country_abrv', '$city', $lat, $long)"; // storing data in db
        
    $result_hostip = $this->query($query_hostip);        // do the query, baby!
    return;
  }
dcigary
Regular
Posts: 6
Joined: Sat Sep 23, 2006 3:17 am

Post by dcigary »

Thanks, azel, I'll give it a try!
Post Reply