Ejemplo n.º 1
1
 /**
  * {@inheritDoc}
  */
 public function geocode($address)
 {
     if (!filter_var($address, FILTER_VALIDATE_IP)) {
         throw new UnsupportedOperation('The Geoip provider does not support street addresses, only IPv4 addresses.');
     }
     // This API does not support IPv6
     if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
         throw new UnsupportedOperation('The Geoip provider does not support IPv6 addresses, only IPv4 addresses.');
     }
     if ('127.0.0.1' === $address) {
         return $this->returnResults([$this->getLocalhostDefaults()]);
     }
     $results = @geoip_record_by_name($address);
     if (!is_array($results)) {
         throw new NoResult(sprintf('Could not find "%s" IP address in database.', $address));
     }
     if (!empty($results['region']) && !empty($results['country_code'])) {
         $timezone = @geoip_time_zone_by_country_and_region($results['country_code'], $results['region']) ?: null;
         $region = @geoip_region_name_by_code($results['country_code'], $results['region']) ?: $results['region'];
     } else {
         $timezone = null;
         $region = $results['region'];
     }
     return $this->returnResults([$this->fixEncoding(array_merge($this->getDefaults(), ['latitude' => $results['latitude'], 'longitude' => $results['longitude'], 'locality' => $results['city'], 'postalCode' => $results['postal_code'], 'adminLevels' => $results['region'] ? [['name' => $region, 'code' => $results['region'], 'level' => 1]] : [], 'country' => $results['country_name'], 'countryCode' => $results['country_code'], 'timezone' => $timezone]))]);
 }
Ejemplo n.º 2
0
 /**
  *	Return geolocation data based on specified/auto-detected IP address
  *	@return array|FALSE
  *	@param $ip string
  **/
 function location($ip = NULL)
 {
     $fw = \Base::instance();
     $web = \Web::instance();
     if (!$ip) {
         $ip = $fw->get('IP');
     }
     $public = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_NO_PRIV_RANGE);
     if (function_exists('geoip_db_avail') && geoip_db_avail(GEOIP_CITY_EDITION_REV1) && ($out = @geoip_record_by_name($ip))) {
         $out['request'] = $ip;
         $out['region_code'] = $out['region'];
         $out['region_name'] = geoip_region_name_by_code($out['country_code'], $out['region']);
         unset($out['country_code3'], $out['region'], $out['postal_code']);
         return $out;
     }
     if (($req = $web->request('http://www.geoplugin.net/json.gp' . ($public ? '?ip=' . $ip : ''))) && ($data = json_decode($req['body'], TRUE))) {
         $out = array();
         foreach ($data as $key => $val) {
             if (!strpos($key, 'currency') && $key !== 'geoplugin_status' && $key !== 'geoplugin_region') {
                 $out[$fw->snakecase(substr($key, 10))] = $val;
             }
         }
         return $out;
     }
     return FALSE;
 }
Ejemplo n.º 3
0
 /**
  * {@inheritDoc}
  */
 public function getGeocodedData($address, $boundingBox = null)
 {
     if (!filter_var($address, FILTER_VALIDATE_IP)) {
         throw new UnsupportedException('The GeoipProvider does not support Street addresses.');
     }
     // This API does not support IPv6
     if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
         throw new UnsupportedException('The GeoipProvider does not support IPv6 addresses.');
     }
     if ('127.0.0.1' === $address) {
         return $this->getLocalhostDefaults();
     }
     $results = @geoip_record_by_name($address);
     if (!is_array($results)) {
         throw new NoResultException(sprintf('Could not find %s ip address in database.', $address));
     }
     $timezone = @geoip_time_zone_by_country_and_region($results['country_code'], $results['region']) ?: null;
     $region = @geoip_region_name_by_code($results['country_code'], $results['region']) ?: $results['region'];
     return array_merge($this->getDefaults(), array('latitude' => $results['latitude'], 'longitude' => $results['longitude'], 'city' => $results['city'], 'zipcode' => $results['postal_code'], 'region' => $region, 'regionCode' => $results['region'], 'country' => $results['country_name'], 'countryCode' => $results['country_code'], 'timezone' => $timezone));
 }
Ejemplo n.º 4
0
 /**
  * @return string|bool
  */
 public function getRegionName()
 {
     $result = false;
     $country = $this->getCountryCode();
     $region = $this->getRegion();
     if (!empty($country) && !empty($region)) {
         $result = geoip_region_name_by_code($country, $region);
     }
     return $result;
 }
Ejemplo n.º 5
0
 public function record($ipaddress)
 {
     if (!$this->_extension) {
         global $GEOIP_REGION_NAME;
     }
     if ($this->_config->internalcache && array_key_exists($ipaddress, $this->_cache)) {
         return $this->_cache[$ipaddress];
     }
     if ($this->_extension) {
         $rec = (object) geoip_record_by_name($ipaddress);
     } else {
         $rec = geoip_record_by_addr($this->_geoinstance, $ipaddress);
     }
     if ($rec) {
         if ($this->_extension) {
             $rec->region = geoip_region_name_by_code($rec->country_code, $rec->region);
         } else {
             $rec->region = $GEOIP_REGION_NAME[$rec->country_code][$rec->region];
         }
     }
     if ($this->_config->internalcache) {
         return $this->_cache[$ipaddress] = $rec;
     }
     return $rec;
 }
Ejemplo n.º 6
0
 /**
  * getRegion
  * 
  * Returns the region for the set IP, such as Quebec or California.
  * 
  * @notes  will only act as a province/state lookup for certain regions
  *         (eg. Canada & US)
  * @access protected
  * @static
  * @return string
  */
 protected static function getRegion()
 {
     return geoip_region_name_by_code(self::getCountryCode(2), self::_getDetail('region'));
 }