Browse Source

Second rework of the GeoIP code, now supporting cities!

pull/40/head
Jano Slota 10 years ago
committed by czaks
parent
commit
2488e77e86
  1. 3
      inc/config.php
  2. 20
      inc/functions.php
  3. BIN
      inc/lib/geoip/GeoIPv6.dat
  4. BIN
      inc/lib/geoip/GeoLiteCityv6.dat
  5. 171
      inc/lib/geoip/geoipcity.inc
  6. 4630
      inc/lib/geoip/geoipregionvars.php
  7. 2235
      inc/lib/geoip/timezone.php

3
inc/config.php

@ -776,10 +776,11 @@
// The default name (e.g. Anonymous). Can be an array - in that case it's picked randomly from the array.
// Example: $config['anonymous'] = array('Bernd', 'Senpai', 'Jonne', 'ChanPro');
$config['anonymous'] = 'Anonymous';
// Pick the default name based on the poster's country. Can contain subarrays and is picked randomly from the them.
// Pick the default name based on the poster's country or city. Can contain subarrays and is picked randomly from them.
// If the user posts from a country you have not set a name for, his name is going to be whatever you set in $config['anonymous'].
// Example 1: $config['country_anonymous'] = array('cz' => 'Pepik', 'pl' => 'Karol Wojtyla', 'de' => 'Bernd', 'fi' => 'Jonne');
// Example 2: $config['country_anonymous'] = array('cz' => array('Pepik', 'Bohus', 'Lada'), 'pl' => array('Karol Wojtyla', 'Demon Pedofyl');
// Example 3: $config['country_anonymous'] = array('cz' => array('Prague' => 'Pepik', 'Ostrava' => array('Bohus', 'Lada')), 'pl' => array('Karol Wojtyla', 'Demon Pedofyl'));
$config['country_anonymous'] = false;
// Number of reports you can create at once.

20
inc/functions.php

