install
This commit is contained in:
72
app/Modules/IpStack/IpStackModule.php
Normal file
72
app/Modules/IpStack/IpStackModule.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\IpStack;
|
||||
|
||||
use App\Modules\Core\ModulePackage;
|
||||
use App\Modules\Core\ModulePackageType;
|
||||
use App\Modules\Makeable;
|
||||
use App\Modules\ModuleContract;
|
||||
|
||||
class IpStackModule implements ModuleContract
|
||||
{
|
||||
use Makeable;
|
||||
|
||||
/**
|
||||
* Module is enabled
|
||||
*/
|
||||
protected bool $enabled = true;
|
||||
|
||||
/**
|
||||
* Check if is module enabled
|
||||
*/
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable module
|
||||
*/
|
||||
public function disable(): void
|
||||
{
|
||||
$this->enabled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable module
|
||||
*/
|
||||
public function enable(): void
|
||||
{
|
||||
$this->enabled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if module has a filament resource
|
||||
*/
|
||||
public function hasFilamentResource(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get module composer requirements
|
||||
*/
|
||||
public function getComposerRequirements(): array
|
||||
{
|
||||
return [
|
||||
new ModulePackage(
|
||||
type: ModulePackageType::PACKAGE,
|
||||
name: 'stevebauman/location',
|
||||
message: 'Required for IP-based geolocation services.',
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get module composer suggestions
|
||||
*/
|
||||
public function getComposerSuggestions(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
28
app/Modules/IpStack/Repositories/IpStackRepository.php
Normal file
28
app/Modules/IpStack/Repositories/IpStackRepository.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\IpStack\Repositories;
|
||||
|
||||
use Stevebauman\Location\Facades\Location;
|
||||
|
||||
class IpStackRepository
|
||||
{
|
||||
/**
|
||||
* Get ip stack
|
||||
*/
|
||||
public static function isLocalIp(string $ip): bool
|
||||
{
|
||||
return ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get country code from ip
|
||||
*/
|
||||
public static function getCountryCodeFromIp(?string $ip): string
|
||||
{
|
||||
if (is_null($ip)) {
|
||||
return 'TM';
|
||||
}
|
||||
|
||||
return self::isLocalIp($ip) ? 'TM' : Location::get($ip)->countryCode ?? 'TM';
|
||||
}
|
||||
}
|
||||
19
app/Modules/IpStack/ip-stack-helpers.php
Normal file
19
app/Modules/IpStack/ip-stack-helpers.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\IpStack\Repositories\IpStackRepository;
|
||||
|
||||
/**
|
||||
* Check if a client IP is in our Server subnet
|
||||
*/
|
||||
function isLocalIp(string $ip = ''): bool
|
||||
{
|
||||
return IpStackRepository::isLocalIp($ip);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get country code from ip
|
||||
*/
|
||||
function getCountryCodeFromIp(?string $ip = ''): string
|
||||
{
|
||||
return IpStackRepository::getCountryCodeFromIp($ip);
|
||||
}
|
||||
Reference in New Issue
Block a user