<?php
// ===========================================
// FUNKTIONEN FÜR DISPOSABLE EMAIL CHECK
// ===========================================

function is_disposable_email($email, $blocklist_path = null) {
    // Standard-Pfad zur lokalen Blocklist-Datei
    if (!$blocklist_path) {
        $blocklist_path = __DIR__ . '/disposable_email_blocklist.conf';
    }
    
    // Lade die Blocklist falls nötig
    if (!file_exists($blocklist_path)) {
        download_blocklist($blocklist_path);
    }
    
    // Lese alle Domains aus der Datei
    $disposable_domains = file($blocklist_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    
    // Extrahiere Domain aus der E-Mail-Adresse
    $email_parts = explode('@', trim($email));
    if (count($email_parts) != 2) {
        return false;
    }
    
    $domain = strtolower($email_parts[1]);
    
    // Prüfe ob Domain in der Liste steht
    $ret =  in_array($domain, $disposable_domains);

    // Check, ob für Domain (mindestens) ein MX-Record existiert
    if ($ret === false ) {
	// MX-Record(s) abholen 
	getmxrr($domain, $mx_records, $mx_weight);

	$mxs =[]; 
	// Array zusammenstellen für Sortierung
	for($i=0;$i<count($mx_records);$i++){
    		$mxs[$mx_records[$i]] = $mx_weight[$i];
	}

	if (count($mxs) == 0)
	       return true;	
    }

    return false  ;
}

function download_blocklist($file_path) {
    // Alternative Liste: $url = 'https://raw.githubusercontent.com/disposable-email-domains/disposable-email-domains/main/disposable_email_blocklist.conf';
    $url = 'https://raw.githubusercontent.com/disposable/disposable-email-domains/master/domains.txt';
    
    // echo "Lade Blocklist herunter...\n";
    
    $content = file_get_contents($url);
    if ($content === false) {
        die("FEHLER: Konnte Blocklist nicht herunterladen von: $url\n");
    }

    // Zusätzliche lokale Liste der nicht zugelassenen Domains
    $localContent = '';
    if (file_exists(__DIR__ . '/localdomains.conf')) {
	    $localContent = file_get_contents(__DIR__ ."/localdomains.conf"); 
    }
    
    // Listen kombinieren
    $content = $content ."\n" .$localContent; 

    if (file_put_contents($file_path, $content) === false) {
        die("FEHLER: Konnte Blocklist nicht speichern: $file_path\n");
    }
    
}

function update_blocklist_if_needed($blocklist_path = null, $max_age_hours = 1) {
    if (!$blocklist_path) {
        $blocklist_path = __DIR__ . '/disposable_email_blocklist.conf';
    }
    
    // Prüfe ob Datei existiert und wie alt sie ist
    if (!file_exists($blocklist_path)) {
        download_blocklist($blocklist_path);
        return true;
    }
    
    $file_age = time() - filemtime($blocklist_path);
    $max_age_seconds = $max_age_hours * 3600;
    
    if ($file_age > $max_age_seconds) {
        download_blocklist($blocklist_path);
        return true;
    }
    
    return false;
}

function filter_disposable_emails($emails, $blocklist_path = null) {
    $valid_emails = array();
    
    foreach ($emails as $email) {
        if (!is_disposable_email($email, $blocklist_path)) {
            $valid_emails[] = $email;
        }
    }
    
    return $valid_emails;
}

function get_email_domain($email) {
    $email_parts = explode('@', trim($email));
    return count($email_parts) === 2 ? strtolower($email_parts[1]) : '';
}

function validate_email_form($email) {

    // Blocklist aktualisieren falls nötig (jede Stunde)
    $updated = update_blocklist_if_needed();

    if (is_disposable_email($email)) {
	// echo "Wegwerfadresse\n"; 
        return false;
    }
    
    // echo "Adresse gültig\n"; 
    return true;
}

// Zum Testen
// validate_email_form("roland.keck@webxxx.de"); 

?>
