forked from lucaslealdev/CSSTrackr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5459064
commit 4f80407
Showing
1 changed file
with
2 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,4 @@ | ||
<?php | ||
declare(strict_types=1); | ||
function ip_in_range(string $ip, string $range) : bool { | ||
if (strpos($range, '/') == false) | ||
$range .= '/32'; | ||
|
||
// $range is in IP/CIDR format eg 127.0.0.1/24 | ||
list($range, $netmask) = explode('/', $range, 2); | ||
$range_decimal = ip2long($range); | ||
$ip_decimal = ip2long($ip); | ||
$wildcard_decimal = pow(2, (32 - $netmask)) - 1; | ||
$netmask_decimal = ~ $wildcard_decimal; | ||
return (($ip_decimal & $netmask_decimal) == ($range_decimal & $netmask_decimal)); | ||
} | ||
|
||
function _cloudflare_CheckIP(string $ip) : bool { | ||
$cf_ips = array( | ||
'199.27.128.0/21', | ||
'173.245.48.0/20', | ||
'103.21.244.0/22', | ||
'103.22.200.0/22', | ||
'103.31.4.0/22', | ||
'141.101.64.0/18', | ||
'108.162.192.0/18', | ||
'190.93.240.0/20', | ||
'188.114.96.0/20', | ||
'197.234.240.0/22', | ||
'198.41.128.0/17', | ||
'162.158.0.0/15', | ||
'104.16.0.0/12', | ||
); | ||
$is_cf_ip = false; | ||
foreach ($cf_ips as $cf_ip) { | ||
if (ip_in_range($ip, $cf_ip)) { | ||
$is_cf_ip = true; | ||
break; | ||
} | ||
} return $is_cf_ip; | ||
} | ||
|
||
function _cloudflare_Requests_Check() : bool { | ||
$flag = true; | ||
|
||
if(!isset($_SERVER['HTTP_CF_CONNECTING_IP'])) $flag = false; | ||
if(!isset($_SERVER['HTTP_CF_IPCOUNTRY'])) $flag = false; | ||
if(!isset($_SERVER['HTTP_CF_RAY'])) $flag = false; | ||
if(!isset($_SERVER['HTTP_CF_VISITOR'])) $flag = false; | ||
return $flag; | ||
} | ||
|
||
function isCloudflare() : bool { | ||
$ipCheck = _cloudflare_CheckIP($_SERVER['REMOTE_ADDR']); | ||
$requestCheck = _cloudflare_Requests_Check(); | ||
return ($ipCheck && $requestCheck); | ||
} | ||
|
||
// Use when handling ip's | ||
function getRequestIP() : string { | ||
$check = isCloudflare(); | ||
|
||
if($check) { | ||
return $_SERVER['HTTP_CF_CONNECTING_IP']; | ||
} else { | ||
return $_SERVER['REMOTE_ADDR']; | ||
} | ||
function getRequestIP(){ | ||
return (isset($_SERVER['HTTP_CF_CONNECTING_IP']))? $_SERVER['HTTP_CF_CONNECTING_IP'] : $_SERVER['REMOTE_ADDR']; | ||
} |