/** * Returns the timezone for an IP address. * * @param string $ip IP address to check (defaults to <get_ip_address>) * @return string Timezone identifier or false on error */ function get_timezone_by_ip($ip = false) { if ($ip === false) { $ip = $GLOBALS['current_ip_addr']; } if (starts_with($ip, "1.1 ") || starts_with($ip, "192.168.")) { return false; } $key = "get_timezone_by_ip." . getAppVersion('nc') . "-" . $ip; $ret = cache_get($key); if ($ret) { return $ret; } /* // new url with api key: $url = "https://api.ipinfodb.com/v3/ip-city/?key=ae4dea477cd8a36cc678c582c3f990fb57a5aae696f878b4e0eee70afa53bf1e&ip=".$GLOBALS['current_ip_addr']."&format=xml"; try { $xml = downloadData($url, false, false, 60 * 60, 2); }catch(Exception $ex){ WdfException::Log("Unable to get Timezone for ".$ip." ($url)",$ex); return false; } if( preg_match_all('/<timeZone>([^<]*)<\/timeZone>/', $xml, $zone, PREG_SET_ORDER) ) { $zone = $zone[0]; if($zone[1] != "") { cache_set($key,$zone[1], 24 * 60 * 60); return $zone[1]; } } // log_error("No timezone found for ".$GLOBALS['current_ip_addr']." via ipinfodb.com"); */ $url = "http://ip-api.com/php/" . $ip; try { $data = @unserialize(downloadData($url, false, false, 60 * 60, 2)); } catch (Exception $ex) { WdfException::Log("Unable to get Timezone for " . $ip . " ({$url}) " . $ex->getMessage(), $ex); return false; } if ($data && $data['status'] == 'success') { $zone = $data['timezone']; cache_set($key, $zone, 24 * 60 * 60); return $zone; } log_error("No timezone found for " . $ip . " via ip-api.com"); $coords = get_coordinates_by_ip($ip); if ($coords === false) { log_error("No timezone found for IP " . $ip . " (missing coordinates)"); // disaster-fallback: use our timezone: return "Etc/GMT+2"; } // ALTERNATIVE 1: // ws.geonames.org had only timeouts on 2/10/2010... // $url = "http://ws.geonames.org/timezone?lat=".$coords['latitude'].'&lng='.$coords['longitude']; $url = "http://api.geonames.org/timezone?lat=" . $coords['latitude'] . '&lng=' . $coords['longitude'] . "&username=scavix"; try { $xml = downloadData($url, false, false, 60 * 60, 2); } catch (Exception $ex) { WdfException::Log("Unable to get Timezone for " . $ip . " ({$url}) " . $ex->getMessage(), $ex); return false; } if (preg_match_all('/<timezoneId>([^<]*)<\\/timezoneId>/', $xml, $zone, PREG_SET_ORDER)) { $zone = $zone[0]; cache_set($key, $zone[1], 24 * 60 * 60); return $zone[1]; } log_error("No timezone found for " . $ip . " via geonames.org"); // ALTERNATIVE 2: $url = "http://www.earthtools.org/timezone/" . $coords['latitude'] . '/' . $coords['longitude']; try { $xml = downloadData($url, false, false, 60 * 60, 2); } catch (Exception $ex) { WdfException::Log("Unable to get Timezone for " . $ip . " ({$url})", $ex); return false; } if (preg_match_all('/<offset>([^<]*)<\\/offset>/', $xml, $zone, PREG_SET_ORDER)) { $zone = $zone[0]; $zone[1] = round($zone[1], 0); $ret = "Etc/GMT" . ($zone[1] < 0 ? $zone[1] : "+" . $zone[1]); cache_set($key, $ret, 24 * 60 * 60); return $ret; } log_error("No timezone found for " . $ip . " via earthtools.org"); // disaster-fallback: use our timezone: return "Etc/GMT+2"; }
/** * Returns the timezone for an IP address. * * @param string $ip IP address to check (defaults to <get_ip_address>) * @return string Timezone identifier or false on error */ function get_timezone_by_ip($ip = false) { if ($ip === false) { $ip = $GLOBALS['current_ip_addr']; } if (starts_with($ip, "1.1 ") || starts_with($ip, "192.168.1.")) { return false; } $key = "get_timezone_by_ip." . getAppVersion('nc') . "-" . $ip; $ret = cache_get($key); if ($ret) { return $ret; } // new url with api key: $url = "http://api.ipinfodb.com/v2/ip_query.php?key=6a6ef9d4d82491036a4f3dbd465d52d2e2d5253d1285a3dda02b65752b5474f8&ip=" . $GLOBALS['current_ip_addr'] . "&timezone=true"; try { $xml = downloadData($url, false, false, 60 * 60, 2); } catch (Exception $ex) { WdfException::Log("Unable to get Timezone for " . $ip . " ({$url})", $ex); return false; } if (preg_match_all('/<TimezoneName>([^<]*)<\\/TimezoneName>/', $xml, $zone, PREG_SET_ORDER)) { $zone = $zone[0]; if ($zone[1] != "") { cache_set($key, $zone[1], 24 * 60 * 60); return $zone[1]; } } // log_error("No timezone found for ".$GLOBALS['current_ip_addr']." via ipinfodb.com"); $coords = get_coordinates_by_ip($ip); if ($coords === false) { log_error("No timezone found for IP " . $ip . " (missing coordinates)"); // disaster-fallback: use our timezone: return "Etc/GMT+2"; } // ALTERNATIVE 1: // ws.geonames.org had only timeouts on 2/10/2010... // $url = "http://ws.geonames.org/timezone?lat=".$coords['latitude'].'&lng='.$coords['longitude']; $url = "http://api.geonames.org/timezone?lat=" . $coords['latitude'] . '&lng=' . $coords['longitude'] . "&username=scendix"; try { $xml = downloadData($url, false, false, 60 * 60, 2); } catch (Exception $ex) { WdfException::Log("Unable to get Timezone for " . $ip . " ({$url}) " . $ex->getMessage(), $ex); return false; } if (preg_match_all('/<timezoneId>([^<]*)<\\/timezoneId>/', $xml, $zone, PREG_SET_ORDER)) { $zone = $zone[0]; cache_set($key, $zone[1], 24 * 60 * 60); return $zone[1]; } log_error("No timezone found for " . $ip . " via geonames.org"); // ALTERNATIVE 2: $url = "http://www.earthtools.org/timezone/" . $coords['latitude'] . '/' . $coords['longitude']; try { $xml = downloadData($url, false, false, 60 * 60, 2); } catch (Exception $ex) { WdfException::Log("Unable to get Timezone for " . $ip . " ({$url})", $ex); return false; } if (preg_match_all('/<offset>([^<]*)<\\/offset>/', $xml, $zone, PREG_SET_ORDER)) { $zone = $zone[0]; $zone[1] = round($zone[1], 0); $ret = "Etc/GMT" . ($zone[1] < 0 ? $zone[1] : "+" . $zone[1]); cache_set($key, $ret, 24 * 60 * 60); return $ret; } log_error("No timezone found for " . $ip . " via earthtools.org"); // disaster-fallback: use our timezone: return "Etc/GMT+2"; }