コード例 #1
0
 public static function get_as_data_maxmind($keep_temporary_file = false)
 {
     // path and filename of source files at maxmind
     define('MAXMIND_PATH', 'http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum2.zip');
     define('MAXMIND_CSV_FILENAME', 'GeoIPASNum2.csv');
     // check whether there is (maybe) enough memory
     echo 'memory_limit = ' . ini_get('memory_limit') . "<br />\n";
     #if () {
     #}
     // download temporary copy of maxmind zip file
     $temporary_file_path = '';
     $zip_download = ZipDownloader::temporary_zip_download(MAXMIND_PATH, 'GeoIPASNum2', '');
     echo $zip_download['success_message'] . '<br />';
     // read content of csv file in temporary zip file (if download successful)
     if ($zip_download['successful']) {
         echo 'Total time for download: ' . $zip_download['total_time'] . '<br />';
         echo 'Size of downloaded file: ' . $zip_download['size_download'] . '<br />';
         $zip_handle = new ZipArchive();
         if ($zip_handle->open($temporary_file_path . $zip_download['temp_file_name'])) {
             $csv_file_handle = $zip_handle->getStream(MAXMIND_CSV_FILENAME);
             if (!$csv_file_handle) {
                 exit('Error: could not read CSV file within ZIP file.<br />');
             }
             // put data into an array
             $as_data = array();
             while (($data = fgetcsv($csv_file_handle, 0, ',', '"')) !== false) {
                 if (count($data) !== 3) {
                     exit('Error: CSV file structure faulty.');
                 }
                 $new_entry = array();
                 $new_entry['ip_from'] = $data[0];
                 $new_entry['ip_to'] = $data[1];
                 $as_number_and_name = $data[2];
                 $pattern_asn = '/AS\\d+/';
                 preg_match($pattern_asn, $as_number_and_name, $parsed_asn);
                 $new_entry['number'] = substr($parsed_asn[0], 2);
                 $pattern_name = '/\\s.+/';
                 preg_match($pattern_name, $as_number_and_name, $parsed_name);
                 $new_entry['name'] = substr($parsed_name[0], 1);
                 $as_data[] = $new_entry;
             }
             fclose($csv_file_handle);
             echo 'Data successfully extracted: ' . count($as_data) . ' lines.<br />';
         } else {
             echo 'Error: Could not open downloaded temporary copy of ZIP file.<br />';
         }
         // delete temporary zip file if specified by parameter
         if (!$keep_temporary_file) {
             unlink($temporary_file_path . $temporary_file_name);
         }
     }
     #for ($i = count($as_data) - 1; $i > 230677; $i--) {
     #    echo $as_data[$i]['ip_from'] . ' // ' . $as_data[$i]['ip_to'] . ' // ' . $as_data[$i]['number'] . ' // ' . $as_data[$i]['name'] . '<br />';
     #}
     /*
     for ($i = 0; $i < count($as_data); $i++) {
         if ($as_data[$i]['number'] == NULL) {
     echo 'Fehler: ' . $i . '<br />';
     echo $as_data[$i - 1]['number'] . '<br />';
     echo $as_data[$i]['name'] . '<br />';
     echo $as_data[$i + 1]['number'] . '<br />';
         }
     }
     */
     return $as_data;
 }
 public static function get_geolocation_data_maxmind($keep_temporary_file = false)
 {
     // path and filename of source files at ip2location
     define('MAXMIND_PATH', 'http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip');
     define('MAXMIND_CSV_FILENAME', 'GeoIPCountryWhois.csv');
     // download temporary copy of maxmind GeoLite zip file
     $temporary_file_path = '';
     $zip_download = ZipDownloader::temporary_zip_download(MAXMIND_PATH, 'Maxmind-GeoLite-GeoIPCountry', '');
     echo $zip_download['success_message'] . '<br />';
     // read content of csv file in temporary zip file (if download successful)
     if ($zip_download['successful']) {
         echo 'Total time for download: ' . $zip_download['total_time'] . '<br />';
         echo 'Size of downloaded file: ' . $zip_download['size_download'] . '<br />';
         $zip_handle = new ZipArchive();
         if ($zip_handle->open($temporary_file_path . $zip_download['temp_file_name'])) {
             $csv_file_handle = $zip_handle->getStream(MAXMIND_CSV_FILENAME);
             if (!$csv_file_handle) {
                 exit('Error: could not read CSV file within ZIP file.<br />');
             }
             // put data into an array
             $geolocation_data = array();
             while (($data = fgetcsv($csv_file_handle, 0, ',', '"')) !== false) {
                 if (count($data) !== 6) {
                     exit('Error: CSV file structure faulty.');
                 }
                 $new_entry = array();
                 $new_entry['ip_from'] = floatval($data[2]);
                 $new_entry['ip_to'] = floatval($data[3]);
                 if ($data[4] == 'A1' || $data[4] == 'A2' || $data[4] == 'AP') {
                     // A1 stands for "Anonymous Proxy"
                     // A2 stands for "satellite provider"
                     // AP stands for "Asia/Pacific Region"
                     $new_entry['country'] = NULL;
                 } else {
                     $new_entry['country'] = $data[4];
                 }
                 $new_entry['assigning_rir'] = NULL;
                 $new_entry['assigning_date'] = NULL;
                 $geolocation_data[] = $new_entry;
             }
             fclose($csv_file_handle);
             echo 'Data successfully extracted: ' . count($geolocation_data) . ' lines.<br />';
         } else {
             echo 'Error: Could not open downloaded temporary copy of ZIP file.<br />';
         }
         // delete temporary zip file if specified by parameter
         if (!$keep_temporary_file) {
             unlink($temporary_file_path . $zip_download['temp_file_name']);
         }
     }
     return $geolocation_data;
 }