From f008f8879df39e31df7da29d5044636a700db24d Mon Sep 17 00:00:00 2001 From: Zankaria Date: Sun, 6 Oct 2024 12:43:22 +0200 Subject: [PATCH] functions.php: use updated config format --- inc/functions.php | 80 ++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 014c7be1..d9e8e588 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -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; }