@ -246,15 +246,23 @@ function loadConfig() {
$config['anonymous'] = $config['anonymous'][array_rand($config['anonymous'])];
if ($config['country_flags'] || $config['country_anonymous']) {
require_once 'inc/lib/geoip/geoip.inc';
$gi=geoip_open('inc/lib/geoip/GeoIPv6.dat', GEOIP_STANDARD);
require_once 'inc/lib/geoip/geoipcity.inc';
require_once 'inc/lib/geoip/geoipregionvars.php';
$gi=geoip_open('inc/lib/geoip/GeoLiteCityv6.dat', GEOIP_STANDARD);
$ipaddy = $_SERVER['REMOTE_ADDR'];
if (IP::isIPv4($ipaddy))
$ipaddy = IP::to_ipv6($ipaddy, true);
global $country_code, $country_name;
$country_code = strtolower(geoip_country_code_by_addr_v6($gi, $ipaddy));
$country_name = geoip_country_name_by_addr_v6($gi, $ipaddy);
if (isset($config['country_anonymous'][$country_code])) {
$record = geoip_record_by_addr_v6($gi, $ipaddy);
global $country_code, $country_name, $city;
$country_code = strtolower($record->country_code);
$country_name = $record->country_name;
$city = $record->city;
if (isset($config['country_anonymous'][$country_code][$city]))
if (is_array($config['country_anonymous'][$country_code][$city]))
$config['anonymous'] = $config['country_anonymous'][$country_code][$city][array_rand($config['country_anonymous'][$country_code][$city])];
else
$config['anonymous'] = $config['country_anonymous'][$country_code][$city];
else if (isset($config['country_anonymous'][$country_code])) {
if (is_array($config['country_anonymous'][$country_code]))
$config['anonymous'] = $config['country_anonymous'][$country_code][array_rand($config['country_anonymous'][$country_code])];
else

BIN
inc/lib/geoip/GeoIPv6.dat

Binary file not shown.

BIN
inc/lib/geoip/GeoLiteCityv6.dat

Binary file not shown.

171
inc/lib/geoip/geoipcity.inc

@ -0,0 +1,171 @@
<?php
/* geoipcity.inc
*
* Copyright (C) 2013 MaxMind, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
define("FULL_RECORD_LENGTH", 50);
require_once 'geoip.inc';
require_once 'geoipregionvars.php';
class geoiprecord
{
public $country_code;
public $country_code3;
public $country_name;
public $region;
public $city;
public $postal_code;
public $latitude;
public $longitude;
public $area_code;
public $dma_code; # metro and dma code are the same. use metro_code
public $metro_code;
public $continent_code;
}
function _get_record_v6($gi, $ipnum)
{
$seek_country = _geoip_seek_country_v6($gi, $ipnum);
if ($seek_country == $gi->databaseSegments) {
return null;
}
return _common_get_record($gi, $seek_country);
}
function _common_get_record($gi, $seek_country)
{
// workaround php's broken substr, strpos, etc handling with
// mbstring.func_overload and mbstring.internal_encoding
$mbExists = extension_loaded('mbstring');
if ($mbExists) {
$enc = mb_internal_encoding();
mb_internal_encoding('ISO-8859-1');
}
$record_pointer = $seek_country + (2 * $gi->record_length - 1) * $gi->databaseSegments;
if ($gi->flags & GEOIP_MEMORY_CACHE) {
$record_buf = substr($gi->memory_buffer, $record_pointer, FULL_RECORD_LENGTH);
} elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
$record_buf = @shmop_read($gi->shmid, $record_pointer, FULL_RECORD_LENGTH);
} else {
fseek($gi->filehandle, $record_pointer, SEEK_SET);
$record_buf = fread($gi->filehandle, FULL_RECORD_LENGTH);
}
$record = new geoiprecord;
$record_buf_pos = 0;
$char = ord(substr($record_buf, $record_buf_pos, 1));
$record->country_code = $gi->GEOIP_COUNTRY_CODES[$char];
$record->country_code3 = $gi->GEOIP_COUNTRY_CODES3[$char];
$record->country_name = $gi->GEOIP_COUNTRY_NAMES[$char];
$record->continent_code = $gi->GEOIP_CONTINENT_CODES[$char];
$record_buf_pos++;
$str_length = 0;
// Get region
$char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
while ($char != 0) {
$str_length++;
$char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
}
if ($str_length > 0) {
$record->region = substr($record_buf, $record_buf_pos, $str_length);
}
$record_buf_pos += $str_length + 1;
$str_length = 0;
// Get city
$char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
while ($char != 0) {
$str_length++;
$char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
}
if ($str_length > 0) {
$record->city = substr($record_buf, $record_buf_pos, $str_length);
}
$record_buf_pos += $str_length + 1;
$str_length = 0;
// Get postal code
$char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
while ($char != 0) {
$str_length++;
$char = ord(substr($record_buf, $record_buf_pos + $str_length, 1));
}
if ($str_length > 0) {
$record->postal_code = substr($record_buf, $record_buf_pos, $str_length);
}
$record_buf_pos += $str_length + 1;
$str_length = 0;
// Get latitude and longitude
$latitude = 0;
$longitude = 0;
for ($j = 0; $j < 3; ++$j) {
$char = ord(substr($record_buf, $record_buf_pos++, 1));
$latitude += ($char << ($j * 8));
}
$record->latitude = ($latitude / 10000) - 180;
for ($j = 0; $j < 3; ++$j) {
$char = ord(substr($record_buf, $record_buf_pos++, 1));
$longitude += ($char << ($j * 8));
}
$record->longitude = ($longitude / 10000) - 180;
if (GEOIP_CITY_EDITION_REV1 == $gi->databaseType) {
$metroarea_combo = 0;
if ($record->country_code == "US") {
for ($j = 0; $j < 3; ++$j) {
$char = ord(substr($record_buf, $record_buf_pos++, 1));
$metroarea_combo += ($char << ($j * 8));
}
$record->metro_code = $record->dma_code = floor($metroarea_combo / 1000);
$record->area_code = $metroarea_combo % 1000;
}
}
if ($mbExists) {
mb_internal_encoding($enc);
}
return $record;
}
function GeoIP_record_by_addr_v6($gi, $addr)
{
if ($addr == null) {
return 0;
}
$ipnum = inet_pton($addr);
return _get_record_v6($gi, $ipnum);
}
function _get_record($gi, $ipnum)
{
$seek_country = _geoip_seek_country($gi, $ipnum);
if ($seek_country == $gi->databaseSegments) {
return null;
}
return _common_get_record($gi, $seek_country);
}
function GeoIP_record_by_addr($gi, $addr)
{
if ($addr == null) {
return 0;
}
$ipnum = ip2long($addr);
return _get_record($gi, $ipnum);
}

4630
inc/lib/geoip/geoipregionvars.php

File diff suppressed because it is too large

2235
inc/lib/geoip/timezone.php

File diff suppressed because it is too large
Loading…
Cancel
Save