All checks were successful
Tests / Laravel tests (pull_request) Successful in 12m3s
178 lines
4.5 KiB
PHP
178 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Personalia;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class TelegramNotificationService
|
|
{
|
|
/**
|
|
* @param array<string, string|null> $ipHeaders
|
|
*/
|
|
public function notifyContactMessage(
|
|
string $name,
|
|
string $message,
|
|
string $ip,
|
|
string $userAgent,
|
|
?string $email = null,
|
|
?string $phone = null,
|
|
array $ipHeaders = []
|
|
): void {
|
|
$email ??= '-';
|
|
$phone ??= '-';
|
|
$ipHeaderText = $this->formatIpHeaders($ipHeaders);
|
|
|
|
$text = <<<TEXT
|
|
Nieuw contactbericht ontvangen
|
|
|
|
Naam: {$name}
|
|
Email: {$email}
|
|
Telefoon: {$phone}
|
|
IP: {$ip}
|
|
{$ipHeaderText}
|
|
User Agent: {$userAgent}
|
|
|
|
Bericht:
|
|
{$message}
|
|
|
|
Tijdstip: {now()->format('d-m-Y H:i')}
|
|
TEXT;
|
|
|
|
$this->send($text);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, string|null> $ipHeaders
|
|
*/
|
|
public function notifyPersonaliaClick(Personalia $personalia, ?string $ip, ?string $userAgent, array $ipHeaders = []): void
|
|
{
|
|
$ip ??= '-';
|
|
$userAgent ??= '-';
|
|
$ipHeaderText = $this->formatIpHeaders($ipHeaders);
|
|
|
|
$message = <<<TEXT
|
|
Persoonlijke gegevens bekeken
|
|
|
|
Naam: {$personalia->value}
|
|
IP: {$ip}
|
|
{$ipHeaderText}
|
|
User Agent: {$userAgent}
|
|
|
|
Tijdstip: {now()->format('d-m-Y H:i')}
|
|
TEXT;
|
|
|
|
$this->send($message);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $visit
|
|
* @param array<string, string|null> $ipHeaders
|
|
*/
|
|
public function notifyPageVisit(array $visit, ?string $ip, ?string $userAgent, ?string $acceptLanguage, array $ipHeaders = []): void
|
|
{
|
|
$screen = $this->formatDimensions($visit['screen'] ?? null);
|
|
$viewport = $this->formatDimensions($visit['viewport'] ?? null);
|
|
$ipHeaderText = $this->formatIpHeaders($ipHeaders);
|
|
|
|
$message = <<<TEXT
|
|
Pagina bezocht
|
|
|
|
URL: {$this->value($visit['url'] ?? null)}
|
|
Pad: {$this->value($visit['path'] ?? null)}
|
|
Titel: {$this->value($visit['title'] ?? null)}
|
|
Referrer: {$this->value($visit['referrer'] ?? null)}
|
|
|
|
Visitor ID: {$this->value($visit['visitor_id'] ?? null)}
|
|
IP: {$this->value($ip)}
|
|
{$ipHeaderText}
|
|
User Agent: {$this->value($userAgent)}
|
|
Accept-Language: {$this->value($acceptLanguage)}
|
|
Browser taal: {$this->value($visit['language'] ?? null)}
|
|
Timezone: {$this->value($visit['timezone'] ?? null)}
|
|
Platform: {$this->value($visit['platform'] ?? null)}
|
|
Vendor: {$this->value($visit['vendor'] ?? null)}
|
|
Scherm: {$screen}
|
|
Viewport: {$viewport}
|
|
DPR: {$this->value($visit['device_pixel_ratio'] ?? null)}
|
|
Color depth: {$this->value($visit['color_depth'] ?? null)}
|
|
CPU cores: {$this->value($visit['hardware_concurrency'] ?? null)}
|
|
Device memory: {$this->value($visit['device_memory'] ?? null)}
|
|
Cookies: {$this->boolean($visit['cookies_enabled'] ?? null)}
|
|
Online: {$this->boolean($visit['online'] ?? null)}
|
|
|
|
Tijdstip: {now()->format('d-m-Y H:i')}
|
|
TEXT;
|
|
|
|
$this->send($message);
|
|
}
|
|
|
|
protected function send(string $text): void
|
|
{
|
|
$botToken = config('services.telegram.bot_token');
|
|
$chatId = config('services.telegram.chat_id');
|
|
|
|
if (! is_string($botToken) || $botToken === '' || ! is_string($chatId) || $chatId === '') {
|
|
return;
|
|
}
|
|
|
|
Http::post("https://api.telegram.org/bot{$botToken}/sendMessage", [
|
|
'chat_id' => $chatId,
|
|
'text' => $text,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, string|null> $ipHeaders
|
|
*/
|
|
protected function formatIpHeaders(array $ipHeaders): string
|
|
{
|
|
$headers = [
|
|
'CF-Connecting-IP',
|
|
'X-Forwarded-For',
|
|
'X-Real-IP',
|
|
'Forwarded',
|
|
];
|
|
|
|
return collect($headers)
|
|
->map(fn (string $header): string => "{$header}: {$this->value($ipHeaders[$header] ?? null)}")
|
|
->implode("\n");
|
|
}
|
|
|
|
protected function value(mixed $value): string
|
|
{
|
|
if (is_bool($value)) {
|
|
return $this->boolean($value);
|
|
}
|
|
|
|
if (is_scalar($value)) {
|
|
$value = trim((string) $value);
|
|
|
|
return $value !== '' ? $value : '-';
|
|
}
|
|
|
|
return '-';
|
|
}
|
|
|
|
protected function boolean(mixed $value): string
|
|
{
|
|
return match ($value) {
|
|
true => 'ja',
|
|
false => 'nee',
|
|
default => '-',
|
|
};
|
|
}
|
|
|
|
protected function formatDimensions(mixed $dimensions): string
|
|
{
|
|
if (! is_array($dimensions)) {
|
|
return '-';
|
|
}
|
|
|
|
$width = $this->value($dimensions['width'] ?? null);
|
|
$height = $this->value($dimensions['height'] ?? null);
|
|
|
|
return "{$width}x{$height}";
|
|
}
|
|
}
|