/** * normalize helper function * Setting the country correctly. Country is... kinda important. * If we have no country, or nonsense, we try to get something rational * through GeoIP lookup. */ protected function setCountry() { $regen = true; $country = ''; if ($this->isSomething('country')) { $country = strtoupper($this->getVal('country')); if (DataValidator::is_valid_iso_country_code($country)) { $regen = false; } else { //check to see if it's one of those other codes that comes out of CN, for the logs //If this logs annoying quantities of nothing useful, go ahead and kill this whole else block later. //we're still going to try to regen. $near_countries = array('XX', 'EU', 'AP', 'A1', 'A2', 'O1'); if (!in_array($country, $near_countries)) { $this->logger->warning(__FUNCTION__ . ": {$country} is not a country, or a recognized placeholder."); } } } else { $this->logger->warning(__FUNCTION__ . ': Country not set.'); } //try to regenerate the country if we still don't have a valid one yet if ($regen) { // If no valid country was passed, try to do GeoIP lookup // Requires php5-geoip package if (function_exists('geoip_country_code_by_name')) { $ip = $this->getVal('user_ip'); if (WmfFramework::validateIP($ip)) { //I hate @suppression at least as much as you do, but this geoip function is being genuinely horrible. //try/catch did not help me suppress the notice it emits when it can't find a host. //The goggles; They do *nothing*. // TODO: to change error_reporting is less worse? $country = @geoip_country_code_by_name($ip); if (!$country) { $this->logger->warning(__FUNCTION__ . ": GeoIP lookup function found nothing for {$ip}! No country available."); } } } else { $this->logger->warning('GeoIP lookup function is missing! No country available.'); } //still nothing good? Give up. if (!DataValidator::is_valid_iso_country_code($country)) { $country = 'XX'; } } if ($country != $this->getVal('country')) { $this->setVal('country', $country); } }