function testgeoipdatabase($type, $flags, $msg, $numlookups) { $gi = geoip_open($this->dbfilename[$type], $flags); if ($gi == null) { print "error: " . $this->dbfilename[$type] . " does not exist\n"; return; } $t1 = $this->ftime(); $i4 = 0; for ($i2 = 0; $i2 < $numlookups; $i2++) { switch ($type) { case GEOIP_COUNTRY_DATABASE: geoip_country_code_by_addr($gi, $this->randomipaddress()); break; case GEOIP_REGION_DATABASE: geoip_region_by_addr($gi, $this->randomipaddress()); break; case GEOIP_CITY_DATABASE: GeoIP_record_by_addr($gi, $this->randomipaddress()); break; } } $t2 = $this->ftime(); $t3 = $t2 - $t1; print $msg . "\n"; print $numlookups . " lookups made in " . $t3 . " seconds \n"; geoip_close($gi); }
public function testRegion() { $gi = geoip_open("tests/data/GeoIPRegion.dat", GEOIP_STANDARD); list($countryCode, $region) = geoip_region_by_addr($gi, '64.17.254.223'); $this->assertEquals('CA', $region); $this->assertEquals('US', $countryCode); global $GEOIP_REGION_NAME; $this->assertEquals('California', $GEOIP_REGION_NAME[$countryCode][$region]); }
/** * Uses a GeoIP database to get a visitor's location based on their IP address. * * This function will return different results based on the data used. If a city * database is used, it may return the country code, region code, city name, area * code, latitude, longitude and postal code of the visitor. * * Alternatively, if used with a country database, only the country code will be * returned. * * @param array $info Must have an 'ip' field. * @return array */ public function getLocation($info) { $ip = $this->getIpFromInfo($info); $result = array(); $locationGeoIp = $this->getGeoIpInstance($key = 'loc'); if ($locationGeoIp) { switch ($locationGeoIp->databaseType) { case GEOIP_CITY_EDITION_REV0: // city database type // city database type case GEOIP_CITY_EDITION_REV1: case GEOIP_CITYCOMBINED_EDITION: $location = geoip_record_by_addr($locationGeoIp, $ip); if (!empty($location)) { $result[self::COUNTRY_CODE_KEY] = $location->country_code; $result[self::REGION_CODE_KEY] = $location->region; $result[self::CITY_NAME_KEY] = utf8_encode($location->city); $result[self::AREA_CODE_KEY] = $location->area_code; $result[self::LATITUDE_KEY] = $location->latitude; $result[self::LONGITUDE_KEY] = $location->longitude; $result[self::POSTAL_CODE_KEY] = $location->postal_code; } break; case GEOIP_REGION_EDITION_REV0: // region database type // region database type case GEOIP_REGION_EDITION_REV1: $location = geoip_region_by_addr($locationGeoIp, $ip); if (!empty($location)) { $result[self::COUNTRY_CODE_KEY] = $location[0]; $result[self::REGION_CODE_KEY] = $location[1]; } break; case GEOIP_COUNTRY_EDITION: // country database type $result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr($locationGeoIp, $ip); break; default: // unknown database type, log warning and fallback to country edition Log::warning("Found unrecognized database type: %s", $locationGeoIp->databaseType); $result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr($locationGeoIp, $ip); break; } } // NOTE: ISP & ORG require commercial dbs to test. this code has been tested manually, // but not by integration tests. $ispGeoIp = $this->getGeoIpInstance($key = 'isp'); if ($ispGeoIp) { $isp = geoip_org_by_addr($ispGeoIp, $ip); if (!empty($isp)) { $result[self::ISP_KEY] = utf8_encode($isp); } } $orgGeoIp = $this->getGeoIpInstance($key = 'org'); if ($orgGeoIp) { $org = geoip_org_by_addr($orgGeoIp, $ip); if (!empty($org)) { $result[self::ORG_KEY] = utf8_encode($org); } } if (empty($result)) { return false; } $this->completeLocationResult($result); return $result; }
#!/usr/bin/php -q <?php // This code demonstrates how to lookup the country and region by IP Address // It is designed to work with GeoIP Region available from MaxMind include "geoip.inc"; include "geoipregionvars.php"; $gi = geoip_open("/usr/local/share/GeoIP/GeoIPRegion.dat", GEOIP_STANDARD); list($countrycode, $region) = geoip_region_by_addr($gi, "24.24.24.24"); print $countrycode . " " . $region . " " . $GEOIP_REGION_NAME[$countrycode][$region] . "\n"; list($countrycode, $region) = geoip_region_by_addr($gi, "80.24.24.24"); print $countrycode . " " . $region . " " . $GEOIP_REGION_NAME[$countrycode][$region] . "\n"; list($countrycode, $region) = geoip_region_by_addr($gi, "199.243.137.184"); print $countrycode . " " . $region . " " . $GEOIP_REGION_NAME[$countrycode][$region] . "\n"; geoip_close($gi);
/** * Uses a GeoIP database to get a visitor's location based on their IP address. * * This function will return different results based on the data used. If a city * database is used, it may return the country code, region code, city name, area * code, latitude, longitude and postal code of the visitor. * * Alternatively, if used with a country database, only the country code will be * returned. * * @param array $info Must have an 'ip' field. * @return array */ public function getLocation($info) { $ip = $this->getIpFromInfo($info); $isIPv6 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); $result = array(); $locationGeoIp = $this->getGeoIpInstance($key = 'loc'); if ($locationGeoIp) { switch ($locationGeoIp->databaseType) { case GEOIP_CITY_EDITION_REV0: // city database type // city database type case GEOIP_CITY_EDITION_REV1: case GEOIP_CITYCOMBINED_EDITION: if ($isIPv6) { $location = geoip_record_by_addr_v6($locationGeoIp, $ip); } else { $location = geoip_record_by_addr($locationGeoIp, $ip); } if (!empty($location)) { $result[self::COUNTRY_CODE_KEY] = $location->country_code; $result[self::REGION_CODE_KEY] = $location->region; if ($location->region == "18" && $location->city == "Milan") { $result[self::REGION_CODE_KEY] = "09"; } $result[self::CITY_NAME_KEY] = utf8_encode($location->city); $result[self::AREA_CODE_KEY] = $location->area_code; $result[self::LATITUDE_KEY] = $location->latitude; $result[self::LONGITUDE_KEY] = $location->longitude; $result[self::POSTAL_CODE_KEY] = $location->postal_code; } break; case GEOIP_REGION_EDITION_REV0: // region database type // region database type case GEOIP_REGION_EDITION_REV1: if ($isIPv6) { // NOTE: geoip_region_by_addr_v6 does not exist (yet?), so we // return the country code and an empty region code $location = array(geoip_country_code_by_addr_v6($locationGeoIp, $ip), ''); } else { $location = geoip_region_by_addr($locationGeoIp, $ip); } if (!empty($location)) { $result[self::COUNTRY_CODE_KEY] = $location[0]; $result[self::REGION_CODE_KEY] = $location[1]; } break; case GEOIP_COUNTRY_EDITION: // country database type if ($isIPv6) { $result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr_v6($locationGeoIp, $ip); } else { $result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr($locationGeoIp, $ip); } break; default: // unknown database type, log warning and fallback to country edition Log::warning("Found unrecognized database type: %s", $locationGeoIp->databaseType); if ($isIPv6) { $result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr_v6($locationGeoIp, $ip); } else { $result[self::COUNTRY_CODE_KEY] = geoip_country_code_by_addr($locationGeoIp, $ip); } break; } } // NOTE: ISP & ORG require commercial dbs to test. The code has been tested manually, // but not by system tests. $ispGeoIp = $this->getGeoIpInstance($key = 'isp'); if ($ispGeoIp) { if ($isIPv6) { $isp = geoip_name_by_addr_v6($ispGeoIp, $ip); } else { $isp = geoip_org_by_addr($ispGeoIp, $ip); } if (!empty($isp)) { $result[self::ISP_KEY] = utf8_encode($isp); } } $orgGeoIp = $this->getGeoIpInstance($key = 'org'); if ($orgGeoIp) { if ($isIPv6) { $org = geoip_name_by_addr_v6($orgGeoIp, $ip); } else { $org = geoip_org_by_addr($orgGeoIp, $ip); } if (!empty($org)) { $result[self::ORG_KEY] = utf8_encode($org); } } if (empty($result)) { return false; } $this->completeLocationResult($result); return $result; }
/** * Retrieve shipping and billing addresses, * and boolean flag about their equality * * For the registered customer with available addresses returns * appropriate address. * For the Guest trying to detect country with geo-ip technology * * @return array */ protected function _getDefaultAddress() { $result = array('shipping' => array('country_id' => null, 'city' => null, 'region_id' => null, 'postcode' => null, 'customer_address_id' => false), 'billing' => array('country_id' => null, 'city' => null, 'region_id' => null, 'postcode' => null, 'customer_address_id' => false, 'use_for_shipping' => Mage::getStoreConfig('firecheckout/general/shipping_address_checkbox_state'), 'register_account' => 0)); if (($customer = Mage::getSingleton('customer/session')->getCustomer()) && ($addresses = $customer->getAddresses())) { if ($this->getQuote()->getShippingAddress()->getCountryId()) { $shippingAddress = $this->getQuote()->getShippingAddress(); if (!$shippingAddress->getSameAsBilling()) { $billingAddress = $this->getQuote()->getBillingAddress(); } else { $billingAddress = $shippingAddress; } $result['shipping'] = $shippingAddress->getData(); $result['billing'] = $billingAddress->getData(); $result['billing']['use_for_shipping'] = $shippingAddress->getSameAsBilling(); } else { if (!($shippingAddress = $customer->getPrimaryShippingAddress())) { foreach ($addresses as $address) { $shippingAddress = $address; break; } } if (!($billingAddress = $customer->getPrimaryBillingAddress())) { foreach ($addresses as $address) { $billingAddress = $address; break; } } $result['shipping']['country_id'] = $shippingAddress->getCountryId(); $result['shipping']['customer_address_id'] = $shippingAddress->getId(); $result['billing']['country_id'] = $billingAddress->getCountryId(); $result['billing']['customer_address_id'] = $billingAddress->getId(); $result['billing']['use_for_shipping'] = $shippingAddress->getId() === $billingAddress->getId(); } } else { if ($this->getQuote()->getShippingAddress()->getCountryId()) { // Estimated shipping cost from shopping cart $address = $this->getQuote()->getShippingAddress(); $result['shipping'] = $address->getData(); if (!$address->getSameAsBilling()) { $address = $this->getQuote()->getBillingAddress(); $result['billing'] = $address->getData(); $result['billing']['use_for_shipping'] = false; } else { $result['billing'] = $result['shipping']; $result['billing']['use_for_shipping'] = true; } } else { $detectCountry = Mage::getStoreConfig('firecheckout/geo_ip/country'); $detectRegion = Mage::getStoreConfig('firecheckout/geo_ip/region'); $detectCity = Mage::getStoreConfig('firecheckout/geo_ip/city'); $geoipIncluded = true; if ($detectCountry || $detectRegion || $detectCity) { if (!function_exists('geoip_open')) { $geoipIncluded = false; $this->_checkoutSession->addError(Mage::helper('firecheckout')->__("GeoIP is enabled but not included. geoip_open function doesn't found")); } } $remoteAddr = Mage::helper('core/http')->getRemoteAddr(); if ($detectCountry && $geoipIncluded) { $filename = Mage::getBaseDir('lib') . DS . "MaxMind/GeoIP/data/" . Mage::getStoreConfig('firecheckout/geo_ip/country_file'); if (is_readable($filename)) { $gi = geoip_open($filename, GEOIP_STANDARD); $result['shipping']['country_id'] = $result['billing']['country_id'] = geoip_country_code_by_addr($gi, $remoteAddr); geoip_close($gi); } else { $this->_checkoutSession->addError(Mage::helper('firecheckout')->__("Country detection is enabled but %s not found", Mage::getStoreConfig('firecheckout/geo_ip/country_file'))); } } if ($detectRegion && $geoipIncluded) { $filename = Mage::getBaseDir('lib') . DS . "MaxMind/GeoIP/data/" . Mage::getStoreConfig('firecheckout/geo_ip/region_file'); if (is_readable($filename)) { $gi = geoip_open($filename, GEOIP_STANDARD); list($countryCode, $regionCode) = geoip_region_by_addr($gi, $remoteAddr); $region = Mage::getModel('directory/region')->loadByCode($regionCode, $countryCode); $result['shipping']['country_id'] = $result['billing']['country_id'] = $countryCode; $result['shipping']['region_id'] = $result['billing']['region_id'] = $region->getId(); geoip_close($gi); } else { $this->_checkoutSession->addError(Mage::helper('firecheckout')->__("Region detection is enabled but %s not found", Mage::getStoreConfig('firecheckout/geo_ip/region_file'))); } } if ($detectCity && $geoipIncluded) { $filename = Mage::getBaseDir('lib') . DS . "MaxMind/GeoIP/data/" . Mage::getStoreConfig('firecheckout/geo_ip/city_file'); if (is_readable($filename)) { $gi = geoip_open($filename, GEOIP_STANDARD); $record = geoip_record_by_addr($gi, $remoteAddr); if ($record) { $result['shipping']['city'] = $result['billing']['city'] = $record->city; $result['shipping']['postcode'] = $result['billing']['postcode'] = $record->postal_code; } geoip_close($gi); } else { $this->_checkoutSession->addError(Mage::helper('firecheckout')->__("City detection is enabled but %s not found", Mage::getStoreConfig('firecheckout/geo_ip/city_file'))); } } if (empty($result['shipping']['country_id']) || !Mage::getResourceModel('directory/country_collection')->addCountryCodeFilter($result['shipping']['country_id'])->loadByStore()->count()) { $result['shipping']['country_id'] = $result['billing']['country_id'] = Mage::getStoreConfig('firecheckout/general/country'); } } } return $result; }