-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateIP2Location.php
180 lines (160 loc) · 5.38 KB
/
updateIP2Location.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/php
<?php
require_once 'classes/class.IP2Location.php';
// Config section - use an administrative user when running the setup process
$dbhost='localhost';
$dbname='ip2location';
$dbuser='ip2location';
$dbpass=''; // host-based auth.
$ip2lapikey='PUTYOURAPIKEYHERE';
// These are specific to setup.
$createdbuser=TRUE; // True means we'll create a user named below and assign privs during setup.
$importuserspec="'ip2location'@'localhost'"; // MySQL format username. Set up your own auth and password outside of this script.
// Defaults.
$debug=0;
$reimport=FALSE;
while ( $arg = array_shift($argv) ) {
switch ( $arg ) {
case "-debug":
$debug++;
break;
case "-reimport":
// pull from an existing downloaded file.
$reimport=TRUE;
break;
case "setup":
// Connect to the database as an administrative user.
$db=new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// No token as we are just creating table sets.
$ip2lv4=new IP2Location5v4('', $db);
$ip2lv6=new IP2Location5v6('', $db);
$ip2pv4=new IP2Proxy11v4('', $db);
$ip2pv6=new IP2Proxy11v6('', $db);
$ip2av4=new IP2ASNv4('', $db);
$ip2av6=new IP2ASNv6('', $db);
// See if we need to create a user and call setup correctly.
if ( $createdbuser ) {
$ip2lv4->createDBUser($importuserspec);
$ip2lv6->createDBUser($importuserspec);
$ip2pv4->createDBUser($importuserspec);
$ip2pv6->createDBUser($importuserspec);
$ip2av4->createDBUser($importuserspec);
$ip2av6->createDBUser($importuserspec);
}
// Now create the tablesets.
$ip2lv4->createTableSet();
$ip2lv6->createTableSet();
$ip2pv4->createTableSet();
$ip2pv6->createTableSet();
$ip2av4->createTableSet();
$ip2av6->createTableSet();
// Clean up
$db->close();
print "Database setup complete. Confirm that:" .
" " . " - new tables exist in '$dbname'," .
" " . " - if requested, the dedicated user has been created (you must configure credentials and auth methods), and" .
" " . " - the user id that will perform the imports has privileges on the created tables." .
" " . "Once confirmed, reconfigure this script to use the proper user id for normal operation." . PHP_EOL;
exit;
break;
default:
$operation=$arg;
break;
}
}
//
// MAIN
//
// connect as the user with privs to do the import.
$db=new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$db->set_charset('utf8');
// Create an IP2Location object with a token and a database connection.
$ip2lv4=new IP2Location5v4($ip2lapikey,$db);
$ip2lv6=new IP2Location5v6($ip2lapikey,$db);
$ip2pv4=new IP2Proxy11v4($ip2lapikey,$db);
$ip2pv6=new IP2Proxy11v6($ip2lapikey,$db);
$ip2av4=new IP2ASNv4($ip2lapikey,$db);
$ip2av6=new IP2ASNv6($ip2lapikey,$db);
// Turn on debug to see output.
$ip2lv4->setDebugLevel($debug);
$ip2lv6->setDebugLevel($debug);
$ip2pv4->setDebugLevel($debug);
$ip2pv6->setDebugLevel($debug);
$ip2av4->setDebugLevel($debug);
$ip2av6->setDebugLevel($debug);
switch ( $operation ) {
case "restore":
try {
$ip2lv4->restoreBackup();
$ip2lv6->restoreBackup();
$ip2pv4->restoreBackup();
$ip2pv6->restoreBackup();
$ip2av4->restoreBackup();
$ip2av6->restoreBackup();
} catch (exception $e) {
print "Unable to restore previous backup table in one or more packages." . PHP_EOL;
exit(1);
}
break;
case "rowcount":
$pkg['DB5LITE']=$ip2lv4->getRowcounts();
$pkg['DB5LITEIPV6']=$ip2lv6->getRowcounts();
$pkg['PX11LITE']=$ip2pv4->getRowcounts();
$pkg['PX11LITEIPV6']=$ip2pv6->getRowcounts();
foreach ( array_keys($pkg) as $pkgname ) {
foreach ( array("stage", "current", "backup") as $suffix ) {
print "Table " . $pkgname . "_" . $suffix . " contains " . $pkg[$pkgname][$suffix] . " rows." . PHP_EOL;
}
}
break;
case "update":
// Download the new file.
if ( ! $reimport ) {
$ip2lv4->download();
$ip2lv6->download();
$ip2pv4->download();
$ip2pv6->download();
$ip2av4->download();
$ip2av6->download();
}
// Extract the CSV
$ip2lv4->extract();
$ip2lv6->extract();
$ip2pv4->extract();
$ip2pv6->extract();
$ip2av4->extract();
$ip2av6->extract();
// Import it into the newip2location5 table
$loadouts[]=$ip2lv4->loadCSV();
$loadouts[]=$ip2lv6->loadCSV();
$loadouts[]=$ip2pv4->loadCSV();
$loadouts[]=$ip2pv6->loadCSV();
$loadouts[]=$ip2av4->loadCSV();
$loadouts[]=$ip2av6->loadCSV();
foreach ( $loadouts as $loadout ) {
// If the imported rows matches the rows in the table and no warnings, activate.
if ( $loadout['warnings'] != 0 ) {
print 'Error during IP2Location database import!' . PHP_EOL;
exit(1);
}
}
// Trim the mapped v4's from the v6 tables.
$ip2lv6->deleteMappedv4();
$ip2pv6->deleteMappedv4();
// Shorten the countries to more readable names
$ip2lv4->shortenCountries();
$ip2lv6->shortenCountries();
$ip2pv4->shortenCountries();
$ip2pv6->shortenCountries();
// Activate.
$ip2lv4->activateStage();
$ip2lv6->activateStage();
$ip2pv4->activateStage();
$ip2pv6->activateStage();
$ip2av4->activateStage();
$ip2av6->activateStage();
break;
default:
print "One argument of [update|restore|rowcount] required." . PHP_EOL;
}
?>