Ajout du dossier api avec la géolocalisation automatique des casernes de pompiers
This commit is contained in:
97
api/src/Utils/ClientDetector.php
Normal file
97
api/src/Utils/ClientDetector.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
class ClientDetector {
|
||||
/**
|
||||
* Détecte le type de client basé sur l'User-Agent
|
||||
*
|
||||
* @return string 'mobile', 'web' ou 'unknown'
|
||||
*/
|
||||
public static function getClientType(): string {
|
||||
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
|
||||
|
||||
// Détection des applications mobiles natives
|
||||
if (preg_match('/(Android|iOS)\/[0-9\.]+\s+\w+\/[0-9\.]+/', $userAgent)) {
|
||||
return 'mobile';
|
||||
}
|
||||
|
||||
// Détection des navigateurs mobiles
|
||||
if (preg_match('/(Android|iPhone|iPad|iPod|Windows Phone)/i', $userAgent)) {
|
||||
return 'mobile';
|
||||
}
|
||||
|
||||
// Détection des navigateurs web
|
||||
if (preg_match('/(Mozilla|Chrome|Safari|Firefox|Edge|MSIE|Trident)/i', $userAgent)) {
|
||||
return 'web';
|
||||
}
|
||||
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère l'identifiant de l'application mobile depuis l'User-Agent
|
||||
* Format attendu: AppName/VersionNumber (Platform/Version)
|
||||
*
|
||||
* @return string Identifiant de l'application ou 'unknown'
|
||||
*/
|
||||
public static function getAppIdentifier(): string {
|
||||
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
|
||||
|
||||
// Extraction de l'identifiant de l'application
|
||||
if (preg_match('/^([A-Za-z0-9_]+)\/[0-9\.]+/', $userAgent, $matches)) {
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
// Extraction depuis un format alternatif
|
||||
if (preg_match('/\b(GeoSector|Prokov|Resalice)\/[0-9\.]+\b/i', $userAgent, $matches)) {
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère des informations détaillées sur le client
|
||||
*
|
||||
* @return array Informations sur le client
|
||||
*/
|
||||
public static function getClientInfo(): array {
|
||||
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
|
||||
|
||||
// Information par défaut
|
||||
$clientInfo = [
|
||||
'type' => self::getClientType(),
|
||||
'ip' => $_SERVER['REMOTE_ADDR'] ?? 'unknown',
|
||||
'userAgent' => $userAgent,
|
||||
'browser' => [
|
||||
'name' => 'unknown',
|
||||
'version' => 'unknown'
|
||||
],
|
||||
'os' => [
|
||||
'name' => 'unknown',
|
||||
'version' => 'unknown'
|
||||
]
|
||||
];
|
||||
|
||||
// Détection du navigateur
|
||||
if (preg_match('/(Chrome|Safari|Firefox|Edge|MSIE|Trident)[\s\/]([0-9\.]+)/i', $userAgent, $matches)) {
|
||||
$clientInfo['browser']['name'] = $matches[1];
|
||||
$clientInfo['browser']['version'] = $matches[2];
|
||||
}
|
||||
|
||||
// Détection du système d'exploitation
|
||||
if (preg_match('/(Android|iOS|iPhone OS|iPad|iPod|Windows NT|Mac OS X|Linux)[\s\/]([0-9\._]+)/i', $userAgent, $matches)) {
|
||||
$clientInfo['os']['name'] = $matches[1];
|
||||
$clientInfo['os']['version'] = $matches[2];
|
||||
}
|
||||
|
||||
// Si c'est une application mobile, ajouter l'identifiant
|
||||
if ($clientInfo['type'] === 'mobile') {
|
||||
$clientInfo['appIdentifier'] = self::getAppIdentifier();
|
||||
}
|
||||
|
||||
return $clientInfo;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user