From 573acaed8941115f951efaf01ae6610ab3fee81e Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Fri, 17 Dec 2010 01:30:48 +0000 Subject: [PATCH] FEATURE: added Geoip::set_default_country_code() to set fall back country (thanks ronan). ENHANCEMENT: added set_enabled() and is_enabled() to Geoip to allow disabling of Geoip lookups. (Fixes #4452) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@115157 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- integration/Geoip.php | 62 ++++++++++++++++++++++++++++++++++++++----- tests/GeoipTest.php | 11 ++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 tests/GeoipTest.php diff --git a/integration/Geoip.php b/integration/Geoip.php index 7993e228b..ee2e6fa26 100755 --- a/integration/Geoip.php +++ b/integration/Geoip.php @@ -1,4 +1,5 @@ "Anonymous Proxy", @@ -264,6 +269,34 @@ class Geoip { 'ZW' => "Zimbabwe" ); + /** + * Set whether the Geoip lookup should be enabled or not. Useful + * to disable while testing or in environments Geoip lookup is wrong + * + * @param bool + */ + public static function set_enabled($bool) { + self::$enabled = $bool; + } + + /** + * Return whether Geoip lookups are enabled + * + * @return bool + */ + public static function is_enabled() { + return (bool) self::$enabled; + } + + /** + * Set the default country + * + * @param string $country_code + */ + public static function set_default_country_code($country_code) { + self::$default_country_code = $country_code; + } + /** * Find the country for an IP address. * @@ -278,7 +311,8 @@ class Geoip { * @param boolean $codeOnly Returns just the country code */ static function ip2country($address, $codeOnly = false) { - + if(!self::is_enabled()) return false; + // Return if in CLI, or you'll get this error: "sh: geoiplookup: command not found" if(Director::is_cli() || !function_exists('exec')) return false; @@ -318,15 +352,30 @@ class Geoip { /** * Returns the country code, for the current visitor + * + * @return string|bool */ static function visitor_country() { - if( ereg('^dev(\\.|$)', $_SERVER['HTTP_HOST']) && isset($_GET['country'])) return $_GET['country']; - else if(isset($_SERVER['REMOTE_ADDR'])) return Geoip::ip2country($_SERVER['REMOTE_ADDR'], true); + if(ereg('^dev(\\.|$)', $_SERVER['HTTP_HOST']) && isset($_GET['country'])){ + $code = $_GET['country']; + } + else if(isset($_SERVER['REMOTE_ADDR']) && self::is_enabled()) { + $code = Geoip::ip2country($_SERVER['REMOTE_ADDR'], true); + } + + // if geoip fails, lets default to default country code (if any) + if(!isset($code) || !$code) { + $code = self::$default_country_code; + } + + return ($code) ? $code : false; } /** * Sanity Checker for this class, which helps us debug, * or ensure that its working as expected + * + * @return bool */ static function ip2country_check() { global $ss_disableFeatures; @@ -368,6 +417,8 @@ class Geoip { /** * Returns an array of ISO Country Codes -> Country Names + * + * @return array */ static function getCountryDropDown() { $dropdown = Geoip::$iso_3166_countryCodes; @@ -377,5 +428,4 @@ class Geoip { asort($dropdown); return $dropdown; } -} -?> +} \ No newline at end of file diff --git a/tests/GeoipTest.php b/tests/GeoipTest.php new file mode 100644 index 000000000..74f10aa61 --- /dev/null +++ b/tests/GeoipTest.php @@ -0,0 +1,11 @@ +assertEquals('DE', Geoip::visitor_country()); + } +} \ No newline at end of file