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

120 lines
3.9 KiB
PHP

<?php
/*
name:TinyImg 兼容上传 API
version:v2.0.0
update:2026-11-04
说明:保留旧版 API 通道;推荐使用 functions/upload.php + X-TinyImg-Token
*/
header('Access-Control-Allow-Origin:*');
//载入配置文件
include_once("./class/class.user.php");
@$type = $_GET['type'];
$type = strip_tags($type);
//如果没有post文件
if(!$_FILES['file']){
$basis->re_error('未选择文件!');
}
//检查用户是否登录
$status = $basis->check($config);
//检查用户是否登陆来判断上传目录
if($status == 'islogin') {
//设置上传路径
$updir = $config['admindir'];
}
else{
$updir = $config['userdir'];
//限制用户上传数量
$basis->limitnum();
}
//获取上传者信息
$ip = $basis->getip();
$ua = $_SERVER['HTTP_USER_AGENT'];
$date = date('Y-m-d',time());
//载入上传类
include('./class/class.upload.php');
//上传方法
$handle = new upload($_FILES['file']);
if ($handle->uploaded) {
$handle->file_new_name_body = 'image_resized';
//允许上传大小2m
$handle->file_max_size = '2097152';
//允许的MIME类型,仅运行上传图片
$handle->allowed = array('image/*');
// 当前月份
$current_time = date('ym',time());
//上传路径:目录 + 时间
$handle->process('../'.$updir.'/'.$current_time."/");
if ($handle->processed) {
//获取站点域名
$domain = $config['domain'];
//生成文件hash
$fhash = hash_file("md5",$handle->file_dst_pathname,FALSE);
$fhash = substr($fhash,8,16);
//新的文件名(../temp/1804/\d64c8036c0605175.jpg)
$new_img = $handle->file_dst_path.$fhash.'.'.$handle->file_dst_name_ext;
//图片URL地址
$tinyimg = $domain.$updir.'/'.$current_time.'/'.$fhash.'.'.$handle->file_dst_name_ext;
//图片路径(temp/1804/d64c8036c0605175.jpg)
$imgdir = $updir.'/'.$current_time.'/'.$fhash.'.'.$handle->file_dst_name_ext;
//判断文件是否已经存在
if(!is_file($new_img)) {
//对文件更名
rename($handle->file_dst_pathname,$new_img);
}
else{
//删除原始文件
unlink($handle->file_dst_pathname);
}
//检查某张图片是否已经上传过,如果已经上传了,直接返回数据并终止操作
$basis->isupload($imgdir);
//没有上传过的图片,继续写入数据库
$last_user_id = $database->insert("imginfo", [
"path" => $imgdir,
"ip" => $ip,
"ua" => $ua,
"date" => $date,
"dir" => $updir,
"compress" => 0,
"level" => 0
]);
//返回最后的ID
$account_id = $database->id();
//上传成功,返回json数据
$redata = array(
"code" => 1,
"id" => $account_id,
"url" => $tinyimg,
"width" => $handle->image_dst_x,
"height" => $handle->image_dst_y
);
//直接返回图片URL
if($type == 'url'){
echo $tinyimg;
}
else{
//返回json
echo $redata = json_encode($redata);
}
$handle->clean();
//继续请求鉴黄接口
$basis->curlZip($account_id,$config['domain']);
} else {
//上传出现错误,返回报错信息
$redata = array(
"code" => 0,
"msg" => $handle->error
);
echo json_encode($redata);
}
}
?>