1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Session |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Session\Validator; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
18
|
|
|
use Phossa2\Session\Interfaces\ValidatorInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* RemoteIp |
22
|
|
|
* |
23
|
|
|
* Check remote ip is allowed or not |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\package |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see ObjectAbstract |
28
|
|
|
* @see ValidatorInterface |
29
|
|
|
* @version 2.1.0 |
30
|
|
|
* @since 2.1.0 added |
31
|
|
|
*/ |
32
|
|
|
class RemoteIp extends ObjectAbstract implements ValidatorInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Allowed ip patterns |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
* @access protected |
39
|
|
|
*/ |
40
|
|
|
protected $allowed; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Denied ip patterns |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
* @access protected |
47
|
|
|
*/ |
48
|
|
|
protected $denied; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Inject allowed or denied ip patterns like '216.110.124.0/24' etc. |
52
|
|
|
* |
53
|
|
|
* @param array $denied |
54
|
|
|
* @param array $allowed |
55
|
|
|
* @access protected |
56
|
|
|
*/ |
57
|
|
|
public function __construct(array $denied = [], array $allowed = []) |
58
|
|
|
{ |
59
|
|
|
$this->allowed = $this->processPattern($allowed); |
60
|
|
|
$this->denied = $this->processPattern($denied); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
|
public function validate()/*# : bool */ |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$ip = $_SERVER['REMOTE_ADDR']; |
69
|
|
|
|
70
|
|
|
// allowed ? |
71
|
|
|
if ($this->matchIp($ip, $this->allowed)) { |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// blocked ? |
76
|
|
|
if ($this->matchIp($ip, $this->denied)) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return empty($this->allowed) ? true : false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Match ip with patterns |
85
|
|
|
* |
86
|
|
|
* @param string $ip |
87
|
|
|
* @param array $patterns |
88
|
|
|
* @return boolean |
89
|
|
|
* @access protected |
90
|
|
|
*/ |
91
|
|
|
protected function matchIp(/*# string */ $ip, array $patterns)/*# : bool */ |
92
|
|
|
{ |
93
|
|
|
$num = ip2long($ip); |
94
|
|
|
foreach ($patterns as $pat) { |
95
|
|
|
if ($num & $pat[1] == $pat[0]) { |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Pre-process ip matching pattern |
104
|
|
|
* |
105
|
|
|
* @param array $patterns |
106
|
|
|
* @return array |
107
|
|
|
* @access protected |
108
|
|
|
*/ |
109
|
|
|
protected function processPattern(array $patterns)/*# : array */ |
110
|
|
|
{ |
111
|
|
|
$result = []; |
112
|
|
|
foreach ($patterns as $pat) { |
113
|
|
|
$part = explode('/', $pat); |
114
|
|
|
$addr = ip2long($part[0]); |
115
|
|
|
$mask = isset($part[1]) ? ((int) $part[1]) : 32; |
116
|
|
|
|
117
|
|
|
// fix |
118
|
|
|
$mask = $this->getMask($mask); |
119
|
|
|
$addr = ip2long($addr) & $mask; |
120
|
|
|
|
121
|
|
|
$result[] = [$addr, $mask]; |
122
|
|
|
} |
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Convert mask length to mask in decimal |
128
|
|
|
* |
129
|
|
|
* @param int $length |
130
|
|
|
* @return int |
131
|
|
|
* @access protected |
132
|
|
|
*/ |
133
|
|
|
protected function getMask(/*# int */ $length = 32)/*# : int */ |
134
|
|
|
{ |
135
|
|
|
$bin = substr(str_repeat('1', $length) . str_repeat('0', 32), 0, 32); |
136
|
|
|
return bindec($bin); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: