Upload via GUI (396 Dateien)

This commit is contained in:
2026-11-04 07:56:07 +00:00
parent 3a408cd8dc
commit 79c6424c77
8 changed files with 524 additions and 8 deletions
+20 -7
View File
@@ -28,6 +28,7 @@ if (php_sapi_name() !== 'cli' && !defined('DESKHUB_CRON_RUNNER')) { http_respons
require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/../includes/mail_text.php';
require_once __DIR__ . '/../includes/mail_threading.php';
/** Verarbeitet eine Mail nach dem Einlesen gemäß dem gewählten Modus */
function handle_after_fetch(mixed $mbox, int $emailNum, string $mode, string $folder): void
@@ -194,17 +195,23 @@ function fetch_emails_for_account(PDO $pdo, array $account, bool $verbose): arra
if (trim($body) === '') $body = '(kein Inhalt)';
// ── Antwort auf bestehendes Ticket? ──────────────────────────────────
if (preg_match('/\[#([0-9]{6}-[0-9]{4})\]/', $subject, $m)) {
$ticketNumber = $m[1];
$stmt = $pdo->prepare('SELECT * FROM tickets WHERE ticket_number = ?');
$stmt->execute([$ticketNumber]);
$ticket = $stmt->fetch();
// Zuordnung über die Nummer im Betreff, eine andere Nummer (früheres Ticketsystem) oder die Kopfzeilen In-Reply-To/References
$replyMatch = mail_find_ticket_for_reply($pdo, $subject, (string) @imap_fetchheader($mbox, $emailNum), (string) $fromEmail);
if ($replyMatch) {
$ticket = $replyMatch['ticket'];
$ticketNumber = (string) $ticket['ticket_number'];
$replyVia = ['subject' => '', 'legacy' => ' – zugeordnet über die Nummer im Betreff', 'header' => ' – zugeordnet über die Mail-Kopfzeilen'][$replyMatch['via']] ?? '';
if ($ticket) {
$now = db_now();
$pdo->prepare(
'INSERT INTO ticket_threads (ticket_id, author_type, message, is_internal_note, created_at) VALUES (?,?,?,0,?)'
)->execute([$ticket['id'], 'customer', $body, $now]);
$replyThreadId = (int) $pdo->lastInsertId();
$fetchPart = fn(string $no) => (string) imap_fetchbody($mbox, $emailNum, $no);
$htmlBody = ticket_mail_html_ready($pdo) && $structure ? mail_html_for_display($structure, $fetchPart) : null;
if ($htmlBody !== null) $pdo->prepare('UPDATE ticket_threads SET message_html = ? WHERE id = ?')->execute([$htmlBody, $replyThreadId]);
mail_store_attachments($pdo, $structure ?: null, $fetchPart, $replyThreadId, (int) $ticket['id'], $label, $log, false, $htmlBody !== null);
$newStatus = in_array($ticket['status'], ['answered', 'resolved', 'pending']) ? 'open' : $ticket['status'];
$pdo->prepare('UPDATE tickets SET status=?, updated_at=? WHERE id=?')->execute([$newStatus, $now, $ticket['id']]);
@@ -216,7 +223,7 @@ function fetch_emails_for_account(PDO $pdo, array $account, bool $verbose): arra
'INSERT INTO ticket_email_sources (ticket_id, imap_account_id, message_uid, message_id, current_folder, created_at) VALUES (?,?,?,?,?,?)'
)->execute([$ticket['id'], $account['id'], $messageUid ?: null, $messageId ?: null, $sourceFolderAfterFetch, $now]);
$log[] = "[$label] Antwort zu Ticket #{$ticketNumber} hinzugefügt (von {$fromEmail}).";
$log[] = "[$label] Antwort zu Ticket #{$ticketNumber} hinzugefügt (von {$fromEmail}){$replyVia}.";
handle_after_fetch($mbox, $emailNum, $afterFetch, $archiveFolder);
$processed++;
continue;
@@ -303,6 +310,11 @@ function fetch_emails_for_account(PDO $pdo, array $account, bool $verbose): arra
$pdo->prepare(
'INSERT INTO ticket_threads (ticket_id, author_type, message, is_internal_note, created_at) VALUES (?,?,?,0,?)'
)->execute([$ticketId, 'customer', $body, $now]);
$firstThreadId = (int) $pdo->lastInsertId();
$fetchPart = fn(string $no) => (string) imap_fetchbody($mbox, $emailNum, $no);
$htmlBody = ticket_mail_html_ready($pdo) && $structure ? mail_html_for_display($structure, $fetchPart) : null;
if ($htmlBody !== null) $pdo->prepare('UPDATE ticket_threads SET message_html = ? WHERE id = ?')->execute([$htmlBody, $firstThreadId]);
mail_store_attachments($pdo, $structure ?: null, $fetchPart, $firstThreadId, $ticketId, $label, $log, false, $htmlBody !== null);
log_activity($pdo, $ticketId, null, 'Ticket per E-Mail erstellt');
$log[] = "[$label] Neues Ticket #{$ticketNumber} erstellt (von {$fromEmail}).";
@@ -365,7 +377,8 @@ function fetch_emails_for_account(PDO $pdo, array $account, bool $verbose): arra
return ['processed' => $processed, 'log' => $log];
}
if (php_sapi_name() === 'cli') {
// Nur starten, wenn diese Datei selbst aufgerufen wird (Cronjob), nicht wenn sie von einer anderen Datei eingebunden wird (Abruf von Hand, Tests)
if (php_sapi_name() === 'cli' && isset($_SERVER['argv'][0]) && realpath((string) $_SERVER['argv'][0]) === realpath(__FILE__)) {
$result = run_fetch_emails(get_db(), true);
foreach ($result['log'] as $line) {
echo $line . "\n";