Your IP : 216.73.216.39


Current Path : /home/wirbesti/public_html/
Upload File :
Current File : /home/wirbesti/public_html/mail-PDF-it.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/logs/php_errors.log');

$logFile = __DIR__ . '/logs/debug_it.log';
file_put_contents($logFile, date('Y-m-d H:i:s') . " - Script started\n", FILE_APPEND);

// Start session
session_start();
file_put_contents($logFile, date('Y-m-d H:i:s') . " - Session started\n", FILE_APPEND);

// Get URL parameters
$nachname = $_GET['nachname'] ?? '';
$vorname = $_GET['vorname'] ?? '';
$email = $_GET['email'] ?? '';
$campaign = $_GET['campaign'] ?? '';
file_put_contents($logFile, date('Y-m-d H:i:s') . " - Parameters: nachname=$nachname, vorname=$vorname, email=$email, campaign=$campaign\n", FILE_APPEND);

// Validate email
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Invalid email: $email\n", FILE_APPEND);
    die('Invalid email address');
}

// Validate campaign
if (!is_numeric($campaign)) {
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Invalid campaign ID: $campaign\n", FILE_APPEND);
    die('Invalid campaign ID');
}

// Check for duplicate email
$sessionKey = 'email_sent_it_' . $campaign . '_' . $email;
if (isset($_SESSION[$sessionKey])) {
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Email already sent, redirecting\n", FILE_APPEND);
    header('Location: https://wirbestimmen.ch/index.php/it/unterschriftsbogen-dl-it');
    exit;
}

// Include dependencies
$requiredFiles = [
    'vendor/phpmailer/phpmailer/src/PHPMailer.php',
    'vendor/phpmailer/phpmailer/src/SMTP.php',
    'vendor/phpmailer/phpmailer/src/Exception.php',
    'php/DBConnection.php',
    'php/Metadata.php',
    'php/setting.config.php',
    'php/InitiatorCoverSheet.php',
    'libraries2/dompdf/vendor/autoload.php'
];
foreach ($requiredFiles as $file) {
    $filePath = __DIR__ . '/' . $file;
    if (file_exists($filePath)) {
        require_once $filePath;
        file_put_contents($logFile, date('Y-m-d H:i:s') . " - Included file: $file\n", FILE_APPEND);
    } else {
        file_put_contents($logFile, date('Y-m-d H:i:s') . " - File not found: $file\n", FILE_APPEND);
        die("Required file not found: $file");
    }
}

// Verify PHPMailer
if (!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Error: PHPMailer class not found\n", FILE_APPEND);
    die('PHPMailer class not available');
}

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
file_put_contents($logFile, date('Y-m-d H:i:s') . " - PHPMailer loaded successfully\n", FILE_APPEND);

// Database connection
try {
    $db = new DBConnection($config);
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Database connection established\n", FILE_APPEND);
} catch (Exception $e) {
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Database connection failed: " . $e->getMessage() . "\n", FILE_APPEND);
    die('Database connection failed');
}

// Metadata query
try {
    $meta = new Metadata($db);
    $metaEntry = $meta->getMetadataByCampaignid($campaign);
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Metadata query executed\n", FILE_APPEND);
} catch (Exception $e) {
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Metadata query failed: " . $e->getMessage() . "\n", FILE_APPEND);
    die('Metadata query failed');
}

if ($metaEntry) {
    $signatureFilePath = $metaEntry['signature-form-path-pdf-it'] ?? 'Name.pdf';
    $subjectname = $metaEntry['slogan-it'] ?? '"Testo dell\'iniziativa"';
} else {
    $signatureFilePath = 'Name.pdf';
    $subjectname = '"Testo dell\'iniziativa"';
}
file_put_contents($logFile, date('Y-m-d H:i:s') . " - Metadata: signature_file=$signatureFilePath, subjectname=$subjectname\n", FILE_APPEND);

// Generate cover sheet
$coverSheetFilePath = '';
try {
    $coverSheet = new InitiatorCoverSheet();
    $coverSheetData = [
        'initiatorname' => $metaEntry['initiatorname'] ?? '',
        'initiatorstreet' => $metaEntry['initiatorstreet'] ?? '',
        'initiatorzipcode' => $metaEntry['initiatorzipcode'] ?? '',
        'initiatortown' => $metaEntry['initiatortown'] ?? '',
        'language' => 'it',
        'randnum' => rand(1000, 9999),
        'slogan' => $subjectname
    ];
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Generating cover sheet with data: " . json_encode($coverSheetData) . "\n", FILE_APPEND);
    $coverSheetFile = $coverSheet->generateCoverSheet($coverSheetData);
    $coverSheetFilePath = $coverSheetFile ? '/home/wirbesti/public_html/images/CoverSheets/' . $coverSheetFile : '';
    if (!$coverSheetFile || !file_exists($coverSheetFilePath)) {
        file_put_contents($logFile, date('Y-m-d H:i:s') . " - Cover sheet generation failed or file not found: $coverSheetFilePath\n", FILE_APPEND);
        die('Failed to generate cover sheet');
    }
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Cover sheet generated: $coverSheetFilePath\n", FILE_APPEND);
} catch (Exception $e) {
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Cover sheet generation failed: " . $e->getMessage() . "\n", FILE_APPEND);
    die('Failed to generate cover sheet');
}

// Signature form attachment
$path = getcwd();
$signatureFile = $path . '/' . ltrim($signatureFilePath, '/');
file_put_contents($logFile, date('Y-m-d H:i:s') . " - Signature file path: $signatureFile\n", FILE_APPEND);

