| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Repository\Entry; |
| 4 |
|
| 5 |
use IvyForms\Entity\Entry\Entry; |
| 6 |
|
| 7 |
class EntryHelper |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Get the client IP address. |
| 11 |
* |
| 12 |
* @return string|null |
| 13 |
*/ |
| 14 |
public static function getClientIpAddress(): ?string |
| 15 |
{ |
| 16 |
$ip = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP); |
| 17 |
return $ip ?: null; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Get the user agent string. |
| 22 |
* |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
public static function getUserAgent(): string |
| 26 |
{ |
| 27 |
$userAgent = filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'); |
| 28 |
return $userAgent ?: ''; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get the source URL. |
| 33 |
* |
| 34 |
* @return string|null |
| 35 |
*/ |
| 36 |
public static function getSourceURL(): ?string |
| 37 |
{ |
| 38 |
$sourceURL = filter_input(INPUT_SERVER, 'HTTP_REFERER'); |
| 39 |
return $sourceURL ?: ''; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get the browser name from the user agent string. |
| 44 |
* |
| 45 |
* @param string $userAgent |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public static function getBrowserName(string $userAgent): string |
| 49 |
{ |
| 50 |
if (str_contains($userAgent, 'Firefox')) { |
| 51 |
return 'Mozilla Firefox'; |
| 52 |
} elseif (str_contains($userAgent, 'Chrome') && !str_contains($userAgent, 'Edg')) { |
| 53 |
return 'Google Chrome'; |
| 54 |
} elseif (str_contains($userAgent, 'Safari') && !str_contains($userAgent, 'Chrome')) { |
| 55 |
return 'Apple Safari'; |
| 56 |
} elseif (str_contains($userAgent, 'Edg')) { |
| 57 |
return 'Microsoft Edge'; |
| 58 |
} elseif (str_contains($userAgent, 'MSIE') || str_contains($userAgent, 'Trident')) { |
| 59 |
return 'Internet Explorer'; |
| 60 |
} |
| 61 |
return 'Unknown'; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Build entry data array for form submissions. |
| 66 |
* |
| 67 |
* @param int $formId |
| 68 |
* @return array<string, mixed> |
| 69 |
*/ |
| 70 |
public static function buildEntryData(int $formId): array |
| 71 |
{ |
| 72 |
return [ |
| 73 |
'formId' => $formId, |
| 74 |
'userId' => get_current_user_id() ?: null, |
| 75 |
'status' => Entry::DEFAULT_STATUS, |
| 76 |
'ipAddress' => self::getClientIpAddress(), |
| 77 |
'userAgent' => self::getUserAgent(), |
| 78 |
'sourceURL' => self::getSourceURL(), |
| 79 |
]; |
| 80 |
} |
| 81 |
} |
| 82 |
|