You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
223 lines
9.4 KiB
PHP
223 lines
9.4 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
$directory = sys_get_temp_dir() . '/virt-cloud-mail-render-' . bin2hex(random_bytes(8));
|
|
if (!mkdir($directory, 0700)) {
|
|
throw new RuntimeException('Cannot create private mail-render directory');
|
|
}
|
|
$assert = static function ($condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$render = static function (string $template, array $variables) use ($directory): string {
|
|
$smarty = new Smarty();
|
|
$smarty->setTemplateDir(dirname(__DIR__) . '/resources/email');
|
|
$smarty->setCompileDir($directory);
|
|
$smarty->setCacheDir($directory);
|
|
$smarty->assign('config', [
|
|
'appName' => 'Virt <Cloud>',
|
|
'baseUrl' => 'https://cloud.example.test',
|
|
]);
|
|
foreach ($variables as $name => $value) {
|
|
$smarty->assign($name, $value);
|
|
}
|
|
return $smarty->fetch($template);
|
|
};
|
|
|
|
try {
|
|
$registration = $render('auth/verify.tpl', [
|
|
'language' => 'false',
|
|
'scene' => 'registration',
|
|
'code' => '12<345',
|
|
'expire' => '2026-09-28 18:00:00',
|
|
]);
|
|
$login = $render('auth/verify.tpl', [
|
|
'language' => 'true',
|
|
'scene' => 'login',
|
|
'code' => '654321',
|
|
'expire' => '2026-09-28 18:00:00',
|
|
]);
|
|
$emailChange = $render('auth/verify.tpl', [
|
|
'language' => 'false',
|
|
'scene' => 'email_change',
|
|
'code' => '246810',
|
|
'expire' => '2026-09-28 18:00:00',
|
|
]);
|
|
$reset = $render('password/reset.tpl', [
|
|
'language' => 'false',
|
|
'resetUrl' => 'https://cloud.example.test/password/token/a?next=<unsafe>&x=1',
|
|
'expire' => '2026-09-29 18:00:00',
|
|
]);
|
|
$renewal = $render('cloud/invoice.tpl', [
|
|
'language' => 'false',
|
|
'invoice_id' => '42',
|
|
'instance_name' => '<script>alert("renewal")</script>',
|
|
'amount' => '12.00',
|
|
'due_at' => '2026-10-05 18:00:00',
|
|
'payment_deadline_at' => '2026-10-08 18:00:00',
|
|
'invoice_url' => 'https://cloud.example.test/user/cloud/invoices/42?x=1&y=2',
|
|
]);
|
|
$provisioned = $render('cloud/provision-success.tpl', [
|
|
'language' => 'false',
|
|
'instance_name' => '<script>alert("ready")</script>',
|
|
'instance_id' => '43',
|
|
'region' => 'HongKong',
|
|
'public_ip' => '203.0.113.43',
|
|
'os_name' => 'AlmaLinux 10',
|
|
'login_name' => 'root',
|
|
'created_at' => '2026-09-28 18:00:00',
|
|
'expires_at' => '2026-10-28 18:00:00',
|
|
'instance_url' => 'https://cloud.example.test/user/cloud/instances/43?x=1&y=2',
|
|
]);
|
|
$maintenance = $render('cloud/maintenance-notice.tpl', [
|
|
'notice_subject' => '网络 <维护>',
|
|
'message_text' => "请保存 <script>alert('x')</script>\n第二行",
|
|
'scope_label' => 'HKG-A & IPv4 pool',
|
|
'impact_label' => '网络可能短时中断',
|
|
'maintenance_starts_utc' => '2026-11-06 03:00:00 UTC',
|
|
'maintenance_ends_utc' => '2026-11-06 04:00:00 UTC',
|
|
'instances' => [
|
|
['name' => 'vm-<one>', 'ipv4' => '203.0.113.10',
|
|
'ipv6' => '2001:db8::10'],
|
|
['name' => 'vm-two', 'ipv4' => '', 'ipv6' => ''],
|
|
],
|
|
]);
|
|
$compensation = $render('cloud/service-compensation.tpl', [
|
|
'reason_text' => "网络 <故障>\n已恢复",
|
|
'scope_label' => 'HKG-A & IPv4 pool',
|
|
'compensation_duration' => '2 小时',
|
|
'incident_window_utc' => '2026-11-06 03:00:00 UTC — '
|
|
. '2026-11-06 04:00:00 UTC',
|
|
'instances' => [[
|
|
'instance_name' => 'vm-<one>',
|
|
'previous_expires_utc' => '2026-12-01 00:00:00 UTC',
|
|
'new_expires_utc' => '2026-12-01 02:00:00 UTC',
|
|
]],
|
|
]);
|
|
|
|
$user = (object) [
|
|
'user_name' => '云端 <用户>',
|
|
'email' => 'user@example.test',
|
|
];
|
|
$ticket = (object) [
|
|
'id' => 17,
|
|
'title' => '网络 <异常> & 排查',
|
|
];
|
|
$ticketCreated = $render('ticket/created.tpl', [
|
|
'user' => $user,
|
|
'ticket' => $ticket,
|
|
'ticket_url' => 'https://cloud.example.test/user/tickets/17?x=1&y=2',
|
|
]);
|
|
$ticketReplied = $render('ticket/replied.tpl', [
|
|
'user' => $user,
|
|
'ticket' => $ticket,
|
|
'ticket_url' => 'https://cloud.example.test/user/tickets/17?x=1&y=2',
|
|
]);
|
|
$backup = $render('news/backup.tpl', [
|
|
'text' => '数据库备份已完成。',
|
|
]);
|
|
$monitor = $render('news/monitor.tpl', [
|
|
'user' => $user,
|
|
'text' => '<script>alert("notice")</script>\n第二行',
|
|
]);
|
|
$warning = $render('news/warn.tpl', [
|
|
'user' => $user,
|
|
'text' => '工单 #17 有新的消息。',
|
|
]);
|
|
$finance = $render('news/finance.tpl', [
|
|
'user' => $user,
|
|
'title' => '余额 <提醒>',
|
|
'text' => '您的账户余额发生变化。',
|
|
]);
|
|
$addressNotice = $render('news/daily-traffic-report.tpl', [
|
|
'user' => $user,
|
|
]);
|
|
$newYear = $render('news/nian.tpl', []);
|
|
$testMail = $render('test.tpl', [
|
|
'time' => '2026-10-23 12:00:00',
|
|
]);
|
|
|
|
$renderedEmails = [$registration, $login, $emailChange, $reset, $renewal,
|
|
$provisioned, $maintenance, $compensation, $ticketCreated,
|
|
$ticketReplied, $backup, $monitor, $warning, $finance,
|
|
$addressNotice, $newYear, $testMail];
|
|
foreach ($renderedEmails as $html) {
|
|
$assert(str_contains(strtolower($html), '<!doctype html>')
|
|
&& str_contains($html, 'CONTROL PANEL')
|
|
&& str_contains(strtolower($html), '#102235')
|
|
&& str_contains($html,
|
|
'© 2026 Virt <Cloud>. All rights reserved.')
|
|
&& str_contains($html,
|
|
'@media only screen and (max-width: 620px)'),
|
|
'A rendered email lost its shared theme, responsive CSS or footer.');
|
|
}
|
|
$assert(str_contains($registration, 'Verify your email address')
|
|
&& str_contains($registration, '12<345')
|
|
&& str_contains($login, 'Confirm your sign-in')
|
|
&& str_contains($emailChange, 'Confirm your email change')
|
|
&& str_contains($emailChange, 'verify this new email address')
|
|
&& str_contains($emailChange, '246810'),
|
|
'Registration, login or email-change verification rendered the wrong scene.');
|
|
$assert(str_contains($reset, 'Reset your password')
|
|
&& str_contains($reset, '<unsafe>')
|
|
&& str_contains($reset, '&x=1'),
|
|
'Password-reset email did not render or escape its secure URL.');
|
|
$assert(str_contains($renewal, 'Renewal invoice created')
|
|
&& str_contains($renewal, '<script>')
|
|
&& str_contains($renewal, '#42')
|
|
&& str_contains($renewal, '2026-10-08 18:00:00'),
|
|
'Renewal invoice email did not render its safe public details.');
|
|
$assert(str_contains($provisioned, 'Your instance is ready')
|
|
&& str_contains($provisioned, '<script>')
|
|
&& str_contains($provisioned, '203.0.113.43')
|
|
&& !str_contains($provisioned, 'not-for-email'),
|
|
'Provision-success email did not render safe instance details.');
|
|
$assert(str_contains($maintenance, '网络 <维护>')
|
|
&& str_contains($maintenance,
|
|
'<script>alert('x')</script>')
|
|
&& !str_contains($maintenance, '<script>')
|
|
&& str_contains($maintenance, 'vm-<one>')
|
|
&& str_contains($maintenance, '203.0.113.10')
|
|
&& str_contains($maintenance, '2001:db8::10')
|
|
&& str_contains($maintenance, '2026-11-06 03:00:00 UTC'),
|
|
'Maintenance email did not escape content or render its UTC scope.');
|
|
$assert(str_contains($compensation, 'Service time compensation applied')
|
|
&& str_contains($compensation, '网络 <故障>')
|
|
&& !str_contains($compensation, '<故障>')
|
|
&& str_contains($compensation, 'HKG-A & IPv4 pool')
|
|
&& str_contains($compensation, 'vm-<one>')
|
|
&& str_contains($compensation, '2026-12-01 02:00:00 UTC'),
|
|
'Compensation email did not escape its frozen result.');
|
|
$assert(str_contains($ticketCreated, 'Ticket submitted')
|
|
&& str_contains($ticketReplied, 'Ticket reply received')
|
|
&& str_contains($ticketCreated, '网络 <异常> & 排查'),
|
|
'Ticket emails did not render their escaped shared content.');
|
|
$assert(str_contains($backup, '数据库备份已完成')
|
|
&& str_contains($backup, 'Database backup completed')
|
|
&& str_contains($monitor, '<script>alert("notice")</script>')
|
|
&& !str_contains($monitor, '<script>')
|
|
&& str_contains($warning, 'Action required')
|
|
&& str_contains($finance, '余额 <提醒>')
|
|
&& str_contains($addressNotice, 'https://cloud.example.test')
|
|
&& str_contains($newYear, 'New Year offer')
|
|
&& str_contains($testMail, 'Email delivery is working'),
|
|
'A legacy or test email did not migrate safely to the shared layout.');
|
|
} finally {
|
|
$generatedFiles = new RecursiveIteratorIterator(
|
|
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS),
|
|
RecursiveIteratorIterator::CHILD_FIRST
|
|
);
|
|
foreach ($generatedFiles as $generated) {
|
|
if ($generated->isFile()) {
|
|
unlink($generated->getPathname());
|
|
} elseif ($generated->isDir()) {
|
|
rmdir($generated->getPathname());
|
|
}
|
|
}
|
|
rmdir($directory);
|
|
}
|
|
|
|
echo "All email templates rendered safely with the responsive shared layout.\n";
|