From 2d1a8ccba54f7df2e40ffbb23f098a0f1b18624a Mon Sep 17 00:00:00 2001 From: ypsdf1 Date: Tue, 24 Nov 2026 18:56:14 +0800 Subject: [PATCH] =?UTF-8?q?15m44=20=E5=B7=A5=E5=8D=95=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E5=BC=95=E5=85=A5EasyImages2.0(=E8=AF=BBimg.json=E7=9A=84baseu?= =?UTF-8?q?rl+key):=20lsky.php=E5=8A=A0loadImgJson(=E8=AF=BBimg.json?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F.type/baseurl/key)+easyUploadFile(POST=20{bas?= =?UTF-8?q?eurl}/api/index.php,=20multipart=20image+token,=20=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E5=8F=96url/thumb,=20EasyImages2.0=E8=A7=84=E8=8C=83)?= =?UTF-8?q?+ticketImageUpload(=E6=8C=89type=E8=B7=AF=E7=94=B1,=20=E6=9C=AC?= =?UTF-8?q?=E6=9C=9F=E5=8F=AA=E5=81=9A=E5=9B=BE=E5=BA=8AGitHub=20issue?= =?UTF-8?q?=E5=85=88=E4=B8=8D=E5=81=9A)+ticketImageAllowedPrefix(baseurl?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E7=94=9F=E6=88=90=E5=9B=BE=E5=BA=8AURL?= =?UTF-8?q?=E7=99=BD=E5=90=8D=E5=8D=95).=20ticket.php:=20ticketUploadImage?= =?UTF-8?q?=E6=94=B9=E8=B0=83ticketImageUpload,=20normalizeTicketImages?= =?UTF-8?q?=E5=89=8D=E7=BC=80=E6=94=B9=E5=8A=A8=E6=80=81.=20img.json?= =?UTF-8?q?=E4=BF=9D=E6=8C=81=E6=A8=A1=E6=9D=BF=E5=80=BC=E6=9C=AA=E5=85=A5?= =?UTF-8?q?=E5=BA=93(=E4=B8=8B=E4=B8=80=E6=AD=A5=E6=94=B9=E7=9C=9F?= =?UTF-8?q?=E5=AE=9E=E5=80=BC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/ticket.php | 6 +-- lsky.php | 118 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 3 deletions(-) diff --git a/api/ticket.php b/api/ticket.php index dbdbb92..229a556 100644 --- a/api/ticket.php +++ b/api/ticket.php @@ -156,14 +156,14 @@ define('TICKET_MAX_IMAGES', 3); /** * 读取并校验请求里的 images 参数(数组或 JSON 字符串均可),返回 URL 列表。 - * 每个 URL 必须以自家图床前缀开头(LSKY_ALLOWED_HOST_PREFIX),最多 3 张。 + * 每个 URL 必须以自家图床前缀开头(ticketImageAllowedPrefix,按 img.json baseurl 动态),最多 3 张。 */ function normalizeTicketImages() { $raw = getParam('images', []); if (is_string($raw)) $raw = json_decode($raw, true); if ($raw === null || $raw === '') $raw = []; if (!is_array($raw)) error('images 参数格式错误'); - $prefix = LSKY_ALLOWED_HOST_PREFIX; + $prefix = ticketImageAllowedPrefix(); $out = []; foreach ($raw as $u) { if (!is_string($u)) error('图片地址格式错误'); @@ -282,7 +282,7 @@ function ticketUploadImage() { if ($base === '') $base = 'ticket_image'; $orig = mb_substr($base, 0, 60) . '.' . $realExt; - $r = lskyUploadFile($f['tmp_name'], $orig); + $r = ticketImageUpload($f['tmp_name'], $orig); if (empty($r['ok'])) error('图片上传失败: ' . (isset($r['msg']) && $r['msg'] !== '' ? $r['msg'] : '未知错误')); success(['url' => $r['url'], 'thumbnail' => isset($r['thumb']) ? $r['thumb'] : $r['url']], '上传成功'); diff --git a/lsky.php b/lsky.php index e006719..30b103b 100644 --- a/lsky.php +++ b/lsky.php @@ -17,10 +17,128 @@ */ // 图床前缀白名单:写库前校验图片 URL 只能来自自家图床(防存任意 URL)。 +// 15m44 改为动态:优先读 img.json 的 baseurl 生成前缀,未配置时兜底旧值。 +// normalizeTicketImages / ticketImageUpload 都调 ticketImageAllowedPrefix()。 if (!defined('LSKY_ALLOWED_HOST_PREFIX')) { define('LSKY_ALLOWED_HOST_PREFIX', 'https://img.ypshidifu.cn/'); } +/** + * 读 img.json 的 EasyImages2.0 配置(baseurl + key + 模式.type)。 + * 返回 ['type'=>''|'GitHub', 'baseurl'=>..., 'key'=>..., 'easy'=>bool]。 + * - type 留空 或 未填 GitHub → 走 EasyImages2.0(图床) + * - type = GitHub → 走 GitHub issue(本期不实现,返回 easy=false) + * 文件缺失/解析失败 → 全空(调用方按"未配置"兜底 LSKY_ALLOWED_HOST_PREFIX)。 + */ +function loadImgJson() { + $cfg = ['type' => '', 'baseurl' => '', 'key' => '']; + $file = __DIR__ . '/img.json'; + if (is_readable($file)) { + $j = json_decode((string)@file_get_contents($file), true); + if (is_array($j)) { + // 模式.type: 留空=走图床, GitHub=走 issue(本期不做, 仍回退图床) + $t = isset($j['模式']['type']) ? trim((string)$j['模式']['type']) : ''; + $cfg['type'] = $t; + $cfg['baseurl'] = isset($j['baseurl']) ? trim((string)$j['baseurl']) : ''; + $cfg['key'] = isset($j['key']) ? trim((string)$j['key']) : ''; + } + } + // easy = 走 EasyImages2.0(type 非 GitHub 且有 baseurl+key) + $cfg['easy'] = ($cfg['type'] !== 'GitHub' && $cfg['baseurl'] !== '' && $cfg['key'] !== ''); + return $cfg; +} + +/** + * 图床 URL 白名单前缀:按 img.json 的 baseurl 动态生成(图床校验跟随配置)。 + * baseurl 形如 https://img.ypshidifu.cn/(带尾斜杠),直接作为前缀。 + * 未配置 baseurl 时兜底 LSKY_ALLOWED_HOST_PREFIX。 + */ +function ticketImageAllowedPrefix() { + $cfg = loadImgJson(); + if ($cfg['baseurl'] !== '') { + $p = $cfg['baseurl']; + if (substr($p, -1) !== '/') $p .= '/'; + return $p; + } + return LSKY_ALLOWED_HOST_PREFIX; +} + +/** + * EasyImages2.0 图床上传(15m44 引入)。 + * 接口规范(icret/EasyImages2.0 API): + * POST {baseurl}/api/index.php + * multipart/form-data: image(文件) + token(key) + * 成功响应: {result:"success", code:200, url, thumb, srcName, del, message} + * 凭据在 img.json(baseurl+key),本函数不内置敏感值。 + * 返回 ['ok'=>true,'url'=>..,'thumb'=>..] 或 ['ok'=>false,'msg'=>..] + */ +function easyUploadFile($tmpPath, $origName) { + $cfg = loadImgJson(); + if (!$cfg['easy']) { + return ['ok' => false, 'msg' => '图床未配置:img.json 缺少 baseurl 或 key(模式.type 留空走图床)']; + } + $api = rtrim($cfg['baseurl'], '/') . '/api/index.php'; + $mime = 'application/octet-stream'; + $info = @getimagesize($tmpPath); + if (!empty($info['mime'])) $mime = (string)$info['mime']; + if (!function_exists('curl_init')) { + return ['ok' => false, 'msg' => '服务器缺少 PHP curl 扩展']; + } + $ch = curl_init(); + $fields = [ + 'image' => new CURLFile($tmpPath, $mime, $origName), + 'token' => $cfg['key'], + ]; + curl_setopt($ch, CURLOPT_URL, $api); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); // 数组 -> multipart + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($ch, CURLOPT_TIMEOUT, 60); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']); + $body = curl_exec($ch); + $errno = curl_errno($ch); + $err = curl_error($ch); + $status = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE); + curl_close($ch); + if ($errno) return ['ok' => false, 'msg' => 'cURL错误: ' . $err]; + $j = json_decode((string)$body, true); + // EasyImages2.0 成功: {result:"success", url:..., thumb:...};失败: {result:"failed", message:...} 或 code!=200 + if ($status >= 200 && $status < 300 && is_array($j) + && isset($j['url']) && $j['url'] !== '' + && (!isset($j['result']) || $j['result'] === 'success')) { + $url = (string)$j['url']; + $thumb = isset($j['thumb']) && $j['thumb'] !== '' ? (string)$j['thumb'] : $url; + return ['ok' => true, 'url' => $url, 'thumb' => $thumb]; + } + $msg = (is_array($j) && isset($j['message']) && $j['message'] !== '') + ? (string)$j['message'] + : ('图床响应异常 HTTP ' . $status . ' ' . substr((string)$body, 0, 160)); + return ['ok' => false, 'msg' => $msg]; +} + +/** + * 工单图片上传路由(15m44):按 img.json 的 模式.type 选图床。 + * - type 留空(或填了其它非 GitHub 值)→ EasyImages2.0(easyUploadFile) + * - type = GitHub → 本期不实现,回退 EasyImages2.0(loadImgJson.easy 仍为 true 时); + * 若 easy 未配置则报"图床未配置"。GitHub issue 上传留后续批次。 + * 返回结构对齐 lskyUploadFile: ['ok','url','thumb'(可选)]。 + */ +function ticketImageUpload($tmpPath, $origName) { + $cfg = loadImgJson(); + // 本期只做 EasyImages2.0(用户明令 GitHub issue 先不做) + if ($cfg['easy']) { + return easyUploadFile($tmpPath, $origName); + } + // easy 未配置且 type 非 GitHub:兜底旧兰空图床(lsky_keys.php 还在时) + if ($cfg['type'] === '') { + return lskyUploadFile($tmpPath, $origName); + } + return ['ok' => false, 'msg' => '图床未配置:img.json 的 baseurl/key 缺失(GitHub issue 本期未启用)']; +} + /** 加载 lsky_keys.php(只加载一次),并检查必填项 */ function lskyLoadKeys() { static $done = false;