-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip2location.php
executable file
·101 lines (78 loc) · 3.46 KB
/
ip2location.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
ini_set('memory_limit', '-1');
ini_set("display_errors", true);
ini_set("error_reporting", E_ALL);
require_once __DIR__ . '/ip2location.usage.php';
require_once __DIR__ . '/vendor/autoload.php';
try {
// initialization of the parameters
if (isset($options['input'])) {
$source = $options['input'];
if (!is_readable($source)) { throw new \Exception("Input file not found or readable!"); }
}
else { throw new \Exception("Missing <input> argument!"); }
if (isset($options['ip'])) {
$ipfield = intval($options['ip']);
if ($ipfield == 0) { throw new \Exception("Invalid value for argument <ip>!"); }
}
else { $ipfield = 1; }
$ipfield -= 1; // 0 based for array index
if (isset($options['output'])) { $destination = $options['output']; }
else { throw new \Exception("Missing <output> argument!"); }
if (isset($options['delimiter'])) { $delimiter = $options['delimiter']; }
else { $delimiter = "\t"; }
if (isset($options['database'])) { $delimiter = $options['database']; }
else { $database = __DIR__ . '/databases/GeoLite2-City.mmdb'; }
$discardNoState = isset($options['discard-no-us-states']);
$keepAllStates = isset($options['keep-all-states']);
$countryOnly = isset($options['country-only']);
// initialize GeoIp database reader
$reader = new \GeoIp2\Database\Reader($database, ['en'], true);
// load whole input file to memory
echo "Loading input file to memory\n";
$memfile = 'php://memory';
$memfin = new \SplFileObject($memfile, 'w+');
$memfin->fwrite(file_get_contents($source));
echo "Reading data from memory\n";
$memfin->rewind();
$memfin->setFlags(\SPLFileObject::SKIP_EMPTY | \SPLFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD | \SplFileObject::READ_CSV);
$memfin->setCsvControl($delimiter);
$content = '';
echo "Parsing data\n";
$previousIp = -1;
foreach ($memfin as $rawsources) {
$ip = $rawsources[$ipfield];
if ($previousIp != $ip) {
$state = '';
$country = '';
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
try {
$record = $reader->city($ip); // throw an exception if not found
$country = strtoupper($record->country->isoCode);
$state = strtoupper($record->mostSpecificSubdivision->isoCode);
$country = preg_match('/^[A-Z]{2}$/', $country) == 1 ? $country : '';
$state = !$keepAllStates && $country != 'US' || preg_match('/^[A-Z]{2}$/', $state) != 1 ? '' : $state;
if (empty($state) && $discardNoState) {
$country = '';
$state = '';
}
} catch (\Exception $e) {
$state = '';
$country = '';
}
}
}
if ($countryOnly) {
$content .= implode($delimiter, $rawsources) . $delimiter . strtoupper($country) . PHP_EOL;
} else {
$content .= implode($delimiter, $rawsources) . $delimiter . strtoupper($country) . $delimiter . strtoupper($state) . PHP_EOL;
}
$previousIp = $ip;
}
// write content to file
echo "Writing content to output file\n";
file_put_contents($destination, $content);
} catch (\Exception $e) {
print $e->getMessage() . PHP_EOL;
}
echo "Done.\n";