Пример #1
16
function get_geodata($ip)
{
    require _TRACK_LIB_PATH . "/maxmind/geoipregionvars.php";
    // названия регионов
    if (defined('_PHP5_GEOIP_ENABLED') && _PHP5_GEOIP_ENABLED || function_exists('geoip_record_by_name')) {
        $geoinfo = geoip_record_by_name($ip);
        $ispname = geoip_isp_by_name($ip);
        $cur_city = $geoinfo['city'];
        $cur_region = $geoinfo['region'];
        $cur_country = $geoinfo['country_code'];
    } else {
        require _TRACK_LIB_PATH . "/maxmind/geoip.inc.php";
        require _TRACK_LIB_PATH . "/maxmind/geoipcity.inc.php";
        $gi = geoip_open(_TRACK_STATIC_PATH . "/maxmind/MaxmindCity.dat", GEOIP_STANDARD);
        $record = geoip_record_by_addr($gi, $ip);
        $ispname = geoip_org_by_addr($gi, $ip);
        geoip_close($gi);
        $cur_city = $record->city;
        $cur_region = $record->region;
        $cur_country = $record->country_code;
        // Resolve GeoIP extension conflict
        if (function_exists('geoip_country_code_by_name') && $cur_country == '') {
            $cur_country = geoip_country_code_by_name($ip);
        }
    }
    return array('country' => $cur_country, 'region' => $cur_region, 'state' => $GEOIP_REGION_NAME[$cur_country][$cur_region], 'city' => $cur_city, 'isp' => $ispname);
}
Пример #2
0
 /**
  * Uses the GeoIP PECL module to get a visitor's location based on their IP address.
  * 
  * This function will return different results based on the data available. If a city
  * database can be detected by the PECL module, it may return the country code,
  * region code, city name, area code, latitude, longitude and postal code of the visitor.
  * 
  * Alternatively, if only the country database can be detected, only the country code
  * will be returned.
  * 
  * The GeoIP PECL module will detect the following filenames:
  * - GeoIP.dat
  * - GeoIPCity.dat
  * - GeoIPISP.dat
  * - GeoIPOrg.dat
  * 
  * Note how GeoLiteCity.dat, the name for the GeoLite city database, is not detected
  * by the PECL module.
  * 
  * @param array $info Must have an 'ip' field.
  * @return array
  */
 public function getLocation($info)
 {
     $ip = $this->getIpFromInfo($info);
     $result = array();
     // get location data
     if (self::isCityDatabaseAvailable()) {
         // Must hide errors because missing IPV6:
         $location = @geoip_record_by_name($ip);
         if (!empty($location)) {
             $result[self::COUNTRY_CODE_KEY] = $location['country_code'];
             $result[self::REGION_CODE_KEY] = $location['region'];
             $result[self::CITY_NAME_KEY] = utf8_encode($location['city']);
             $result[self::AREA_CODE_KEY] = $location['area_code'];
             $result[self::LATITUDE_KEY] = $location['latitude'];
             $result[self::LONGITUDE_KEY] = $location['longitude'];
             $result[self::POSTAL_CODE_KEY] = $location['postal_code'];
         }
     } else {
         if (self::isRegionDatabaseAvailable()) {
             $location = @geoip_region_by_name($ip);
             if (!empty($location)) {
                 $result[self::REGION_CODE_KEY] = $location['region'];
                 $result[self::COUNTRY_CODE_KEY] = $location['country_code'];
             }
         } else {
             $result[self::COUNTRY_CODE_KEY] = @geoip_country_code_by_name($ip);
         }
     }
     // get organization data if the org database is available
     if (self::isOrgDatabaseAvailable()) {
         $org = @geoip_org_by_name($ip);
         if ($org !== false) {
             $result[self::ORG_KEY] = utf8_encode($org);
         }
     }
     // get isp data if the isp database is available
     if (self::isISPDatabaseAvailable()) {
         $isp = @geoip_isp_by_name($ip);
         if ($ip !== false) {
             $result[self::ISP_KEY] = utf8_encode($isp);
         }
     }
     if (empty($result)) {
         return false;
     }
     $this->completeLocationResult($result);
     return $result;
 }
