/** * Guesses a visitor's location using a visitor's browser language. * * @param array $info Contains 'ip' & 'lang' keys. * @return array Contains the guessed country code mapped to LocationProvider::COUNTRY_CODE_KEY. */ public function getLocation($info) { $enableLanguageToCountryGuess = Config::getInstance()->Tracker['enable_language_to_country_guess']; if (empty($info['lang'])) { $info['lang'] = Common::getBrowserLanguage(); } $country = Common::getCountry($info['lang'], $enableLanguageToCountryGuess, $info['ip']); $location = array(parent::COUNTRY_CODE_KEY => $country); $this->completeLocationResult($location); return $location; }
/** * Returns the language the visitor is viewing. * * @return string browser language code, eg. "en-gb,en;q=0.5" */ public function getBrowserLanguage() { return Common::getRequestVar('lang', Common::getBrowserLanguage(), 'string', $this->params); }
/** * Returns an array mapping provider IDs w/ information about the provider, * for each location provider. * * The following information is provided for each provider: * 'id' - The provider's unique string ID. * 'title' - The provider's title. * 'description' - A description of how the location provider works. * 'status' - Either self::NOT_INSTALLED, self::INSTALLED or self::BROKEN. * 'statusMessage' - If the status is self::BROKEN, then the message describes why. * 'location' - A pretty formatted location of the current IP address * (IP::getIpFromHeader()). * * An example result: * array( * 'geoip_php' => array('id' => 'geoip_php', * 'title' => '...', * 'desc' => '...', * 'status' => GeoIp::BROKEN, * 'statusMessage' => '...', * 'location' => '...') * 'geoip_serverbased' => array(...) * ) * * @param string $newline What to separate lines with in the pretty locations. * @param bool $includeExtra Whether to include ISP/Org info in formatted location. * @return array */ public static function getAllProviderInfo($newline = "\n", $includeExtra = false) { $allInfo = array(); foreach (self::getAllProviders() as $provider) { $info = $provider->getInfo(); $status = self::INSTALLED; $location = false; $statusMessage = false; $availableOrMessage = $provider->isAvailable(); if ($availableOrMessage !== true) { $status = self::NOT_INSTALLED; if (is_string($availableOrMessage)) { $statusMessage = $availableOrMessage; } } else { $workingOrError = $provider->isWorking(); if ($workingOrError === true) { $locInfo = array('ip' => IP::getIpFromHeader(), 'lang' => Common::getBrowserLanguage(), 'disable_fallbacks' => true); $location = $provider->getLocation($locInfo); $location = self::prettyFormatLocation($location, $newline, $includeExtra); } else { $status = self::BROKEN; $statusMessage = $workingOrError; } } $info['status'] = $status; $info['statusMessage'] = $statusMessage; $info['location'] = $location; $allInfo[$info['order']] = $info; } ksort($allInfo); $result = array(); foreach ($allInfo as $info) { $result[$info['id']] = $info; } return $result; }
/** * @dataProvider getBrowserLanguageData * @group Core */ public function testGetBrowserLanguage($useragent, $browserLanguage) { $res = Common::getBrowserLanguage($useragent); $this->assertEquals($browserLanguage, $res); }
/** * @return string Two letters language code, eg. "fr" */ public static function getLanguageCodeForCurrentUser() { $languageCode = self::getLanguageFromPreferences(); if (!API::getInstance()->isLanguageAvailable($languageCode)) { $languageCode = Common::extractLanguageCodeFromBrowserLanguage(Common::getBrowserLanguage(), API::getInstance()->getAvailableLanguages()); } if (!API::getInstance()->isLanguageAvailable($languageCode)) { $languageCode = Translate::getLanguageDefault(); } return $languageCode; }
/** * Echo's a pretty formatted location using a specific LocationProvider. * * Input: * The 'id' query parameter must be set to the ID of the LocationProvider to use. * * Output: * The pretty formatted location that was obtained. Will be HTML. */ public function getLocationUsingProvider() { $providerId = Common::getRequestVar('id'); $provider = LocationProvider::getProviderById($providerId); if (empty($provider)) { throw new Exception("Invalid provider ID: '{$providerId}'."); } $location = $provider->getLocation(array('ip' => IP::getIpFromHeader(), 'lang' => Common::getBrowserLanguage(), 'disable_fallbacks' => true)); $location = LocationProvider::prettyFormatLocation($location, $newline = '<br/>', $includeExtra = true); return $location; }