TinyImgUploader 2.0.0:Typecho 1.3 图片上传插件(对接 TinyImg 图床)
在撰写文章 / 页面的右侧栏增加图片上传窗口,选图后上传到自建 TinyImg
图床,并把 Markdown 链接插入编辑器光标处。
主要功能
1. 侧栏卡片 + 弹窗上传
- 挂在 admin/write-post.php 与 write-page.php 的 option 钩子
- 支持点选、拖入、Ctrl+V 粘贴,上传前可预览
- 用 getSelection / replaceSelection 插入光标处并触发 input 事件,
草稿自动保存与实时预览都能同步
2. 三种图床鉴权方式
- token(默认):X-TinyImg-Token 请求头,未配置则明确拒绝
- admin:模拟图床后台登录 Cookie(无需改图床,但凭证等价于后台密码)
- guest:游客上传,图片进 temp/ 并受每日配额限制
- token / admin 两种模式强制服务端代理,凭证绝不下发浏览器
3. 「测试连接」按钮
- 读取输入框当前值,无需先保存
- 经博客同源 Action(带 CSRF)调用图床 functions/upload.php?ping=1
- 返回图床版本、鉴权方式与管理员目录,并比对是否落在 upload/
4. 为什么必须走服务端代理
- 实测图床 upload.php 不返回 Access-Control-Allow-Origin,
浏览器从博客后台直接 POST 会被同源策略拦截
5. 服务端校验
- 登录态 + 用户组(可选「仅管理员」) + CSRF 令牌
- 上传大小上限、getimagesize 真实图片头校验(拦 SVG / 伪装脚本)
- 未认出管理员身份时返回 warn,前端不自动关闭弹窗
6. 附带工具与测试
- tools/probe.php:命令行连通性自检(只读探测 / 真实上传 / 目录判定)
- tools/cookie.php:本地计算图床管理员 Cookie 值,不联网
- tests/:六组共 207 项,含故意错误实现的反证
版本:2.0.0
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
use TypechoPlugin\TinyImgUploader\Util;
|
||||
|
||||
require_once __DIR__ . '/../Util.php';
|
||||
|
||||
/**
|
||||
* Util 纯函数测试(无需 Typecho、无需联网)
|
||||
* 运行:php tests/util_test.php
|
||||
*
|
||||
* 除了正例 / 反例,还内置两个「故意写错的实现」,用来证明测试集
|
||||
* 确实能区分正确实现与错误实现,而不是无论怎么写都能通过。
|
||||
*/
|
||||
|
||||
$stat = ['pass' => 0, 'fail' => 0];
|
||||
|
||||
$check = function ($name, $ok, $detail = '') use (&$stat) {
|
||||
if ($ok) {
|
||||
$stat['pass']++;
|
||||
echo ' [PASS] ' . $name . PHP_EOL;
|
||||
} else {
|
||||
$stat['fail']++;
|
||||
echo ' [FAIL] ' . $name . ($detail === '' ? '' : ' -> ' . $detail) . PHP_EOL;
|
||||
}
|
||||
};
|
||||
|
||||
$base = 'https://img.example.com/tinyimg/functions/upload.php';
|
||||
|
||||
echo PHP_EOL . '1. 图床响应解析' . PHP_EOL;
|
||||
|
||||
$cases = [
|
||||
[
|
||||
'name' => 'TinyImg 2.0 成功响应(源码里的字段结构)',
|
||||
'body' => '{"code":1,"id":9527,"url":"https://img.example.com/tinyimg/temp/2611/abcd.jpg","width":800,"height":600}',
|
||||
'code' => 1,
|
||||
'url' => 'https://img.example.com/tinyimg/temp/2611/abcd.jpg',
|
||||
],
|
||||
[
|
||||
'name' => 'TinyImg 2.0 失败响应(Token 无效 / 非法请求)',
|
||||
'body' => '{"code":0,"msg":"\u975e\u6cd5\u8bf7\u6c42\uff01"}',
|
||||
'code' => 0,
|
||||
'url' => null,
|
||||
],
|
||||
[
|
||||
'name' => 'TinyImg 2.x 成功响应(data.url 嵌套)',
|
||||
'body' => '{"code":200,"msg":"success","data":{"url":"https://img.example.com/a.png"}}',
|
||||
'code' => 1,
|
||||
'url' => 'https://img.example.com/a.png',
|
||||
],
|
||||
[
|
||||
'name' => '通用 success / data.url 响应',
|
||||
'body' => '{"success":true,"data":{"url":"https://img.example.com/b.jpg"}}',
|
||||
'code' => 1,
|
||||
'url' => 'https://img.example.com/b.jpg',
|
||||
],
|
||||
[
|
||||
'name' => 'code=0 但带了 url,仍必须判失败',
|
||||
'body' => '{"code":0,"url":"https://img.example.com/c.jpg","msg":"非法请求"}',
|
||||
'code' => 0,
|
||||
'url' => null,
|
||||
],
|
||||
[
|
||||
'name' => '非 JSON 的 HTML 错误页',
|
||||
'body' => '<html><body>500 Internal Server Error</body></html>',
|
||||
'code' => 0,
|
||||
'url' => null,
|
||||
],
|
||||
[
|
||||
'name' => '空响应(PHP 报错 / 5xx 常见表现)',
|
||||
'body' => '',
|
||||
'code' => 0,
|
||||
'url' => null,
|
||||
],
|
||||
[
|
||||
'name' => 'JSON 前有 PHP notice 也能解析',
|
||||
'body' => 'Notice: undefined index in upload.php' . "\n" . '{"code":1,"url":"https://img.example.com/d.png"}',
|
||||
'code' => 1,
|
||||
'url' => 'https://img.example.com/d.png',
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($cases as $case) {
|
||||
$got = Util::parseResponse($case['body'], $base);
|
||||
$ok = ($got['code'] === $case['code']) && (($got['url'] ?: null) === $case['url']);
|
||||
$check($case['name'], $ok, json_encode($got, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
}
|
||||
|
||||
echo PHP_EOL . '2. 图片地址归一化' . PHP_EOL;
|
||||
|
||||
$normalized = [
|
||||
['{"code":1,"url":"/tinyimg/temp/2611/x.png"}', 'https://img.example.com/tinyimg/temp/2611/x.png'],
|
||||
['{"code":1,"url":"//cdn.example.com/x.png"}', 'https://cdn.example.com/x.png'],
|
||||
['{"code":1,"url":"temp/2611/x.jpg"}', 'https://img.example.com/tinyimg/functions/temp/2611/x.jpg'],
|
||||
];
|
||||
|
||||
foreach ($normalized as $item) {
|
||||
$got = Util::parseResponse($item[0], $base);
|
||||
$check('归一化 ' . $item[1], $got['code'] === 1 && $got['url'] === $item[1], (string) $got['url']);
|
||||
}
|
||||
|
||||
echo PHP_EOL . '3. 配置文本解析' . PHP_EOL;
|
||||
|
||||
$pairs = Util::parsePairs("# 注释行\ntoken=abc123\nuid=1\n\nfolder=3", '=');
|
||||
$check('key=value 解析', count($pairs) === 3 && $pairs['token'] === 'abc123' && $pairs['uid'] === '1', json_encode($pairs));
|
||||
|
||||
$headerPairs = Util::parsePairs("Referer: https://img.example.com/tinyimg/\nCookie: sid=abc", ':');
|
||||
$check(
|
||||
'Key: Value 中值含冒号',
|
||||
$headerPairs['Referer'] === 'https://img.example.com/tinyimg/' && $headerPairs['Cookie'] === 'sid=abc',
|
||||
json_encode($headerPairs)
|
||||
);
|
||||
|
||||
$check(
|
||||
'deriveReferer 去掉 functions 层',
|
||||
Util::deriveReferer($base) === 'https://img.example.com/tinyimg/',
|
||||
Util::deriveReferer($base)
|
||||
);
|
||||
$check(
|
||||
'deriveReferer 通用 /api/upload',
|
||||
Util::deriveReferer('https://x.example.com/api/upload') === 'https://x.example.com/api/'
|
||||
);
|
||||
|
||||
echo PHP_EOL . '4. 连接自检地址(?ping=1)' . PHP_EOL;
|
||||
|
||||
$pingCases = [
|
||||
['https://img.example.com/functions/upload.php', 'https://img.example.com/functions/upload.php?ping=1'],
|
||||
['https://img.example.com/functions/upload.php?x=1', 'https://img.example.com/functions/upload.php?x=1&ping=1'],
|
||||
['https://img.example.com/functions/upload.php#frag', 'https://img.example.com/functions/upload.php?ping=1'],
|
||||
['', ''],
|
||||
];
|
||||
foreach ($pingCases as $i => $case) {
|
||||
$got = Util::pingUrl($case[0]);
|
||||
$check('pingUrl 用例 ' . ($i + 1), $got === $case[1], $got);
|
||||
}
|
||||
$check('pingUrl 输出是 http(s) 绝对地址',
|
||||
(bool) preg_match('#^https://.+\?ping=1$#', Util::pingUrl('https://img.example.com/functions/upload.php')));
|
||||
|
||||
echo PHP_EOL . '5. 测试集有效性反证(故意错误的实现)' . PHP_EOL;
|
||||
|
||||
// 错误实现 A:只认 TinyImg 2.x 的 data.url,漏掉 1.x 的顶层 url
|
||||
$badV2Only = function ($body) {
|
||||
$data = json_decode($body, true);
|
||||
return isset($data['data']['url']) ? $data['data']['url'] : null;
|
||||
};
|
||||
$check(
|
||||
'错误实现 A 在 1.x 成功样例上必须取不到 URL',
|
||||
$badV2Only($cases[0]['body']) === null
|
||||
);
|
||||
$check(
|
||||
'正确实现在同一份样例上必须成功',
|
||||
Util::parseResponse($cases[0]['body'], $base)['code'] === 1
|
||||
);
|
||||
|
||||
// 错误实现 B:忽略 code=0,只要看到 url 就算成功
|
||||
$badIgnoreCode = function ($body) {
|
||||
$data = json_decode($body, true);
|
||||
return isset($data['url']) ? $data['url'] : null;
|
||||
};
|
||||
$failureWithUrl = '{"code":0,"url":"https://img.example.com/c.jpg","msg":"非法请求"}';
|
||||
$check(
|
||||
'错误实现 B 会把失败响应误判为成功',
|
||||
$badIgnoreCode($failureWithUrl) === 'https://img.example.com/c.jpg'
|
||||
);
|
||||
$check(
|
||||
'正确实现必须把该响应判为失败',
|
||||
Util::parseResponse($failureWithUrl, $base)['code'] === 0
|
||||
);
|
||||
|
||||
echo PHP_EOL . str_repeat('-', 56) . PHP_EOL;
|
||||
echo '结果:' . $stat['pass'] . ' 通过 / ' . $stat['fail'] . ' 失败' . PHP_EOL;
|
||||
exit($stat['fail'] === 0 ? 0 : 1);
|
||||
Reference in New Issue
Block a user