Пример #3
0
 /**
  * Get GeoIp host information
  *
  * @return void
  */
 protected function getGeoIpHostInfo()
 {
     if (function_exists('geoip_db_get_all_info') && null !== $this->host && $this->host != '127.0.0.1' && $this->host != 'localhost') {
         // Get base info by city
         if ($this->databases['city']) {
             $data = geoip_record_by_name($this->host);
             $this->hostInfo['areaCode'] = $data['area_code'];
             $this->hostInfo['city'] = $data['city'];
             $this->hostInfo['continentCode'] = $data['continent_code'];
             $this->hostInfo['country'] = $data['country_name'];
             $this->hostInfo['countryCode'] = $data['country_code'];
             $this->hostInfo['countryCode3'] = $data['country_code3'];
             $this->hostInfo['dmaCode'] = $data['dma_code'];
             $this->hostInfo['latitude'] = $data['latitude'];
             $this->hostInfo['longitude'] = $data['longitude'];
             $this->hostInfo['postalCode'] = $data['postal_code'];
             $this->hostInfo['region'] = $data['region'];
             // Else, get base info by country
         } else {
             if ($this->databases['country']) {
                 $this->hostInfo['continentCode'] = geoip_continent_code_by_name($this->host);
                 $this->hostInfo['country'] = geoip_country_name_by_name($this->host);
                 $this->hostInfo['countryCode'] = geoip_country_code_by_name($this->host);
                 $this->hostInfo['countryCode3'] = geoip_country_code3_by_name($this->host);
             }
         }
         // If available, get ISP name
         if ($this->databases['isp']) {
             $this->hostInfo['isp'] = geoip_isp_by_name($this->host);
         }
         // If available, get internet connection speed
         if ($this->databases['netspeed']) {
             $netspeed = geoip_id_by_name($this->host);
             switch ($netspeed) {
                 case GEOIP_DIALUP_SPEED:
                     $this->hostInfo['netspeed'] = 'Dial-Up';
                     break;
                 case GEOIP_CABLEDSL_SPEED:
                     $this->hostInfo['netspeed'] = 'Cable/DSL';
                     break;
                 case GEOIP_CORPORATE_SPEED:
                     $this->hostInfo['netspeed'] = 'Corporate';
                     break;
                 default:
                     $this->hostInfo['netspeed'] = 'Unknown';
             }
         }
         // If available, get Organization name
         if ($this->databases['org']) {
             $this->hostInfo['org'] = geoip_org_by_name($this->host);
         }
     }
 }
 public function processIP($ip)
 {
     $status = null;
     $request['ip'] = $ip;
     $request['type'] = self::ipVersion($ip);
     if ($request['type'] == 'IPv4') {
         $isPrivate = self::isPrivateIP($ip);
         if ($isPrivate) {
             $status = self::setStatus('IP_ADDRESS_RESERVED', null, $status);
         }
         $record = geoip_record_by_name($ip);
     }
     $countryCode = null;
     if ($record && is_array($record)) {
         try {
             // fetch continent by continent_code
             $continents = Config::inst()->get('IPInfoCache', 'Continents');
             if (array_key_exists($record['continent_code'], $continents)) {
                 $result['location']['continent_code'] = $record['continent_code'];
             }
             if (isset($record['continent_code']) && isset($continents[$record['continent_code']])) {
                 $result['location']['continent_names']['en'] = $continents[$record['continent_code']];
             }
             $countryCode = $record['country_code'];
             $result['location']['country_code'] = $countryCode;
             $result['location']['country_names']['en'] = $record['country_name'];
             $result['location']['postal_code'] = $record['postal_code'];
             $result['location']['city_names']['en'] = $record['city'];
             $result['location']['latitude'] = $record['latitude'];
             $result['location']['longitude'] = $record['longitude'];
             $result['location']['time_zone'] = geoip_time_zone_by_country_and_region($record['country_code'], $record['region']);
         } catch (Exception $e) {
             $status = self::setStatus('GEOIP_EXCEPTION', $e, $status);
         }
     }
     // fetch ISP details
     if ($request['type'] == 'IPv4') {
         $record = geoip_isp_by_name($ip);
     }
     if ($record) {
         $result['organization']['name'] = $record;
         $result['organization']['isp'] = $record;
     }
     if ($status) {
         $statusArray['code'] = self::setStatus(null, null, $status);
         $statusArray['message'] = self::getStatusMessage($status);
         // do not cache a failure
         $this->json = json_encode(array('request' => $request, 'status' => $statusArray, 'result' => array('maxmind-geoip-legacy' => $result)));
         return null;
     } else {
         // return cached success message
         $statusArray['code'] = self::setStatus('SUCCESS_CACHED', null, $status);
         $statusArray['message'] = self::getStatusMessage($status);
         $this->json = json_encode(array('request' => $request, 'status' => $statusArray, 'result' => array('maxmind-geoip-legacy' => $result)));
     }
     // we write a different json object with a cached status to the DB
     $statusArray['code'] = self::setStatus('SUCCESS', null);
     $statusArray['message'] = self::getStatusMessage($statusArray['code']);
     $dbJson = json_encode(array('request' => $request, 'status' => $statusArray, 'result' => array('maxmind-geoip-legacy' => $result)));
     return $dbJson;
 }
