From dc50d84d48531b083b8a1a646c8668cbbf920de1 Mon Sep 17 00:00:00 2001 From: Jaroslav Drzik Date: Fri, 19 Jan 2024 19:15:14 +0100 Subject: [PATCH] GeoIP add --- app/Http/Controllers/GeoIPController.php | 14 ++ composer.json | 3 +- composer.lock | 82 ++++++++++- config/app.php | 4 +- config/geoip.php | 165 +++++++++++++++++++++++ routes/web.php | 3 +- 6 files changed, 267 insertions(+), 4 deletions(-) create mode 100644 app/Http/Controllers/GeoIPController.php create mode 100644 config/geoip.php diff --git a/app/Http/Controllers/GeoIPController.php b/app/Http/Controllers/GeoIPController.php new file mode 100644 index 0000000..b47772e --- /dev/null +++ b/app/Http/Controllers/GeoIPController.php @@ -0,0 +1,14 @@ +getLocation(); + } +} diff --git a/composer.json b/composer.json index e8a8a64..b2ad9ce 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "paquettg/php-html-parser": "^2.2", "pusher/pusher-php-server": "^7.0", "spatie/laravel-permission": "^5.5", - "tightenco/ziggy": "^1.0" + "tightenco/ziggy": "^1.0", + "torann/geoip": "^3.0" }, "require-dev": { "barryvdh/laravel-debugbar": "^3.7", diff --git a/composer.lock b/composer.lock index 4049c26..70b624f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "86cdd8c61f5c7ee9dee51ebeae179259", + "content-hash": "f8f22c83d6734feaadd1beafe3a4b702", "packages": [ { "name": "bacon/bacon-qr-code", @@ -8422,6 +8422,86 @@ }, "time": "2023-01-03T09:29:04+00:00" }, + { + "name": "torann/geoip", + "version": "3.0.5", + "source": { + "type": "git", + "url": "https://github.com/Torann/laravel-geoip.git", + "reference": "af934013bdc073c8f4775e62cac8cc89b3de3ce6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Torann/laravel-geoip/zipball/af934013bdc073c8f4775e62cac8cc89b3de3ce6", + "reference": "af934013bdc073c8f4775e62cac8cc89b3de3ce6", + "shasum": "" + }, + "require": { + "illuminate/cache": "^8.0|^9.0|^10.0", + "illuminate/console": "^8.0|^9.0|^10.0", + "illuminate/support": "^8.0|^9.0|^10.0", + "php": "^8.0|^8.1" + }, + "require-dev": { + "geoip2/geoip2": "~2.1", + "mockery/mockery": "^1.3", + "phpstan/phpstan": "^0.12.14", + "phpunit/phpunit": "^8.0|^9.0|^10.0", + "squizlabs/php_codesniffer": "^3.5", + "vlucas/phpdotenv": "^5.0" + }, + "suggest": { + "geoip2/geoip2": "Required to use the MaxMind database or web service with GeoIP (~2.1).", + "monolog/monolog": "Allows for storing location not found errors to the log" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + }, + "laravel": { + "providers": [ + "Torann\\GeoIP\\GeoIPServiceProvider" + ], + "aliases": { + "GeoIP": "Torann\\GeoIP\\Facades\\GeoIP" + } + } + }, + "autoload": { + "files": [ + "src/helpers.php" + ], + "psr-4": { + "Torann\\GeoIP\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "authors": [ + { + "name": "Daniel Stainback", + "email": "torann@gmail.com" + } + ], + "description": "Support for multiple GeoIP services.", + "keywords": [ + "IP API", + "geoip", + "geolocation", + "infoDB", + "laravel", + "location", + "maxmind" + ], + "support": { + "issues": "https://github.com/Torann/laravel-geoip/issues", + "source": "https://github.com/Torann/laravel-geoip/tree/3.0.5" + }, + "time": "2023-02-21T14:02:27+00:00" + }, { "name": "ua-parser/uap-php", "version": "v3.9.14", diff --git a/config/app.php b/config/app.php index 2f595be..1548a69 100644 --- a/config/app.php +++ b/config/app.php @@ -185,7 +185,7 @@ return [ /* * Package Service Providers... */ - + /* * Application Service Providers... */ @@ -198,6 +198,7 @@ return [ App\Providers\JetstreamServiceProvider::class, \hisorange\BrowserDetect\ServiceProvider::class, + \Torann\GeoIP\GeoIPServiceProvider::class, ], /* @@ -213,6 +214,7 @@ return [ 'aliases' => Facade::defaultAliases()->merge([ // 'ExampleClass' => App\Example\ExampleClass::class, + 'GeoIP' => \Torann\GeoIP\Facades\GeoIP::class, ])->toArray(), ]; diff --git a/config/geoip.php b/config/geoip.php new file mode 100644 index 0000000..0f6570e --- /dev/null +++ b/config/geoip.php @@ -0,0 +1,165 @@ + true, + + /* + |-------------------------------------------------------------------------- + | Include Currency in Results + |-------------------------------------------------------------------------- + | + | When enabled the system will do it's best in deciding the user's currency + | by matching their ISO code to a preset list of currencies. + | + */ + + 'include_currency' => true, + + /* + |-------------------------------------------------------------------------- + | Default Service + |-------------------------------------------------------------------------- + | + | Here you may specify the default storage driver that should be used + | by the framework. + | + | Supported: "maxmind_database", "maxmind_api", "ipapi" + | + */ + + 'service' => 'ipapi', + + /* + |-------------------------------------------------------------------------- + | Storage Specific Configuration + |-------------------------------------------------------------------------- + | + | Here you may configure as many storage drivers as you wish. + | + */ + + 'services' => [ + + 'maxmind_database' => [ + 'class' => \Torann\GeoIP\Services\MaxMindDatabase::class, + 'database_path' => storage_path('app/geoip.mmdb'), + 'update_url' => sprintf('https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=%s&suffix=tar.gz', env('MAXMIND_LICENSE_KEY')), + 'locales' => ['en'], + ], + + 'maxmind_api' => [ + 'class' => \Torann\GeoIP\Services\MaxMindWebService::class, + 'user_id' => env('MAXMIND_USER_ID'), + 'license_key' => env('MAXMIND_LICENSE_KEY'), + 'locales' => ['en'], + ], + + 'ipapi' => [ + 'class' => \Torann\GeoIP\Services\IPApi::class, + 'secure' => true, + 'key' => env('IPAPI_KEY'), + 'continent_path' => storage_path('app/continents.json'), + 'lang' => 'en', + ], + + 'ipgeolocation' => [ + 'class' => \Torann\GeoIP\Services\IPGeoLocation::class, + 'secure' => true, + 'key' => env('IPGEOLOCATION_KEY'), + 'continent_path' => storage_path('app/continents.json'), + 'lang' => 'en', + ], + + 'ipdata' => [ + 'class' => \Torann\GeoIP\Services\IPData::class, + 'key' => env('IPDATA_API_KEY'), + 'secure' => true, + ], + + 'ipfinder' => [ + 'class' => \Torann\GeoIP\Services\IPFinder::class, + 'key' => env('IPFINDER_API_KEY'), + 'secure' => true, + 'locales' => ['en'], + ], + + ], + + /* + |-------------------------------------------------------------------------- + | Default Cache Driver + |-------------------------------------------------------------------------- + | + | Here you may specify the type of caching that should be used + | by the package. + | + | Options: + | + | all - All location are cached + | some - Cache only the requesting user + | none - Disable cached + | + */ + + 'cache' => 'all', + + /* + |-------------------------------------------------------------------------- + | Cache Tags + |-------------------------------------------------------------------------- + | + | Cache tags are not supported when using the file or database cache + | drivers in Laravel. This is done so that only locations can be cleared. + | + */ + + 'cache_tags' => ['torann-geoip-location'], + + /* + |-------------------------------------------------------------------------- + | Cache Expiration + |-------------------------------------------------------------------------- + | + | Define how long cached location are valid. + | + */ + + 'cache_expires' => 30, + + /* + |-------------------------------------------------------------------------- + | Default Location + |-------------------------------------------------------------------------- + | + | Return when a location is not found. + | + */ + + 'default_location' => [ + 'ip' => '127.0.0.0', + 'iso_code' => 'US', + 'country' => 'United States', + 'city' => 'New Haven', + 'state' => 'CT', + 'state_name' => 'Connecticut', + 'postal_code' => '06510', + 'lat' => 41.31, + 'lon' => -72.92, + 'timezone' => 'America/New_York', + 'continent' => 'NA', + 'default' => true, + 'currency' => 'USD', + ], + +]; diff --git a/routes/web.php b/routes/web.php index 70a4f0e..b8880c6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -5,6 +5,7 @@ use Inertia\Inertia; use App\Http\Controllers\CountryCodeController; use App\Http\Controllers\CountryCompareController; use App\Http\Controllers\CurrencyRatesController; +use App\Http\Controllers\GeoIPController; use App\Http\Controllers\IkeaProductsController; use App\Http\Controllers\ProductsCompareController; @@ -31,7 +32,7 @@ Route::get('/exchange/', function () { })->name('exchange'); Route::get('/menu/get', [App\Http\Controllers\Superuser\UserMenuController::class, 'get'])->name('menu.user'); - +Route::get('/ip/get/{ip?}', [GeoIPController::class, 'index'])->name('geo.ip.get'); Route::get('/ccountry/', [CountryCodeController::class, 'index'])->name('ccountry.index'); Route::get('/ccountry/codes/', [CountryCodeController::class, 'codes'])->name('ccountry.codes'); Route::get('/ccountry/active/', [CountryCodeController::class, 'active'])->name('ccountry.active');