Skip to content

Commit 5851116

Browse files
committed
- Remove queries to DB-IP API.
1 parent 92164d3 commit 5851116

File tree

3 files changed

+52
-81
lines changed

3 files changed

+52
-81
lines changed

.env.example

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,3 @@ PUSHER_APP_CLUSTER=
3434

3535
GOOGLE_RECAPTCHA_SITE_SECRET=
3636
GOOGLE_RECAPTCHA_SITE_KEY=
37-
38-
DB_IP_API_KEY=

app/Helper.php

Lines changed: 52 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@ class Helper {
1111
static $pusher = null;
1212
static $lsr = null;
1313
static $tzs = null;
14-
static $db_ip_api_keys_info = [];
15-
static $db_ip_api_ips_info = [];
1614
static $db_ip_html_ips_info = [];
1715

18-
public static function getDbIpApiKey() {
19-
return env('DB_IP_API_KEY');
20-
}
21-
2216
public static function getPusherOptions() {
2317
return [
2418
'cluster' => env('PUSHER_APP_CLUSTER'),
@@ -231,70 +225,24 @@ public static function getRequestIp() {
231225
;
232226
}
233227

234-
public static function dbIpGetApiKeyInfo($api_key) {
235-
if(isset(Helper::$db_ip_api_keys_info[$api_key])):
236-
return Helper::$db_ip_api_keys_info[$api_key];
237-
endif;
238-
239-
$api_key_info = json_decode(@file_get_contents("http://api.db-ip.com/v2/$api_key/"));
240-
241-
return Helper::$db_ip_api_keys_info[$api_key] = is_object($api_key_info) && property_exists($api_key_info, 'queriesLeft') && $api_key_info->queriesLeft > 0
242-
? $api_key_info
243-
: null
244-
;
245-
}
246-
247-
public static function dbIpDecorateResponse($ip_info, $ip, $api_key = null) {
228+
public static function dbIpDecorateResponse($ip_info, $ip) {
248229
if(!is_object($ip_info)):
249230
return $ip_info;
250231
endif;
251232
$ip_info->Ip = $ip;
252-
// $ip_info->ApiKey = $api_key;
253233
return $ip_info;
254234
}
255235

256-
public static function dbIpGetIpInfo($ip, $api_key = null) {
236+
public static function dbIpGetIpInfo($ip) {
257237
$ip_from_html = Helper::dbIpGetIpInfoFromHtml($ip);
258238

259239
if($ip_from_html):
260-
return Helper::dbIpDecorateResponse($ip_from_html, $ip, $api_key);
261-
endif;
262-
263-
$ip_from_api = Helper::dbIpGetIpInfoFromApi($ip, $api_key);
264-
265-
if($ip_from_api):
266-
return Helper::dbIpDecorateResponse($ip_from_api, $ip, $api_key);
240+
return Helper::dbIpDecorateResponse($ip_from_html, $ip);
267241
endif;
268242

269243
return false;
270244
}
271245

272-
public static function dbIpGetIpInfoFromApi($ip, $api_key = null) {
273-
if(isset(Helper::$db_ip_api_ips_info[$ip])):
274-
return Helper::$db_ip_api_ips_info[$ip];
275-
endif;
276-
277-
$start_time = -Helper::ms();
278-
279-
if($api_key === null):
280-
$api_key = Helper::getDbIpApiKey();
281-
endif;
282-
$api_key_info = Helper::dbIpGetApiKeyInfo($api_key);
283-
284-
if(!is_object($api_key_info)):
285-
return false;
286-
endif;
287-
288-
$ip_info = json_decode(@file_get_contents("http://api.db-ip.com/v2/{$api_key_info->apiKey}/$ip"));
289-
290-
if(!is_object($ip_info)):
291-
return false;
292-
endif;
293-
294-
$ip_info->Elapsed = $start_time + Helper::ms();
295-
return Helper::$db_ip_api_ips_info[$ip] = Helper::dbIpDecorateResponse($ip_info, $ip, $api_key);
296-
}
297-
298246
public static function dbIpGetIpInfoFromHtml($ip) {
299247
if(isset(Helper::$db_ip_html_ips_info[$ip])):
300248
return Helper::$db_ip_html_ips_info[$ip];
@@ -308,24 +256,51 @@ public static function dbIpGetIpInfoFromHtml($ip) {
308256

309257
$tables = $dom->getElementsByTagName('table');
310258

311-
foreach($tables as $table):
259+
foreach($tables as $table_index => $table):
312260
$trs = $table->getElementsByTagName('tr');
313261

314-
foreach($trs as $tr):
315-
$th = $tr->getElementsByTagName('th');
316-
$td = $tr->getElementsByTagName('td');
317-
318-
if(!($th->length === 1 && $td->length === 1)):
319-
continue;
320-
endif;
262+
if($table_index === 1):
263+
foreach($trs as $tr_index => $tr):
264+
$th = $tr->getElementsByTagName('th');
265+
$td = $tr->getElementsByTagName('td');
266+
267+
if($tr_index === 0):
268+
if(!(
269+
$th->length === 3 &&
270+
$td->length === 0 &&
271+
$th->item(0)->textContent === 'Crawler' &&
272+
$th->item(1)->textContent === 'Proxy' &&
273+
$th->item(2)->textContent === 'Attack source'
274+
)):
275+
break;
276+
endif;
277+
else:
278+
if(!($th->length === 0 && $td->length === 3)):
279+
break;
280+
endif;
281+
282+
$props['Security / Crawler'] = preg_replace('#^[\x09\xa0\xc2]*|[\x09\xa0\xc2]*$#', '', trim($td->item(0)->textContent)) === 'Yes';
283+
$props['Security / Proxy'] = preg_replace('#^[\x09\xa0\xc2]*|[\x09\xa0\xc2]*$#', '', trim($td->item(1)->textContent)) === 'Yes';
284+
$props['Security / Attack source'] = preg_replace('#^[\x09\xa0\xc2]*|[\x09\xa0\xc2]*$#', '', trim($td->item(2)->textContent)) === 'Yes';
285+
endif;
286+
endforeach;
287+
else:
288+
foreach($trs as $tr_index => $tr):
289+
$th = $tr->getElementsByTagName('th');
290+
$td = $tr->getElementsByTagName('td');
291+
292+
if(!($th->length === 1 && $td->length === 1)):
293+
continue;
294+
endif;
321295

322-
$text_th = preg_replace('#^[\x09\xa0\xc2]*|[\x09\xa0\xc2]*$#', '', trim($th->item(0)->textContent));
323-
$text_td = preg_replace('#^[\x09\xa0\xc2]*|[\x09\xa0\xc2]*$#', '', trim($td->item(0)->textContent));
296+
$text_th = preg_replace('#^[\x09\xa0\xc2]*|[\x09\xa0\xc2]*$#', '', trim($th->item(0)->textContent));
297+
$text_td = preg_replace('#^[\x09\xa0\xc2]*|[\x09\xa0\xc2]*$#', '', trim($td->item(0)->textContent));
324298

325-
if($text_th && $text_td):
326-
$props[$text_th] = $text_td;
327-
endif;
328-
endforeach;
299+
if($text_th && $text_td):
300+
$props[$text_th] = $text_td;
301+
endif;
302+
endforeach;
303+
endif;
329304
endforeach;
330305

331306
if(count($props) < 1):
@@ -345,4 +320,12 @@ public static function dbIpGetIpInfoFromHtml($ip) {
345320
public static function ms() {
346321
return microtime(true) * 1000 | 0;
347322
}
323+
324+
public static function readDotEnvFile() {
325+
return parse_ini_file(
326+
dirname($_SERVER['DOCUMENT_ROOT']) . "/.env",
327+
false,
328+
INI_SCANNER_RAW
329+
);
330+
}
348331
}

readme.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Pre-requisites
1212
- PHP v5.6+
1313
- Pusher
1414
- GoogleRecaptcha
15-
- Db-Ip
1615

1716
Building
1817
========
@@ -117,20 +116,11 @@ GOOGLE_RECAPTCHA_SITE_SECRET=
117116
GOOGLE_RECAPTCHA_SITE_KEY=
118117
```
119118

120-
# Db-Ip
121-
122-
Register at [db-ip][db-ip-url] to get your key. Edit the `.env` file afterwards.
123-
124-
```
125-
DB_IP_API_KEY=
126-
```
127-
128119
License
129120
=======
130121

131122
[MIT][LICENSE]
132123

133-
[db-ip-url]: https://db-ip.com/login/
134124
[google-recaptcha-url]: https://www.google.com/recaptcha/admin#list
135125
[pusher-url]: https://dashboard.pusher.com/accounts/sign_in
136126
[composer-url]: https://getcomposer.org/

0 commit comments

Comments
 (0)