Пример #5
0
}
try {
    $insert_sth = $dbconn->prepare($sql_event);
    $insert_sth->bindParam(":sensorid", $sensor_id);
    $insert_sth->bindParam(":PhaseATimestamp", $PhaseA['Timestamp']);
    $insert_sth->bindParam(":PhaseATimezone", $PhaseA['Timezone']);
    $insert_sth->bindParam(":PhaseADate", $PhaseA['Date']);
    $insert_sth->bindParam(":PhaseAUniqID", $PhaseA['UniqID']);
    $insert_sth->bindParam(":PhaseAClientIP", $PhaseA['ClientIP']);
    // Get Country Code of IP Address
    $ClientIPCC = geoip_country_code_by_name($PhaseA['ClientIP']);
    if (!$ClientIPCC) {
        $ClientIPCC = '';
    }
    // Get Country Code of IP ASN
    $ClientIPASN = str_ireplace('AS', "", strstr(geoip_isp_by_name($PhaseA['ClientIP']), ' ', true));
    if (!$ClientIPASN) {
        $ClientIPASN = '0';
    } elseif ($ClientIPASN == "") {
        $ClientIPASN = '0';
    }
    $insert_sth->bindParam(":PhaseAClientIPCC", $ClientIPCC);
    $insert_sth->bindParam(":PhaseAClientIPASN", $ClientIPASN);
    $insert_sth->bindParam(":PhaseASourcePort", $PhaseA['SourcePort']);
    $insert_sth->bindParam(":PhaseAServerIP", $PhaseA['ServerIP']);
    $insert_sth->bindParam(":PhaseAServerPort", $PhaseA['ServerPort']);
    $insert_sth->bindParam(":PhaseBMethod", $PhaseB['Method']);
    $insert_sth->bindParam(":PhaseBPath", $PhaseB['path']);
    $insert_sth->bindParam(":PhaseBPathParameter", $PhaseB['pathParameter']);
    $webHostID = getWebHostID($PhaseB['Host']);
    $insert_sth->bindParam(":PhaseBProtocol", $PhaseB['Protocol']);
Пример #6
0
 function _network()
 {
     return is_this_wifi(geoip_isp_by_name($_SERVER['REMOTE_ADDR'])) ? 'wifi' : '3g';
 }