forked from ivancduran/Ipfilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ipfilter.php
67 lines (52 loc) · 1.18 KB
/
Ipfilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Ipfilter;
use Phalcon\Mvc\User\Component;
class Ipfilter extends Component
{
public $ips;
function __construct($ips){
$this->ips = $ips;
}
public function getRealIP()
{
if (!empty($_SERVER['HTTP_CLIENT_IP']))
return $_SERVER['HTTP_CLIENT_IP'];
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
return $_SERVER['HTTP_X_FORWARDED_FOR'];
return $_SERVER['REMOTE_ADDR'];
}
private function ipListFromRange($range)
{
$parts = explode('/',$range);
$exponent = 32-$parts[1].'-';
$count = pow(2,$exponent);
$start = ip2long($parts[0]);
$end = $start+$count;
return array_map('long2ip', range($start, $end) );
}
public function filter()
{
$ip = $this->getRealIP();
$valid = false;
$newArr = array();
// new Array
foreach ($this->ips as $iptable) {
$exp = explode("/", $iptable);
if (isset($exp[1])){
$var = $this->ipListFromRange($iptable);
foreach ($var as $key) {
$newArr[] = $key;
}
}else{
$newArr[] = $iptable;
}
}
$unicArr = array_unique($newArr);
foreach ($unicArr as $key) {
if($key === $ip){
$valid = true;
}
}
return $valid;
}
}