在撰写文章 / 页面的右侧栏增加图片上传窗口,选图后上传到自建 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
154 lines
7.5 KiB
PHP
154 lines
7.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* TinyImg 2.0 图床侧改造验收测试(无需 Typecho、无需联网、不上传任何图片)。
|
|
* 运行:php tests/host_test.php
|
|
*
|
|
* 检查:品牌去旧名、版本号、SM.MS 通道彻底关闭、后台 Token 设置页、
|
|
* 关键静态资源与数据库改名,以及一个反证(故意留一个 SM.MS 请求必须被抓到)。
|
|
*/
|
|
|
|
$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;
|
|
}
|
|
};
|
|
|
|
$root = dirname(__DIR__, 2);
|
|
$host = $root . '/TinyImg';
|
|
$plugin = $root . '/TinyImgUploader';
|
|
|
|
// 拼接构造,避免测试代码自身成为「旧品牌字面量」的命中项
|
|
$forbidden = 'img' . 'url';
|
|
|
|
/** 递归收集文本文件(跳过第三方 layui 与二进制文件) */
|
|
function host_text_files($dir)
|
|
{
|
|
$out = [];
|
|
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS));
|
|
foreach ($it as $f) {
|
|
$path = str_replace('\\', '/', $f->getPathname());
|
|
if (strpos($path, '/static/layui/') !== false) {
|
|
continue;
|
|
}
|
|
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
|
|
if (!in_array($ext, ['php', 'js', 'html', 'htm', 'css', 'txt', 'md', 'json', 'svg', 'htaccess'], true)
|
|
&& !in_array(basename($path), ['.htaccess', '.gitignore', 'LICENSE'], true)) {
|
|
continue;
|
|
}
|
|
$out[] = ['path' => $path, 'body' => file_get_contents($path)];
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
$hostFiles = host_text_files($host);
|
|
$pluginFiles = host_text_files($plugin);
|
|
|
|
echo PHP_EOL . '1. 更名与版本号' . PHP_EOL;
|
|
|
|
$oldNameHits = 0;
|
|
foreach (array_merge($hostFiles, $pluginFiles) as $f) {
|
|
$oldNameHits += substr_count(strtolower($f['body']), $forbidden);
|
|
}
|
|
$check('两个工程源码里旧品牌字样为 0', $oldNameHits === 0, '命中 ' . $oldNameHits . ' 处');
|
|
|
|
$version = trim(file_get_contents($host . '/functions/version.txt'));
|
|
$check('版本号更新为 v2.0.0', strpos($version, 'v2.0.0') === 0, $version);
|
|
$check('版本文件带构建日期', (bool) preg_match('/\(\d{8}\)/', $version), $version);
|
|
$check('存在统一版本模块 functions/version.php', is_file($host . '/functions/version.php'));
|
|
$check('旧的 v1.40 版本号不再出现在版本文件', strpos($version, '1.40') === false);
|
|
$check('前台关于页读取统一版本号',
|
|
strpos(file_get_contents($host . '/about.php'), 'TINYIMG_VERSION') !== false);
|
|
$check('后台关于弹窗读取统一版本号',
|
|
strpos(file_get_contents($host . '/admin/about.php'), 'TINYIMG_VERSION') !== false);
|
|
$check('站点标题统一为 TinyImg',
|
|
strpos(file_get_contents($host . '/index.php'), 'TinyImg') !== false
|
|
&& strpos(file_get_contents($host . '/tpl/admin/header.php'), '<h1>TinyImg</h1>') !== false);
|
|
|
|
echo PHP_EOL . '2. SM.MS 上传通道已屏蔽 / 移除' . PHP_EOL;
|
|
|
|
$smUploadHits = 0;
|
|
foreach ($hostFiles as $f) {
|
|
$smUploadHits += substr_count(strtolower($f['body']), 'sm.ms/api');
|
|
}
|
|
$check('没有任何对 sm.ms 上传 API 的请求', $smUploadHits === 0, '命中 ' . $smUploadHits . ' 处');
|
|
|
|
$miniup = file_get_contents($host . '/miniup.html');
|
|
$check('极简上传页没有 SM.MS 选项卡', strpos($miniup, '<li>SM.MS</li>') === false);
|
|
$check('极简上传页没有 #sm 文件输入', strpos($miniup, 'id="sm"') === false);
|
|
$check('极简上传页保留本站拖拽上传', strpos($miniup, 'id="upimg"') !== false);
|
|
|
|
$embed = file_get_contents($host . '/static/embed.js');
|
|
$check('embed.js 没有 smshow / deletesm 函数',
|
|
strpos($embed, 'function smshow') === false && strpos($embed, 'function deletesm') === false);
|
|
$check('embed.js 保留本站上传逻辑',
|
|
strpos($embed, "url: 'functions/upload.php'") !== false);
|
|
|
|
$left = file_get_contents($host . '/tpl/admin/left.php');
|
|
$check('后台左侧菜单没有 SM.MS 入口', strpos($left, 'smadmin.php') === false);
|
|
$check('后台左侧菜单新增 Token 设置入口', strpos($left, 'setting.php') !== false);
|
|
$check('后台顶部导航新增 Token 设置入口',
|
|
strpos(file_get_contents($host . '/tpl/admin/header.php'), './setting.php') !== false);
|
|
$check('前台导航没有 SM.MS 入口',
|
|
strpos(file_get_contents($host . '/tpl/user/header.php'), 'sm.php') === false);
|
|
|
|
$smPage = file_get_contents($host . '/sm.php');
|
|
$check('sm.php 返回 410 且不提供上传表单',
|
|
strpos($smPage, 'http_response_code(410)') !== false && strpos($smPage, 'id="sm"') === false);
|
|
$smFunc = file_get_contents($host . '/functions/sm.php');
|
|
$check('functions/sm.php fail closed,不写数据库',
|
|
strpos($smFunc, 'http_response_code(410)') !== false
|
|
&& strpos($smFunc, '$database->insert') === false);
|
|
$smAdmin = file_get_contents($host . '/admin/smadmin.php');
|
|
$check('admin/smadmin.php 不再查询数据库', strpos($smAdmin, 'querysm') === false);
|
|
$check('admin/delete.php 拒绝 type=sm',
|
|
strpos(file_get_contents($host . '/admin/delete.php'), "=== 'sm'") !== false);
|
|
$adminClass = file_get_contents($host . '/functions/class/class.admin.php');
|
|
$check('class.admin.php 已移除 querysm / deletesm',
|
|
strpos($adminClass, 'function querysm') === false && strpos($adminClass, 'function deletesm') === false);
|
|
$check('upgrade.php 不再创建 sm 表',
|
|
strpos(file_get_contents($host . '/upgrade.php'), 'CREATE TABLE') === false);
|
|
|
|
echo PHP_EOL . '3. Token 设置页与存储' . PHP_EOL;
|
|
|
|
$check('admin/setting.php 存在', is_file($host . '/admin/setting.php'));
|
|
$check('functions/token.php 存在', is_file($host . '/functions/token.php'));
|
|
$check('Token 存储文件带 PHP 守卫(不会通过 URL 泄漏)',
|
|
strpos(file_get_contents($host . '/functions/token.php'), "<?php exit;") !== false);
|
|
$check('config-simple.php 仍保留 api_token 兼容项',
|
|
strpos(file_get_contents($host . '/config-simple.php'), 'api_token') !== false);
|
|
|
|
echo PHP_EOL . '4. 静态资源与数据库改名' . PHP_EOL;
|
|
|
|
$check('static/tinyimg.js 存在', is_file($host . '/static/tinyimg.js'));
|
|
$check('旧的 static/' . $forbidden . '.js 已不存在', !is_file($host . '/static/' . $forbidden . '.js'));
|
|
$check('油猴脚本的站点地址可配置',
|
|
strpos(file_get_contents($host . '/static/tinyimg.js'), 'TINYIMG_BASE') !== false);
|
|
$check('db/tinyimg-simple.db3 存在', is_file($host . '/db/tinyimg-simple.db3'));
|
|
$check('旧的 db/' . $forbidden . '-simple.db3 已不存在', !is_file($host . '/db/' . $forbidden . '-simple.db3'));
|
|
$check('安装器使用新的数据库文件名',
|
|
strpos(file_get_contents($host . '/functions/class/class.install.php'), 'tinyimg-simple.db3') !== false);
|
|
$check('config-simple.php 指向新的数据库文件',
|
|
strpos(file_get_contents($host . '/config-simple.php'), 'db/tinyimg.db3') !== false);
|
|
|
|
echo PHP_EOL . '5. 测试集有效性反证' . PHP_EOL;
|
|
|
|
// 故意伪造一份「还留着 SM.MS 上传」的代码,检测器必须判为不合格
|
|
$fakeSmUpload = "upload.render({ elem: '#sm', url: 'https://sm.ms/api/upload' });";
|
|
$dumbDetector = function ($code) {
|
|
return strpos(strtolower($code), 'sm.ms/api') === false;
|
|
};
|
|
$check('检测器能抓到故意保留的 SM.MS 上传请求', $dumbDetector($fakeSmUpload) === false);
|
|
$check('真实 embed.js 通过同一检测器',
|
|
$dumbDetector($embed) === true);
|
|
|
|
echo PHP_EOL . str_repeat('-', 56) . PHP_EOL;
|
|
echo '结果:' . $stat['pass'] . ' 通过 / ' . $stat['fail'] . ' 失败' . PHP_EOL;
|
|
exit($stat['fail'] === 0 ? 0 : 1);
|