1414 */
1515class APIAccessChecker
1616{
17- /** @var string */
17+ /** @var string */
1818 private $ hostname ;
1919
2020 /**
2121 * The allowed IP address.
2222 * @var array
23+ * [
24+ * '127.0.0.1',
25+ * '::1',
26+ * '172.19.49.*'
27+ * ]
2328 */
2429 protected $ allowedIps ;
2530
@@ -30,13 +35,13 @@ class APIAccessChecker
3035 protected $ allowedHosts ;
3136
3237 /**
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
3641 */
3742 public function __construct ($ ips , $ hosts = null )
3843 {
39- $ this ->hostname = gethostname ();
44+ $ this ->hostname = \defined ( ' HOSTNAME ' ) ? HOSTNAME : explode ( ' . ' , gethostname ())[ 0 ] ;
4045 $ this ->allowedIps = (array )$ ips ;
4146 $ this ->allowedHosts = (array )$ hosts ;
4247 }
@@ -45,8 +50,12 @@ public function __construct($ips, $hosts = null)
4550 * @param string $clientIp client Ip
4651 * @return bool
4752 */
48- public function isAllowedAccess ($ clientIp )
53+ public function isAllowedAccess (string $ clientIp ): bool
4954 {
55+ if (APP_ENV === APP_DEV || APP_ENV === APP_TEST ) {
56+ return true ;
57+ }
58+
5059 if ($ this ->checkHostname ()) {
5160 return true ;
5261 }
@@ -70,11 +79,10 @@ public function isAllowedAccess($clientIp)
7079
7180 /**
7281 * Check if IP address for securing area matches the given
73- *
7482 * @param string $clientIp
7583 * @return bool
7684 */
77- private function checkIp ($ clientIp )
85+ private function checkIp (string $ clientIp ): bool
7886 {
7987 // local env
8088 if ($ clientIp === '127.0.0.1 ' || $ clientIp === '::1 ' ) {
@@ -85,6 +93,10 @@ private function checkIp($clientIp)
8593 if (0 === strpos ($ clientIp , $ allowedIp )) {
8694 return true ;
8795 }
96+
97+ if (fnmatch ($ allowedIp , $ clientIp )) {
98+ return true ;
99+ }
88100 }
89101
90102 return false ;
@@ -93,7 +105,7 @@ private function checkIp($clientIp)
93105 /**
94106 * @return bool
95107 */
96- public function checkHostname ()
108+ public function checkHostname (): bool
97109 {
98110 $ host = $ this ->hostname ;
99111
@@ -109,7 +121,7 @@ public function checkHostname()
109121 /**
110122 * @return array
111123 */
112- public function getAllowedIps ()
124+ public function getAllowedIps (): array
113125 {
114126 return $ this ->allowedIps ;
115127 }
@@ -125,7 +137,7 @@ public function setAllowedIps($allowedIps)
125137 /**
126138 * @return array
127139 */
128- public function getAllowedHosts ()
140+ public function getAllowedHosts (): array
129141 {
130142 return $ this ->allowedHosts ;
131143 }
0 commit comments