Files
domnitor/app/Services/Channels/DiscordChannel.php

116 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2025-10-08 14:23:07 +03:00
<?php
namespace App\Services\Channels;
use GuzzleHttp\Client;
use App\Services\Logger;
2025-10-08 14:23:07 +03:00
class DiscordChannel implements NotificationChannelInterface
{
private Client $client;
private Logger $logger;
2025-10-08 14:23:07 +03:00
public function __construct()
{
$this->client = new Client(['timeout' => 10]);
$this->logger = new Logger('discord_channel');
2025-10-08 14:23:07 +03:00
}
public function send(array $config, string $message, array $data = []): bool
{
if (!isset($config['webhook_url'])) {
return false;
}
try {
$embed = $this->createEmbed($message, $data);
$response = $this->client->post($config['webhook_url'], [
'json' => [
'embeds' => [$embed]
]
]);
$ok = $response->getStatusCode() === 204;
if ($ok) {
$this->logger->info('Discord message sent', [
'status' => $response->getStatusCode()
]);
} else {
$this->logger->error('Discord non-204 status', [
'status' => $response->getStatusCode()
]);
}
return $ok;
2025-10-08 14:23:07 +03:00
} catch (\Exception $e) {
$this->logger->error('Discord send failed', [
'exception' => $e->getMessage()
]);
2025-10-08 14:23:07 +03:00
return false;
}
}
private function createEmbed(string $message, array $data): array
{
Enhance DNS discovery, validation & transfers Add comprehensive DNS management and input validation, plus safer transfer and logging behavior. - Add CronHelper utilities for cron scripts and unify logging/formatting. - Improve InputValidator: sanitizeDomainInput and validateRootDomain (handles multi-level TLDs) and use throughout domain import/create flows to reject subdomains. - DomainController: refactor DNS refresh to support quick/deep discovery (background deep scans), add endpoints to discover, add/delete/bulk-delete DNS records, import BIND zone files, enrich IP metadata via enrichIpDetails, and strengthen bulk import/reporting messages. - DnsRecord model: add source column handling (discovered/manual/imported), avoid auto-deleting manual/imported records, and add helpers for deleting, bulk deleting, manual adding and importing zone records. - Tag, NotificationGroup and Domain transfer logic: unlink groups when ownership changes, remove tags that belong to other users, add audit logging via Logger and improved bulk transfer reporting. TagController/View: show transferable users for admins and skip global tags on transfer. - Notification channels (Discord, Mattermost, etc.) and EmailHelper: allow explicit subjects and improve payload fields based on notification type. - Add new migration 029_add_dns_record_source.sql and wire it into the installer; update migrations detection. - Add new views/partials for confirm/import/transfer modals, update various domain/group/tag templates, and update cron scripts and routes for discovery. These changes preserve manual/imported DNS records, improve root-domain validation, enable background deep discovery, and add better logging/audit trails for transfers and imports.
2026-03-10 22:54:28 +02:00
$title = $data['subject'] ?? '🔔 Domain Monitor Alert';
2025-10-08 14:23:07 +03:00
$color = $this->getColorByDaysLeft($data['days_left'] ?? null);
$embed = [
Enhance DNS discovery, validation & transfers Add comprehensive DNS management and input validation, plus safer transfer and logging behavior. - Add CronHelper utilities for cron scripts and unify logging/formatting. - Improve InputValidator: sanitizeDomainInput and validateRootDomain (handles multi-level TLDs) and use throughout domain import/create flows to reject subdomains. - DomainController: refactor DNS refresh to support quick/deep discovery (background deep scans), add endpoints to discover, add/delete/bulk-delete DNS records, import BIND zone files, enrich IP metadata via enrichIpDetails, and strengthen bulk import/reporting messages. - DnsRecord model: add source column handling (discovered/manual/imported), avoid auto-deleting manual/imported records, and add helpers for deleting, bulk deleting, manual adding and importing zone records. - Tag, NotificationGroup and Domain transfer logic: unlink groups when ownership changes, remove tags that belong to other users, add audit logging via Logger and improved bulk transfer reporting. TagController/View: show transferable users for admins and skip global tags on transfer. - Notification channels (Discord, Mattermost, etc.) and EmailHelper: allow explicit subjects and improve payload fields based on notification type. - Add new migration 029_add_dns_record_source.sql and wire it into the installer; update migrations detection. - Add new views/partials for confirm/import/transfer modals, update various domain/group/tag templates, and update cron scripts and routes for discovery. These changes preserve manual/imported DNS records, improve root-domain validation, enable background deep discovery, and add better logging/audit trails for transfers and imports.
2026-03-10 22:54:28 +02:00
'title' => $title,
2025-10-08 14:23:07 +03:00
'description' => $message,
'color' => $color,
'timestamp' => date('c'),
'footer' => [
'text' => 'Domain Monitor'
]
];
if (isset($data['domain'])) {
Enhance DNS discovery, validation & transfers Add comprehensive DNS management and input validation, plus safer transfer and logging behavior. - Add CronHelper utilities for cron scripts and unify logging/formatting. - Improve InputValidator: sanitizeDomainInput and validateRootDomain (handles multi-level TLDs) and use throughout domain import/create flows to reject subdomains. - DomainController: refactor DNS refresh to support quick/deep discovery (background deep scans), add endpoints to discover, add/delete/bulk-delete DNS records, import BIND zone files, enrich IP metadata via enrichIpDetails, and strengthen bulk import/reporting messages. - DnsRecord model: add source column handling (discovered/manual/imported), avoid auto-deleting manual/imported records, and add helpers for deleting, bulk deleting, manual adding and importing zone records. - Tag, NotificationGroup and Domain transfer logic: unlink groups when ownership changes, remove tags that belong to other users, add audit logging via Logger and improved bulk transfer reporting. TagController/View: show transferable users for admins and skip global tags on transfer. - Notification channels (Discord, Mattermost, etc.) and EmailHelper: allow explicit subjects and improve payload fields based on notification type. - Add new migration 029_add_dns_record_source.sql and wire it into the installer; update migrations detection. - Add new views/partials for confirm/import/transfer modals, update various domain/group/tag templates, and update cron scripts and routes for discovery. These changes preserve manual/imported DNS records, improve root-domain validation, enable background deep discovery, and add better logging/audit trails for transfers and imports.
2026-03-10 22:54:28 +02:00
$fields = [
['name' => 'Domain', 'value' => $data['domain'], 'inline' => true]
2025-10-08 14:23:07 +03:00
];
Enhance DNS discovery, validation & transfers Add comprehensive DNS management and input validation, plus safer transfer and logging behavior. - Add CronHelper utilities for cron scripts and unify logging/formatting. - Improve InputValidator: sanitizeDomainInput and validateRootDomain (handles multi-level TLDs) and use throughout domain import/create flows to reject subdomains. - DomainController: refactor DNS refresh to support quick/deep discovery (background deep scans), add endpoints to discover, add/delete/bulk-delete DNS records, import BIND zone files, enrich IP metadata via enrichIpDetails, and strengthen bulk import/reporting messages. - DnsRecord model: add source column handling (discovered/manual/imported), avoid auto-deleting manual/imported records, and add helpers for deleting, bulk deleting, manual adding and importing zone records. - Tag, NotificationGroup and Domain transfer logic: unlink groups when ownership changes, remove tags that belong to other users, add audit logging via Logger and improved bulk transfer reporting. TagController/View: show transferable users for admins and skip global tags on transfer. - Notification channels (Discord, Mattermost, etc.) and EmailHelper: allow explicit subjects and improve payload fields based on notification type. - Add new migration 029_add_dns_record_source.sql and wire it into the installer; update migrations detection. - Add new views/partials for confirm/import/transfer modals, update various domain/group/tag templates, and update cron scripts and routes for discovery. These changes preserve manual/imported DNS records, improve root-domain validation, enable background deep discovery, and add better logging/audit trails for transfers and imports.
2026-03-10 22:54:28 +02:00
// Only add expiration fields for domain expiration alerts
if (array_key_exists('days_left', $data) || array_key_exists('expiration_date', $data)) {
$fields[] = ['name' => 'Days Left', 'value' => (string) ($data['days_left'] ?? 'N/A'), 'inline' => true];
$fields[] = ['name' => 'Expiration Date', 'value' => $data['expiration_date'] ?? 'N/A', 'inline' => true];
} elseif (isset($data['hostname']) && $data['hostname'] !== $data['domain']) {
$fields[] = ['name' => 'Hostname', 'value' => $data['hostname'], 'inline' => true];
}
if (isset($data['new_status'])) {
$fields[] = ['name' => 'Status', 'value' => $data['new_status'], 'inline' => true];
}
$embed['fields'] = $fields;
2025-10-08 14:23:07 +03:00
}
return $embed;
}
private function getColorByDaysLeft(?int $daysLeft): int
{
if ($daysLeft === null) {
return 0x808080; // Gray
}
if ($daysLeft <= 0) {
return 0xFF0000; // Red
}
if ($daysLeft <= 3) {
return 0xFF4500; // Orange Red
}
if ($daysLeft <= 7) {
return 0xFFA500; // Orange
}
if ($daysLeft <= 30) {
return 0xFFFF00; // Yellow
}
return 0x00FF00; // Green
}
}