- 统一验证入口 aksrt_require_captcha(),返回 true|WP_Error,启用场景配置缺失时 fail-closed 拒绝 - 前端公共 API 重构为 window.AKSRTCaptcha(verify/collect/reset/run),单飞锁防并发重复验证,一次性 proof 绑定浏览器 cookie+场景+服务商+主机名 - challenge/exchange REST 端点:challenge 原子占用防票据放大厂商调用,增加频率限制 - 新增 OAuth bind pass:服务端签发 5 分钟一次性令牌,绑定用户+服务商+浏览器,微信扫码延迟消费 - 覆盖场景:评论、登录、注册、绑定第三方、绑定手机、找回密码、修改密码、更改邮箱、资料下载、签到、前台投稿 - 移除 Service 层重复验证与 _auth_code_verified/_defer_login 等客户端可伪造标志,删除下载共享 pass 死代码 - 修复腾讯 TC3 签名 scope、Cloudflare action/cdata/hostname 严格校验、阿里云验签错误码映射 - 后台 Schema 保存对 secret 字段做掩码/保留合并;导入导出过滤 aksrt_captcha_config - 重建 public bundle 与 admin SPA;新增 captcha 运行时/安全/业务契约测试与 harness
236 lines
7.6 KiB
PHP
236 lines
7.6 KiB
PHP
<?php
|
|
/**
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Admin;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
trait ImportExportControllerPart1 {
|
|
|
|
/**
|
|
* Get the schema definition for a domain (for frontend introspection).
|
|
*/
|
|
public function rest_get_schema(\WP_REST_Request $request): \WP_REST_Response {
|
|
$domain = $request->get_param('domain');
|
|
$schema_provider = SchemaProvider::get_instance();
|
|
|
|
$schema = $schema_provider->get_schema($domain);
|
|
if (!$schema) {
|
|
return $this->error('未知的设置域: ' . esc_html($domain), 404);
|
|
}
|
|
|
|
return $this->success(['schema' => $schema]);
|
|
}
|
|
|
|
public function rest_get_schema_section(\WP_REST_Request $request): \WP_REST_Response {
|
|
return $this->get_section_settings($request, (string) $request->get_param('domain'));
|
|
}
|
|
|
|
public function rest_save_schema_section(\WP_REST_Request $request): \WP_REST_Response {
|
|
return $this->save_section_settings($request, (string) $request->get_param('domain'));
|
|
}
|
|
|
|
public function list_setting_backups(\WP_REST_Request $request): \WP_REST_Response {
|
|
$backups = get_option('aksrt_settings_backups', []);
|
|
if (!is_array($backups)) {
|
|
$backups = [];
|
|
}
|
|
|
|
usort($backups, static function ($a, $b) {
|
|
return strcmp((string) ($b['created_at'] ?? ''), (string) ($a['created_at'] ?? ''));
|
|
});
|
|
|
|
return $this->success([
|
|
'data' => [
|
|
'items' => array_values($backups),
|
|
'total' => count($backups),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function create_setting_backup(\WP_REST_Request $request): \WP_REST_Response {
|
|
$params = $this->get_json_params($request);
|
|
$type = sanitize_key((string) ($params['type'] ?? 'manual'));
|
|
$label = sanitize_text_field((string) ($params['label'] ?? ''));
|
|
|
|
$allowed_types = ['manual', 'scheduled', 'reset_all', 'reset_domain', 'update_theme'];
|
|
if (!in_array($type, $allowed_types, true)) {
|
|
$type = 'manual';
|
|
}
|
|
|
|
$backup = $this->build_settings_backup($type, $label);
|
|
$this->persist_setting_backup($backup);
|
|
|
|
return $this->success([
|
|
'message' => __('设置快照已创建', 'aksrt'),
|
|
'data' => $backup,
|
|
]);
|
|
}
|
|
|
|
public function restore_setting_backup(\WP_REST_Request $request): \WP_REST_Response {
|
|
$id = (int) $request->get_param('id');
|
|
$backups = get_option('aksrt_settings_backups', []);
|
|
if (!is_array($backups)) {
|
|
$backups = [];
|
|
}
|
|
|
|
$target = null;
|
|
foreach ($backups as $backup) {
|
|
if ((int) ($backup['id'] ?? 0) === $id) {
|
|
$target = $backup;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$target || !is_array($target['payload'] ?? null)) {
|
|
return $this->error(__('备份快照不存在', 'aksrt'), 404);
|
|
}
|
|
|
|
$payload = $target['payload'];
|
|
$domains = $payload['domains'] ?? [];
|
|
$standalone = \AKSRT\Settings\Settings::filter_sensitive_import(
|
|
is_array($payload['standalone'] ?? null) ? $payload['standalone'] : []
|
|
);
|
|
$provider = SchemaProvider::get_instance();
|
|
|
|
foreach ($domains as $domain => $domainPayload) {
|
|
$data = $domainPayload['data'] ?? $domainPayload;
|
|
if (is_array($data) && in_array((string) $domain, $provider->get_domain_keys(), true)) {
|
|
$clean = $provider->sanitize_payload((string) $domain, $data);
|
|
$validation = $provider->validate((string) $domain, $clean);
|
|
if ($validation['valid']) {
|
|
$provider->write_settings((string) $domain, $clean);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ($standalone as $key => $value) {
|
|
if (is_string($key) && in_array($key, \AKSRT\Settings\Settings::standalone_export_keys(), true)) {
|
|
update_option($key, $value);
|
|
}
|
|
}
|
|
|
|
\AKSRT\Settings\Settings::flush_settings_cache();
|
|
|
|
return $this->success([
|
|
'message' => __('设置已从快照恢复', 'aksrt'),
|
|
'data' => [
|
|
'id' => $id,
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function delete_setting_backup(\WP_REST_Request $request): \WP_REST_Response {
|
|
$id = (int) $request->get_param('id');
|
|
$backups = get_option('aksrt_settings_backups', []);
|
|
if (!is_array($backups)) {
|
|
$backups = [];
|
|
}
|
|
|
|
$filtered = [];
|
|
$found = false;
|
|
foreach ($backups as $backup) {
|
|
if ((int) ($backup['id'] ?? 0) === $id) {
|
|
$found = true;
|
|
continue;
|
|
}
|
|
$filtered[] = $backup;
|
|
}
|
|
|
|
if (!$found) {
|
|
return $this->error(__('备份快照不存在', 'aksrt'), 404);
|
|
}
|
|
|
|
update_option('aksrt_settings_backups', array_values($filtered));
|
|
|
|
return $this->success([
|
|
'message' => __('备份快照已删除', 'aksrt'),
|
|
]);
|
|
}
|
|
|
|
|
|
private function persist_setting_backup(array $backup): void {
|
|
$backups = get_option('aksrt_settings_backups', []);
|
|
if (!is_array($backups)) {
|
|
$backups = [];
|
|
}
|
|
|
|
array_unshift($backups, $backup);
|
|
update_option('aksrt_settings_backups', $this->trim_setting_backups($backups));
|
|
}
|
|
|
|
private function create_backup_record(string $type, string $label = ''): array {
|
|
return $this->build_settings_backup($type, $label);
|
|
}
|
|
|
|
public function register_backup_schedules(array $schedules): array {
|
|
if (!isset($schedules[self::MONTHLY_SCHEDULE])) {
|
|
$schedules[self::MONTHLY_SCHEDULE] = [
|
|
'interval' => MONTH_IN_SECONDS,
|
|
'display' => __('每月', 'aksrt'),
|
|
];
|
|
}
|
|
|
|
return $schedules;
|
|
}
|
|
|
|
public function sync_backup_schedule(): void {
|
|
$settings = $this->get_backup_schedule_settings();
|
|
$next = wp_next_scheduled(self::SCHEDULED_BACKUP_HOOK);
|
|
|
|
if (!$settings['enabled']) {
|
|
if ($next) {
|
|
wp_unschedule_event($next, self::SCHEDULED_BACKUP_HOOK);
|
|
}
|
|
return;
|
|
}
|
|
|
|
$schedule = $settings['schedule'];
|
|
if ($next && wp_get_schedule(self::SCHEDULED_BACKUP_HOOK) !== $schedule) {
|
|
wp_unschedule_event($next, self::SCHEDULED_BACKUP_HOOK);
|
|
$next = false;
|
|
}
|
|
|
|
if (!$next) {
|
|
wp_schedule_event(time() + HOUR_IN_SECONDS, $schedule, self::SCHEDULED_BACKUP_HOOK);
|
|
}
|
|
}
|
|
|
|
public function clear_backup_schedule(): void {
|
|
$next = wp_next_scheduled(self::SCHEDULED_BACKUP_HOOK);
|
|
while ($next) {
|
|
wp_unschedule_event($next, self::SCHEDULED_BACKUP_HOOK);
|
|
$next = wp_next_scheduled(self::SCHEDULED_BACKUP_HOOK);
|
|
}
|
|
}
|
|
|
|
public function run_scheduled_backup(): void {
|
|
$settings = $this->get_backup_schedule_settings();
|
|
if (!$settings['enabled']) {
|
|
return;
|
|
}
|
|
|
|
$backup = $this->create_backup_record('scheduled');
|
|
$this->persist_setting_backup($backup);
|
|
}
|
|
|
|
public function handle_theme_upgrade_snapshot($response, array $hook_extra) {
|
|
if (($hook_extra['type'] ?? '') !== 'theme') {
|
|
return $response;
|
|
}
|
|
|
|
$themes = $hook_extra['themes'] ?? [];
|
|
$stylesheet = wp_get_theme()->get_stylesheet();
|
|
if (!in_array($stylesheet, (array) $themes, true)) {
|
|
return $response;
|
|
}
|
|
|
|
$backup = $this->create_backup_record('update_theme');
|
|
$this->persist_setting_backup($backup);
|
|
|
|
return $response;
|
|
}
|
|
}
|