15m44 工单图片引入EasyImages2.0(读img.json的baseurl+key): lsky.php加loadImgJson(读img.json模式.type/baseurl/key)+easyUploadFile(POST {baseurl}/api/index.php, multipart image+token, 响应取url/thumb, EasyImages2.0规范)+ticketImageUpload(按type路由, 本期只做图床GitHub issue先不做)+ticketImageAllowedPrefix(baseurl动态生成图床URL白名单). ticket.php: ticketUploadImage改调ticketImageUpload, normalizeTicketImages前缀改动态. img.json保持模板值未入库(下一步改真实值)
This commit is contained in:
+3
-3
@@ -156,14 +156,14 @@ define('TICKET_MAX_IMAGES', 3);
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取并校验请求里的 images 参数(数组或 JSON 字符串均可),返回 URL 列表。
|
* 读取并校验请求里的 images 参数(数组或 JSON 字符串均可),返回 URL 列表。
|
||||||
* 每个 URL 必须以自家图床前缀开头(LSKY_ALLOWED_HOST_PREFIX),最多 3 张。
|
* 每个 URL 必须以自家图床前缀开头(ticketImageAllowedPrefix,按 img.json baseurl 动态),最多 3 张。
|
||||||
*/
|
*/
|
||||||
function normalizeTicketImages() {
|
function normalizeTicketImages() {
|
||||||
$raw = getParam('images', []);
|
$raw = getParam('images', []);
|
||||||
if (is_string($raw)) $raw = json_decode($raw, true);
|
if (is_string($raw)) $raw = json_decode($raw, true);
|
||||||
if ($raw === null || $raw === '') $raw = [];
|
if ($raw === null || $raw === '') $raw = [];
|
||||||
if (!is_array($raw)) error('images 参数格式错误');
|
if (!is_array($raw)) error('images 参数格式错误');
|
||||||
$prefix = LSKY_ALLOWED_HOST_PREFIX;
|
$prefix = ticketImageAllowedPrefix();
|
||||||
$out = [];
|
$out = [];
|
||||||
foreach ($raw as $u) {
|
foreach ($raw as $u) {
|
||||||
if (!is_string($u)) error('图片地址格式错误');
|
if (!is_string($u)) error('图片地址格式错误');
|
||||||
@@ -282,7 +282,7 @@ function ticketUploadImage() {
|
|||||||
if ($base === '') $base = 'ticket_image';
|
if ($base === '') $base = 'ticket_image';
|
||||||
$orig = mb_substr($base, 0, 60) . '.' . $realExt;
|
$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'] : '未知错误'));
|
if (empty($r['ok'])) error('图片上传失败: ' . (isset($r['msg']) && $r['msg'] !== '' ? $r['msg'] : '未知错误'));
|
||||||
|
|
||||||
success(['url' => $r['url'], 'thumbnail' => isset($r['thumb']) ? $r['thumb'] : $r['url']], '上传成功');
|
success(['url' => $r['url'], 'thumbnail' => isset($r['thumb']) ? $r['thumb'] : $r['url']], '上传成功');
|
||||||
|
|||||||
@@ -17,10 +17,128 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// 图床前缀白名单:写库前校验图片 URL 只能来自自家图床(防存任意 URL)。
|
// 图床前缀白名单:写库前校验图片 URL 只能来自自家图床(防存任意 URL)。
|
||||||
|
// 15m44 改为动态:优先读 img.json 的 baseurl 生成前缀,未配置时兜底旧值。
|
||||||
|
// normalizeTicketImages / ticketImageUpload 都调 ticketImageAllowedPrefix()。
|
||||||
if (!defined('LSKY_ALLOWED_HOST_PREFIX')) {
|
if (!defined('LSKY_ALLOWED_HOST_PREFIX')) {
|
||||||
define('LSKY_ALLOWED_HOST_PREFIX', 'https://img.ypshidifu.cn/');
|
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(只加载一次),并检查必填项 */
|
/** 加载 lsky_keys.php(只加载一次),并检查必填项 */
|
||||||
function lskyLoadKeys() {
|
function lskyLoadKeys() {
|
||||||
static $done = false;
|
static $done = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user