getProviderById() публичный статический Метод

Returns a provider instance by ID or false if the ID is invalid or unavailable.
public static getProviderById ( string $providerId ) : LocationProvider | null
$providerId string
Результат LocationProvider | null
Пример #1
0
 public function enrichVisitWithLocation(&$visitorInfo, \Piwik\Tracker\Request $request)
 {
     require_once PIWIK_INCLUDE_PATH . "/plugins/UserCountry/LocationProvider.php";
     $ipAddress = IP::N2P(Config::getInstance()->Tracker['use_anonymized_ip_for_visit_enrichment'] == 1 ? $visitorInfo['location_ip'] : $request->getIp());
     $userInfo = array('lang' => $visitorInfo['location_browser_lang'], 'ip' => $ipAddress);
     $id = Common::getCurrentLocationProviderId();
     $provider = LocationProvider::getProviderById($id);
     if ($provider === false) {
         $id = DefaultProvider::ID;
         $provider = LocationProvider::getProviderById($id);
         Common::printDebug("GEO: no current location provider sent, falling back to default '{$id}' one.");
     }
     $location = $provider->getLocation($userInfo);
     // if we can't find a location, use default provider
     if ($location === false) {
         $defaultId = DefaultProvider::ID;
         $provider = LocationProvider::getProviderById($defaultId);
         $location = $provider->getLocation($userInfo);
         Common::printDebug("GEO: couldn't find a location with Geo Module '{$id}', using Default '{$defaultId}' provider as fallback...");
         $id = $defaultId;
     }
     Common::printDebug("GEO: Found IP {$ipAddress} location (provider '" . $id . "'): " . var_export($location, true));
     if (empty($location['country_code'])) {
         // sanity check
         $location['country_code'] = \Piwik\Tracker\Visit::UNKNOWN_CODE;
     }
     // add optional location components
     $this->updateVisitInfoWithLocation($visitorInfo, $location);
 }
Пример #2
0
 /**
  * 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 and based
  * on how the GeoIP module is configured.
  *
  * If a region database is used, it may return the country code, region code,
  * city name, area code, latitude, longitude and postal code of the visitor.
  *
  * Alternatively, only the country code may be returned for another database.
  *
  * If your HTTP server is not configured to include all GeoIP information, some
  * information will not be available to Piwik.
  *
  * @param array $info Must have an 'ip' field.
  * @return array
  */
 public function getLocation($info)
 {
     $ip = $this->getIpFromInfo($info);
     // geoip modules that are built into servers can't use a forced IP. in this case we try
     // to fallback to another version.
     $myIP = IP::getIpFromHeader();
     if (!self::isSameOrAnonymizedIp($ip, $myIP) && (!isset($info['disable_fallbacks']) || !$info['disable_fallbacks'])) {
         Common::printDebug("The request is for IP address: " . $info['ip'] . " but your IP is: {$myIP}. GeoIP Server Module (apache/nginx) does not support this use case... ");
         $fallbacks = array(Pecl::ID, Php::ID);
         foreach ($fallbacks as $fallbackProviderId) {
             $otherProvider = LocationProvider::getProviderById($fallbackProviderId);
             if ($otherProvider) {
                 Common::printDebug("Used {$fallbackProviderId} to detect this visitor IP");
                 return $otherProvider->getLocation($info);
             }
         }
         Common::printDebug("FAILED to lookup the geo location of this IP address, as no fallback location providers is configured. We recommend to configure Geolocation PECL module to fix this error.");
         return false;
     }
     $result = array();
     foreach (self::$geoIpServerVars as $resultKey => $geoipVarName) {
         if (!empty($_SERVER[$geoipVarName])) {
             $result[$resultKey] = $_SERVER[$geoipVarName];
         }
     }
     foreach (self::$geoIpUtfServerVars as $resultKey => $geoipVarName) {
         if (!empty($_SERVER[$geoipVarName])) {
             $result[$resultKey] = utf8_encode($_SERVER[$geoipVarName]);
         }
     }
     $this->completeLocationResult($result);
     return $result;
 }
Пример #3
0
 /**
  * Uses a location provider to find/guess the location of an IP address.
  *
  * See LocationProvider::getLocation to see the details
  * of the result of this function.
  *
  * @param string $ip The IP address.
  * @param bool|string $provider The ID of the provider to use or false to use the
  *                               currently configured one.
  * @throws Exception
  * @return array|false
  */
 public function getLocationFromIP($ip, $provider = false)
 {
     Piwik::checkUserHasSomeViewAccess();
     if (empty($provider)) {
         $provider = LocationProvider::getCurrentProviderId();
     }
     $oProvider = LocationProvider::getProviderById($provider);
     if (empty($oProvider)) {
         throw new Exception("Cannot find the '{$provider}' provider. It is either an invalid provider " . "ID or the ID of a provider that is not working.");
     }
     $location = $oProvider->getLocation(array('ip' => $ip));
     if (empty($location)) {
         throw new Exception("Could not geolocate '{$ip}'!");
     }
     $location['ip'] = $ip;
     return $location;
 }
 private function createGeolocator(OutputInterface $output, InputInterface $input)
 {
     $providerId = $input->getOption(self::PROVIDER_ARGUMENT);
     $geolocator = new VisitorGeolocator(LocationProvider::getProviderById($providerId) ?: null);
     $usedProvider = $geolocator->getProvider();
     if (!$usedProvider->isAvailable()) {
         throw new \InvalidArgumentException("The provider '{$providerId}' is not currently available, please make sure it is configured correctly.");
     }
     $isWorkingOrErrorMessage = $usedProvider->isWorking();
     if ($isWorkingOrErrorMessage !== true) {
         $errorMessage = "The provider '{$providerId}' does not appear to be working correctly. Details: {$isWorkingOrErrorMessage}";
         $forceGeolocation = $input->getOption(self::FORCE_OPTION);
         if ($forceGeolocation) {
             $output->writeln("<error>{$errorMessage}</error>");
             $output->writeln("<comment>Ignoring location provider issue, geolocating anyway due to --force option.</comment>");
         } else {
             throw new \InvalidArgumentException($errorMessage);
         }
     }
     return $geolocator;
 }
Пример #5
0
 /**
  * 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;
 }
Пример #6
0
 private function getDefaultProvider()
 {
     return LocationProvider::getProviderById(DefaultProvider::ID);
 }
Пример #7
0
 private function getProvider()
 {
     $id = Common::getCurrentLocationProviderId();
     $provider = LocationProvider::getProviderById($id);
     if ($provider === false) {
         $provider = $this->getDefaultProvider();
         Common::printDebug("GEO: no current location provider sent, falling back to default '{$id}' one.");
     }
     return $provider;
 }