/** * 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(); }
/** * Returns true if this provider has been setup correctly, the error message if * otherwise. * * @return bool|string */ public function isWorking() { if (!function_exists('mb_internal_encoding')) { return Piwik::translate('UserCountry_GeoIPCannotFindMbstringExtension', array('mb_internal_encoding', 'mbstring')); } $geoIpError = false; $catchGeoIpError = function ($errno, $errstr, $errfile, $errline) use(&$geoIpError) { $filename = basename($errfile); if ($filename == 'geoip.inc' || $filename == 'geoipcity.inc') { $geoIpError = array($errno, $errstr, $errfile, $errline); } else { throw new \Exception("Error in PHP GeoIP provider: {$errstr} on line {$errline} of {$errfile}"); // unexpected } }; // catch GeoIP errors set_error_handler($catchGeoIpError); $result = parent::isWorking(); restore_error_handler(); if ($geoIpError) { list($errno, $errstr, $errfile, $errline) = $geoIpError; Log::warning("Got GeoIP error when testing PHP GeoIP location provider: %s(%s): %s", $errfile, $errline, $errstr); return Piwik::translate('UserCountry_GeoIPIncorrectDatabaseFormat'); } return $result; }