forked from leftypol/leftypol
functions.php: refactor IPHub
This commit is contained in:
parent
44e07295e5
commit
e7bae1d972
1 changed files with 24 additions and 18 deletions
|
@ -1823,40 +1823,46 @@ function buildJavascript() {
|
|||
file_write($config['file_script'], $script);
|
||||
}
|
||||
|
||||
function checkIPAPI() {
|
||||
/**
|
||||
* @param string $ip The IP to check.
|
||||
* @return bool True if the IP should be blocked.
|
||||
*/
|
||||
function checkIPAPI(string $ip): bool {
|
||||
global $config;
|
||||
|
||||
// query IPHub's database with the poster's IP
|
||||
if (isset($config['iphub_key']) && $config['iphub_key'] !== "") {
|
||||
if (isset($config['iphub_whitelisted_ips']) && array_search($_SERVER['REMOTE_ADDR'], $config['iphub_whitelisted_ips']) !== false) {
|
||||
// IP is whitelisted, don't bother querying
|
||||
error("return true;");
|
||||
// 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/" . $_SERVER['REMOTE_ADDR']);
|
||||
curl_setopt($ch, CURLOPT_URL, "http://v2.api.iphub.info/ip/$ip");
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Key: " . $config['iphub_key']));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [ "X-Key: {$config['iphub_key']}" ]);
|
||||
$reply = curl_exec($ch);
|
||||
if ($reply == "") {
|
||||
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)) {
|
||||
$logdata = array();
|
||||
$logdata['time'] = date(DATE_ATOM);
|
||||
$logdata['action'] = 'iphub_query';
|
||||
$logdata['msg'] = $json->error;
|
||||
|
||||
$logline = json_encode($logdata);
|
||||
error_log($logline);
|
||||
error_log("IPHub query failed: $json->error");
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($json->block == 1
|
||||
|| (isset($config['iphub_banned_asns']) && array_search($json->asn, $config['iphub_banned_asns']) !== false)
|
||||
|| (isset($config['iphub_banned_isps']) && array_search($json->isp, $config['iphub_banned_isps']) !== false)) {
|
||||
|| array_search($json->asn, $config['iphub_banned_asns']) !== false
|
||||
|| array_search($json->isp, $config['iphub_banned_isps']) !== false) {
|
||||
error($config['error']['proxy']);
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue