Upload via GUI (396 Dateien)
This commit is contained in:
@@ -144,6 +144,277 @@ function mail_collect_text_parts(object $part, string $no = ''): array
|
||||
return $found;
|
||||
}
|
||||
|
||||
/** Dateiname eines Anhangs: MIME-Kodierung (=?UTF-8?B?…?=) und RFC 2231 (UTF-8''…) werden aufgelöst. */
|
||||
function mail_decode_filename(string $value): string
|
||||
{
|
||||
$value = trim($value);
|
||||
if (preg_match("/^([A-Za-z0-9_-]+)'[^']*'(.*)$/", $value, $m)) {
|
||||
$decoded = rawurldecode($m[2]);
|
||||
$cs = strtolower($m[1]);
|
||||
if ($cs !== 'utf-8' && $cs !== 'utf8') {
|
||||
try { $decoded = (string) mb_convert_encoding($decoded, 'UTF-8', $cs); } catch (Throwable) { /* bleibt */ }
|
||||
}
|
||||
return $decoded;
|
||||
}
|
||||
if (str_contains($value, '=?')) {
|
||||
$decoded = function_exists('iconv_mime_decode') ? @iconv_mime_decode($value, 0, 'UTF-8') : false;
|
||||
if ($decoded === false || $decoded === '') $decoded = mb_decode_mimeheader($value);
|
||||
return (string) $decoded;
|
||||
}
|
||||
return mb_check_encoding($value, 'UTF-8') ? $value : (string) mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sammelt die Anhänge einer IMAP-Struktur: Teile mit Dateinamen oder der Anordnung "attachment" sowie weitergeleitete Mails
|
||||
* (message/rfc822). Bilder mit Content-ID, die im Text der Mail verwendet werden (Logos, Signaturbilder, eingefügte Fotos), sind als
|
||||
* "inline" gekennzeichnet: Sie gehören zur Darstellung der Mail und werden nur dann als Anhang gezeigt, wenn es keine HTML-Fassung gibt.
|
||||
*
|
||||
* @return array<int,array{no:string,name:string,encoding:int,bytes:int,inline:bool,cid:string,eml:bool}>
|
||||
*/
|
||||
function mail_collect_attachment_parts(object $part, string $no = ''): array
|
||||
{
|
||||
$type = (int) ($part->type ?? -1);
|
||||
if (!empty($part->parts) && is_array($part->parts) && $type !== 2) {
|
||||
$found = [];
|
||||
foreach ($part->parts as $i => $sub) {
|
||||
$found = array_merge($found, mail_collect_attachment_parts($sub, $no === '' ? (string) ($i + 1) : $no . '.' . ($i + 1)));
|
||||
}
|
||||
return $found;
|
||||
}
|
||||
$disposition = strtolower((string) ($part->disposition ?? ''));
|
||||
$name = '';
|
||||
foreach (array_merge((array) ($part->dparameters ?? []), (array) ($part->parameters ?? [])) as $p) {
|
||||
$attr = strtolower((string) ($p->attribute ?? ''));
|
||||
if ($name === '' && ($attr === 'filename' || $attr === 'filename*' || $attr === 'name' || $attr === 'name*')) $name = mail_decode_filename((string) $p->value);
|
||||
}
|
||||
$cid = strtolower(trim((string) ($part->id ?? ''), " \t\r\n<>"));
|
||||
$isEml = $type === 2;
|
||||
$inline = $type === 5 && $disposition !== 'attachment' && !empty($part->ifid) && $cid !== '';
|
||||
if (!$isEml && !$inline && $name === '' && $disposition !== 'attachment') return [];
|
||||
if ($name === '') {
|
||||
if ($isEml) $name = 'Weitergeleitete E-Mail.eml';
|
||||
else $name = 'Anhang' . (!empty($part->subtype) ? '.' . strtolower((string) $part->subtype) : '');
|
||||
}
|
||||
return [['no' => $no === '' ? '1' : $no, 'name' => $name, 'encoding' => (int) ($part->encoding ?? 0), 'bytes' => (int) ($part->bytes ?? 0), 'inline' => $inline, 'cid' => $cid, 'eml' => $isEml]];
|
||||
}
|
||||
|
||||
/** Dateiendung für ein Rasterbild anhand seines Inhalts (nur gif, jpg, png, webp), sonst null: HTML oder Skripte im Bild-Gewand fallen so durch. */
|
||||
function mail_image_extension(string $data): ?string
|
||||
{
|
||||
$info = @getimagesizefromstring($data);
|
||||
if (!$info) return null;
|
||||
return match ($info[2] ?? 0) {
|
||||
IMAGETYPE_GIF => 'gif',
|
||||
IMAGETYPE_JPEG => 'jpg',
|
||||
IMAGETYPE_PNG => 'png',
|
||||
IMAGETYPE_WEBP => 'webp',
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
/** Name einer weitergeleiteten Mail (.eml) aus ihrem Betreff. */
|
||||
function mail_eml_name(string $raw, string $fallback): string
|
||||
{
|
||||
if (preg_match('/^Subject:[ \t]*(.*(?:\r?\n[ \t]+.*)*)/mi', substr($raw, 0, 8192), $m)) {
|
||||
$subject = trim(mail_decode_filename(preg_replace('/\r?\n[ \t]+/', ' ', $m[1]) ?? ''));
|
||||
$subject = trim((string) preg_replace('~[\\\\/:*?"<>|\x00-\x1F]+~u', ' ', $subject));
|
||||
if ($subject !== '') return mb_substr($subject, 0, 120) . '.eml';
|
||||
}
|
||||
return $fallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* HTML-Fassung einer Mail für die Anzeige im Ticket (bereinigt: nur erlaubte Tags und Eigenschaften), null ohne HTML-Teil.
|
||||
*
|
||||
* @param callable(string):string $fetchPart liefert den rohen Inhalt eines Teils (imap_fetchbody)
|
||||
*/
|
||||
function mail_html_for_display(object $structure, callable $fetchPart): ?string
|
||||
{
|
||||
foreach (mail_collect_text_parts($structure) as $p) {
|
||||
if ($p['subtype'] !== 'HTML') continue;
|
||||
$html = mail_decode_part($fetchPart($p['no']), $p['encoding'], $p['charset']);
|
||||
if (trim($html) === '' || strlen($html) > 3000000) return null;
|
||||
$clean = sanitize_staff_html($html);
|
||||
if (trim(strip_tags($clean)) === '' && stripos($clean, '<img') === false) return null;
|
||||
return strlen($clean) > 400000 ? null : $clean;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Rohdaten eines Mail-Teils nach der Transferkodierung (Anhänge sind Binärdaten, kein Zeichensatz). */
|
||||
function mail_decode_binary(string $raw, int $encoding): string
|
||||
{
|
||||
return match ($encoding) {
|
||||
3 => (string) base64_decode(preg_replace('/\s+/', '', $raw) ?? $raw, false),
|
||||
4 => quoted_printable_decode($raw),
|
||||
default => $raw,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Übernimmt die Anhänge einer Mail an die Nachricht $threadId des Tickets. Nicht übernommene Anhänge (Typ nicht erlaubt, zu groß)
|
||||
* werden im Verlauf des Tickets vermerkt, damit niemand glaubt, die Mail hätte keinen gehabt.
|
||||
*
|
||||
* @param callable(string):string $fetchPart liefert den rohen Inhalt eines Teils (imap_fetchbody)
|
||||
* @param bool $skipExisting Anhänge, die an dieser Nachricht schon hängen (gleicher Name und gleiche Größe), nicht noch einmal speichern
|
||||
* @param bool $inlineImages die Mail hat eine HTML-Fassung: Bilder mit Content-ID werden als eingebettete Bilder gespeichert (und nicht als
|
||||
* Anhang aufgelistet); sonst zählen sie wie jeder andere Anhang
|
||||
* @return array{added:int,inline:int,skipped:int,existing:int}
|
||||
*/
|
||||
function mail_store_attachments(PDO $pdo, ?object $structure, callable $fetchPart, int $threadId, int $ticketId, string $label, array &$log, bool $skipExisting = false, bool $inlineImages = false): array
|
||||
{
|
||||
$count = ['added' => 0, 'inline' => 0, 'skipped' => 0, 'existing' => 0];
|
||||
if (!$structure || $threadId <= 0) return $count;
|
||||
$max = get_max_upload_size();
|
||||
$parts = mail_collect_attachment_parts($structure);
|
||||
$imageExts = ['png', 'jpg', 'jpeg', 'gif', 'webp'];
|
||||
foreach (array_slice($parts, 0, 40) as $p) {
|
||||
$reason = null;
|
||||
$name = $p['name'];
|
||||
$asInline = $inlineImages && $p['inline'];
|
||||
// Vorab nach Größe prüfen, ohne den Teil zu laden (Base64 ist rund ein Drittel größer als die Datei)
|
||||
$tooBig = $p['bytes'] > 0 && (int) ($p['encoding'] === 3 ? $p['bytes'] * 0.75 : $p['bytes']) > $max;
|
||||
if ($tooBig) $reason = 'zu groß (max. ' . round($max / 1048576) . ' MB)';
|
||||
if ($reason === null) {
|
||||
$raw = $fetchPart($p['no']);
|
||||
$data = $p['eml'] ? $raw : mail_decode_binary($raw, $p['encoding']);
|
||||
$opts = ['extra_allowed' => mail_attachment_extra_extensions()];
|
||||
if ($p['eml']) {
|
||||
$name = mail_eml_name($data, $name);
|
||||
if (!str_ends_with(strtolower($name), '.eml')) $name .= '.eml';
|
||||
$opts['extra_allowed'][] = 'eml';
|
||||
} elseif ($asInline) {
|
||||
$ext = mail_image_extension($data);
|
||||
if ($ext === null) {
|
||||
$log[] = "[$label] → Eingebettetes Bild übersprungen (kein Bild): {$name}";
|
||||
continue;
|
||||
}
|
||||
if (!in_array(strtolower(pathinfo($name, PATHINFO_EXTENSION)), $imageExts, true)) $name = 'bild-' . substr(md5($p['cid']), 0, 8) . '.' . $ext;
|
||||
$opts = ['extra_allowed' => $imageExts, 'content_id' => $p['cid'], 'inline' => true];
|
||||
}
|
||||
if ($skipExisting) {
|
||||
if ($asInline) {
|
||||
$dup = $pdo->prepare('SELECT COUNT(*) FROM ticket_attachments WHERE thread_id = ? AND content_id = ?');
|
||||
$dup->execute([$threadId, mb_substr($p['cid'], 0, 255)]);
|
||||
} else {
|
||||
$dup = $pdo->prepare('SELECT COUNT(*) FROM ticket_attachments WHERE thread_id = ? AND original_name = ? AND filesize = ?');
|
||||
$dup->execute([$threadId, mb_substr(trim(basename(str_replace(chr(92), '/', $name))), 0, 255), strlen($data)]);
|
||||
}
|
||||
if ((int) $dup->fetchColumn() > 0) {
|
||||
$count['existing']++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$res = store_ticket_attachment_data($pdo, $threadId, $name, $data, $opts);
|
||||
if ($res['ok']) {
|
||||
$log[] = "[$label] → " . ($asInline ? 'Eingebettetes Bild' : 'Anhang') . " übernommen: {$res['name']}";
|
||||
$count[$asInline ? 'inline' : 'added']++;
|
||||
continue;
|
||||
}
|
||||
$reason = (string) ($res['error'] ?? 'unbekannter Fehler');
|
||||
}
|
||||
if ($asInline) {
|
||||
$log[] = "[$label] → Eingebettetes Bild nicht übernommen: {$name} ({$reason})";
|
||||
continue;
|
||||
}
|
||||
$count['skipped']++;
|
||||
$log[] = "[$label] → Anhang nicht übernommen: {$name} ({$reason})";
|
||||
log_activity($pdo, $ticketId, null, 'Anhang aus E-Mail nicht übernommen: ' . mb_substr($name, 0, 80) . ' (' . mb_substr($reason, 0, 100) . ')');
|
||||
}
|
||||
if (count($parts) > 40) log_activity($pdo, $ticketId, null, 'Anhänge aus E-Mail nicht übernommen: mehr als 40 Teile, die übrigen fehlen');
|
||||
return $count;
|
||||
}
|
||||
|
||||
/** Kopfzeilen-Schlüssel eines zitierten Mailkopfs ("Von: …", "Gesendet: …") am Zeilenanfang. */
|
||||
const MAIL_QUOTE_HEADER_KEYS = '(?:Von|From|Gesendet|Sent|Datum|Date|An|To|Cc|Betreff|Subject)';
|
||||
|
||||
/**
|
||||
* Trennt eine Nachricht (Text) in den neuen Teil und den zitierten Verlauf früherer Mails. Der Verlauf beginnt bei der ersten
|
||||
* Zeile, die ein Zitat ankündigt: "Am … schrieb …:", "On … wrote:", "-----Ursprüngliche Nachricht-----", ein zitierter Mailkopf
|
||||
* ("Von: … Gesendet: … An: … Betreff: …") oder eine mit ">" beginnende Zeile. Steht vor dieser Stelle kein Text (die ganze Mail ist
|
||||
* zitiert oder weitergeleitet), wird nichts getrennt.
|
||||
*
|
||||
* @return array{0:string,1:string} [neuer Teil, zitierter Verlauf (leer, wenn es keinen gibt)]
|
||||
*/
|
||||
function mail_split_quoted_text(string $text): array
|
||||
{
|
||||
$text = str_replace(["\r\n", "\r"], "\n", $text);
|
||||
$lines = explode("\n", $text);
|
||||
$n = count($lines);
|
||||
for ($i = 0; $i < $n; $i++) {
|
||||
$line = $lines[$i];
|
||||
$isMarker = false;
|
||||
if (preg_match('/^\s*-{2,}\s*(Original Message|Ursprüngliche Nachricht|Urspr\. Nachricht|Weitergeleitete Nachricht|Forwarded message)\s*-{2,}\s*$/iu', $line)) {
|
||||
$isMarker = true;
|
||||
} elseif (preg_match('/^\s*(Am|On)\s.+/iu', $line)) {
|
||||
$joined = $line . ' ' . ($lines[$i + 1] ?? '');
|
||||
if (preg_match('/^\s*(Am|On)\s.{5,200}\s(schrieb|wrote)\b.*:\s*$/iu', $joined) || preg_match('/^\s*(Am|On)\s.{5,200}\s(schrieb|wrote)\b.*:\s*$/iu', $line)) $isMarker = true;
|
||||
} elseif (preg_match('/^\s*>/', $line)) {
|
||||
$isMarker = true;
|
||||
} elseif (preg_match('/^\s*' . MAIL_QUOTE_HEADER_KEYS . '\s*:/iu', $line)) {
|
||||
// Zitierter Mailkopf: mehrere solche Zeilen hintereinander, darunter "Von"
|
||||
$keys = [];
|
||||
for ($j = $i; $j < min($n, $i + 7); $j++) {
|
||||
if (preg_match('/^\s*(' . MAIL_QUOTE_HEADER_KEYS . ')\s*:/iu', $lines[$j], $m)) $keys[] = mb_strtolower($m[1]);
|
||||
elseif (trim($lines[$j]) !== '') break;
|
||||
}
|
||||
if (count($keys) >= 3 && (in_array('von', $keys, true) || in_array('from', $keys, true))) $isMarker = true;
|
||||
}
|
||||
if (!$isMarker) continue;
|
||||
$body = trim(implode("\n", array_slice($lines, 0, $i)));
|
||||
if ($body === '') return [$text, ''];
|
||||
return [$body, trim(implode("\n", array_slice($lines, $i)))];
|
||||
}
|
||||
return [$text, ''];
|
||||
}
|
||||
|
||||
/** Bringt ein HTML-Bruchstück in Ordnung (offene Tags schließen, überzählige schließende Tags entfernen). */
|
||||
function mail_balance_html(string $html): string
|
||||
{
|
||||
if (trim($html) === '' || !class_exists('DOMDocument')) return $html;
|
||||
$prev = libxml_use_internal_errors(true);
|
||||
$dom = new DOMDocument();
|
||||
$ok = $dom->loadHTML('<?xml encoding="UTF-8"><div id="dh-root">' . $html . '</div>', LIBXML_NOWARNING | LIBXML_NOERROR | LIBXML_NONET | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
|
||||
libxml_clear_errors();
|
||||
libxml_use_internal_errors($prev);
|
||||
if (!$ok) return $html;
|
||||
$root = $dom->getElementById('dh-root');
|
||||
if (!$root) return $html;
|
||||
$out = '';
|
||||
foreach ($root->childNodes as $child) $out .= $dom->saveHTML($child);
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trennt bereinigtes Mail-HTML in den neuen Teil und den zitierten Verlauf. Gesucht wird die Trennlinie (<hr>) oder der Block mit
|
||||
* oberem Rahmen, den Outlook vor dem zitierten Mailkopf setzt, und danach der Mailkopf mit mindestens drei Angaben (Von, Gesendet, An …).
|
||||
*
|
||||
* @return array{0:string,1:string}
|
||||
*/
|
||||
function mail_split_quoted_html(string $html): array
|
||||
{
|
||||
if (!preg_match_all('/<hr\b[^>]*>|<div\b[^>]*border-top[^>]*>/i', $html, $matches, PREG_OFFSET_CAPTURE)) return [$html, ''];
|
||||
foreach ($matches[0] as [$tag, $offset]) {
|
||||
$after = html_entity_decode(preg_replace('/<[^>]*>/', ' ', substr($html, $offset, 1500)) ?? '', ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
$after = preg_replace('/\s+/u', ' ', $after) ?? $after;
|
||||
preg_match_all('/(?:^|\s)(' . MAIL_QUOTE_HEADER_KEYS . ')\s*:/iu', mb_substr($after, 0, 600), $m);
|
||||
$keys = array_map('mb_strtolower', $m[1] ?? []);
|
||||
if (count($keys) < 3 || !(in_array('von', $keys, true) || in_array('from', $keys, true))) continue;
|
||||
$head = substr($html, 0, $offset);
|
||||
if (trim(strip_tags($head)) === '' && stripos($head, '<img') === false) return [$html, ''];
|
||||
$a = mail_balance_html($head);
|
||||
$b = mail_balance_html(substr($html, $offset));
|
||||
return [$a, $b];
|
||||
}
|
||||
return [$html, ''];
|
||||
}
|
||||
|
||||
/** Zuklappbarer Block für zitierten Verlauf (derselbe Aufbau wie bei eingeklappten Zitaten). */
|
||||
function mail_quote_wrap(string $contentHtml): string
|
||||
{
|
||||
return '<div class="email-quote-wrap"><button type="button" class="email-quote-toggle" onclick="this.nextElementSibling.style.display=this.nextElementSibling.style.display===\'none\'?\'block\':\'none\';this.textContent=this.nextElementSibling.style.display===\'none\'?\'▶ Verlauf anzeigen\':\'▼ Verlauf ausblenden\'">▶ Verlauf anzeigen</button><div class="email-quoted" style="display:none">' . $contentHtml . '</div></div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Bester Text einer Mail: bevorzugt text/plain; ist der nur eine kurze Floskel ("Bitte HTML-Ansicht verwenden") und die HTML-Fassung
|
||||
* deutlich länger, wird die HTML-Fassung als Text genommen.
|
||||
|
||||
Reference in New Issue
Block a user