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 '
' . MailNotifier_Plugin::e($message) . '
关闭页面