Example #1
0
 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'];
 }
Example #2
0
if(Core::IsComponentAvailable('geographic-codes') && class_exists('GeoIp2\\Database\\Reader')){
	try{
		if(REMOTE_IP == '127.0.0.1'){
			// Load local connections up with Columbus, OH.
			// 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{