diff --git a/geoip.module b/geoip.module index 6ec2538..49603f5 100644 --- a/geoip.module +++ b/geoip.module @@ -88,18 +88,22 @@ function geoip_ip_address() { * Singleton wrapper around geoip_open(). */ function geoip_instance() { - $data_file = geoip_data_file(); - if (!$data_file || !file_exists($data_file)) { - return FALSE; - } - - // The file geoip.inc is common to both database types. - _geoip_load_lib(); - $instance = geoip_open($data_file, GEOIP_STANDARD); - - // Conditionally load the geoipcity include file. - if ($instance->databaseType == GEOIP_CITY_EDITION_REV1) { - _geoip_load_lib('geoipcity.inc'); + $instance = FALSE; + + if (!extension_loaded('geoip')) { + $data_file = geoip_data_file(); + if (!$data_file || !file_exists($data_file)) { + return FALSE; + } + + // The file geoip.inc is common to both database types. + _geoip_load_lib(); + $instance = geoip_open($data_file, GEOIP_STANDARD); + + // Conditionally load the geoipcity include file. + if ($instance->databaseType == GEOIP_CITY_EDITION_REV1) { + _geoip_load_lib('geoipcity.inc'); + } } return $instance; @@ -170,16 +174,16 @@ function _geoip_load_lib($file = 'geoip.inc') { * Country Code */ function geoip_country_code($ip = NULL) { + $cc = FALSE; $ip = $ip ? $ip : geoip_ip_address(); - $gi = geoip_instance(); - if (!$gi) { - return FALSE; + if (extension_loaded('geoip')) { + $cc = geoip_country_code_by_name($ip); + } + elseif ($gi = geoip_instance()) { + $cc = geoip_country_code_by_addr($gi, $ip); + geoip_close($gi); } - - $cc = geoip_country_code_by_addr($gi, $ip); - - geoip_close($gi); if (variable_get('geoip_debug', FALSE) && !empty($_GET['geoip_debug'])) { drupal_set_message(t('GeoIP reports country: %cc', array('%cc' => $cc))); @@ -201,23 +205,21 @@ function geoip_country_code($ip = NULL) { * City name or FALSE on errors. */ function geoip_city($ip = NULL) { + $record = FALSE; $ip = $ip ? $ip : geoip_ip_address(); $gi = geoip_instance(); - if (!$gi) { - return FALSE; + if (extension_loaded('geoip')) { + $record = geoip_record_by_name($ip); } - elseif ($gi->databaseType != GEOIP_CITY_EDITION_REV1) { - // If not using a city database, there is nothing to do. + elseif ($gi = geoip_instance()) { + if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) { + $record = geoip_record_by_addr($gi, $ip); + geoip_close($gi); + } geoip_close($gi); - return FALSE; } - - $record = geoip_record_by_addr($gi, $ip); - - geoip_close($gi); - if (variable_get('geoip_debug', FALSE) && !empty($_GET['geoip_debug'])) { drupal_set_message(t('GeoIP reports city: %city', array('%city' => $record->city))); }