Initial Commit
This commit is contained in:
192
cron/check_domains.php
Normal file
192
cron/check_domains.php
Normal file
@@ -0,0 +1,192 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Domain Expiration Check Cron Job
|
||||
*
|
||||
* This script should be run periodically (recommended: daily) to check domain expirations
|
||||
* and send notifications when domains are approaching expiration.
|
||||
*
|
||||
* Usage: php cron/check_domains.php
|
||||
* Crontab: 0 9 * * * /usr/bin/php /path/to/project/cron/check_domains.php
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
use App\Models\Domain;
|
||||
use App\Models\NotificationChannel;
|
||||
use App\Models\NotificationLog;
|
||||
use App\Services\WhoisService;
|
||||
use App\Services\NotificationService;
|
||||
use Core\Database;
|
||||
|
||||
// Load environment variables
|
||||
$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
|
||||
$dotenv->load();
|
||||
|
||||
// Initialize database
|
||||
new Database();
|
||||
|
||||
// Initialize services
|
||||
$domainModel = new Domain();
|
||||
$channelModel = new NotificationChannel();
|
||||
$logModel = new NotificationLog();
|
||||
$whoisService = new WhoisService();
|
||||
$notificationService = new NotificationService();
|
||||
|
||||
// Log file
|
||||
$logFile = __DIR__ . '/../logs/cron.log';
|
||||
|
||||
function logMessage(string $message) {
|
||||
global $logFile;
|
||||
$timestamp = date('Y-m-d H:i:s');
|
||||
file_put_contents($logFile, "[$timestamp] $message\n", FILE_APPEND);
|
||||
echo "[$timestamp] $message\n";
|
||||
}
|
||||
|
||||
logMessage("=== Starting domain check cron job ===");
|
||||
|
||||
// Get notification days from settings
|
||||
$notificationDays = explode(',', $_ENV['NOTIFICATION_DAYS_BEFORE'] ?? '30,15,7,3,1');
|
||||
$notificationDays = array_map('intval', $notificationDays);
|
||||
|
||||
logMessage("Notification thresholds (days): " . implode(', ', $notificationDays));
|
||||
|
||||
// Get all active domains
|
||||
$domains = $domainModel->where('is_active', 1);
|
||||
logMessage("Found " . count($domains) . " active domains to check");
|
||||
|
||||
$stats = [
|
||||
'checked' => 0,
|
||||
'updated' => 0,
|
||||
'notifications_sent' => 0,
|
||||
'errors' => 0
|
||||
];
|
||||
|
||||
foreach ($domains as $domain) {
|
||||
$domainName = $domain['domain_name'];
|
||||
logMessage("Checking domain: $domainName");
|
||||
|
||||
try {
|
||||
// Refresh WHOIS data
|
||||
$whoisData = $whoisService->getDomainInfo($domainName);
|
||||
|
||||
if (!$whoisData) {
|
||||
logMessage(" ✗ Failed to get WHOIS data for $domainName");
|
||||
$stats['errors']++;
|
||||
|
||||
// Update domain status to error
|
||||
$domainModel->update($domain['id'], [
|
||||
'status' => 'error',
|
||||
'last_checked' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Update domain information
|
||||
$status = $whoisService->getDomainStatus($whoisData['expiration_date'], $whoisData['status'] ?? []);
|
||||
$domainModel->update($domain['id'], [
|
||||
'registrar' => $whoisData['registrar'],
|
||||
'expiration_date' => $whoisData['expiration_date'],
|
||||
'last_checked' => date('Y-m-d H:i:s'),
|
||||
'status' => $status,
|
||||
'whois_data' => json_encode($whoisData)
|
||||
]);
|
||||
|
||||
$stats['checked']++;
|
||||
$stats['updated']++;
|
||||
|
||||
logMessage(" ✓ Updated WHOIS data for $domainName");
|
||||
logMessage(" Expiration: {$whoisData['expiration_date']}, Status: $status");
|
||||
|
||||
// Check if notifications should be sent
|
||||
$daysLeft = $whoisService->daysUntilExpiration($whoisData['expiration_date']);
|
||||
|
||||
if ($daysLeft === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if this domain should trigger a notification
|
||||
$shouldNotify = false;
|
||||
$notificationType = '';
|
||||
|
||||
if ($daysLeft <= 0) {
|
||||
$shouldNotify = true;
|
||||
$notificationType = 'expired';
|
||||
} elseif (in_array($daysLeft, $notificationDays)) {
|
||||
$shouldNotify = true;
|
||||
$notificationType = "expiring_in_{$daysLeft}_days";
|
||||
}
|
||||
|
||||
if (!$shouldNotify) {
|
||||
logMessage(" → No notification needed ($daysLeft days left)");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if notification was already sent recently (within last 23 hours)
|
||||
if ($logModel->wasSentRecently($domain['id'], $notificationType, 23)) {
|
||||
logMessage(" → Notification already sent recently");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get notification channels for this domain's group
|
||||
if (!$domain['notification_group_id']) {
|
||||
logMessage(" → No notification group assigned");
|
||||
continue;
|
||||
}
|
||||
|
||||
$channels = $channelModel->getActiveByGroupId($domain['notification_group_id']);
|
||||
|
||||
if (empty($channels)) {
|
||||
logMessage(" → No active notification channels configured");
|
||||
continue;
|
||||
}
|
||||
|
||||
logMessage(" 📤 Sending notifications to " . count($channels) . " channel(s)");
|
||||
|
||||
// Refresh domain data with group info
|
||||
$domainData = $domainModel->find($domain['id']);
|
||||
|
||||
// Send notifications
|
||||
$results = $notificationService->sendDomainExpirationAlert($domainData, $channels);
|
||||
|
||||
foreach ($results as $result) {
|
||||
$success = $result['success'];
|
||||
$channel = $result['channel'];
|
||||
|
||||
if ($success) {
|
||||
logMessage(" ✓ Sent to $channel");
|
||||
$stats['notifications_sent']++;
|
||||
} else {
|
||||
logMessage(" ✗ Failed to send to $channel");
|
||||
}
|
||||
|
||||
// Log the notification attempt
|
||||
$logModel->log(
|
||||
$domain['id'],
|
||||
$notificationType,
|
||||
$channel,
|
||||
"Domain $domainName expires in $daysLeft days",
|
||||
$success,
|
||||
$success ? null : "Failed to send notification"
|
||||
);
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
logMessage(" ✗ Error processing $domainName: " . $e->getMessage());
|
||||
$stats['errors']++;
|
||||
}
|
||||
}
|
||||
|
||||
// Summary
|
||||
logMessage("\n=== Cron job completed ===");
|
||||
logMessage("Domains checked: {$stats['checked']}");
|
||||
logMessage("Domains updated: {$stats['updated']}");
|
||||
logMessage("Notifications sent: {$stats['notifications_sent']}");
|
||||
logMessage("Errors: {$stats['errors']}");
|
||||
logMessage("==========================\n");
|
||||
|
||||
exit(0);
|
||||
|
||||
206
cron/import_tld_registry.php
Normal file
206
cron/import_tld_registry.php
Normal file
@@ -0,0 +1,206 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
/**
|
||||
* TLD Registry Import Script
|
||||
*
|
||||
* This script imports TLD registry data from IANA sources:
|
||||
* - RDAP servers from https://data.iana.org/rdap/dns.json
|
||||
* - WHOIS servers from individual IANA TLD pages
|
||||
*
|
||||
* Usage: php cron/import_tld_registry.php [options]
|
||||
*
|
||||
* Options:
|
||||
* --tld-list-only Import only TLD list from IANA
|
||||
* --rdap-only Import only RDAP data
|
||||
* --whois-only Import only WHOIS data for missing TLDs
|
||||
* --tlds=LIST Import WHOIS data for specific TLDs (comma-separated, e.g., --tlds=ro,de,fr)
|
||||
* --check-updates Check for IANA updates without importing
|
||||
* --force Force import even if no updates available
|
||||
* --verbose Enable verbose output
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
use App\Services\TldRegistryService;
|
||||
use Core\Database;
|
||||
|
||||
// Load environment variables
|
||||
$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
|
||||
$dotenv->load();
|
||||
|
||||
// Initialize database
|
||||
new Database();
|
||||
|
||||
// Parse command line arguments
|
||||
$options = getopt('', ['tld-list-only', 'rdap-only', 'whois-only', 'tlds:', 'check-updates', 'force', 'verbose', 'help']);
|
||||
|
||||
if (isset($options['help'])) {
|
||||
showHelp();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
$verbose = isset($options['verbose']);
|
||||
$force = isset($options['force']);
|
||||
|
||||
// Initialize service
|
||||
$tldService = new TldRegistryService();
|
||||
|
||||
// Log file
|
||||
$logFile = __DIR__ . '/../logs/tld_import.log';
|
||||
|
||||
function logMessage(string $message, bool $verbose = false) {
|
||||
global $logFile, $options;
|
||||
|
||||
if ($verbose && !isset($options['verbose'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$timestamp = date('Y-m-d H:i:s');
|
||||
$logLine = "[$timestamp] $message\n";
|
||||
|
||||
file_put_contents($logFile, $logLine, FILE_APPEND);
|
||||
echo $logLine;
|
||||
}
|
||||
|
||||
logMessage("=== Starting TLD Registry Import ===");
|
||||
|
||||
try {
|
||||
// Check for updates first
|
||||
if (isset($options['check-updates'])) {
|
||||
logMessage("Checking for IANA updates...");
|
||||
|
||||
$updateInfo = $tldService->checkForUpdates();
|
||||
|
||||
if ($updateInfo['needs_update']) {
|
||||
logMessage("✓ IANA data has been updated!");
|
||||
logMessage(" Current publication: " . ($updateInfo['current_publication'] ?? 'Unknown'));
|
||||
logMessage(" Last publication: " . ($updateInfo['last_publication'] ?? 'None'));
|
||||
|
||||
if (!$force) {
|
||||
logMessage("Use --force to import the updated data.");
|
||||
exit(0);
|
||||
}
|
||||
} else {
|
||||
logMessage("✓ TLD registry is up to date");
|
||||
if (isset($updateInfo['error'])) {
|
||||
logMessage(" Error: " . $updateInfo['error']);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
$totalStats = [
|
||||
'tld_list' => ['total_tlds' => 0, 'new_tlds' => 0, 'updated_tlds' => 0, 'failed_tlds' => 0],
|
||||
'rdap' => ['total_tlds' => 0, 'new_tlds' => 0, 'updated_tlds' => 0, 'failed_tlds' => 0],
|
||||
'whois' => ['total_tlds' => 0, 'new_tlds' => 0, 'updated_tlds' => 0, 'failed_tlds' => 0]
|
||||
];
|
||||
|
||||
// Import TLD list
|
||||
if (!isset($options['rdap-only']) && !isset($options['whois-only'])) {
|
||||
logMessage("Importing TLD list from IANA...");
|
||||
|
||||
$tldListStats = $tldService->importTldList();
|
||||
$totalStats['tld_list'] = $tldListStats;
|
||||
|
||||
logMessage("TLD list import completed:");
|
||||
logMessage(" Total TLDs: {$tldListStats['total_tlds']}");
|
||||
logMessage(" New TLDs: {$tldListStats['new_tlds']}");
|
||||
logMessage(" Updated TLDs: {$tldListStats['updated_tlds']}");
|
||||
if ($tldListStats['failed_tlds'] > 0) {
|
||||
logMessage(" Failed TLDs: {$tldListStats['failed_tlds']}");
|
||||
}
|
||||
}
|
||||
|
||||
// Import RDAP data
|
||||
if (!isset($options['tld-list-only']) && !isset($options['whois-only'])) {
|
||||
logMessage("Importing RDAP data from IANA...");
|
||||
|
||||
$rdapStats = $tldService->importRdapData();
|
||||
$totalStats['rdap'] = $rdapStats;
|
||||
|
||||
logMessage("RDAP import completed:");
|
||||
logMessage(" Total TLDs: {$rdapStats['total_tlds']}");
|
||||
logMessage(" New TLDs: {$rdapStats['new_tlds']}");
|
||||
logMessage(" Updated TLDs: {$rdapStats['updated_tlds']}");
|
||||
if ($rdapStats['failed_tlds'] > 0) {
|
||||
logMessage(" Failed TLDs: {$rdapStats['failed_tlds']}");
|
||||
}
|
||||
}
|
||||
|
||||
// Import WHOIS data for missing TLDs or specific TLDs
|
||||
if (!isset($options['tld-list-only']) && !isset($options['rdap-only'])) {
|
||||
if (isset($options['tlds'])) {
|
||||
// Import specific TLDs
|
||||
$tldList = array_map('trim', explode(',', $options['tlds']));
|
||||
logMessage("Importing WHOIS data for specific TLDs: " . implode(', ', $tldList));
|
||||
|
||||
$whoisStats = $tldService->importWhoisForSpecificTlds($tldList);
|
||||
$totalStats['whois'] = $whoisStats;
|
||||
|
||||
logMessage("WHOIS import completed:");
|
||||
logMessage(" Total TLDs: {$whoisStats['total_tlds']}");
|
||||
logMessage(" New TLDs: {$whoisStats['new_tlds']}");
|
||||
logMessage(" Updated TLDs: {$whoisStats['updated_tlds']}");
|
||||
if ($whoisStats['failed_tlds'] > 0) {
|
||||
logMessage(" Failed TLDs: {$whoisStats['failed_tlds']}");
|
||||
}
|
||||
} else {
|
||||
// Import WHOIS data for missing TLDs
|
||||
logMessage("Importing WHOIS data for missing TLDs...");
|
||||
|
||||
$whoisStats = $tldService->importWhoisDataForMissingTlds();
|
||||
$totalStats['whois'] = $whoisStats;
|
||||
|
||||
logMessage("WHOIS import completed:");
|
||||
logMessage(" Total TLDs: {$whoisStats['total_tlds']}");
|
||||
logMessage(" Updated TLDs: {$whoisStats['updated_tlds']}");
|
||||
if ($whoisStats['failed_tlds'] > 0) {
|
||||
logMessage(" Failed TLDs: {$whoisStats['failed_tlds']}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Summary
|
||||
logMessage("\n=== Import Summary ===");
|
||||
logMessage("TLD List: {$totalStats['tld_list']['total_tlds']} total, {$totalStats['tld_list']['new_tlds']} new, {$totalStats['tld_list']['updated_tlds']} updated");
|
||||
logMessage("RDAP: {$totalStats['rdap']['total_tlds']} total, {$totalStats['rdap']['new_tlds']} new, {$totalStats['rdap']['updated_tlds']} updated");
|
||||
logMessage("WHOIS: {$totalStats['whois']['total_tlds']} total, {$totalStats['whois']['updated_tlds']} updated");
|
||||
|
||||
$totalNew = $totalStats['tld_list']['new_tlds'] + $totalStats['rdap']['new_tlds'];
|
||||
$totalUpdated = $totalStats['tld_list']['updated_tlds'] + $totalStats['rdap']['updated_tlds'] + $totalStats['whois']['updated_tlds'];
|
||||
$totalFailed = $totalStats['tld_list']['failed_tlds'] + $totalStats['rdap']['failed_tlds'] + $totalStats['whois']['failed_tlds'];
|
||||
|
||||
logMessage("Overall: {$totalNew} new, {$totalUpdated} updated, {$totalFailed} failed");
|
||||
logMessage("==========================\n");
|
||||
|
||||
} catch (Exception $e) {
|
||||
logMessage("✗ Import failed: " . $e->getMessage());
|
||||
logMessage("Stack trace: " . $e->getTraceAsString());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
logMessage("✓ TLD Registry import completed successfully");
|
||||
exit(0);
|
||||
|
||||
function showHelp() {
|
||||
echo "TLD Registry Import Script\n\n";
|
||||
echo "Usage: php cron/import_tld_registry.php [options]\n\n";
|
||||
echo "Options:\n";
|
||||
echo " --tld-list-only Import only TLD list from IANA\n";
|
||||
echo " --rdap-only Import only RDAP data from IANA\n";
|
||||
echo " --whois-only Import only WHOIS data for missing TLDs\n";
|
||||
echo " --tlds=LIST Import WHOIS data for specific TLDs (comma-separated)\n";
|
||||
echo " --check-updates Check for IANA updates without importing\n";
|
||||
echo " --force Force import even if no updates available\n";
|
||||
echo " --verbose Enable verbose output\n";
|
||||
echo " --help Show this help message\n\n";
|
||||
echo "Examples:\n";
|
||||
echo " php cron/import_tld_registry.php # Full import\n";
|
||||
echo " php cron/import_tld_registry.php --tld-list-only # TLD list only\n";
|
||||
echo " php cron/import_tld_registry.php --rdap-only # RDAP only\n";
|
||||
echo " php cron/import_tld_registry.php --tlds=ro,de,fr # Import specific TLDs\n";
|
||||
echo " php cron/import_tld_registry.php --check-updates # Check for updates\n";
|
||||
echo " php cron/import_tld_registry.php --force --verbose # Force import with verbose output\n\n";
|
||||
}
|
||||
Reference in New Issue
Block a user