if (!empty($signatureFilePath) && !file_exists($signatureFile)) {
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Signature file not found: $signatureFile\n", FILE_APPEND);
    if (file_exists($coverSheetFilePath)) {
        unlink($coverSheetFilePath);
        file_put_contents($logFile, date('Y-m-d H:i:s') . " - Cover sheet file deleted on error: $coverSheetFilePath\n", FILE_APPEND);
    }
    die('Attachment file not found');
}
file_put_contents($logFile, date('Y-m-d H:i:s') . " - Signature file size: " . filesize($signatureFile) . " bytes\n", FILE_APPEND);

// Email setup
$mail = new PHPMailer(true);
try {
    $mail->SMTPDebug = 2;
    $mail->Debugoutput = function($str, $level) use ($logFile) {
        file_put_contents($logFile, date('Y-m-d H:i:s') . " - [PHPMailer Level $level] $str\n", FILE_APPEND);
    };
    $mail->isSendmail();
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Using Sendmail\n", FILE_APPEND);
    $mail->setFrom('kontakt@wirbestimmen.ch', 'Wir bestimmen');
    $mail->addAddress($email);
    $mail->isHTML(true);
    $mail->CharSet = 'UTF-8';
    $mail->Subject = 'Modulo vuoto per la raccolta delle firme - ' . $subjectname;
    $mail->Body = '<p><strong>Ciao ' . htmlspecialchars($vorname) . ' ' . htmlspecialchars($nachname) . '</strong></p>
<p>A nome del Comitato promotore, La ringraziamo per averci sostenuto.</p>
<p>In allegato Le inviamo il modulo per la firma e una copertina.</p>
<p><strong>IMPORTANTE:</strong><br />
Affinché il suo impegno non sia vano e le firme siano valide, è necessario osservare alcune regole:</p>
<ol>
    <li>Tutte le persone devono essere dello stesso paese o della stessa città, come menzionato sopra alla voce “Municipalità politica”.</li>
    <li>Il campo "Cognome, Nome, Compleanno e Indirizzo" deve essere compilato a mano dai singoli ed essere leggibile. Tutti i campi devono essere completati e accompagnati alla fine della firma della persona.</li>
    <li>Il campo "Cognomi e nomi" deve essere compilato a mano dalla persona che firma.</li>
</ol>
<p>Può ordinare altri moduli su kontakt@wirbesimmen.ch o telefonicamente al numero +41 840 123 456.</p>
<p>Le saremmo molto grati se potesse imbucare il modulo per la firma piegato e inserito in una busta, oppure piegato e chiuso per mezzo di una striscia adesiva, nella cassetta postale più vicina. Gli organizzatori dell’iniziativa popolare le saranno <strong>molto grati</strong> qualora decidesse di non restituire le firme all’ultimo momento. La verifica dell’eventuale presenza di errori richiede un po’ di tempo.<br />
La ringraziamo di cuore.</p>
<p>Se lo desidera, può sostenere la nostra raccolta con un francobollo. La ringraziamo di cuore anche per questo.</p>
<p>Le auguriamo tutto il meglio e di conservare il suo entusiasmo per la Svizzera e per il futuro. Le porgiamo cordiali saluti.<br />
<a href="http://www.wirbestimmen.ch" target="_blank" rel="noopener noreferrer"><br />
Noi determiniamo<br />
Wir bestimmen<br />
</a>
Tel.: +41 840 123 456<br />
Mail: <a href="mailto:kontakt@wirbestimmen.ch">kontakt@wirbestimmen.ch</a></p>
<p> </p>
<p>Il nostro impegno in favore di una democrazia diretta diffusa e forte è molto costosa. <br />
La ringraziamo per la Sua donazione.<br /><br />
<strong>Le donazioni possono essere versate direttamente sul seguente conto</strong><em><strong>:</strong></em></p>
<p>Wir bestimmen<br />
3006 Berna<br /><br />
Konto: 61-933005-1<br />
IBAN: CH68 0900 0000 6193 3005 1</p>';

    // Attachments
    if (!empty($signatureFile) && file_exists($signatureFile)) {
        $mail->addAttachment($signatureFile, 'SignatureForm.pdf');
        file_put_contents($logFile, date('Y-m-d H:i:s') . " - Signature file attached: $signatureFile\n", FILE_APPEND);
    }
    if (!empty($coverSheetFilePath) && file_exists($coverSheetFilePath)) {
        $mail->addAttachment($coverSheetFilePath, 'CoverSheet.pdf');
        file_put_contents($logFile, date('Y-m-d H:i:s') . " - Cover sheet attached: $coverSheetFilePath\n", FILE_APPEND);
    }

    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Attempting to send email\n", FILE_APPEND);
    $mail->send();
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Email sent successfully\n", FILE_APPEND);

    // Mark email as sent
    $_SESSION[$sessionKey] = true;

    // Clean up cover sheet
    if (file_exists($coverSheetFilePath)) {
        unlink($coverSheetFilePath);
        file_put_contents($logFile, date('Y-m-d H:i:s') . " - Cover sheet file deleted: $coverSheetFilePath\n", FILE_APPEND);
    }

    header('Location: https://wirbestimmen.ch/index.php/it/unterschriftsbogen-dl-it');
    exit;
} catch (Exception $e) {
    file_put_contents($logFile, date('Y-m-d H:i:s') . " - Mailer Error: " . $e->getMessage() . "\n", FILE_APPEND);
    if (file_exists($coverSheetFilePath)) {
        unlink($coverSheetFilePath);
        file_put_contents($logFile, date('Y-m-d H:i:s') . " - Cover sheet file deleted on error: $coverSheetFilePath\n", FILE_APPEND);
    }
    die("Email failed: " . htmlspecialchars($e->getMessage()));
}
?>