|
2 | 2 |
|
3 | 3 | namespace MeShaon\RequestAnalytics\Services; |
4 | 4 |
|
| 5 | +use GeoIp2\Database\Reader; |
| 6 | +use GeoIp2\Exception\AddressNotFoundException; |
5 | 7 | use Illuminate\Support\Facades\Cache; |
6 | 8 | use Illuminate\Support\Facades\Http; |
7 | 9 | use Illuminate\Support\Facades\Log; |
@@ -119,9 +121,111 @@ protected function lookupWithIpGeolocation(string $ip): array |
119 | 121 |
|
120 | 122 | protected function lookupWithMaxMind(string $ip): array |
121 | 123 | { |
122 | | - // This would require the GeoIP2 PHP library |
123 | | - // composer require geoip2/geoip2 |
124 | | - // Implementation would depend on whether using web service or local database |
| 124 | + $maxmindType = config('request-analytics.geolocation.maxmind.type', 'webservice'); |
| 125 | + |
| 126 | + return match ($maxmindType) { |
| 127 | + 'database' => $this->lookupWithMaxMindDatabase($ip), |
| 128 | + 'webservice' => $this->lookupWithMaxMindWebService($ip), |
| 129 | + default => $this->getDefaultLocation(), |
| 130 | + }; |
| 131 | + } |
| 132 | + |
| 133 | + protected function lookupWithMaxMindWebService(string $ip): array |
| 134 | + { |
| 135 | + $userId = config('request-analytics.geolocation.maxmind.user_id'); |
| 136 | + $licenseKey = config('request-analytics.geolocation.maxmind.license_key'); |
| 137 | + |
| 138 | + if (! $userId || ! $licenseKey) { |
| 139 | + Log::warning('MaxMind web service credentials not configured'); |
| 140 | + |
| 141 | + return $this->getDefaultLocation(); |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + $response = Http::timeout(10) |
| 146 | + ->withBasicAuth($userId, $licenseKey) |
| 147 | + ->get("https://geoip.maxmind.com/geoip/v2.1/city/{$ip}"); |
| 148 | + |
| 149 | + if ($response->successful()) { |
| 150 | + $data = $response->json(); |
| 151 | + |
| 152 | + return [ |
| 153 | + 'country' => $data['country']['names']['en'] ?? '', |
| 154 | + 'country_code' => $data['country']['iso_code'] ?? '', |
| 155 | + 'region' => $data['subdivisions'][0]['names']['en'] ?? '', |
| 156 | + 'city' => $data['city']['names']['en'] ?? '', |
| 157 | + 'latitude' => $data['location']['latitude'] ?? null, |
| 158 | + 'longitude' => $data['location']['longitude'] ?? null, |
| 159 | + 'timezone' => $data['location']['time_zone'] ?? '', |
| 160 | + 'isp' => $data['traits']['isp'] ?? '', |
| 161 | + ]; |
| 162 | + } |
| 163 | + |
| 164 | + if ($response->status() === 404) { |
| 165 | + // IP not found in database |
| 166 | + return $this->getDefaultLocation(); |
| 167 | + } |
| 168 | + |
| 169 | + Log::warning('MaxMind web service returned error', [ |
| 170 | + 'ip' => $ip, |
| 171 | + 'status' => $response->status(), |
| 172 | + 'body' => $response->body(), |
| 173 | + ]); |
| 174 | + |
| 175 | + } catch (\Exception $e) { |
| 176 | + Log::warning('MaxMind web service lookup failed', [ |
| 177 | + 'ip' => $ip, |
| 178 | + 'error' => $e->getMessage(), |
| 179 | + ]); |
| 180 | + } |
| 181 | + |
| 182 | + return $this->getDefaultLocation(); |
| 183 | + } |
| 184 | + |
| 185 | + protected function lookupWithMaxMindDatabase(string $ip): array |
| 186 | + { |
| 187 | + $databasePath = config('request-analytics.geolocation.maxmind.database_path'); |
| 188 | + |
| 189 | + if (! $databasePath || ! file_exists($databasePath)) { |
| 190 | + Log::warning('MaxMind database file not found', [ |
| 191 | + 'path' => $databasePath, |
| 192 | + ]); |
| 193 | + |
| 194 | + return $this->getDefaultLocation(); |
| 195 | + } |
| 196 | + |
| 197 | + // Check if GeoIP2 library is available |
| 198 | + if (! class_exists('GeoIp2\Database\Reader')) { |
| 199 | + Log::warning('GeoIP2 library not installed. Please run: composer require geoip2/geoip2'); |
| 200 | + |
| 201 | + return $this->getDefaultLocation(); |
| 202 | + } |
| 203 | + |
| 204 | + try { |
| 205 | + $reader = new Reader($databasePath); |
| 206 | + $record = $reader->city($ip); |
| 207 | + |
| 208 | + return [ |
| 209 | + 'country' => $record->country->name ?? '', |
| 210 | + 'country_code' => $record->country->isoCode ?? '', |
| 211 | + 'region' => $record->mostSpecificSubdivision->name ?? '', |
| 212 | + 'city' => $record->city->name ?? '', |
| 213 | + 'latitude' => $record->location->latitude, |
| 214 | + 'longitude' => $record->location->longitude, |
| 215 | + 'timezone' => $record->location->timeZone ?? '', |
| 216 | + 'isp' => '', // ISP data requires separate database |
| 217 | + ]; |
| 218 | + |
| 219 | + } catch (AddressNotFoundException) { |
| 220 | + // IP not found in database - this is normal for some IPs |
| 221 | + return $this->getDefaultLocation(); |
| 222 | + } catch (\Exception $e) { |
| 223 | + Log::warning('MaxMind database lookup failed', [ |
| 224 | + 'ip' => $ip, |
| 225 | + 'database' => $databasePath, |
| 226 | + 'error' => $e->getMessage(), |
| 227 | + ]); |
| 228 | + } |
125 | 229 |
|
126 | 230 | return $this->getDefaultLocation(); |
127 | 231 | } |
|
0 commit comments