Files
terry aac91aa9b6 TinyImg 2.0.0:更名、下线 SM.MS、新增后台 Token 设置页
基于 ImgURL 1.x 的二次开发版本,主要变更:

1. 更名
   - 项目定名为 TinyImg,源码中不再出现旧品牌字样(0 处)
   - static/imgurl.js -> static/tinyimg.js
   - db/imgurl-simple.db3 -> db/tinyimg-simple.db3

2. 移除 SM.MS 上传
   - 删除 embed.js 中的 upload.render / smshow / deletesm
   - miniup.html 去掉 SM.MS 选项卡;前台与后台导航移除入口
   - sm.php、functions/sm.php、admin/smadmin.php 改为下线说明页
   - admin/delete.php 移除 type=sm 分支;class.admin.php 删除 querysm()/deletesm()

3. 新增后台 Token 设置(admin/setting.php + functions/token.php)
   - 生成 / 自定义 / 启用 / 停用 / 清空,全部校验 Session CSRF
   - 存储于 db/tinyimg-token.php(首行 <?php exit; 守卫)
   - config.php 的 api_token 仅作兼容回退,保存一次后由文件接管
   - 完整 Token 不写入页面 HTML:默认只渲染掩码,点「显示/复制」时
     经 POST action=reveal(同源 + CSRF + 仅登录会话)按需取回

4. 上传端点鉴权
   - 统一使用 X-TinyImg-Token 请求头,hash_equals 比较,空配置 fail closed
   - 新增 ?ping=1 自检,返回版本、鉴权方式、管理员目录,供写作插件测试连接

5. 版本号
   - 统一为 v2.0.0(20261104),前后台页脚引用 functions/version.php

6. 兼容性修复
   - class.upload.php 中 $size{...} 花括号下标改为 $size[...](PHP 8 已移除该语法)

7. 仓库整理
   - .gitignore 排除 config.php / db/tinyimg-token.php / 运行时目录,
     保留 db/.htaccess 与空模板 db/tinyimg-simple.db3
   - .gitattributes 统一 LF
2026-11-05 17:30:05 +08:00

310 lines
15 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* TinyImg 后台 —— API Token 设置页
*
* 权限:复用 class.admin.php 的登录校验(未登录直接跳登录页)。
* 防护:所有写操作校验 Session CSRF,Token 只在本页显示,不写进任何前端静态文件。
*/
session_start();
require_once __DIR__ . '/../functions/version.php';
require_once __DIR__ . '/../functions/token.php';
// 载入后台鉴权(同时带来 $config / $database / $pic)
include_once("../functions/class/class.admin.php");
if (empty($_SESSION['tinyimg_csrf'])) {
$_SESSION['tinyimg_csrf'] = tinyimg_token_generate(16);
}
$csrf = $_SESSION['tinyimg_csrf'];
$flash = isset($_SESSION['tinyimg_flash']) ? $_SESSION['tinyimg_flash'] : [];
unset($_SESSION['tinyimg_flash']);
if (strtoupper(isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '') === 'POST') {
$ok = isset($_POST['csrf']) && is_string($_POST['csrf'])
&& hash_equals($csrf, $_POST['csrf']);
$action = isset($_POST['action']) ? (string) $_POST['action'] : '';
$state = tinyimg_token_load($config);
if (!$ok) {
$_SESSION['tinyimg_flash'] = ['error', '安全校验失败,请刷新页面后重试'];
} elseif ($action === 'regenerate') {
list($ok2, $msg) = tinyimg_token_save(tinyimg_token_generate(32), true, $config);
$_SESSION['tinyimg_flash'] = $ok2
? ['ok', '已生成新的 API Token,请同步更新 Typecho 插件设置']
: ['error', '保存失败:' . $msg];
} elseif ($action === 'save') {
$custom = isset($_POST['custom_token']) ? trim((string) $_POST['custom_token']) : '';
if (!tinyimg_token_valid_format($custom)) {
$_SESSION['tinyimg_flash'] = ['error',
'Token 格式不合法:只允许 16~128 位字母、数字、下划线、短横线'];
} else {
list($ok2, $msg) = tinyimg_token_save($custom, true, $config);
$_SESSION['tinyimg_flash'] = $ok2
? ['ok', '自定义 API Token 已保存并启用']
: ['error', '保存失败:' . $msg];
}
} elseif ($action === 'toggle') {
if ($state['token'] === '' && empty($state['enabled'])) {
$_SESSION['tinyimg_flash'] = ['error', '尚未设置 Token,请先生成或填入'];
} else {
$next = empty($state['enabled']) ? true : false;
list($ok2, $msg) = tinyimg_token_save($state['token'], $next, $config);
$_SESSION['tinyimg_flash'] = $ok2
? ['ok', $next ? 'Token 上传已启用' : 'Token 上传已停用(原有网页上传不受影响)']
: ['error', '保存失败:' . $msg];
}
} elseif ($action === 'reveal') {
// 完整 Token 不写进页面 HTML(避免留在源码 / 浏览器缓存 / 截图中)。
// 只有已登录且通过 CSRF 的会话,才能在点「显示 / 复制」时按需取一次。
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['code' => 1, 'token' => $state['token']], JSON_UNESCAPED_UNICODE);
exit;
} elseif ($action === 'clear') {
list($ok2, $msg) = tinyimg_token_save('', false, $config);
$_SESSION['tinyimg_flash'] = $ok2
? ['ok', 'Token 已清空并停用']
: ['error', '保存失败:' . $msg];
} else {
$_SESSION['tinyimg_flash'] = ['error', '未知操作'];
}
header('Location: setting.php');
exit;
}
$state = tinyimg_token_load($config);
$title = "Token 设置 - TinyImg";
include_once("../tpl/admin/header.php");
$enabled = !empty($state['enabled']) && $state['token'] !== '';
$sourceText = $state['source'] === 'file'
? 'Token 存储文件(db/tinyimg-token.php)'
: ($state['source'] === 'config'
? 'config.php 的 api_token(兼容旧版,保存一次后自动接管)'
: '尚未配置');
$esc = function ($value) {
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
};
?>
<div class="layui-container" style="margin-top:2em;">
<div class="layui-row layui-col-space20">
<div class="layui-col-lg3">
<?php include_once("../tpl/admin/left.php"); ?>
</div>
<div class="layui-col-lg9">
<h1 style="margin-bottom:.6em;color:#515151;font-size:22px;">API Token 设置</h1>
<?php if (!empty($flash)) { ?>
<div class="msg" style="border-left:4px solid <?php echo $flash[0] === 'ok' ? '#5FB878' : '#FF5722'; ?>;">
<?php echo $esc($flash[1]); ?>
</div>
<?php } ?>
<table class="layui-table" lay-skin="nob">
<tbody>
<tr>
<td style="width:130px;">当前状态</td>
<td>
<?php if ($enabled) { ?>
<span style="color:#5FB878;font-weight:600;">● 已启用</span>
<?php } elseif ($state['token'] !== '') { ?>
<span style="color:#FFB800;font-weight:600;">● 已停用</span>
<?php } else { ?>
<span style="color:#FF5722;font-weight:600;">● 未设置</span>
<?php } ?>
<span style="color:#999;margin-left:1em;"><?php echo $esc(TINYIMG_NAME . ' ' . TINYIMG_VERSION); ?></span>
</td>
</tr>
<tr>
<td>当前 Token</td>
<td>
<div style="display:flex;gap:8px;align-items:center;flex-wrap:wrap;">
<input type="password" id="ti-token" class="layui-input"
style="max-width:460px;font-family:monospace;"
value="<?php echo $esc($state['token'] === '' ? '' : tinyimg_token_mask($state['token'])); ?>"
data-masked="<?php echo $esc($state['token'] === '' ? '' : tinyimg_token_mask($state['token'])); ?>"
readonly>
<button type="button" class="layui-btn layui-btn-primary layui-btn-sm" id="ti-reveal"
<?php echo $state['token'] === '' ? 'disabled' : ''; ?>>显示</button>
<button type="button" class="layui-btn layui-btn-primary layui-btn-sm" id="ti-copy"
<?php echo $state['token'] === '' ? 'disabled' : ''; ?>>复制</button>
</div>
<p style="color:#999;margin-top:6px;">
脱敏视图:<?php echo $esc(tinyimg_token_mask($state['token'])); ?>
· 长度 <?php echo strlen($state['token']); ?> 位
· 完整值不写入页面,点「显示」时才向服务器取一次
</p>
</td>
</tr>
<tr>
<td>来源</td>
<td><?php echo $esc($sourceText); ?></td>
</tr>
<tr>
<td>创建 / 更新</td>
<td><?php echo $esc(($state['created_at'] ?: '—') . ' / ' . ($state['updated_at'] ?: '—')); ?></td>
</tr>
</tbody>
</table>
<div style="margin:1.4em 0 .6em;font-weight:600;">管理操作</div>
<div style="display:flex;gap:8px;flex-wrap:wrap;">
<form method="post" style="display:inline;">
<input type="hidden" name="csrf" value="<?php echo $esc($csrf); ?>">
<input type="hidden" name="action" value="regenerate">
<button type="submit" class="layui-btn layui-btn-normal"
onclick="return confirm('重新生成后旧 Token 立即失效,Typecho 插件需要同步更新。继续?');">
重新生成 Token
</button>
</form>
<form method="post" style="display:inline;">
<input type="hidden" name="csrf" value="<?php echo $esc($csrf); ?>">
<input type="hidden" name="action" value="toggle">
<button type="submit" class="layui-btn layui-btn-primary">
<?php echo $enabled ? '停用 Token 上传' : '启用 Token 上传'; ?>
</button>
</form>
<form method="post" style="display:inline;">
<input type="hidden" name="csrf" value="<?php echo $esc($csrf); ?>">
<input type="hidden" name="action" value="clear">
<button type="submit" class="layui-btn layui-btn-primary"
onclick="return confirm('确认清空 Token?清空后插件的 Token 上传会立即被拒绝。');">
清空
</button>
</form>
</div>
<div style="margin:1.6em 0 .6em;font-weight:600;">填入自定义 Token</div>
<form method="post" style="display:flex;gap:8px;flex-wrap:wrap;align-items:center;">
<input type="hidden" name="csrf" value="<?php echo $esc($csrf); ?>">
<input type="hidden" name="action" value="save">
<input type="text" name="custom_token" class="layui-input" style="max-width:460px;font-family:monospace;"
placeholder="16~128 位字母 / 数字 / _ / -" required>
<button type="submit" class="layui-btn">保存并启用</button>
</form>
<div style="margin:1.8em 0 .6em;font-weight:600;">给 Typecho 插件的连接信息</div>
<table class="layui-table" lay-skin="nob">
<tbody>
<tr><td style="width:130px;">上传接口</td><td><code>functions/upload.php</code>(与网页上传同一个文件)</td></tr>
<tr><td>鉴权请求头</td><td><code>X-TinyImg-Token: &lt;本页的 Token&gt;</code></td></tr>
<tr><td>身份</td><td>Token 校验通过即按管理员上传:进 <code>upload/</code>,跳过游客每日配额</td></tr>
<tr><td>连接自检</td><td><code>functions/upload.php?ping=1</code>,插件设置里的「测试连接」按钮会调用它</td></tr>
</tbody>
</table>
<pre style="background:#f7f7f7;padding:12px;border-radius:4px;overflow:auto;font-size:12px;">curl -sS -X POST "https://你的域名/functions/upload.php?ping=1" \
-H "X-TinyImg-Token: &lt;你的 Token&gt;"</pre>
<div class="msg" style="margin-top:1.2em;">
<i class="layui-icon">&#xe645;</i>
安全提示:Token 只被上传接口识别,泄露也只能上传图片,不影响后台登录;请务必使用 HTTPS,
不要提交到公开仓库。轮换后记得同步修改 Typecho 插件设置,并用「测试连接」验证。
存储文件已用 <code>&lt;?php exit;</code> 守卫,直接访问 URL 不会输出内容。
</div>
</div>
</div>
</div>
<script>
(function () {
var input = document.getElementById('ti-token');
var reveal = document.getElementById('ti-reveal');
var copy = document.getElementById('ti-copy');
if (!input) { return; }
// 完整 Token 不在页面里,按需向服务器取一次(带 CSRF,同源 POST)
var real = null;
var csrfEl = document.querySelector('input[name="csrf"]');
function fetchToken(cb) {
if (real !== null) { cb(real); return; }
if (!csrfEl) { cb(null); return; }
var body = new URLSearchParams();
body.set('csrf', csrfEl.value);
body.set('action', 'reveal');
fetch('setting.php', {
method: 'POST',
body: body,
credentials: 'same-origin',
headers: { 'X-Requested-With': 'XMLHttpRequest' }
}).then(function (res) { return res.json(); }).then(function (data) {
if (data && data.code === 1 && typeof data.token === 'string') {
real = data.token;
cb(real);
} else {
cb(null);
}
}).catch(function () { cb(null); });
}
function showMasked() {
input.type = 'password';
input.value = input.getAttribute('data-masked') || '';
if (reveal) { reveal.textContent = '显示'; }
}
if (reveal) {
reveal.onclick = function () {
if (input.type === 'text') { showMasked(); return; }
reveal.disabled = true;
fetchToken(function (token) {
reveal.disabled = false;
if (token === null) { reveal.textContent = '取回失败'; return; }
input.type = 'text';
input.value = token;
reveal.textContent = '隐藏';
});
};
}
if (copy) {
copy.onclick = function () {
copy.disabled = true;
fetchToken(function (token) {
copy.disabled = false;
if (token === null) { copy.textContent = '取回失败'; return; }
var done = function () {
copy.textContent = '已复制';
setTimeout(function () { copy.textContent = '复制'; }, 1500);
};
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(token).then(done, function () {
copyViaInput(token, done);
});
} else {
copyViaInput(token, done);
}
});
};
}
function copyViaInput(token, done) {
var prevValue = input.value;
var prevType = input.type;
input.type = 'text';
input.value = token;
input.select();
try {
document.execCommand('copy');
done();
} catch (e) {
copy.textContent = '请手动复制';
}
input.type = prevType;
input.value = prevValue;
}
})();
</script>
<?php
include_once("../tpl/admin/footer.php");
?>