public function __construct($ip_addr) { try { if ($ip_addr == '127.0.0.1') { // Load local connections up with Columbus, OH. // Why? ;) $cache = ['city' => 'Columbus', 'province' => 'OH', 'country' => 'US', 'timezone' => 'America/New_York', 'postal' => '43215']; } else { $cacheKey = 'iplookup-' . $ip_addr; $cache = Cache::Get($cacheKey); if (!$cache) { $reader = new \GeoIp2\Database\Reader(ROOT_PDIR . 'components/geographic-codes/libs/maxmind-geolite-db/GeoLite2-City.mmdb'); /** @var \GeoIp2\Model\CityIspOrg $geo */ $geo = $reader->cityIspOrg($ip_addr); //$geo = $reader->cityIspOrg('67.149.214.236'); $reader->close(); $sd = isset($geo->subdivisions[0]) ? $geo->subdivisions[0] : null; $cache = ['city' => $geo->city->name, 'province' => $sd ? $sd->isoCode : '', 'country' => $geo->country->isoCode, 'timezone' => $geo->location->timeZone, 'postal' => $geo->postal->code]; Cache::Set($cacheKey, $cache, SECONDS_ONE_WEEK); } } } catch (\Exception $e) { // Well, we tried! Load something at least. $cacheKey = 'iplookup-' . $ip_addr; $cache = ['city' => 'McMurdo Base', 'province' => '', 'country' => 'AQ', 'timezone' => 'CAST', 'postal' => '']; Cache::Set($cacheKey, $cache, SECONDS_ONE_HOUR); } $this->city = $cache['city']; $this->province = $cache['province']; $this->country = $cache['country']; $this->timezone = $cache['timezone']; $this->postal = $cache['postal']; }
public static function maxmindValidateFilename($filename) { if (file_exists(ABSPATH . $filename)) { $filename = ABSPATH . $filename; } if (!is_readable($filename)) { return ''; } try { $reader = new \GeoIp2\Database\Reader($filename); $metadata = $reader->metadata(); $reader->close(); } catch (\Exception $e) { if (WP_DEBUG) { echo 'Error while creating reader for "' . $filename . '": ' . $e->getMessage(); } return ''; } return $filename; }
public static function maxmindValidateFilename($filename) { // Maybe make path absolute if (file_exists(ABSPATH . $filename)) { $filename = ABSPATH . $filename; } if (!is_readable($filename)) { return ''; } try { $reader = new \GeoIp2\Database\Reader($filename); $metadata = $reader->metadata(); $reader->close(); } catch (\Exception $e) { if (WP_DEBUG) { echo printf(__('Error while creating reader for "%s": %s', 'geoip-detect'), $filename, $e->getMessage()); } return ''; } return $filename; }
// Why? ;) $geocity = 'Columbus'; $geoprovince = 'OH'; $geocountry = 'US'; $geotimezone = 'America/New_York'; $geopostal = '43215'; } else{ $reader = new GeoIp2\Database\Reader(ROOT_PDIR . 'components/geographic-codes/libs/maxmind-geolite-db/GeoLite2-City.mmdb'); $profiler->record('Initialized GeoLite Database'); $geo = $reader->cityIspOrg(REMOTE_IP); //$geo = $reader->cityIspOrg('67.149.214.236'); $profiler->record('Read GeoLite Database'); $reader->close(); $profiler->record('Closed GeoLite Database'); $geocity = $geo->city->name; // Some IP addresses do not resolve as a valid province. //This tends to happen with privately owned networks. if(isset($geo->subdivisions[0]) && $geo->subdivisions[0] !== null){ /** @var GeoIp2\Record\Subdivision $geoprovinceobj */ $geoprovinceobj = $geo->subdivisions[0]; $geoprovince = $geoprovinceobj->isoCode; } else{ $geoprovince = ''; } $geocountry = $geo->country->isoCode;