Files
MailNotifier/Action.php

149 lines
6.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* MailNotifier 邮件通知插件 - 邮件发送 Action
*
* @package MailNotifier
* @author 石头厝
*/
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
if (!class_exists('MailNotifier_Plugin')) {
require_once dirname(__FILE__) . '/Plugin.php';
}
class MailNotifier_Action extends Typecho_Widget implements Widget_Interface_Do
{
public function execute() {}
public function action()
{
$this->test();
}
public function test()
{
$auth = isset($_REQUEST['auth']) ? trim((string) $_REQUEST['auth']) : '';
$savedAuth = MailNotifier_Plugin::configStr('auth', '');
header('Content-Type: text/html; charset=UTF-8');
if ($savedAuth === '' || !hash_equals($savedAuth, $auth)) {
$this->renderResult(false, '测试密钥不正确,请从插件设置页点击“测试发送”。');
return;
}
$to = isset($_REQUEST['to']) ? trim((string) $_REQUEST['to']) : '';
$result = MailNotifier_Plugin::sendTest($to);
$this->renderResult(!empty($result['ok']), isset($result['message']) ? $result['message'] : '');
}
/**
* 插件设置备份 / 恢复
*
* 入口在「插件设置」页顶部,但动作不能交给 options-plugin.php:
* 那页的表单是 Typecho 自己的保存表单,里面再嵌一个 form 属于非法 HTML,
* 浏览器会把内层 form 丢掉,点备份反而触发一次「保存设置」。
* 所以走独立路由,处理完 302 回设置页,用 _cnot 参数把结果带回去。
*/
public function settings()
{
if (!class_exists('MailNotifier_Settings')) {
require_once dirname(__FILE__) . '/Settings.php';
}
$user = null;
try {
$user = Typecho_Widget::widget('Widget_User');
} catch (Exception $e) {
$user = null;
} catch (Throwable $e) {
$user = null;
}
$isAdmin = false;
try {
$isAdmin = $user && $user->pass('administrator', true);
} catch (Exception $e) {
$isAdmin = false;
} catch (Throwable $e) {
$isAdmin = false;
}
if (!$isAdmin) {
$this->redirectTo(MailNotifier_Plugin::configUrl('_cnot=auth'));
return;
}
if (!MailNotifier_Plugin::checkMailLogToken(isset($_GET['_']) ? (string) $_GET['_'] : '')) {
$this->redirectTo(MailNotifier_Plugin::configUrl('_cnot=token'));
return;
}
$action = isset($_GET['do']) ? trim((string) $_GET['do']) : '';
if ($action === 'backup') {
$result = MailNotifier_Settings::backup();
if (!empty($result['ok'])) {
$this->redirectTo(MailNotifier_Plugin::configUrl(
'_cnot=backup_ok&n=' . intval($result['count'])
. '&t=' . intval($result['created'])
. '&f=' . intval(isset($result['total']) ? $result['total'] : 0)
. '&k=' . intval(isset($result['keep']) ? $result['keep'] : 0)
));
return;
}
$this->redirectTo(MailNotifier_Plugin::configUrl(
'_cnot=backup_fail&detail=' . rawurlencode((string) $result['message'])
));
return;
}
if ($action === 'restore') {
/* 不传 file 就是恢复最新一份;传了走文件名白名单校验(restoreLatest 内部
还会再 basename + isBackupName 校验一次,这里只挡住数组之类的怪值) */
$file = isset($_GET['file']) && is_scalar($_GET['file']) ? trim((string) $_GET['file']) : '';
$result = MailNotifier_Settings::restoreLatest($file);
if (!empty($result['ok'])) {
$this->redirectTo(MailNotifier_Plugin::configUrl(
'_cnot=restore_ok&n=' . intval($result['count']) . '&t=' . intval($result['created'])
));
return;
}
$this->redirectTo(MailNotifier_Plugin::configUrl(
'_cnot=restore_fail&detail=' . rawurlencode((string) $result['message'])
));
return;
}
$this->redirectTo(MailNotifier_Plugin::configUrl('_cnot=unknown'));
}
/**
* 跳转(优先走 Typecho 的 Response,它会正确记下 302)
*/
private function redirectTo($url)
{
try {
if (isset($this->response) && is_object($this->response) && method_exists($this->response, 'redirect')) {
$this->response->redirect($url);
exit;
}
} catch (Exception $e) {
} catch (Throwable $e) {
}
header('Location: ' . $url, true, 302);
exit;
}
private function renderResult($ok, $message)
{
$title = $ok ? '测试邮件发送成功' : '测试邮件发送失败';
$badge = $ok ? '成功' : '失败';
$color = $ok ? '#2f8a57' : '#b4503f';
$bg = $ok ? 'rgba(47,138,87,.10)' : 'rgba(180,80,63,.10)';
echo '<!doctype html><html><head><meta charset="utf-8"><title>' . MailNotifier_Plugin::e($title) . '</title><meta name="viewport" content="width=device-width,initial-scale=1"><style>body{margin:0;background:#f7f1e8;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif;color:#332b24}.wrap{max-width:620px;margin:9vh auto;padding:0 18px}.card{border:1px solid rgba(126,96,60,.16);border-radius:22px;background:rgba(255,255,255,.92);box-shadow:0 18px 46px rgba(74,54,34,.10);overflow:hidden}.head{padding:26px 28px;background:linear-gradient(135deg,#fffdf8,#f5eadc);border-bottom:1px solid rgba(126,96,60,.12)}.badge{display:inline-flex;align-items:center;height:28px;padding:0 13px;border-radius:999px;font-size:12px;font-weight:800;letter-spacing:.08em}.body{padding:24px 28px 28px}.body p{margin:0;color:#7d6b5a;line-height:1.8}.btn{margin-top:20px;display:inline-flex;height:40px;padding:0 18px;align-items:center;border-radius:999px;text-decoration:none;color:#fff;background:linear-gradient(135deg,#8b633b,#a77b4c);font-size:13px;font-weight:800}@media (prefers-color-scheme:dark){body{background:#0f1725;color:#e8eef7}.card{background:#1b2635;border-color:rgba(255,255,255,.10);box-shadow:0 18px 46px rgba(0,0,0,.5)}.head{background:linear-gradient(135deg,#202c3d,#1a2434);border-bottom-color:rgba(255,255,255,.10)}.body p{color:#9aa8bb}}</style></head><body><div class="wrap"><div class="card"><div class="head"><span class="badge" style="color:' . $color . ';background:' . $bg . '">' . $badge . '</span><h1 style="margin:16px 0 0;font-size:24px;line-height:1.35">' . MailNotifier_Plugin::e($title) . '</h1></div><div class="body"><p>' . MailNotifier_Plugin::e($message) . '</p><a class="btn" href="javascript:window.close();">关闭页面</a></div></div></div></body></html>';
}
}