Skip to content

Commit 96009e0

Browse files
committed
skip if no location set up
1 parent 170e545 commit 96009e0

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

src/Middleware/Geo.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public function check($patterns)
1111
{
1212
$status = false;
1313

14+
if ($this->isEmpty()) {
15+
return $status;
16+
}
17+
1418
if (!$location = $this->getLocation()) {
1519
return $status;
1620
}
@@ -34,6 +38,29 @@ public function check($patterns)
3438
return $status;
3539
}
3640

41+
protected function isEmpty()
42+
{
43+
$status = true;
44+
45+
$types = ['continents', 'regions', 'countries', 'cities'];
46+
47+
foreach ($types as $type) {
48+
if (!$list = config('firewall.middleware.' . $this->middleware . '.' . $type)) {
49+
continue;
50+
}
51+
52+
if (empty($list['allow']) && empty($list['block'])) {
53+
continue;
54+
}
55+
56+
$status = false;
57+
58+
break;
59+
}
60+
61+
return $status;
62+
}
63+
3764
protected function isFiltered($location, $type)
3865
{
3966
if (!$list = config('firewall.middleware.' . $this->middleware . '.' . $type)) {
@@ -63,17 +90,6 @@ protected function getLocation()
6390
return $this->$service($location);
6491
}
6592

66-
protected function getResponse($url)
67-
{
68-
try {
69-
$response = json_decode(file_get_contents($url));
70-
} catch (\ErrorException $e) {
71-
$response = null;
72-
}
73-
74-
return $response;
75-
}
76-
7793
protected function ipapi($location)
7894
{
7995
$response = $this->getResponse('http://ip-api.com/json/' . $this->ip() . '?fields=continent,country,regionName,city');
@@ -152,4 +168,23 @@ protected function ipinfo($location)
152168

153169
return $location;
154170
}
171+
172+
protected function getResponse($url)
173+
{
174+
try {
175+
$ch = curl_init();
176+
curl_setopt($ch, CURLOPT_URL, $url);
177+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
178+
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
179+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
180+
$content = curl_exec($ch);
181+
curl_close($ch);
182+
183+
$response = json_decode($content);
184+
} catch (\ErrorException $e) {
185+
$response = null;
186+
}
187+
188+
return $response;
189+
}
155190
}

0 commit comments

Comments
 (0)