14
14
*/
15
15
class APIAccessChecker
16
16
{
17
- /** @var string */
17
+ /** @var string */
18
18
private $ hostname ;
19
19
20
20
/**
21
21
* The allowed IP address.
22
22
* @var array
23
+ * [
24
+ * '127.0.0.1',
25
+ * '::1',
26
+ * '172.19.49.*'
27
+ * ]
23
28
*/
24
29
protected $ allowedIps ;
25
30
@@ -30,13 +35,13 @@ class APIAccessChecker
30
35
protected $ allowedHosts ;
31
36
32
37
/**
33
- * APIAccessChecker constructor.
34
- * @param array $ips
35
- * @param null |array $hosts
38
+ * ApiAccessCheck constructor.
39
+ * @param string| array $ips
40
+ * @param string |array|null $hosts
36
41
*/
37
42
public function __construct ($ ips , $ hosts = null )
38
43
{
39
- $ this ->hostname = gethostname ();
44
+ $ this ->hostname = \defined ( ' HOSTNAME ' ) ? HOSTNAME : explode ( ' . ' , gethostname ())[ 0 ] ;
40
45
$ this ->allowedIps = (array )$ ips ;
41
46
$ this ->allowedHosts = (array )$ hosts ;
42
47
}
@@ -45,8 +50,12 @@ public function __construct($ips, $hosts = null)
45
50
* @param string $clientIp client Ip
46
51
* @return bool
47
52
*/
48
- public function isAllowedAccess ($ clientIp )
53
+ public function isAllowedAccess (string $ clientIp ): bool
49
54
{
55
+ if (APP_ENV === APP_DEV || APP_ENV === APP_TEST ) {
56
+ return true ;
57
+ }
58
+
50
59
if ($ this ->checkHostname ()) {
51
60
return true ;
52
61
}
@@ -70,11 +79,10 @@ public function isAllowedAccess($clientIp)
70
79
71
80
/**
72
81
* Check if IP address for securing area matches the given
73
- *
74
82
* @param string $clientIp
75
83
* @return bool
76
84
*/
77
- private function checkIp ($ clientIp )
85
+ private function checkIp (string $ clientIp ): bool
78
86
{
79
87
// local env
80
88
if ($ clientIp === '127.0.0.1 ' || $ clientIp === '::1 ' ) {
@@ -85,6 +93,10 @@ private function checkIp($clientIp)
85
93
if (0 === strpos ($ clientIp , $ allowedIp )) {
86
94
return true ;
87
95
}
96
+
97
+ if (fnmatch ($ allowedIp , $ clientIp )) {
98
+ return true ;
99
+ }
88
100
}
89
101
90
102
return false ;
@@ -93,7 +105,7 @@ private function checkIp($clientIp)
93
105
/**
94
106
* @return bool
95
107
*/
96
- public function checkHostname ()
108
+ public function checkHostname (): bool
97
109
{
98
110
$ host = $ this ->hostname ;
99
111
@@ -109,7 +121,7 @@ public function checkHostname()
109
121
/**
110
122
* @return array
111
123
*/
112
- public function getAllowedIps ()
124
+ public function getAllowedIps (): array
113
125
{
114
126
return $ this ->allowedIps ;
115
127
}
@@ -125,7 +137,7 @@ public function setAllowedIps($allowedIps)
125
137
/**
126
138
* @return array
127
139
*/
128
- public function getAllowedHosts ()
140
+ public function getAllowedHosts (): array
129
141
{
130
142
return $ this ->allowedHosts ;
131
143
}
0 commit comments