You've already forked Atomcms-edit
72 lines
2.6 KiB
PHP
Executable File
72 lines
2.6 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Services\User;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
readonly class SessionService
|
|
{
|
|
public function fetchSessionLogs(Request $request): Collection
|
|
{
|
|
return collect(
|
|
Auth::user()->sessions,
|
|
)->map(function ($session) use ($request) {
|
|
$info = $this->parseUserAgent($session->user_agent);
|
|
|
|
return (object) [
|
|
'agent' => [
|
|
'is_desktop' => $info['is_desktop'],
|
|
'platform' => $info['platform'],
|
|
'browser' => $info['browser'],
|
|
],
|
|
'ip_address' => $session->ip_address,
|
|
'is_current_device' => $session->id === $request->session()->getId(),
|
|
'last_active' => Carbon::createFromTimestamp($session->last_activity)->diffForHumans(),
|
|
];
|
|
});
|
|
}
|
|
|
|
public function parseUserAgent(string $userAgent): array
|
|
{
|
|
$platform = 'Unknown';
|
|
$browser = 'Unknown';
|
|
$isDesktop = true;
|
|
|
|
if (preg_match('/Windows NT 10\.0|Windows NT 11\.0|Macintosh|Linux|FreeBSD/', $userAgent)) {
|
|
$platform = match (true) {
|
|
preg_match('/Windows NT 10\.0/', $userAgent) => 'Windows 10',
|
|
preg_match('/Windows NT 11\.0/', $userAgent) => 'Windows 11',
|
|
preg_match('/Macintosh/', $userAgent) => 'macOS',
|
|
preg_match('/Linux/', $userAgent) => 'Linux',
|
|
preg_match('/FreeBSD/', $userAgent) => 'FreeBSD',
|
|
default => 'Desktop',
|
|
};
|
|
} elseif (preg_match('/Android/i', $userAgent)) {
|
|
$platform = 'Android';
|
|
$isDesktop = false;
|
|
} elseif (preg_match('/iPhone|iPad/i', $userAgent)) {
|
|
$platform = preg_match('/iPad/', $userAgent) ? 'iPad' : 'iOS';
|
|
$isDesktop = false;
|
|
}
|
|
|
|
$browser = match (true) {
|
|
preg_match('/Edg\/\d+/', $userAgent) => 'Edge',
|
|
preg_match('/Chrome\/\d+/', $userAgent) && ! preg_match('/OPR|Opera/', $userAgent) => 'Chrome',
|
|
preg_match('/Firefox\/\d+/', $userAgent) => 'Firefox',
|
|
preg_match('/Safari\/\d+/', $userAgent) && ! preg_match('/Chrome/', $userAgent) => 'Safari',
|
|
preg_match('/OPR\/\d+/', $userAgent) => 'Opera',
|
|
preg_match('/MSIE|Trident\//', $userAgent) => 'Internet Explorer',
|
|
default => 'Unknown',
|
|
};
|
|
|
|
return [
|
|
'platform' => $platform,
|
|
'browser' => $browser,
|
|
'is_desktop' => $isDesktop,
|
|
];
|
|
}
|
|
}
|