forked from leftypol/leftypol
functions.php: use updated config format
This commit is contained in:
parent
1cc87c31aa
commit
f008f8879d
1 changed files with 44 additions and 36 deletions
|
@ -1830,43 +1830,51 @@ function buildJavascript() {
|
|||
function checkIPAPI(string $ip): bool {
|
||||
global $config;
|
||||
|
||||
// Query IPHub's database with the poster's IP.
|
||||
if ($config['iphub_key'] && !empty($config['iphub_key'])) {
|
||||
if (array_search($ip, $config['iphub_whitelisted_ips']) !== false) {
|
||||
// IP is whitelisted, don't bother querying.
|
||||
return false;
|
||||
}
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, "http://v2.api.iphub.info/ip/$ip");
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [ "X-Key: {$config['iphub_key']}" ]);
|
||||
$reply = curl_exec($ch);
|
||||
if ($reply === false) {
|
||||
$curl_err = curl_error($ch);
|
||||
curl_close($ch);
|
||||
error_log("IPHub query failed: api call failed with curl error $curl_err");
|
||||
return false;
|
||||
}
|
||||
curl_close($ch);
|
||||
if (empty($reply)) {
|
||||
error_log('IPHub query failed: api returned an empty response');
|
||||
return false;
|
||||
}
|
||||
|
||||
$json = json_decode($reply);
|
||||
if (isset($json->error)) {
|
||||
error_log("IPHub query failed: $json->error");
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($json->block == 1
|
||||
|| array_search($json->asn, $config['iphub_banned_asns']) !== false
|
||||
|| array_search($json->isp, $config['iphub_banned_isps']) !== false) {
|
||||
error($config['error']['proxy']);
|
||||
return true;
|
||||
}
|
||||
if (!$config['ip_api']['iphub']['enabled']) {
|
||||
return false;
|
||||
}
|
||||
$iphub_key = $config['ip_api']['iphub']['key'];
|
||||
if (empty($iphub_key)) {
|
||||
error_log('IP api backend is enabled but IPHub API is empty!');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Query IPHub's database with the poster's IP.
|
||||
if (array_search($ip, $config['ip_api']['ip_whitelist']) !== false) {
|
||||
// IP is whitelisted, don't bother querying.
|
||||
return false;
|
||||
}
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, "http://v2.api.iphub.info/ip/$ip");
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [ "X-Key: $iphub_key" ]);
|
||||
$reply = curl_exec($ch);
|
||||
if ($reply === false) {
|
||||
$curl_err = curl_error($ch);
|
||||
curl_close($ch);
|
||||
error_log("IPHub query failed: api call failed with curl error $curl_err");
|
||||
return false;
|
||||
}
|
||||
curl_close($ch);
|
||||
if (empty($reply)) {
|
||||
error_log('IPHub query failed: api returned an empty response');
|
||||
return false;
|
||||
}
|
||||
|
||||
$json = json_decode($reply);
|
||||
if (isset($json->error)) {
|
||||
error_log("IPHub query failed: $json->error");
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($json->block == 1
|
||||
|| array_search($json->isp, $config['ip_api']['isps_blacklist']) !== false
|
||||
|| array_search($json->asn, $config['ip_api']['asns_blacklist']) !== false) {
|
||||
error($config['error']['proxy']);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue