<?php
namespace XFDev\ProxyCheck;
class Listener
{
public static function proxyCheck(\XF\Entity\User &$visitor)
{
if($visitor->user_id != 0)
{
if (self::proxy_exclude($_SERVER["REMOTE_ADDR"]) == false ) {
$ip = self::getUserIP();
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://blackbox.ipinfo.app/lookup/'.$ip);
$result = curl_exec($ch);
curl_close($ch);
if($result == 'Y' or $result == 'X'){
if( ($visitor->is_admin || $visitor->is_moderator) == false) {
//\XF::dump($visitor);
echo "Proxy, VPN or Data center IP ($ip) are not allowed here";
exit();
}
}
}
}
}
public static function proxy_exclude($ip) {
$ip1 = gethostbyname($_SERVER["REMOTE_ADDR"]);
$res = false;
if($ip1 == '127.0.0.1' || $ip1 == '::1') $res = true;
return $res;
}
public static function getUserIP()
{
// Get real visitor IP behind CloudFlare network
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
$_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
return $ip;
}
}