Piwik\Plugins\UserCountry\LocationProvider\GeoIp::isWorking PHP Метод

isWorking() публичный Метод

Returns true if this provider has been setup correctly, the error message if otherwise.
public isWorking ( ) : boolean | string
Результат boolean | string
    public function isWorking()
    {
        // test with an example IP to make sure the provider is working
        // NOTE: At the moment only country, region & city info is tested.
        try {
            $supportedInfo = $this->getSupportedLocationInfo();
            list($testIp, $expectedResult) = self::getTestIpAndResult();
            // get location using test IP
            $location = $this->getLocation(array('ip' => $testIp));
            // check that result is the same as expected
            $isResultCorrect = true;
            foreach ($expectedResult as $key => $value) {
                // if this provider is not configured to support this information type, skip it
                if (empty($supportedInfo[$key])) {
                    continue;
                }
                if (empty($location[$key]) || $location[$key] != $value) {
                    $isResultCorrect = false;
                }
            }
            if (!$isResultCorrect) {
                $unknown = Piwik::translate('General_Unknown');
                $location = "'" . (empty($location[self::CITY_NAME_KEY]) ? $unknown : $location[self::CITY_NAME_KEY]) . ", " . (empty($location[self::REGION_CODE_KEY]) ? $unknown : $location[self::REGION_CODE_KEY]) . ", " . (empty($location[self::COUNTRY_CODE_KEY]) ? $unknown : $location[self::COUNTRY_CODE_KEY]) . "'";
                $expectedLocation = "'" . $expectedResult[self::CITY_NAME_KEY] . ", " . $expectedResult[self::REGION_CODE_KEY] . ", " . $expectedResult[self::COUNTRY_CODE_KEY] . "'";
                $bind = array($testIp, $location, $expectedLocation);
                return Piwik::translate('UserCountry_TestIPLocatorFailed', $bind);
            }
            return true;
        } catch (Exception $ex) {
            return $ex->getMessage();
        }
    }

Usage Example

Пример #1
0
 /**
  * Returns true if the PECL module that is installed can be successfully used
  * to get the location of an IP address.
  *
  * @return bool
  */
 public function isWorking()
 {
     // if no no location database is available, this implementation is not setup correctly
     if (!self::isLocationDatabaseAvailable()) {
         $dbDir = dirname(geoip_db_filename(GEOIP_COUNTRY_EDITION)) . '/';
         $quotedDir = "'{$dbDir}'";
         // check if the directory the PECL module is looking for exists
         if (!is_dir($dbDir)) {
             return Piwik::translate('UserCountry_PeclGeoIPNoDBDir', array($quotedDir, "'geoip.custom_directory'"));
         }
         // check if the user named the city database GeoLiteCity.dat
         if (file_exists($dbDir . 'GeoLiteCity.dat')) {
             return Piwik::translate('UserCountry_PeclGeoLiteError', array($quotedDir, "'GeoLiteCity.dat'", "'GeoIPCity.dat'"));
         }
         return Piwik::translate('UserCountry_CannotFindPeclGeoIPDb', array($quotedDir, "'GeoIP.dat'", "'GeoIPCity.dat'"));
     }
     return parent::isWorking();
 }
All Usage Examples Of Piwik\Plugins\UserCountry\LocationProvider\GeoIp::isWorking