Files
tmail-flutter/core/lib/utils/print_utils.dart
T
Dang Dat 9e4152639f 2509 Escape header fields in the EML preview and remove the unused PDF viewer
The EML / full-message preview and the print page build their header
as an HTML string. Subject, sender, recipients, date, attachment names
and the attachment link were inserted unescaped, so a crafted header
value became markup in a same-origin page.
DownloadManager.openDownloadedFileWeb loaded pdf.js/pdfobject from a
CDN and put the file name unescaped into a blob page. Nothing calls it.

PreviewEmlFileUtils and PrintUtils escape every text field at the sink.
Callers pass raw strings and no longer pre-escape them. The attachment
link is attribute-escaped and built with Uri, so file names survive the
round-trip. The inline-image alt and id attributes are attribute-escaped.
openDownloadedFileWeb, its CDN loaders and js_interop_stub.dart are
removed, and DownloadManager no longer takes DeviceInfoPlugin.
2026-10-01 15:49:46 +07:00

321 lines
9.6 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:core/data/model/print_attachment.dart';
import 'package:core/utils/app_logger.dart';
import 'package:core/utils/html/html_interaction.dart';
import 'package:core/utils/html/html_template.dart';
import 'package:core/utils/html/html_utils.dart';
import 'package:flutter/foundation.dart';
import 'package:html/dom.dart';
import 'package:html/parser.dart';
import 'package:printing/printing.dart';
class PrintUtils {
static const HtmlEscape _textEscape = HtmlEscape();
final void Function(String htmlDocument) _openHtmlDocument;
PrintUtils({@visibleForTesting void Function(String htmlDocument)? openHtmlDocument})
: _openHtmlDocument = openHtmlDocument ?? HtmlUtils.openNewTabHtmlDocument;
String _escapeText(String value) => _textEscape.convert(value);
Future<void> printPDFFile(Uint8List bytes, String fileName) async {
await Printing.layoutPdf(
name: fileName,
onLayout: (_) => bytes);
}
Element? _createUserInformationElement({
required String appName,
required String userName,
}) {
try {
return Element.html('''
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr height="14px">
<td align="right" style="color: #777;">
<b>$userName</b>
</td>
</tr>
</tbody>
</table>
''');
} catch (e) {
logWarning('PrintUtils::_createUserInformationElement: Exception = $e');
return null;
}
}
Element? get dividerElement {
try {
return Element.html('<hr />');
} catch (e) {
logWarning('PrintUtils::dividerElement: Exception = $e');
return null;
}
}
Element? _createSubjectElement(String subject) {
try {
return Element.html('''
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td>
<font size="+1"><b>${_escapeText(subject)}</b></font><br />
</td>
</tr>
</tbody>
</table>
''');
} catch (e) {
logWarning('PrintUtils::_createSubjectElement: Exception = $e');
return null;
}
}
@visibleForTesting
Element? createSenderElement({
required String fromPrefix,
required String senderName,
required String senderEmailAddress,
required String dateTime,
}) {
try {
return Element.html('''
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td>
<font size="-1">$fromPrefix: <b>${_escapeText(senderName)} </b>&lt;${_escapeText(senderEmailAddress)}&gt;</font>
</td>
<td align="right"><font size="-1">${_escapeText(dateTime)}</font></td>
</tr>
</tbody>
</table>
''');
} catch (e) {
logWarning('PrintUtils::createSenderElement: Exception = $e');
return null;
}
}
String? _createRecipientHtmlTag(String prefix, String emailAddress) {
try {
Element element = Element.html('<div class="${prefix.toLowerCase()}"></div>');
element.text = '$prefix: $emailAddress';
return element.outerHtml;
} catch (e) {
logWarning('PrintUtils::_createRecipientHtmlTag: Exception = $e');
return '';
}
}
Element? _createRecipientsElement({
required String toPrefix,
required String ccPrefix,
required String bccPrefix,
required String replyToPrefix,
String? toAddress,
String? ccAddress,
String? bccAddress,
String? replyToAddress,
}) {
try {
return Element.html('''
<tr>
<td colspan="2" style="padding-bottom: 4px;" class="recipient">
${replyToAddress?.isNotEmpty == true ? _createRecipientHtmlTag(replyToPrefix, replyToAddress!) : ''}
${toAddress?.isNotEmpty == true ? _createRecipientHtmlTag(toPrefix, toAddress!) : ''}
${ccAddress?.isNotEmpty == true ? _createRecipientHtmlTag(ccPrefix, ccAddress!) : ''}
${bccAddress?.isNotEmpty == true ? _createRecipientHtmlTag(bccPrefix, bccAddress!) : ''}
</td>
</tr>
''');
} catch (e) {
logWarning('PrintUtils::_createRecipientsElement: Exception = $e');
return null;
}
}
Element? _createEmailContentElement(String emailContent) {
try {
return Element.html('''
<tr>
<td colspan="2">
<table width="100%" cellpadding="12" cellspacing="0" border="0">
<tbody>
<tr>
<td>$emailContent</td>
</tr>
</tbody>
</table>
</td>
</tr>
''');
} catch (e) {
logWarning('PrintUtils::_createEmailContentElement: Exception = $e');
return null;
}
}
String _createTitleAttachmentHtmlTag({
required int countAttachments,
required String titleAttachment
}) {
return '<tr><td colspan="2"><b style="padding-left:3">$countAttachments $titleAttachment</b></td></tr>';
}
String _createAttachmentHtmlTag(PrintAttachment printAttachment) {
return '''
<tr>
<td>
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<a target="_blank" href="">
<img width="16" height="16" src="${HtmlUtils.generateSVGImageData(printAttachment.iconBase64Data)}" />
</a>
</td>
<td width="7"></td>
<td>
<b>${_escapeText(printAttachment.name)}</b><br />
${printAttachment.size}
</td>
</tr>
</tbody>
</table>
</td>
</tr>
''';
}
Element? _createAttachmentsElement({
required String titleAttachment,
required List<PrintAttachment> listAttachment
}) {
try {
return Element.html('''
<table class="attachments" cellspacing="0" cellpadding="5" border="0">
<tbody>
${_createTitleAttachmentHtmlTag(countAttachments: listAttachment.length, titleAttachment: titleAttachment)}
${listAttachment.map((printAttachment) => _createAttachmentHtmlTag(printAttachment)).toList().join('')}
</tbody>
</table>
''');
} catch (e) {
logWarning('PrintUtils::_createAttachmentsElement: Exception = $e');
return null;
}
}
Future<void> printEmail({
required String appName,
required String userName,
required String subject,
required String emailContent,
required String senderName,
required String senderEmailAddress,
required String dateTime,
required String fromPrefix,
required String toPrefix,
required String ccPrefix,
required String bccPrefix,
required String replyToPrefix,
required String titleAttachment,
String? toAddress,
String? ccAddress,
String? bccAddress,
String? replyToAddress,
List<PrintAttachment>? listAttachment,
}) async {
Document document = parse(HtmlUtils.createTemplateHtmlDocument(title: _escapeText('$appName - $subject')));
Element bodyContainerElement = Element.html('<div class="body-container"></div>');
Element mainContentElement = Element.html('<div class="main-content"></div>');
Element messageElement = Element.html('<table width="100%" cellpadding="0" cellspacing="0" border="0" class="message"></table>');
Element messageBodyElement = Element.html('<tbody></tbody>');
Element? userInfoElement = _createUserInformationElement(
appName: appName,
userName: userName);
Element? subjectElement = _createSubjectElement(subject);
Element? senderElement = createSenderElement(
fromPrefix: fromPrefix,
senderName: senderName,
senderEmailAddress: senderEmailAddress,
dateTime: dateTime);
Element? recipientsElement = _createRecipientsElement(
toPrefix: toPrefix,
ccPrefix: ccPrefix,
bccPrefix: bccPrefix,
replyToPrefix: replyToPrefix,
toAddress: toAddress,
ccAddress: ccAddress,
bccAddress: bccAddress,
replyToAddress: replyToAddress,
);
Element? emailContentElement = _createEmailContentElement(emailContent);
if (senderElement != null) {
messageBodyElement.append(senderElement);
}
if (recipientsElement != null) {
messageBodyElement.append(recipientsElement);
}
if (emailContentElement != null) {
messageBodyElement.append(emailContentElement);
}
messageElement.append(messageBodyElement);
if (subjectElement != null) {
mainContentElement.append(subjectElement);
}
if (dividerElement != null) {
mainContentElement.append(dividerElement!);
}
mainContentElement.append(messageElement);
if (listAttachment?.isNotEmpty == true) {
final attachmentsElement = _createAttachmentsElement(
titleAttachment: titleAttachment,
listAttachment: listAttachment!);
if (dividerElement != null) {
mainContentElement.append(dividerElement!);
}
if (attachmentsElement != null) {
mainContentElement.append(attachmentsElement);
}
}
if (userInfoElement != null) {
bodyContainerElement.append(userInfoElement);
}
if (dividerElement != null) {
bodyContainerElement.append(dividerElement!);
}
bodyContainerElement.append(mainContentElement);
Element styleElement = Element.html(HtmlTemplate.printDocumentCssStyle);
document.head?.append(styleElement);
document.body?.append(bodyContainerElement);
Element scriptElement = Element.html(HtmlInteraction.scriptHandleInvokePrinterOnBrowser);
document.body?.append(scriptElement);
final htmlDocument = document.outerHtml;
_openHtmlDocument(htmlDocument);
}
}