$code]; if (is_array($extra)) { $payload = array_merge($payload, $extra); } echo json_encode($payload, JSON_UNESCAPED_UNICODE); exit; } /** * 获取 AES-256 加密密钥(32 字节原始密钥,不可用时返回 null)。 * 来源优先级: * 1. 环境变量 / 常量 MNBT_SECRET_KEY(任意字符串,sha256 派生 32 字节) * 2. 站点根 runtime/zjmf/zjmf_secret.key 密钥文件(插件目录之外, * runtime/ 为仓库既有数据目录惯例并带 .htaccess 禁止 Web 访问; * 不存在时自动生成 64 hex 字符随机密钥并尝试 chmod 600) * 两者均不可用时返回 null(调用方退回旧 authcode 并记日志)。 */ function zjmf_secret_key_raw() { static $cached = null; if ($cached !== null) { return $cached['key'] ?? null; } $cached = ['key' => null]; // 1. 环境变量 / 常量 $secret = getenv('MNBT_SECRET_KEY'); if ($secret === '' || $secret === false) { $secret = defined('MNBT_SECRET_KEY') ? (string)constant('MNBT_SECRET_KEY') : ''; } if ($secret !== '') { $cached['key'] = hash('sha256', (string)$secret, true); return $cached['key']; } // 2. 密钥文件(站点根 runtime/zjmf/,插件 data 目录之外) $base = defined('ROOT') ? ROOT : dirname(dirname(dirname(__DIR__))) . '/'; $dir = $base . 'runtime/zjmf'; $file = $dir . '/zjmf_secret.key'; $secret = ''; if (is_file($file)) { $secret = trim((string)@file_get_contents($file)); } if ($secret === '') { // 自动生成 64 hex 字符随机密钥并落盘 if (!is_dir($dir) && !@mkdir($dir, 0755, true) && !is_dir($dir)) { @error_log('[zjmfmanager_reserve] 加密密钥目录创建失败:' . $dir); return null; } // 目录防 Web 直接访问(.htaccess 拒绝 + 空 index.html 防目录列举) $ht = $dir . '/.htaccess'; if (!is_file($ht)) { @file_put_contents($ht, "Deny from all\n"); } $ix = $dir . '/index.html'; if (!is_file($ix)) { @file_put_contents($ix, ''); } $secret = bin2hex(random_bytes(32)); if (@file_put_contents($file, $secret) === false) { @error_log('[zjmfmanager_reserve] 加密密钥文件写入失败:' . $file . ',退回旧 authcode 加密'); return null; } @chmod($file, 0600); } $cached['key'] = hash('sha256', $secret, true); return $cached['key']; } /** * 加密上游主机密码等敏感信息。 * 密钥可用时 AES-256-GCM,密文格式 'v2:' + base64(nonce.tag.ciphertext); * 密钥不可用时退回旧 authcode(记日志)。 */ function zjmf_encrypt($plain) { $plain = (string)$plain; if ($plain === '') { return ''; } $key = zjmf_secret_key_raw(); if ($key !== null) { $nonce = random_bytes(12); $tag = ''; $cipher = openssl_encrypt($plain, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $nonce, $tag); if ($cipher !== false) { return 'v2:' . base64_encode($nonce . $tag . $cipher); } @error_log('[zjmfmanager_reserve] AES-256-GCM 加密失败,退回旧 authcode'); } return authcode($plain, 'ENCODE', SYS_KEY); } /** * 密文解密(防御式)。 * 'v2:' 前缀走 AES-256-GCM(与 zjmf_encrypt 对称);无前缀走旧 authcode * 解密以兼容存量数据。authcode 解密分支在 PHP 8 下对乱码密文会抛 * TypeError,这里对空/过短密文直接返回,异常兜底为空串。 */ function zjmf_decrypt($cipher) { $cipher = (string)$cipher; if ($cipher === '') { return ''; } if (strpos($cipher, 'v2:') === 0) { $key = zjmf_secret_key_raw(); if ($key === null) { return ''; // 密钥不可用(如密钥文件被删),无法解密 } $raw = base64_decode(substr($cipher, 3), true); if ($raw === false || strlen($raw) <= 12 + 16) { return ''; } $nonce = substr($raw, 0, 12); $tag = substr($raw, 12, 16); $ct = substr($raw, 28); $plain = openssl_decrypt($ct, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $nonce, $tag); return $plain === false ? '' : $plain; } if (strlen($cipher) <= 4) { return ''; // 未设置密码或非 authcode 密文 } try { $out = authcode($cipher, 'DECODE', SYS_KEY); return is_string($out) ? $out : ''; } catch (Throwable $e) { return ''; } } /** 写操作日志。 */ function zjmf_log($user_id, $order_no, $action, $result, $content, $supplier_id = 0) { global $DB, $date; $now = $date ?: date('Y-m-d H:i:s'); $DB->query_prepare( "INSERT INTO MN_plugin_zjmf_log (user_id, supplier_id, order_no, action, result, content, created_at) VALUES (?,?,?,?,?,?,?)", [(int)$user_id, (int)$supplier_id, (string)$order_no, (string)$action, (string)$result, (string)$content, $now] ); } /** 日志列表(管理员,分页)。 */ function zjmf_log_list_all($page = 1, $per_page = 30) { global $DB; $page = max(1, (int)$page); $per_page = max(1, min(200, (int)$per_page)); $offset = ($page - 1) * $per_page; $count_row = $DB->get_row_prepare("SELECT COUNT(*) AS cnt FROM MN_plugin_zjmf_log"); $total = $count_row ? (int)$count_row['cnt'] : 0; $list = $DB->get_all_prepare( "SELECT l.*, u.username AS user_name, s.name AS supplier_name FROM MN_plugin_zjmf_log l LEFT JOIN MN_plugin_user u ON u.id = l.user_id LEFT JOIN MN_plugin_zjmf_supplier s ON s.id = l.supplier_id ORDER BY l.id DESC LIMIT {$offset},{$per_page}" ) ?: []; return ['list' => $list, 'total' => $total, 'page' => $page, 'per_page' => $per_page]; } /* ============================================================ * 计费周期 * ============================================================ */ /** 可用计费周期(key 与上游 billingcycle 一致,WHMCS 风格)。 */ function zjmf_cycles() { return [ 'Monthly' => ['name' => '月付'], 'Quarterly' => ['name' => '季付'], 'SemiAnnually' => ['name' => '半年付'], 'Annually' => ['name' => '年付'], 'Biennially' => ['name' => '两年付'], 'Triennially' => ['name' => '三年付'], ]; } /** * 渲染商品简介为规范的展示 HTML。 * 上游常见 `<li>CPU:4核</li> <li>内存:4G</li>...` 格式: * 解码实体 → 白名单过滤标签 → 压缩标签间空白 → 外层包裹