Skip to content

Commit

Permalink
updating the maxmind GeoIP Module ot work with modern maxmind DB formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Adams committed Apr 2, 2015
1 parent cea602a commit b34582f
Show file tree
Hide file tree
Showing 11 changed files with 1,179 additions and 69 deletions.
2 changes: 1 addition & 1 deletion modules/base/classes/hostip.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function __construct() {
*
* @param string $ip
*/
function get_location($location_map) {
function get_location( $location_map ) {

$city = '';
$state = '';
Expand Down
7 changes: 6 additions & 1 deletion modules/base/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,12 @@ function registerFilters() {


$this->registerFilter('attributed_campaign', $this, 'attributeCampaign', 10);
$this->registerFilter('geolocation', 'hostip', 'get_location', 10, 'classes');

if ( owa_coreAPI::getSetting('base', 'geo_provider') === 'hostip' ) {

$this->registerFilter('geolocation', 'hostip', 'get_location', 10, 'classes');
}


if ( defined( 'OWA_MAIL_EXCEPTIONS' ) ) {

Expand Down
162 changes: 96 additions & 66 deletions modules/maxmind_geoip/classes/maxmind.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,20 @@
// $Id$
//

require_once(OWA_BASE_DIR.'/owa_location.php');
require_once( OWA_BASE_DIR.'/owa_location.php' );

if (!class_exists('PEAR_Exception')) {
set_include_path(get_include_path().':'.OWA_MODULES_DIR.'maxmind_geoip/includes/PEAR-1.9.1/');
}
require_once( OWA_MODULES_DIR . 'maxmind_geoip/includes/MaxMind-DB-Reader-php-1.0.3/src/MaxMind/Db/Reader.php' );
require_once( OWA_MODULES_DIR . 'maxmind_geoip/includes/MaxMind-DB-Reader-php-1.0.3/src/MaxMind/Db/Reader/Decoder.php' );
require_once( OWA_MODULES_DIR . 'maxmind_geoip/includes/MaxMind-DB-Reader-php-1.0.3/src/MaxMind/Db/Reader/InvalidDatabaseException.php' );
require_once( OWA_MODULES_DIR . 'maxmind_geoip/includes/MaxMind-DB-Reader-php-1.0.3/src/MaxMind/Db/Reader/Metadata.php' );
require_once( OWA_MODULES_DIR . 'maxmind_geoip/includes/MaxMind-DB-Reader-php-1.0.3/src/MaxMind/Db/Reader/Util.php' );

define('OWA_MAXMIND_DIR', OWA_MODULES_DIR . 'maxmind_geoip/includes/Net_GeoIP-1.0.0/');

if (!class_exists('Net_GeoIP')) {
require_once(OWA_MAXMIND_DIR.'Net/GeoIP.php');
}
use MaxMind\Db\Reader;

set_include_path(
get_include_path().':'.
OWA_MODULES_DIR.'maxmind_geoip/includes/Net_GeoIP-1.0.0/'
);

require_once(OWA_MODULES_DIR . 'maxmind_geoip/includes/maxmind-ws/GeoCityLocateIspOrg.class.php');

if ( ! defined( 'OWA_MAXMIND_DATA_DIR' ) ) {
define('OWA_MAXMIND_DATA_DIR', OWA_DATA_DIR.'maxmind/');
}

/**
* Maxmind Geolocation Wrapper
Expand All @@ -58,7 +53,7 @@ class owa_maxmind extends owa_location {
*/
var $ws_url = '';
var $db_file_dir;
var $db_file_name = 'GeoLiteCity.dat';
var $db_file_name = 'GeoLite2-City.mmdb';
var $db_file_path;
var $db_file_present = false;

Expand All @@ -68,24 +63,22 @@ class owa_maxmind extends owa_location {
* @return owa_hostip
*/
function __construct() {

if ( ! defined( 'OWA_MAXMIND_DATA_DIR' ) ) {
define('OWA_MAXMIND_DATA_DIR', OWA_DATA_DIR.'maxmind/');
}

return parent::__construct();
}

function isDbReady() {

$this->db_file_path = OWA_MAXMIND_DATA_DIR.$this->db_file_name;

if ( file_exists( $this->db_file_path ) ) {

$this->db_file_present = true;
} else {

owa_coreAPI::notice('Maxmind DB file could is not present at: ' . OWA_MAXMIND_DATA_DIR);
}

return parent::__construct();
}

function isDbReady() {

return $this->db_file_present;
}

Expand All @@ -94,38 +87,26 @@ function isDbReady() {
*
* @param string $ip
*/
function getLocation($location_map) {
function getLocation( $location_map ) {

if ( ! $this->isDbReady() ) {

return $location_map;
}

if ( ! array_key_exists( 'ip_address', $location_map ) ) {
return $location_map;
}

// check for shared memory capability
if ( function_exists( 'shmop_open' ) ) {
$flag = Net_GeoIP::SHARED_MEMORY ;
} else {
$flag = Net_GeoIp::STANDARD ;
}

$geoip = Net_GeoIP::getInstance($this->db_file_path, $flag);
$location = $geoip->lookupLocation(trim($location_map['ip_address']));
$reader = new Reader( $this->db_file_path );

$record = $reader->get( trim( $location_map['ip_address'] ) );

$reader->close();

if ($location) {
if ( $record ) {

$location_map['city'] = utf8_encode( strtolower( trim( $location->__get( 'city' ) ) ) );
$location_map['state'] = utf8_encode( strtolower( trim( $location->__get( 'region' ) ) ) );
$location_map['country'] = utf8_encode( strtolower( trim( $location->__get( 'countryName' ) ) ) );
$location_map['country_code'] = strtoupper(trim($location->__get('countryCode')));
$location_map['country_code3'] = strtoupper(trim($location->__get('countryCode3')));
$location_map['latitude'] = trim($location->__get('latitude'));
$location_map['longitude'] = trim($location->__get('longitude'));
$location_map['dma_code'] = trim($location->__get('dmaCode'));
$location_map['area_code'] = trim($location->__get('areaCode'));
$location_map['postal_code'] = trim($location->__get('postalCode'));
$location_map = $this->mapCityRecord( $record, $location_map );
}

return $location_map;
Expand All @@ -135,34 +116,83 @@ function getLocation($location_map) {
function getLocationFromWebService($location_map) {

$license_key = owa_coreAPI::getSetting('maxmind_geoip', 'ws_license_key');
$user_name = owa_coreAPI::getSetting('maxmind_geoip', 'ws_user_name');

if ( ! array_key_exists( 'ip_address', $location_map ) ) {
return $location_map;
}

$geoloc = GeoCityLocateIspOrg::getInstance();
$geoloc->setLicenceKey( $license_key );
$geoloc->setIP( trim($location_map['ip_address']) );

if ( $geoloc->isError() ) {
owa_coreAPI::debug( $geoloc->isError().": " . $geoloc->getError() );
return $location_map;
}
//use GeoIp2\WebService\Client;

$client = new Client( $user_name, $license_key );

$location_map['city'] = utf8_encode( strtolower( trim( $geoloc->getCity() ) ) );
$location_map['state'] = utf8_encode( strtolower( trim($geoloc->getState() ) ) );
$location_map['country'] = utf8_encode( strtolower( trim( $geoloc->lookupCountryCode( $geoloc->getCountryCode() ) ) ) );
$location_map['country_code'] = strtoupper( trim($geoloc->getCountryCode() ) );
$location_map['latitude'] = trim( $geoloc->getLat() );
$location_map['longitude'] = trim( $geoloc->getLong() );
$location_map['dma_code'] = trim( $geoloc->getMetroCode() );
$location_map['dma'] = trim( $geoloc->lookupMetroCode( $geoloc->getMetroCode() ) );
$location_map['area_code'] = trim( $geoloc->getAreaCode() );
$location_map['postal_code'] = trim( $geoloc->getZip() );
$location_map['isp'] = utf8_encode( trim( $geoloc->getIsp() ) );
$location_map['organization'] = utf8_encode( trim( $geoloc->getOrganization() ) );
$location_map['subcountry_code'] = trim( $geoloc->lookupSubCountryCode( $geoloc->getState(), $geoloc->getCountryCode() ) );
$record = $client->city( trim( $location_map['ip_address'] ) );


if ( $record ) {

$location_map = $this->mapCityRecord( $record, $location_map );
}

return $location_map;
}

private function mapCityRecord( $record, $location_map = array(), $lang = 'en' ) {

if ( $record && is_array( $record ) ) {

if ( isset( $record['city']['names'][ $lang ] ) ) {

$location_map['city'] = utf8_encode( strtolower( trim( $record['city']['names'][ $lang ] ) ) );
}

if ( isset( $record['continent']['code'] ) ) {

$location_map['continent'] = utf8_encode( strtolower( trim( $record['continent']['code'] ) ) );
}

if ( isset( $record['continent']['names'][ $lang ] ) ) {

$location_map['continent_code'] = utf8_encode( strtolower( trim( $record['continent']['names'][ $lang ] ) ) );
}

if ( isset( $record['subdivisions'][0]['names'][ $lang ] ) ) {

$location_map['state'] = utf8_encode( strtolower( trim( $record['subdivisions'][0]['names'][ $lang ] ) ) );
}

if ( isset( $record['subdivisions'][0]['iso_code'] ) ) {

$location_map['state_code'] = utf8_encode( strtolower( trim( $record['subdivisions'][0]['iso_code'] ) ) );
}

if ( isset( $record['country']['names'][ $lang ] ) ) {

$location_map['country'] = utf8_encode( strtolower( trim( $record['country']['names'][ $lang ] ) ) );
}

if ( isset( $record['country']['iso_code'] ) ) {

$location_map['country_code'] = strtoupper( trim( $record['country']['iso_code'] ) );
}

if ( isset( $record['location']['latitude'] ) ) {

$location_map['latitude'] = trim( $record['location']['latitude'] );
}

if ( isset( $record['location']['longitude'] ) ) {

$location_map['longitude'] = trim( $record['location']['longitude'] );
}

if ( isset( $record['postal']['code'] ) ) {

$location_map['postal_code'] = trim( $record['postal']['code'] );
}
}

return $location_map;
}

Expand Down
Loading

0 comments on commit b34582f

Please sign in to comment.