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
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
// 控制器
|
||||
// 载入类
|
||||
include_once("class/class.user.php");
|
||||
$type = $_GET['type'];
|
||||
switch ($type) {
|
||||
case 'login':
|
||||
// 用户登录时输入的信息
|
||||
$user = array(
|
||||
"user" => $_POST['user'],
|
||||
"password" => $_POST['password']
|
||||
);
|
||||
// 配置文件里面的用户信息
|
||||
$admin = array(
|
||||
"user" => $config['user'],
|
||||
"password" => $config['password']
|
||||
);
|
||||
$basis->login($user,$admin);
|
||||
break;
|
||||
case 'check':
|
||||
$basis->check($config);
|
||||
break;
|
||||
default:
|
||||
# code...
|
||||
break;
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,120 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
?>
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,335 @@
|
||||
<?php
|
||||
error_reporting(E_ALL^E_NOTICE^E_WARNING^E_DEPRECATED);
|
||||
// 载入配置文件
|
||||
include_once("../config.php");
|
||||
|
||||
class Admin{
|
||||
var $config;
|
||||
var $database;
|
||||
function __construct($config,$database) {
|
||||
$this->config = $config;
|
||||
$this->database = $database;
|
||||
$user1 = $config['user'].md5("tinyimg".$config['password']);
|
||||
// echo $user1;
|
||||
//COOKIES里面的信息
|
||||
$user2 = $_COOKIE['user'].$_COOKIE['password'];
|
||||
|
||||
//如果两者信息相符,说明已登录
|
||||
if($user1 != $user2) {
|
||||
echo '权限不足,请先登录!';
|
||||
//清理cookie
|
||||
setcookie("user", '', time()-3600,"/");
|
||||
setcookie("password", '', time()-3600,"/");
|
||||
header('Location:login.php');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
//查询图片
|
||||
function querypic($type,$page,$date = null){
|
||||
//对时间进行拆分
|
||||
if($date != null){
|
||||
$date = explode("|",$date);
|
||||
|
||||
$starttime = $date[0];
|
||||
$endttime = $date[1];
|
||||
}
|
||||
|
||||
$config = $this->config;
|
||||
$database = $this->database;
|
||||
|
||||
//分页计算
|
||||
$start = ($page - 1) * 12;
|
||||
//$end = $page * 12;
|
||||
|
||||
if(($page == '') || (!isset($page))) {
|
||||
$page = 1;
|
||||
}
|
||||
|
||||
//要查询的条数
|
||||
$num = 12;
|
||||
|
||||
//判断类型
|
||||
switch ($type) {
|
||||
case 'user':
|
||||
// echo 'dsd';
|
||||
$datas = $database->select("imginfo", "*", [
|
||||
"dir" => $config['userdir'],
|
||||
"ORDER" => ["id" => "DESC"],
|
||||
"LIMIT" => [$start,$num]
|
||||
]);
|
||||
// var_dump( $database->log() );
|
||||
// exit;
|
||||
return $datas;
|
||||
break;
|
||||
case 'admin':
|
||||
$datas = $database->select("imginfo", "*", [
|
||||
"dir" => $config['admindir'],
|
||||
"ORDER" => ["id" => "DESC"],
|
||||
"LIMIT" => [$start,$num]
|
||||
]);
|
||||
return $datas;
|
||||
break;
|
||||
case 'dubious':
|
||||
$datas = $database->select("imginfo", "*", [
|
||||
"level" => 3,
|
||||
"ORDER" => ["id" => "DESC"],
|
||||
"LIMIT" => [$start,$num]
|
||||
]);
|
||||
return $datas;
|
||||
break;
|
||||
case 'unidentification':
|
||||
$datas = $database->select("imginfo", "*", [
|
||||
"level" => null,
|
||||
"ORDER" => ["id" => "DESC"],
|
||||
"LIMIT" => [$start,$num]
|
||||
]);
|
||||
return $datas;
|
||||
break;
|
||||
default:
|
||||
$sql = "SELECT * FROM imginfo WHERE date(date) BETWEEN '$starttime' AND '$endttime' ORDER BY `id` DESC LIMIT $num OFFSET $start";
|
||||
|
||||
$datas = $database->query($sql);
|
||||
return $datas;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//新版查询图片
|
||||
function newquery($type,$date = null){
|
||||
//echo $type;
|
||||
//exit;
|
||||
//获取当前时间
|
||||
$thetime = date('Y-m',time());
|
||||
//对时间进行拆分
|
||||
if($date != null){
|
||||
$date = explode("|",$date);
|
||||
|
||||
$starttime = $date[0];
|
||||
$endttime = $date[1];
|
||||
}
|
||||
|
||||
$config = $this->config;
|
||||
$database = $this->database;
|
||||
|
||||
//判断类型
|
||||
switch ($type) {
|
||||
case 'user':
|
||||
// echo 'dsd';
|
||||
$datas = $database->select("imginfo", "*", [
|
||||
"dir" => $config['userdir'],
|
||||
"date[~]" => $thetime,
|
||||
"ORDER" => ["id" => "DESC"]
|
||||
]);
|
||||
// var_dump( $database->log() );
|
||||
// exit;
|
||||
return $datas;
|
||||
break;
|
||||
case 'admin':
|
||||
$datas = $database->select("imginfo", "*", [
|
||||
"dir" => $config['admindir'],
|
||||
"date[~]" => $thetime,
|
||||
"ORDER" => ["id" => "DESC"]
|
||||
]);
|
||||
return $datas;
|
||||
break;
|
||||
default:
|
||||
$sql = "SELECT * FROM imginfo WHERE date(date) BETWEEN '$starttime' AND '$endttime' ORDER BY `id` DESC";
|
||||
|
||||
$datas = $database->query($sql);
|
||||
//var_dump($database->log());
|
||||
return $datas;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//删除一张图片
|
||||
function delete($id){
|
||||
$config = $this->config;
|
||||
$database = $this->database;
|
||||
//先查询数据库获取图片路径
|
||||
$path = $database->get("imginfo","path",[
|
||||
"id" => $id
|
||||
]);
|
||||
|
||||
|
||||
//完整的图片路径
|
||||
$imgpath = APP.$path;
|
||||
//如果图片删除成功,将再次删除数据库
|
||||
//删除图片
|
||||
unlink($imgpath);
|
||||
//删除数据库
|
||||
$del = $database->delete("imginfo", [
|
||||
"AND" => [
|
||||
"id" => $id
|
||||
]
|
||||
]);
|
||||
echo 'ok';
|
||||
}
|
||||
//统计数据
|
||||
function data() {
|
||||
//获取当前月份
|
||||
$themonth = date('Y-m',time());
|
||||
//获取当天时间
|
||||
$theday = date('Y-m-d',time());
|
||||
|
||||
//统计本月上传图片数量
|
||||
$month = $this->database->count("imginfo",[
|
||||
"date[~]" => $themonth
|
||||
]);
|
||||
|
||||
$day = $this->database->count("imginfo",[
|
||||
"date" => $theday
|
||||
]);
|
||||
|
||||
//统计可疑图片
|
||||
$level = $this->database->count("imginfo",[
|
||||
"level" => 3
|
||||
]);
|
||||
//统计全部图片
|
||||
$all = $this->database->count("imginfo");
|
||||
|
||||
//返回数据
|
||||
$redata = array(
|
||||
"month" => $month,
|
||||
"day" => $day,
|
||||
"level" => $level,
|
||||
"all" => $all
|
||||
);
|
||||
return $redata;
|
||||
}
|
||||
//取消图片可疑状态
|
||||
function cdubious($id){
|
||||
$database = $this->database;
|
||||
$database->update("imginfo",[
|
||||
"level" => 1
|
||||
],[
|
||||
"id" => $id
|
||||
]);
|
||||
echo 'ok';
|
||||
}
|
||||
//对某张图片进行压缩,未开发完成
|
||||
function compress($id,$tinypng){
|
||||
$database = $this->database;
|
||||
$config = $this->config;
|
||||
if($tinypng['option'] != true){
|
||||
$compress['code'] = 0;
|
||||
$compress['msg'] = "未开启图片压缩功能!";
|
||||
}
|
||||
else{
|
||||
$getdata = $database->get("imginfo","*",[
|
||||
"id" => $id
|
||||
]);
|
||||
|
||||
}
|
||||
}
|
||||
// TinyImg 2.0:SM.MS 记录的查询 / 删除方法已整体移除
|
||||
//通过URL批量上传图片
|
||||
function urlup($arr){
|
||||
$config = $this->config;
|
||||
//$arr参数为一个数组
|
||||
$arr = explode("\n",$arr);
|
||||
//计算URL个数
|
||||
$urlnum = count($arr);
|
||||
//不能超过10个
|
||||
if($urlnum > 10){
|
||||
$rejson = array(
|
||||
"code" => 0,
|
||||
"msg" => "URL链接不能超过10个!"
|
||||
);
|
||||
echo json_encode($rejson);
|
||||
exit;
|
||||
}
|
||||
|
||||
//遍历URL并下载图片
|
||||
foreach( $arr as $url )
|
||||
{
|
||||
//去掉空格
|
||||
$url = trim($url);
|
||||
//判断是否是URL
|
||||
if(!filter_var($url, FILTER_VALIDATE_URL)){
|
||||
$rejson = array(
|
||||
"code" => 0,
|
||||
"msg" => $url."不是有效的地址,程序终止!"
|
||||
);
|
||||
echo json_encode($rejson);
|
||||
exit;
|
||||
}
|
||||
//获取图片后缀
|
||||
$suffix = explode(".",$url);
|
||||
$suffix = end($suffix);
|
||||
$suffix = strtolower($suffix);
|
||||
|
||||
//如果是图片后缀,使用curl进行抓取
|
||||
if(($suffix == 'jpg') || ($suffix == 'jpeg') || ($suffix == 'png') || ($suffix == 'bmp') || ($suffix == 'gif')) {
|
||||
|
||||
$curl = curl_init($url);
|
||||
|
||||
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");
|
||||
curl_setopt($curl, CURLOPT_FAILONERROR, true);
|
||||
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
||||
#设置超时时间,最小为1s(可选)
|
||||
curl_setopt($curl , CURLOPT_TIMEOUT, 10);
|
||||
|
||||
$html = curl_exec($curl);
|
||||
curl_close($curl);
|
||||
|
||||
//判断目录是否存在
|
||||
$dir = APP.$config['admindir'].'/'.date('ym',time());
|
||||
if(!is_dir($dir)){
|
||||
mkdir($dir);
|
||||
}
|
||||
//生成文件名
|
||||
$filenme = $dir.date('Y-m-d H:i:s',time()).$suffix;
|
||||
$filenme = substr(md5($url.$filenme), 8, 16).'.'.$suffix;
|
||||
|
||||
$full = $dir.'/'.$filenme;
|
||||
//写入文件
|
||||
$myfile = fopen($full, "w") or die("Unable to open file!");
|
||||
fwrite($myfile, $html);
|
||||
fclose($myfile);
|
||||
$datas = array(
|
||||
"path" => $config['admindir'].'/'.date('ym',time()).'/'.$filenme,
|
||||
"dir" => $config['admindir']
|
||||
);
|
||||
//写入数据库
|
||||
$insert = $this->indb($datas);
|
||||
|
||||
}
|
||||
else{
|
||||
$rejson = array(
|
||||
"code" => 0,
|
||||
"msg" => "包含无效的图片地址,程序终止!"
|
||||
);
|
||||
echo json_encode($rejson);
|
||||
exit;
|
||||
}
|
||||
|
||||
}
|
||||
$rejson = array(
|
||||
"code" => 1,
|
||||
"msg" => "成功上传".$urlnum."张图片!"
|
||||
);
|
||||
echo json_encode($rejson);
|
||||
exit;
|
||||
}
|
||||
|
||||
//图片写入数据库
|
||||
function indb($datas){
|
||||
$database = $this->database;
|
||||
$date = date('Y-m-d',time());
|
||||
$insert = $database->insert("imginfo",[
|
||||
"path" => $datas['path'],
|
||||
"ip" => "127.0.0.1",
|
||||
"ua" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
|
||||
"date" => $date,
|
||||
"dir" => $datas['dir'],
|
||||
"compress" => 0,
|
||||
"level" => 0
|
||||
]);
|
||||
return $insert;
|
||||
}
|
||||
}
|
||||
|
||||
$pic = new Admin($config,$database);
|
||||
?>
|
||||
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
class Install{
|
||||
public $homedir;
|
||||
public $domain;
|
||||
//构造函数
|
||||
public function __construct(){
|
||||
//获取项目绝对路径
|
||||
$thedir = __DIR__;
|
||||
$homedir = str_replace("\\","/",$thedir);
|
||||
$homedir = str_replace("functions/class","",$homedir);
|
||||
$this->homedir = $homedir;
|
||||
|
||||
//获取当前域名
|
||||
//获取当前端口
|
||||
$port = $_SERVER["SERVER_PORT"];
|
||||
//对端口进行判断
|
||||
switch ( $port )
|
||||
{
|
||||
case 80:
|
||||
$protocol = "http://";
|
||||
$port = '';
|
||||
break;
|
||||
case 443:
|
||||
$protocol = "https://";
|
||||
$port = '';
|
||||
break;
|
||||
default:
|
||||
$protocol = "http://";
|
||||
$port = ":".$port;
|
||||
break;
|
||||
}
|
||||
$uri = $_SERVER["REQUEST_URI"];
|
||||
$uri = str_replace("check.php","",$uri);
|
||||
//组合为完整的URL
|
||||
$domain = $protocol.$_SERVER['SERVER_NAME'].$port.$uri;
|
||||
$domain = str_replace("install.php?setup=2","",$domain);
|
||||
$this->domain = $domain;
|
||||
}
|
||||
//检查环境是否符合条件
|
||||
public function check(){
|
||||
$homedir = $this->homedir;
|
||||
|
||||
//echo $homedir.'db';
|
||||
//检查根目录是否可写,结果写入到一个数组
|
||||
//echo $thedir;
|
||||
$checkarr['home'] = is_writable($homedir);
|
||||
if($checkarr['home']){
|
||||
$statusarr['home'] = '通过';
|
||||
|
||||
}
|
||||
else{
|
||||
$statusarr['home'] = '<span style = "color:red;">目录不可写!</span>';
|
||||
|
||||
}
|
||||
$checkarr['db'] = is_writable($homedir.'db');
|
||||
if($checkarr['db']){
|
||||
$statusarr['db'] = '通过';
|
||||
}
|
||||
else{
|
||||
$statusarr['db'] = '<span style = "color:red;">目录不可写!</span>';
|
||||
|
||||
}
|
||||
//检测组建是否支持
|
||||
$ext = get_loaded_extensions();
|
||||
if(array_search('pdo_sqlite',$ext)){
|
||||
$statusarr['pdo'] = '支持';
|
||||
}
|
||||
else{
|
||||
$statusarr['pdo'] = '<span style = "color:red;">不支持!</span>';
|
||||
}
|
||||
//return $checkarr;
|
||||
return $statusarr;
|
||||
}
|
||||
//获取站点信息
|
||||
public function info(){
|
||||
$homedir = $this->homedir;
|
||||
|
||||
$info = array(
|
||||
"homedir" => $this->homedir,
|
||||
"domain" => $this->domain
|
||||
);
|
||||
|
||||
return $info;
|
||||
}
|
||||
//验证函数
|
||||
protected function verify($data,$type){
|
||||
switch ($type) {
|
||||
//检查用户名
|
||||
case 'user':
|
||||
$pattern = '/^[a-zA-Z0-9]+$/';
|
||||
if($data == ''){
|
||||
echo '请填写用户名!';
|
||||
exit;
|
||||
}
|
||||
if(!preg_match($pattern,$data)){
|
||||
echo '用户名格式有误!';
|
||||
exit;
|
||||
}
|
||||
break;
|
||||
case 'pass':
|
||||
$pattern = '/^[a-zA-Z0-9!@#$%^&*.]+$/';
|
||||
if(!preg_match($pattern,$data)){
|
||||
echo '密码格式有误!';
|
||||
exit;
|
||||
}
|
||||
break;
|
||||
case 'pass2':
|
||||
$pass1 = $data['pass1'];
|
||||
$pass2 = $data['pass2'];
|
||||
|
||||
if($pass1 != $pass2){
|
||||
echo '两次密码不一致!';
|
||||
exit;
|
||||
}
|
||||
break;
|
||||
case 'domain':
|
||||
$domain = $data['domain'];
|
||||
if(!filter_var($domain, FILTER_VALIDATE_URL)){
|
||||
echo '域名格式有误!(需要包含https://)';
|
||||
exit;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
# code...
|
||||
break;
|
||||
}
|
||||
}
|
||||
//安装
|
||||
public function setup($data){
|
||||
$homedir = $this->homedir;
|
||||
$dbpath = $this->homedir.'db/';
|
||||
$user = $data['user'];
|
||||
$pass1 = $data['pass1'];
|
||||
$pass2 = $data['pass2'];
|
||||
|
||||
|
||||
$this->verify($user,'user');
|
||||
$this->verify($pass1,'pass');
|
||||
$this->verify($data,'pass2');
|
||||
$this->verify($data,'domain');
|
||||
|
||||
//复制一份数据库
|
||||
copy($dbpath."tinyimg-simple.db3",$dbpath."tinyimg.db3");
|
||||
//复制一份配置文件
|
||||
if(copy($homedir."config-simple.php",$homedir."config.php")){
|
||||
$configdir = $homedir."config.php";
|
||||
|
||||
$myfile = fopen($homedir."config.php", "r") or die("Unable to open file!");
|
||||
|
||||
|
||||
$content = fread($myfile,filesize($configdir));
|
||||
//执行替换
|
||||
$content = str_replace("imguser",$user,$content);
|
||||
$content = str_replace("imgpass",$pass2,$content);
|
||||
$content = str_replace("homedir",$data['homedir'],$content);
|
||||
$content = str_replace("https://img.example.com/",$data['domain'],$content);
|
||||
|
||||
//var_dump($content);
|
||||
|
||||
//写入文件
|
||||
$myfile = fopen($homedir."config.php", "w+") or die("Unable to open file!");
|
||||
|
||||
|
||||
fwrite($myfile, $content);
|
||||
//关闭
|
||||
fclose($myfile);
|
||||
//更名安装文件
|
||||
rename($homedir."install.php",$homedir."install.php.bak");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
error_reporting(E_ALL^E_NOTICE^E_WARNING^E_DEPRECATED);
|
||||
/*
|
||||
图像处理类
|
||||
*/
|
||||
include_once("../../config.php");
|
||||
//载入TinyPNG
|
||||
require_once(APP."functions/tinypng/Tinify/Exception.php");
|
||||
require_once(APP."functions/tinypng/Tinify/ResultMeta.php");
|
||||
require_once(APP."functions/tinypng/Tinify/Result.php");
|
||||
require_once(APP."functions/tinypng/Tinify/Source.php");
|
||||
require_once(APP."functions/tinypng/Tinify/Client.php");
|
||||
require_once(APP."functions/tinypng/Tinify.php");
|
||||
|
||||
//初始化值
|
||||
$dispose['compress'] = 0;
|
||||
$dispose['level'] = 0;
|
||||
|
||||
//获取ID
|
||||
$id = $_GET['id'];
|
||||
$id = (int)$id;
|
||||
//获取tinypng key
|
||||
$tinykey = array_rand($tinypng['key']); //取出数组键值
|
||||
$tinykey = $tinypng['key'][$tinykey];
|
||||
$iscompress = 0;
|
||||
|
||||
//如果ID不存在或为空
|
||||
if((!isset($id)) || ($id == '')) {
|
||||
echo 'ID错误!';
|
||||
exit;
|
||||
}
|
||||
|
||||
//如果没有启用压缩
|
||||
if($tinypng['option'] != true){
|
||||
echo '未启用压缩功能!';
|
||||
exit;
|
||||
}
|
||||
|
||||
//查询对应信息
|
||||
$info = $database->get("imginfo",[
|
||||
"id",
|
||||
"path",
|
||||
"compress",
|
||||
"level"
|
||||
],[
|
||||
"id" => $id
|
||||
]);
|
||||
|
||||
|
||||
|
||||
//组合为完整的URL地址
|
||||
$tinyimg = $config['domain'].$info['path'];
|
||||
//获取压缩状态
|
||||
$compress = $info['compress'];
|
||||
//获取图片等级
|
||||
$level = $info['level'];
|
||||
//获取文件后缀名
|
||||
$suffix = substr(strrchr($info['path'], '.'), 1);
|
||||
//后缀改为小写
|
||||
$suffix = strtolower($suffix);
|
||||
if(($suffix == 'png') || ($suffix == 'jpg') || ($suffix == 'jpeg')) {
|
||||
$iscompress = 1;
|
||||
}
|
||||
if($iscompress == 0){
|
||||
echo '该后缀不支持压缩!';
|
||||
exit;
|
||||
}
|
||||
if($compress == 1){
|
||||
echo '该图片已经压缩!';
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
//对图片进行压缩
|
||||
if(($tinypng['option'] == true) && ($iscompress == 1) && ($compress == 0)) {
|
||||
//初始化
|
||||
\Tinify\setKey($tinykey);
|
||||
$source = \Tinify\fromUrl($tinyimg);
|
||||
//覆盖原有图片
|
||||
$source->toFile(APP.$info['path']);
|
||||
//更新数据库
|
||||
$database->update("imginfo",[
|
||||
"compress" => 1
|
||||
],[
|
||||
"id" => $id
|
||||
]);
|
||||
$dispose['compress'] = 1;
|
||||
echo '压缩成功!';
|
||||
}
|
||||
?>
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
error_reporting(E_ALL^E_NOTICE^E_WARNING^E_DEPRECATED);
|
||||
// 载入配置文件
|
||||
include_once("../config.php");
|
||||
|
||||
class User{
|
||||
var $config;
|
||||
var $database;
|
||||
|
||||
//构造函数
|
||||
public function __construct($config,$database){
|
||||
$this->config = $config;
|
||||
$this->database = $database;
|
||||
}
|
||||
|
||||
// 用户登录,两个参数,一个是用户输入,一个是配置文件里面的用户信息
|
||||
function login($user,$admin){
|
||||
|
||||
// 用户输入的信息进行md5加密和对比
|
||||
$pw1 = md5($user['user'].$user['password']);
|
||||
$pw2 = md5($admin['user'].$admin['password']);
|
||||
|
||||
|
||||
// 判断用户名密码是否正确
|
||||
if($pw1 != $pw2){
|
||||
echo '用户名或密码不正确!';
|
||||
setcookie("user", '', time()-3600,"/");
|
||||
setcookie("password", '', time()-3600,"/");
|
||||
exit;
|
||||
}
|
||||
else{
|
||||
$password = md5("tinyimg".$admin['password']);
|
||||
//生成cookie
|
||||
setcookie("user", $user['user'], time()+3600 * 24 * 30,"/");
|
||||
setcookie("password", $password, time()+3600 * 24 * 30,"/");
|
||||
echo "<script>window.location.href = './index.php'</script>";
|
||||
exit;
|
||||
}
|
||||
}
|
||||
//判断用户状态
|
||||
function check($userinfo){
|
||||
//配置文件里面的用户信息
|
||||
$user1 = $userinfo['user'].md5("tinyimg".$userinfo['password']);
|
||||
// echo $user1;
|
||||
//COOKIES里面的信息
|
||||
$user2 = $_COOKIE['user'].$_COOKIE['password'];
|
||||
|
||||
//如果两者信息相符,说明已登录
|
||||
if($user1 == $user2) {
|
||||
return'islogin';
|
||||
exit;
|
||||
}
|
||||
else{
|
||||
return 'nologin';
|
||||
setcookie("user", '', time()-3600,"/");
|
||||
setcookie("password", '', time()-3600,"/");
|
||||
exit;
|
||||
}
|
||||
}
|
||||
//检查某张图片是否已经上传
|
||||
function isupload($path){
|
||||
$num = $this->database->count("imginfo",["path" => $path]);
|
||||
|
||||
//如果图片已经上传过,直接返回图片信息
|
||||
if($num >=1) {
|
||||
$info = $this->database->get("imginfo","*",["path" => $path]);
|
||||
$tinyimg = $this->config['domain'].$path;
|
||||
//返回json数据
|
||||
$redata = array(
|
||||
"code" => 1,
|
||||
"id" => $info['id'],
|
||||
"url" => $tinyimg,
|
||||
"width" => 0,
|
||||
"height" => 0
|
||||
);
|
||||
echo $redata = json_encode($redata);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
//限制访客上传数量
|
||||
function limitnum(){
|
||||
//获取访客IP
|
||||
$ip = $_SERVER['REMOTE_ADDR'];
|
||||
//获取当前时间
|
||||
$date = date('Y-m-d',time());
|
||||
//获取配置文件限额
|
||||
$limit = $this->config['limit'];
|
||||
//获取用户上传目录
|
||||
$dir = $this->config['userdir'];
|
||||
//查询数据库统计数量
|
||||
$database = $this->database;
|
||||
$num = $database->count("imginfo",[
|
||||
"date" => $date,
|
||||
"ip" => $ip,
|
||||
"dir" => $dir
|
||||
]);
|
||||
|
||||
if($num >= $limit) {
|
||||
$redata = array(
|
||||
"code" => 0,
|
||||
"msg" => "上传达到限制!"
|
||||
);
|
||||
echo $redata = json_encode($redata);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
//获取访客真实IP
|
||||
function getip(){
|
||||
if (getenv('HTTP_CLIENT_IP')) {
|
||||
$ip = getenv('HTTP_CLIENT_IP');
|
||||
}
|
||||
elseif (getenv('HTTP_X_FORWARDED_FOR')) {
|
||||
$ip = getenv('HTTP_X_FORWARDED_FOR');
|
||||
}
|
||||
elseif (getenv('HTTP_X_FORWARDED')) {
|
||||
$ip = getenv('HTTP_X_FORWARDED');
|
||||
}
|
||||
elseif (getenv('HTTP_FORWARDED_FOR')) {
|
||||
$ip = getenv('HTTP_FORWARDED_FOR');
|
||||
|
||||
}
|
||||
elseif (getenv('HTTP_FORWARDED')) {
|
||||
$ip = getenv('HTTP_FORWARDED');
|
||||
}
|
||||
else {
|
||||
$ip = $_SERVER['REMOTE_ADDR'];
|
||||
}
|
||||
return $ip;
|
||||
}
|
||||
//判断文件MIME类型
|
||||
function mime($path){
|
||||
$mime = mime_content_type($path);
|
||||
switch ( $mime )
|
||||
{
|
||||
case 'image/gif':
|
||||
case 'image/png':
|
||||
case 'image/jpeg':
|
||||
case 'image/bmp':
|
||||
return true;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//返回错误json
|
||||
function re_error($msg){
|
||||
$arr = array(
|
||||
"code" => 0,
|
||||
"msg" => $msg
|
||||
);
|
||||
$rejson = json_encode($arr);
|
||||
echo $rejson;
|
||||
exit;
|
||||
}
|
||||
//请求tipng和鉴黄接口
|
||||
function curlZip($id,$domain){
|
||||
//组合为完整的接口
|
||||
$apiurl = $domain.'dispose.php?id='.$id;
|
||||
//请求接口
|
||||
$curl = curl_init($apiurl);
|
||||
|
||||
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
|
||||
curl_setopt($curl, CURLOPT_FAILONERROR, true);
|
||||
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
||||
#设置超时时间,最小为1s(可选)
|
||||
curl_setopt($curl , CURLOPT_TIMEOUT, 2);
|
||||
|
||||
@curl_exec($curl);
|
||||
curl_close($curl);
|
||||
//var_dump($html);
|
||||
}
|
||||
}
|
||||
|
||||
//自动初始化完成一些基础操作
|
||||
$basis = new User($config,$database);
|
||||
?>
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
// +------------------------------------------------------------------------+
|
||||
// | class.upload.zh_CN.php |
|
||||
// +------------------------------------------------------------------------+
|
||||
// | Copyright (c) caoshiwei 2008. All rights reserved. |
|
||||
// | Version 0.25 |
|
||||
// | Last modified 09/29/2008 |
|
||||
// | Email [email protected] |
|
||||
// | Web http://www.hfut.edu.cn |
|
||||
// +------------------------------------------------------------------------+
|
||||
// | This program is free software; you can redistribute it and/or modify |
|
||||
// | it under the terms of the GNU General Public License version 2 as |
|
||||
// | published by the Free Software Foundation. |
|
||||
// | |
|
||||
// | This program is distributed in the hope that it will be useful, |
|
||||
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
||||
// | GNU General Public License for more details. |
|
||||
// | |
|
||||
// | You should have received a copy of the GNU General Public License |
|
||||
// | along with this program; if not, write to the |
|
||||
// | Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
|
||||
// | Boston, MA 02111-1307 USA |
|
||||
// | |
|
||||
// | Please give credit on sites that use class.upload and submit changes |
|
||||
// | of the script so other people can use them as well. |
|
||||
// | This script is free to use, don't abuse. |
|
||||
// +------------------------------------------------------------------------+
|
||||
|
||||
/**
|
||||
* Class upload Chinese translation
|
||||
*
|
||||
* @version 0.25
|
||||
* @codepage gb-2312
|
||||
* @author Shiwei Cao (caoshiwei@gmail.com)
|
||||
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
||||
* @copyright Shiwei Cao
|
||||
* @package cmf
|
||||
* @subpackage external
|
||||
*/
|
||||
|
||||
$translation = array();
|
||||
$translation['file_error'] = '�ļ������������ԡ�';
|
||||
$translation['local_file_missing'] = '�����ļ������ڡ�';
|
||||
$translation['local_file_not_readable'] = '�����ļ����ɶ��';
|
||||
$translation['uploaded_too_big_ini'] = '�ļ��ϼ����� (�ϴ����ļ���С������php.ini��upload_max_filesize���õĴ�С)��';
|
||||
$translation['uploaded_too_big_html'] = '�ļ��ϼ����� (�ϴ����ļ���С������HTML �����õĴ�С)��';
|
||||
$translation['uploaded_partial'] = '�ļ��ϼ����� (�ϴ����ļ����ֶ�ʧ)��';
|
||||
$translation['uploaded_missing'] = '�ļ��ϼ����� (�ϴ��ļ���ʧ)��';
|
||||
$translation['uploaded_unknown'] = '�ļ��ϼ����� (δ֪����).';
|
||||
$translation['try_again'] = '�ļ��ϼ����� �����ԡ�';
|
||||
$translation['file_too_big'] = '�ļ�̫����';
|
||||
$translation['no_mime'] = 'δ֪�ļ����͡�';
|
||||
$translation['incorrect_file'] = '����ȷ���ļ���ʽ��';
|
||||
$translation['image_too_wide'] = 'ͼƬ����̫����';
|
||||
$translation['image_too_narrow'] = 'ͼƬ����̫С��';
|
||||
$translation['image_too_high'] = 'ͼƬ�߶�̫����';
|
||||
$translation['image_too_short'] = 'ͼƬ�߶�̫С��';
|
||||
$translation['ratio_too_high'] = 'ͼƬ��/�߱���̫��(ͼƬ����̫��)��';
|
||||
$translation['ratio_too_low'] = 'ͼƬ��/�߱���̫��(ͼƬ�߶�̫��).';
|
||||
$translation['too_many_pixels'] = 'ͼƬλ��̫�ߡ�';
|
||||
$translation['not_enough_pixels'] = 'ͼƬλ������';
|
||||
$translation['file_not_uploaded'] = '�ļ�δ�ϴ������ܽ��д���';
|
||||
$translation['already_exists'] = '%s �Ѿ����ڣ��������ļ�����';
|
||||
$translation['temp_file_missing'] = '������(��ʱ)Դ�ļ�����ȷ�����ܽ��д���';
|
||||
$translation['source_missing'] = '���ϴ����ļ���ʧ�����ܽ��д���';
|
||||
$translation['destination_dir'] = 'Ŀ���ļ�Ŀ¼���ܱ����������ܽ��д���';
|
||||
$translation['destination_dir_missing'] = 'Ŀ���ļ�Ŀ¼�����ڣ����ܽ��д���';
|
||||
$translation['destination_path_not_dir'] = 'Ŀ¼·������һ����Ч��Ŀ¼�����ܽ��д���';
|
||||
$translation['destination_dir_write'] = '������Ŀ���ļ�Ŀ¼����Ϊ��д�ģ����ܽ��д���';
|
||||
$translation['destination_path_write'] = 'Ŀ¼·���Dz�����д�ģ����ܽ��д���';
|
||||
$translation['temp_file'] = '���ܴ�����ʱ�ļ������ܽ��д���';
|
||||
$translation['source_not_readable'] = 'Դ�ļ������Զ�����ܽ��д���';
|
||||
$translation['no_create_support'] = '%s ��֧�ִ���';
|
||||
$translation['create_error'] = '��Դ�ļ����� %s ͼƬ�����г���';
|
||||
$translation['source_invalid'] = '����ȡԭʼͼƬ��ȷ���Dz�����ȷ��ͼƬ�ļ���';
|
||||
$translation['gd_missing'] = 'GD ������ʹ�á�';
|
||||
$translation['watermark_no_create_support'] = '%s ������֧��, ���ܶ�ȡˮӡ�ļ���';
|
||||
$translation['watermark_create_error'] = '%s ��֧�ֶ�, ���ܴ���ˮӡ��';
|
||||
$translation['watermark_invalid'] = 'δ֪�ļ���ʽ, ����ȡˮӡ�ļ���';
|
||||
$translation['file_create'] = '%s ��֧�ִ�����';
|
||||
$translation['no_conversion_type'] = 'δ����ת������';
|
||||
$translation['copy_failed'] = '�ڷ������ϸ����ļ�ʱ���� copy() ����ʧ��.';
|
||||
$translation['reading_failed'] = '��ȡ�����г���';
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
// +------------------------------------------------------------------------+
|
||||
// | class.upload.zh_CN.php |
|
||||
// +------------------------------------------------------------------------+
|
||||
// | Copyright (c) caoshiwei 2008. All rights reserved. |
|
||||
// | Version 0.25 |
|
||||
// | Last modified 09/29/2008 |
|
||||
// | Email [email protected] |
|
||||
// | Web http://www.hfut.edu.cn |
|
||||
// +------------------------------------------------------------------------+
|
||||
// | This program is free software; you can redistribute it and/or modify |
|
||||
// | it under the terms of the GNU General Public License version 2 as |
|
||||
// | published by the Free Software Foundation. |
|
||||
// | |
|
||||
// | This program is distributed in the hope that it will be useful, |
|
||||
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
||||
// | GNU General Public License for more details. |
|
||||
// | |
|
||||
// | You should have received a copy of the GNU General Public License |
|
||||
// | along with this program; if not, write to the |
|
||||
// | Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
|
||||
// | Boston, MA 02111-1307 USA |
|
||||
// | |
|
||||
// | Please give credit on sites that use class.upload and submit changes |
|
||||
// | of the script so other people can use them as well. |
|
||||
// | This script is free to use, don't abuse. |
|
||||
// +------------------------------------------------------------------------+
|
||||
|
||||
/**
|
||||
* Class upload Chinese translation
|
||||
*
|
||||
* @version 0.25
|
||||
* @author Shiwei Cao (caoshiwei@gmail.com)
|
||||
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
||||
* @copyright Shiwei Cao
|
||||
* @package cmf
|
||||
* @subpackage external
|
||||
*/
|
||||
|
||||
$translation = array();
|
||||
$translation['file_error'] = '文件错误,请重试。';
|
||||
$translation['local_file_missing'] = '本地文件不存在。';
|
||||
$translation['local_file_not_readable'] = '本地文件不可读。';
|
||||
$translation['uploaded_too_big_ini'] = '文件上件出错 (上传的文件大小超过了php.ini中upload_max_filesize设置的大小)。';
|
||||
$translation['uploaded_too_big_html'] = '文件上件出错 (上传的文件大小超过了HTML 表单设置的大小)。';
|
||||
$translation['uploaded_partial'] = '文件上件出错 (上传的文件部分丢失)。';
|
||||
$translation['uploaded_missing'] = '文件上件出错 (上传文件丢失)。';
|
||||
$translation['uploaded_unknown'] = '文件上件出错 (未知错误).';
|
||||
$translation['try_again'] = '文件上件出错。 请重试。';
|
||||
$translation['file_too_big'] = '文件太大。';
|
||||
$translation['no_mime'] = '未知文件类型。';
|
||||
$translation['incorrect_file'] = '不正确的文件格式。';
|
||||
$translation['image_too_wide'] = '图片宽度太大。';
|
||||
$translation['image_too_narrow'] = '图片宽度太小。';
|
||||
$translation['image_too_high'] = '图片高度太大。';
|
||||
$translation['image_too_short'] = '图片高度太小。';
|
||||
$translation['ratio_too_high'] = '图片宽/高比率太高(图片宽度太大)。';
|
||||
$translation['ratio_too_low'] = '图片宽/高比率太低(图片高度太大).';
|
||||
$translation['too_many_pixels'] = '图片位数太高。';
|
||||
$translation['not_enough_pixels'] = '图片位数不够';
|
||||
$translation['file_not_uploaded'] = '文件未上传,不能进行处理。';
|
||||
$translation['already_exists'] = '%s 已经存在,请更换文件名。';
|
||||
$translation['temp_file_missing'] = '处理的(临时)源文件不正确,不能进行处理。';
|
||||
$translation['source_missing'] = '已上传的文件丢失,不能进行处理。';
|
||||
$translation['destination_dir'] = '目标文件目录不能被创建,不能进行处理。';
|
||||
$translation['destination_dir_missing'] = '目标文件目录不存在,不能进行处理。';
|
||||
$translation['destination_path_not_dir'] = '目录路径不是一个有效的目录,不能进行处理。';
|
||||
$translation['destination_dir_write'] = '不能让目标文件目录设置为可写的,不能进行处理。';
|
||||
$translation['destination_path_write'] = '目录路径是不可以写的,不能进行处理。';
|
||||
$translation['temp_file'] = '不能创建临时文件,不能进行处理。';
|
||||
$translation['source_not_readable'] = '源文件不可以读,不能进行处理。';
|
||||
$translation['no_create_support'] = '%s 不支持创建';
|
||||
$translation['create_error'] = '从源文件创建 %s 图片过程中出错。';
|
||||
$translation['source_invalid'] = '无法读取原始图片,确认是不是正确的图片文件?';
|
||||
$translation['gd_missing'] = 'GD 好像不可以使用。';
|
||||
$translation['watermark_no_create_support'] = '%s 创建不支持, 不能读取水印文件。';
|
||||
$translation['watermark_create_error'] = '%s 不支持读, 不能创建水印。';
|
||||
$translation['watermark_invalid'] = '未知文件格式, 无法读取水印文件。';
|
||||
$translation['file_create'] = '%s 不支持创建。';
|
||||
$translation['no_conversion_type'] = '未定义转换类型';
|
||||
$translation['copy_failed'] = '在服务器上复制文件时出错。 copy() 操作失败.';
|
||||
$translation['reading_failed'] = '读取过程中出错。';
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
// +------------------------------------------------------------------------+
|
||||
// | class.upload.zh_TW.php |
|
||||
// +------------------------------------------------------------------------+
|
||||
// | Copyright (c) Yang Chih-Wen 2009. All rights reserved. |
|
||||
// | Version 0.28 |
|
||||
// | Last modified 15/08/2009 |
|
||||
// | Email [email protected] |
|
||||
// | Web http://www.doubleservice.com/ |
|
||||
// +------------------------------------------------------------------------+
|
||||
// | This program is free software; you can redistribute it and/or modify |
|
||||
// | it under the terms of the GNU General Public License version 2 as |
|
||||
// | published by the Free Software Foundation. |
|
||||
// | |
|
||||
// | This program is distributed in the hope that it will be useful, |
|
||||
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
||||
// | GNU General Public License for more details. |
|
||||
// | |
|
||||
// | You should have received a copy of the GNU General Public License |
|
||||
// | along with this program; if not, write to the |
|
||||
// | Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
|
||||
// | Boston, MA 02111-1307 USA |
|
||||
// | |
|
||||
// | Please give credit on sites that use class.upload and submit changes |
|
||||
// | of the script so other people can use them as well. |
|
||||
// | This script is free to use, don't abuse. |
|
||||
// +------------------------------------------------------------------------+
|
||||
|
||||
/**
|
||||
* Class upload Traditional Chinese translation
|
||||
*
|
||||
* @version 0.28
|
||||
* @author Yang Chih-Wen (chihwen.yang@gmail.com)
|
||||
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
||||
* @copyright Yang Chih-Wen
|
||||
* @package cmf
|
||||
* @subpackage external
|
||||
*/
|
||||
|
||||
$translation = array();
|
||||
$translation['file_error'] = '檔案錯誤,請重試。';
|
||||
$translation['local_file_missing'] = '本地端的檔案不存在。';
|
||||
$translation['local_file_not_readable'] = '本地端的檔案不可讀取。';
|
||||
$translation['uploaded_too_big_ini'] = '檔案上傳出錯 (上傳的檔案超過了 php.ini 中 upload_max_filesize 指定的大小)。';
|
||||
$translation['uploaded_too_big_html'] = '檔案上傳出錯 (上傳的檔案超過了 HTML 表單中 MAX_FILE_SIZE 指定的大小)。';
|
||||
$translation['uploaded_partial'] = '檔案上傳出錯 (只有部份的檔案被上傳)。';
|
||||
$translation['uploaded_missing'] = '檔案上傳出錯 (沒有檔案被上傳)。';
|
||||
$translation['uploaded_no_tmp_dir'] = '檔案上傳出錯 (找不到暫存目錄)。';
|
||||
$translation['uploaded_cant_write'] = '檔案上傳出錯 (檔案寫入失敗)。';
|
||||
$translation['uploaded_err_extension'] = '檔案上傳出錯 (檔案上傳被 extension 中斷)。';
|
||||
$translation['uploaded_unknown'] = '檔案上傳出錯 (未知的錯誤)。';
|
||||
$translation['try_again'] = '檔案上傳出錯,請重試。';
|
||||
$translation['file_too_big'] = '檔案太大了。';
|
||||
$translation['no_mime'] = '未知的 MIME Type 檔案類型。';
|
||||
$translation['incorrect_file'] = '不正確的 MIME Type 檔案類型。';
|
||||
$translation['image_too_wide'] = '圖片寬度太大。';
|
||||
$translation['image_too_narrow'] = '圖片寬度太小。';
|
||||
$translation['image_too_high'] = '圖片高度太大。';
|
||||
$translation['image_too_short'] = '圖片高度太小。';
|
||||
$translation['ratio_too_high'] = '圖片寬高比率太大 (圖片寬度太大)。';
|
||||
$translation['ratio_too_low'] = '圖片寬高比率太小 (圖片高度太大)。';
|
||||
$translation['too_many_pixels'] = '圖片像素太多。';
|
||||
$translation['not_enough_pixels'] = '圖片像素太少。';
|
||||
$translation['file_not_uploaded'] = '檔案未上傳,無法繼續進行處理。';
|
||||
$translation['already_exists'] = '%s 已經存在,請更改檔名。';
|
||||
$translation['temp_file_missing'] = '暫存的原始檔案不正確,無法繼續進行處理。';
|
||||
$translation['source_missing'] = '已上傳的原始檔案不正確,無法繼續進行處理。';
|
||||
$translation['destination_dir'] = '無法創建目標目錄,無法繼續進行處理。';
|
||||
$translation['destination_dir_missing'] = '目標目錄不存在,無法繼續進行處理。';
|
||||
$translation['destination_path_not_dir'] = '目標路徑不是一個有效的目錄,無法繼續進行處理。';
|
||||
$translation['destination_dir_write'] = '目標目錄不能設定為可寫入,無法繼續進行處理。';
|
||||
$translation['destination_path_write'] = '目錄路徑不可寫入,無法繼續進行處理。';
|
||||
$translation['temp_file'] = '不能創建暫存檔案,無法繼續進行處理。';
|
||||
$translation['source_not_readable'] = '原始檔案不可讀取,無法繼續進行處理。';
|
||||
$translation['no_create_support'] = '不支援 %s 創建功能。';
|
||||
$translation['create_error'] = '從原始檔案創建 %s 圖片過程中出錯。';
|
||||
$translation['source_invalid'] = '無法讀取原始圖片,請確認是否為正確的圖片檔?';
|
||||
$translation['gd_missing'] = '無法使用 GD 函式庫。';
|
||||
$translation['watermark_no_create_support'] = '不支援 %s 創建功能,無法讀取浮水印。';
|
||||
$translation['watermark_create_error'] = '不支援 %s 讀取功能,無法創建浮水印。';
|
||||
$translation['watermark_invalid'] = '未知的圖片格式,無法讀取浮水印。';
|
||||
$translation['file_create'] = '不支援 %s 創建功能。';
|
||||
$translation['no_conversion_type'] = '未定義的轉換類型。';
|
||||
$translation['copy_failed'] = '在伺服端複製檔案時出錯,copy() 操作失敗。';
|
||||
$translation['reading_failed'] = '讀檔過程中出錯。';
|
||||
?>
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
//载入配置文件
|
||||
include_once("./class/class.user.php");
|
||||
//检查用户是否登录
|
||||
$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());
|
||||
|
||||
//根据IP、ua、时间生成一个唯一的md5值
|
||||
$picname = md5($ip.$ua.date('Y-m-d H:i:s',time()));
|
||||
//截取16个字符
|
||||
$picname = substr($picname,8,16).'.png';
|
||||
//本月图片路径
|
||||
$monthdir = APP.$updir.'/'.date('ym',time());
|
||||
|
||||
$onepath = $updir.'/'.date('ym',time()).'/'.$picname;
|
||||
//路径不存在,则创建路径
|
||||
if(!file_exists($monthdir)){
|
||||
mkdir($monthdir,0777);
|
||||
}
|
||||
//图片完整存储路径
|
||||
$picpath = APP.$onepath;
|
||||
|
||||
//echo $picpath;
|
||||
//替换一下,以免windows出现问题
|
||||
$picpath = str_replace("\\","/",$picpath);
|
||||
|
||||
//echo $picpath;
|
||||
|
||||
//接接收ase64图片
|
||||
$picfile = $_POST['content'];
|
||||
$picfile = base64_decode($picfile);
|
||||
//echo $picfile;
|
||||
//存储图片
|
||||
file_put_contents($picpath, $picfile);
|
||||
|
||||
//获取文件mime类型
|
||||
//如果不是图片文件,终止执行
|
||||
if(!$basis->mime($picpath)){
|
||||
unlink($picpath);
|
||||
$arr = array(
|
||||
"code" => 0,
|
||||
"msg" => '不允许的文件类型'
|
||||
);
|
||||
$json = json_encode($arr);
|
||||
echo $json;
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
//继续执行并写入数据库
|
||||
$last_user_id = $database->insert("imginfo", [
|
||||
"path" => $onepath,
|
||||
"ip" => $ip,
|
||||
"ua" => $ua,
|
||||
"date" => $date,
|
||||
"dir" => $updir,
|
||||
"compress" => 0,
|
||||
"level" => 0
|
||||
]);
|
||||
//var_dump($database->log());
|
||||
//返回最后的ID
|
||||
$account_id = $database->id();
|
||||
//写入数据库成功,返回json数据
|
||||
if($last_user_id){
|
||||
$url = $config['domain'].$onepath;
|
||||
rejson(1,$url,$account_id);
|
||||
}
|
||||
//echo $picpath;
|
||||
//var_dump($picfile);
|
||||
|
||||
?>
|
||||
<?php
|
||||
//返回json数据
|
||||
function rejson($code,$url,$id = 0){
|
||||
$arr = array(
|
||||
"code" => $code,
|
||||
"url" => $url,
|
||||
"id" => $id
|
||||
);
|
||||
$json = json_encode($arr);
|
||||
echo $json;
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
// TinyImg 2.0:第三方 SM.MS 回写通道已关闭。
|
||||
// 不载入任何类、不写数据库,直接 fail closed;即使有人手工构造 POST 也不会留下记录。
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
http_response_code(410);
|
||||
echo json_encode([
|
||||
'code' => 0,
|
||||
'msg' => '第三方 SM.MS 上传功能已关闭',
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
?>
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Tinify;
|
||||
|
||||
const VERSION = "1.5.2";
|
||||
|
||||
class Tinify {
|
||||
private static $key = NULL;
|
||||
private static $appIdentifier = NULL;
|
||||
private static $proxy = NULL;
|
||||
|
||||
private static $compressionCount = NULL;
|
||||
private static $client = NULL;
|
||||
|
||||
public static function setKey($key) {
|
||||
self::$key = $key;
|
||||
self::$client = NULL;
|
||||
}
|
||||
|
||||
public static function setAppIdentifier($appIdentifier) {
|
||||
self::$appIdentifier = $appIdentifier;
|
||||
self::$client = NULL;
|
||||
}
|
||||
|
||||
public static function setProxy($proxy) {
|
||||
self::$proxy = $proxy;
|
||||
self::$client = NULL;
|
||||
}
|
||||
|
||||
public static function getCompressionCount() {
|
||||
return self::$compressionCount;
|
||||
}
|
||||
|
||||
public static function setCompressionCount($compressionCount) {
|
||||
self::$compressionCount = $compressionCount;
|
||||
}
|
||||
|
||||
public static function getClient() {
|
||||
if (!self::$key) {
|
||||
throw new AccountException("Provide an API key with Tinify\setKey(...)");
|
||||
}
|
||||
|
||||
if (!self::$client) {
|
||||
self::$client = new Client(self::$key, self::$appIdentifier, self::$proxy);
|
||||
}
|
||||
|
||||
return self::$client;
|
||||
}
|
||||
|
||||
public static function setClient($client) {
|
||||
self::$client = $client;
|
||||
}
|
||||
}
|
||||
|
||||
function setKey($key) {
|
||||
return Tinify::setKey($key);
|
||||
}
|
||||
|
||||
function setAppIdentifier($appIdentifier) {
|
||||
return Tinify::setAppIdentifier($appIdentifier);
|
||||
}
|
||||
|
||||
function setProxy($proxy) {
|
||||
return Tinify::setProxy($proxy);
|
||||
}
|
||||
|
||||
function getCompressionCount() {
|
||||
return Tinify::getCompressionCount();
|
||||
}
|
||||
|
||||
function compressionCount() {
|
||||
return Tinify::getCompressionCount();
|
||||
}
|
||||
|
||||
function fromFile($path) {
|
||||
return Source::fromFile($path);
|
||||
}
|
||||
|
||||
function fromBuffer($string) {
|
||||
return Source::fromBuffer($string);
|
||||
}
|
||||
|
||||
function fromUrl($string) {
|
||||
return Source::fromUrl($string);
|
||||
}
|
||||
|
||||
function validate() {
|
||||
try {
|
||||
Tinify::getClient()->request("post", "/shrink");
|
||||
} catch (AccountException $err) {
|
||||
if ($err->status == 429) return true;
|
||||
throw $err;
|
||||
} catch (ClientException $err) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace Tinify;
|
||||
|
||||
class Client {
|
||||
const API_ENDPOINT = "https://api.tinify.com";
|
||||
|
||||
const RETRY_COUNT = 1;
|
||||
const RETRY_DELAY = 500;
|
||||
|
||||
private $options;
|
||||
|
||||
public static function userAgent() {
|
||||
$curl = curl_version();
|
||||
return "Tinify/" . VERSION . " PHP/" . PHP_VERSION . " curl/" . $curl["version"];
|
||||
}
|
||||
|
||||
private static function caBundle() {
|
||||
return __DIR__ . "/../data/cacert.pem";
|
||||
}
|
||||
|
||||
function __construct($key, $app_identifier = NULL, $proxy = NULL) {
|
||||
$curl = curl_version();
|
||||
|
||||
if (!($curl["features"] & CURL_VERSION_SSL)) {
|
||||
throw new ClientException("Your curl version does not support secure connections");
|
||||
}
|
||||
|
||||
if ($curl["version_number"] < 0x071201) {
|
||||
$version = $curl["version"];
|
||||
throw new ClientException("Your curl version ${version} is outdated; please upgrade to 7.18.1 or higher");
|
||||
}
|
||||
|
||||
$this->options = array(
|
||||
CURLOPT_BINARYTRANSFER => true,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HEADER => true,
|
||||
CURLOPT_USERPWD => "api:" . $key,
|
||||
CURLOPT_CAINFO => self::caBundle(),
|
||||
CURLOPT_SSL_VERIFYPEER => true,
|
||||
CURLOPT_USERAGENT => join(" ", array_filter(array(self::userAgent(), $app_identifier))),
|
||||
);
|
||||
|
||||
if ($proxy) {
|
||||
$parts = parse_url($proxy);
|
||||
if (isset($parts["host"])) {
|
||||
$this->options[CURLOPT_PROXYTYPE] = CURLPROXY_HTTP;
|
||||
$this->options[CURLOPT_PROXY] = $parts["host"];
|
||||
} else {
|
||||
throw new ConnectionException("Invalid proxy");
|
||||
}
|
||||
|
||||
if (isset($parts["port"])) {
|
||||
$this->options[CURLOPT_PROXYPORT] = $parts["port"];
|
||||
}
|
||||
|
||||
$creds = "";
|
||||
if (isset($parts["user"])) $creds .= $parts["user"];
|
||||
if (isset($parts["pass"])) $creds .= ":" . $parts["pass"];
|
||||
|
||||
if ($creds) {
|
||||
$this->options[CURLOPT_PROXYAUTH] = CURLAUTH_ANY;
|
||||
$this->options[CURLOPT_PROXYUSERPWD] = $creds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function request($method, $url, $body = NULL) {
|
||||
$header = array();
|
||||
if (is_array($body)) {
|
||||
if (!empty($body)) {
|
||||
$body = json_encode($body);
|
||||
array_push($header, "Content-Type: application/json");
|
||||
} else {
|
||||
$body = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
for ($retries = self::RETRY_COUNT; $retries >= 0; $retries--) {
|
||||
if ($retries < self::RETRY_COUNT) {
|
||||
usleep(self::RETRY_DELAY * 1000);
|
||||
}
|
||||
|
||||
$request = curl_init();
|
||||
if ($request === false || $request === null) {
|
||||
throw new ConnectionException(
|
||||
"Error while connecting: curl extension is not functional or disabled."
|
||||
);
|
||||
}
|
||||
|
||||
curl_setopt_array($request, $this->options);
|
||||
|
||||
$url = strtolower(substr($url, 0, 6)) == "https:" ? $url : self::API_ENDPOINT . $url;
|
||||
curl_setopt($request, CURLOPT_URL, $url);
|
||||
curl_setopt($request, CURLOPT_CUSTOMREQUEST, strtoupper($method));
|
||||
|
||||
if (count($header) > 0) {
|
||||
curl_setopt($request, CURLOPT_HTTPHEADER, $header);
|
||||
}
|
||||
|
||||
if ($body) {
|
||||
curl_setopt($request, CURLOPT_POSTFIELDS, $body);
|
||||
}
|
||||
|
||||
$response = curl_exec($request);
|
||||
|
||||
if (is_string($response)) {
|
||||
$status = curl_getinfo($request, CURLINFO_HTTP_CODE);
|
||||
$headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE);
|
||||
curl_close($request);
|
||||
|
||||
$headers = self::parseHeaders(substr($response, 0, $headerSize));
|
||||
$body = substr($response, $headerSize);
|
||||
|
||||
if (isset($headers["compression-count"])) {
|
||||
Tinify::setCompressionCount(intval($headers["compression-count"]));
|
||||
}
|
||||
|
||||
if ($status >= 200 && $status <= 299) {
|
||||
return (object) array("body" => $body, "headers" => $headers);
|
||||
}
|
||||
|
||||
$details = json_decode($body);
|
||||
if (!$details) {
|
||||
$message = sprintf("Error while parsing response: %s (#%d)",
|
||||
PHP_VERSION_ID >= 50500 ? json_last_error_msg() : "Error",
|
||||
json_last_error());
|
||||
$details = (object) array(
|
||||
"message" => $message,
|
||||
"error" => "ParseError"
|
||||
);
|
||||
}
|
||||
|
||||
if ($retries > 0 && $status >= 500) continue;
|
||||
throw Exception::create($details->message, $details->error, $status);
|
||||
} else {
|
||||
$message = sprintf("%s (#%d)", curl_error($request), curl_errno($request));
|
||||
curl_close($request);
|
||||
if ($retries > 0) continue;
|
||||
throw new ConnectionException("Error while connecting: " . $message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static function parseHeaders($headers) {
|
||||
if (!is_array($headers)) {
|
||||
$headers = explode("\r\n", $headers);
|
||||
}
|
||||
|
||||
$res = array();
|
||||
foreach ($headers as $header) {
|
||||
if (empty($header)) continue;
|
||||
$split = explode(":", $header, 2);
|
||||
if (count($split) === 2) {
|
||||
$res[strtolower($split[0])] = trim($split[1]);
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Tinify;
|
||||
|
||||
class Exception extends \Exception {
|
||||
public $status;
|
||||
|
||||
public static function create($message, $type, $status) {
|
||||
if ($status == 401 || $status == 429) {
|
||||
$klass = "Tinify\AccountException";
|
||||
} else if($status >= 400 && $status <= 499) {
|
||||
$klass = "Tinify\ClientException";
|
||||
} else if($status >= 500 && $status <= 599) {
|
||||
$klass = "Tinify\ServerException";
|
||||
} else {
|
||||
$klass = "Tinify\Exception";
|
||||
}
|
||||
|
||||
if (empty($message)) $message = "No message was provided";
|
||||
return new $klass($message, $type, $status);
|
||||
}
|
||||
|
||||
function __construct($message, $type = NULL, $status = NULL) {
|
||||
$this->status = $status;
|
||||
if ($status) {
|
||||
parent::__construct($message . " (HTTP " . $status . "/" . $type . ")");
|
||||
} else {
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class AccountException extends Exception {}
|
||||
class ClientException extends Exception {}
|
||||
class ServerException extends Exception {}
|
||||
class ConnectionException extends Exception {}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Tinify;
|
||||
|
||||
class Result extends ResultMeta {
|
||||
protected $data;
|
||||
|
||||
public function __construct($meta, $data) {
|
||||
$this->meta = $meta;
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function data() {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function toBuffer() {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function toFile($path) {
|
||||
return file_put_contents($path, $this->toBuffer());
|
||||
}
|
||||
|
||||
public function size() {
|
||||
return intval($this->meta["content-length"]);
|
||||
}
|
||||
|
||||
public function mediaType() {
|
||||
return $this->meta["content-type"];
|
||||
}
|
||||
|
||||
public function contentType() {
|
||||
return $this->mediaType();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Tinify;
|
||||
|
||||
class ResultMeta {
|
||||
protected $meta;
|
||||
|
||||
public function __construct($meta) {
|
||||
$this->meta = $meta;
|
||||
}
|
||||
|
||||
public function width() {
|
||||
return intval($this->meta["image-width"]);
|
||||
}
|
||||
|
||||
public function height() {
|
||||
return intval($this->meta["image-height"]);
|
||||
}
|
||||
|
||||
public function location() {
|
||||
return isset($this->meta["location"]) ? $this->meta["location"] : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Tinify;
|
||||
|
||||
class Source {
|
||||
private $url, $commands;
|
||||
|
||||
public static function fromFile($path) {
|
||||
return self::fromBuffer(file_get_contents($path));
|
||||
}
|
||||
|
||||
public static function fromBuffer($string) {
|
||||
$response = Tinify::getClient()->request("post", "/shrink", $string);
|
||||
return new self($response->headers["location"]);
|
||||
}
|
||||
|
||||
public static function fromUrl($url) {
|
||||
$body = array("source" => array("url" => $url));
|
||||
$response = Tinify::getClient()->request("post", "/shrink", $body);
|
||||
return new self($response->headers["location"]);
|
||||
}
|
||||
|
||||
public function __construct($url, $commands = array()) {
|
||||
$this->url = $url;
|
||||
$this->commands = $commands;
|
||||
}
|
||||
|
||||
public function preserve() {
|
||||
$options = $this->flatten(func_get_args());
|
||||
$commands = array_merge($this->commands, array("preserve" => $options));
|
||||
return new self($this->url, $commands);
|
||||
}
|
||||
|
||||
public function resize($options) {
|
||||
$commands = array_merge($this->commands, array("resize" => $options));
|
||||
return new self($this->url, $commands);
|
||||
}
|
||||
|
||||
public function store($options) {
|
||||
$response = Tinify::getClient()->request("post", $this->url,
|
||||
array_merge($this->commands, array("store" => $options)));
|
||||
return new Result($response->headers, $response->body);
|
||||
}
|
||||
|
||||
public function result() {
|
||||
$response = Tinify::getClient()->request("get", $this->url, $this->commands);
|
||||
return new Result($response->headers, $response->body);
|
||||
}
|
||||
|
||||
public function toFile($path) {
|
||||
return $this->result()->toFile($path);
|
||||
}
|
||||
|
||||
public function toBuffer() {
|
||||
return $this->result()->toBuffer();
|
||||
}
|
||||
|
||||
private static function flatten($options) {
|
||||
$flattened = array();
|
||||
foreach ($options as $option) {
|
||||
if (is_array($option)) {
|
||||
$flattened = array_merge($flattened, $option);
|
||||
} else {
|
||||
array_push($flattened, $option);
|
||||
}
|
||||
}
|
||||
return $flattened;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
/**
|
||||
* TinyImg API Token 存储与校验(不依赖 Web 环境,可单独被测试脚本加载)。
|
||||
*
|
||||
* 存储文件:db/tinyimg-token.php
|
||||
* 首行是 PHP 守卫,直接通过 URL 访问时只会执行 exit,不会泄漏 Token;
|
||||
* 第二行是 JSON。即使站点跑在 Nginx 且没有屏蔽 db 目录,也拿不到内容。
|
||||
*
|
||||
* 兼容策略:存储文件不存在时,回落到 config.php 的 api_token(旧版部署),
|
||||
* 后台设置页第一次保存后即以存储文件为准。
|
||||
*/
|
||||
require_once __DIR__ . '/version.php';
|
||||
|
||||
/** Token 存储文件路径 */
|
||||
function tinyimg_token_file()
|
||||
{
|
||||
if (defined('APP')) {
|
||||
return rtrim(APP, "/\\") . '/db/tinyimg-token.php';
|
||||
}
|
||||
|
||||
return dirname(__DIR__) . '/db/tinyimg-token.php';
|
||||
}
|
||||
|
||||
/** 文件首行守卫:任何直接 HTTP 访问都会在这里终止 */
|
||||
function tinyimg_token_guard()
|
||||
{
|
||||
return '<?php exit; /* TinyImg API Token store - DO NOT EDIT */ ?>';
|
||||
}
|
||||
|
||||
/** 把状态数组序列化成文件正文(纯函数,便于测试) */
|
||||
function tinyimg_token_encode(array $state)
|
||||
{
|
||||
$payload = [
|
||||
'token' => isset($state['token']) ? (string) $state['token'] : '',
|
||||
'enabled' => !empty($state['enabled']) ? 1 : 0,
|
||||
'created_at' => isset($state['created_at']) ? (string) $state['created_at'] : '',
|
||||
'updated_at' => isset($state['updated_at']) ? (string) $state['updated_at'] : '',
|
||||
];
|
||||
|
||||
return tinyimg_token_guard() . "\n"
|
||||
. json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n";
|
||||
}
|
||||
|
||||
/** 解析文件正文,失败返回 null(纯函数) */
|
||||
function tinyimg_token_decode($raw)
|
||||
{
|
||||
$raw = (string) $raw;
|
||||
$pos = strpos($raw, '?>');
|
||||
|
||||
if ($pos === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$json = trim(substr($raw, $pos + 2));
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if (!is_array($data) || !array_key_exists('token', $data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'token' => (string) $data['token'],
|
||||
'enabled' => !empty($data['enabled']) ? 1 : 0,
|
||||
'created_at' => isset($data['created_at']) ? (string) $data['created_at'] : '',
|
||||
'updated_at' => isset($data['updated_at']) ? (string) $data['updated_at'] : '',
|
||||
];
|
||||
}
|
||||
|
||||
/** 无存储文件时的默认状态(兼容 config.php 的 api_token) */
|
||||
function tinyimg_token_default($config = [])
|
||||
{
|
||||
$legacy = (is_array($config) && isset($config['api_token']))
|
||||
? trim((string) $config['api_token'])
|
||||
: '';
|
||||
|
||||
return [
|
||||
'token' => $legacy,
|
||||
'enabled' => $legacy === '' ? 0 : 1,
|
||||
'created_at' => '',
|
||||
'updated_at' => '',
|
||||
'source' => $legacy === '' ? 'none' : 'config',
|
||||
];
|
||||
}
|
||||
|
||||
/** 读取当前状态;返回数组一定含 source 字段:file / config / none */
|
||||
function tinyimg_token_load($config = [])
|
||||
{
|
||||
$fallback = tinyimg_token_default($config);
|
||||
$file = tinyimg_token_file();
|
||||
|
||||
if (!is_file($file)) {
|
||||
return $fallback;
|
||||
}
|
||||
|
||||
$raw = @file_get_contents($file);
|
||||
if ($raw === false) {
|
||||
return $fallback;
|
||||
}
|
||||
|
||||
$state = tinyimg_token_decode($raw);
|
||||
if ($state === null) {
|
||||
return $fallback;
|
||||
}
|
||||
|
||||
$state['source'] = 'file';
|
||||
return $state;
|
||||
}
|
||||
|
||||
/** 生成随机 Token;优先 CSPRNG,最后才退化到弱随机 */
|
||||
function tinyimg_token_generate($bytes = 32)
|
||||
{
|
||||
if (function_exists('random_bytes')) {
|
||||
return bin2hex(random_bytes($bytes));
|
||||
}
|
||||
if (function_exists('openssl_random_pseudo_bytes')) {
|
||||
return bin2hex(openssl_random_pseudo_bytes($bytes));
|
||||
}
|
||||
|
||||
return md5(uniqid('', true) . mt_rand() . microtime(true))
|
||||
. md5(uniqid('', true) . mt_rand());
|
||||
}
|
||||
|
||||
/** 自定义 Token 的格式约束:16~128 位字母数字 / 下划线 / 短横线 */
|
||||
function tinyimg_token_valid_format($token)
|
||||
{
|
||||
return (bool) preg_match('/^[A-Za-z0-9_-]{16,128}$/', (string) $token);
|
||||
}
|
||||
|
||||
/** 恒定时间比较;未启用 / 空配置 / 空请求一律拒绝(fail closed) */
|
||||
function tinyimg_token_verify($state, $provided)
|
||||
{
|
||||
$stored = is_array($state) && isset($state['token']) ? (string) $state['token'] : '';
|
||||
$enabled = is_array($state) && !empty($state['enabled']);
|
||||
$provided = (string) $provided;
|
||||
|
||||
if (!$enabled || $stored === '' || $provided === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return function_exists('hash_equals')
|
||||
? hash_equals($stored, $provided)
|
||||
: $stored === $provided;
|
||||
}
|
||||
|
||||
/** 脱敏显示:保留前 6 位与后 4 位 */
|
||||
function tinyimg_token_mask($token)
|
||||
{
|
||||
$token = (string) $token;
|
||||
if ($token === '') {
|
||||
return '(未设置)';
|
||||
}
|
||||
if (strlen($token) <= 12) {
|
||||
return str_repeat('*', strlen($token));
|
||||
}
|
||||
|
||||
return substr($token, 0, 6) . str_repeat('*', 12) . substr($token, -4);
|
||||
}
|
||||
|
||||
/** 原子写入存储文件;返回 [是否成功, 消息] */
|
||||
function tinyimg_token_save($token, $enabled, $config = [])
|
||||
{
|
||||
$file = tinyimg_token_file();
|
||||
$dir = dirname($file);
|
||||
|
||||
if (!is_dir($dir)) {
|
||||
return [false, 'db 目录不存在'];
|
||||
}
|
||||
if (!is_writable($dir)) {
|
||||
return [false, 'db 目录不可写,请给 Web 用户写权限'];
|
||||
}
|
||||
|
||||
$previous = tinyimg_token_load($config);
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$state = [
|
||||
'token' => (string) $token,
|
||||
'enabled' => $enabled ? 1 : 0,
|
||||
'created_at' => $previous['created_at'] !== '' ? $previous['created_at'] : $now,
|
||||
'updated_at' => $now,
|
||||
];
|
||||
|
||||
$suffix = function_exists('random_bytes')
|
||||
? bin2hex(random_bytes(4))
|
||||
: md5(uniqid('', true));
|
||||
$tmp = $file . '.tmp.' . $suffix;
|
||||
|
||||
if (@file_put_contents($tmp, tinyimg_token_encode($state), LOCK_EX) === false) {
|
||||
@unlink($tmp);
|
||||
return [false, '写入临时文件失败'];
|
||||
}
|
||||
|
||||
@chmod($tmp, 0600);
|
||||
|
||||
if (!@rename($tmp, $file)) {
|
||||
@unlink($tmp);
|
||||
return [false, '无法覆盖 Token 存储文件'];
|
||||
}
|
||||
|
||||
@chmod($file, 0600);
|
||||
return [true, 'ok'];
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
// TinyImg 2.0 上传端点
|
||||
require_once __DIR__ . "/token.php"; // Token 存储 + TINYIMG_VERSION
|
||||
//载入配置文件($config / $database / $basis)
|
||||
include_once("./class/class.user.php");
|
||||
|
||||
// ==================================================================
|
||||
// API Token 上传通道(后台「Token 设置」页管理)
|
||||
//
|
||||
// 带 X-TinyImg-Token 头(或 POST 字段 token)时:
|
||||
// - 校验通过 -> 视为管理员上传(admindir + 跳过 limitnum),不依赖 Referer / Cookie
|
||||
// - 校验失败 -> 直接返回错误,绝不静默退化成游客上传
|
||||
// 不带 token 时,完全保留原有的 Referer + 登录 Cookie 逻辑(网页上传不受影响)。
|
||||
// 该 token 只被本端点识别,后台管理(class.admin.php)仍只认登录 Cookie,
|
||||
// 因此 token 泄露的影响范围仅限「上传图片」,可单独轮换而不动管理员密码。
|
||||
//
|
||||
// ?ping=1:只校验 Token、不接收文件,返回版本与管理员目录信息,
|
||||
// 供 Typecho 插件设置页的「测试连接」按钮使用。
|
||||
// ==================================================================
|
||||
$tokenState = tinyimg_token_load($config);
|
||||
$reqToken = '';
|
||||
if (isset($_SERVER['HTTP_X_TINYIMG_TOKEN'])) {
|
||||
$reqToken = (string) $_SERVER['HTTP_X_TINYIMG_TOKEN'];
|
||||
} elseif (isset($_POST['token'])) {
|
||||
$reqToken = (string) $_POST['token'];
|
||||
}
|
||||
$isPing = isset($_GET['ping']) && (string) $_GET['ping'] === '1';
|
||||
|
||||
if ($isPing && $reqToken === '') {
|
||||
$basis->re_error('缺少 API Token(X-TinyImg-Token)');
|
||||
}
|
||||
|
||||
if ($reqToken !== '') {
|
||||
// tinyimg_token_verify 内部已做 fail closed:未启用 / 空配置 / 空请求一律 false
|
||||
if (!tinyimg_token_verify($tokenState, $reqToken)) {
|
||||
$basis->re_error($tokenState['token'] === ''
|
||||
? 'API Token 未配置,请到后台「Token 设置」生成'
|
||||
: (empty($tokenState['enabled'])
|
||||
? 'API Token 上传已停用'
|
||||
: 'API Token 无效!'));
|
||||
}
|
||||
|
||||
if ($isPing) {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode([
|
||||
'code' => 1,
|
||||
'msg' => 'Token 有效',
|
||||
'name' => TINYIMG_NAME,
|
||||
'version' => TINYIMG_VERSION,
|
||||
'auth' => 'token',
|
||||
'admin_dir' => isset($config['admindir']) ? $config['admindir'] : 'upload',
|
||||
'source' => $tokenState['source'],
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
|
||||
$status = 'islogin';
|
||||
} else {
|
||||
//阻止用户直接访问
|
||||
if( !isset($_SERVER['HTTP_REFERER']) || !strstr($_SERVER['HTTP_REFERER'],$config['domain']) )
|
||||
{
|
||||
$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
|
||||
);
|
||||
echo $redata = json_encode($redata);
|
||||
$handle->clean();
|
||||
} else {
|
||||
//上传出现错误,返回报错信息
|
||||
$redata = array(
|
||||
"code" => 0,
|
||||
"msg" => $handle->error
|
||||
);
|
||||
echo json_encode($redata);
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
include_once("class/class.admin.php");
|
||||
|
||||
@$arr = $_POST['arr'];
|
||||
|
||||
$pic->urlup($arr);
|
||||
?>
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* TinyImg 版本信息:全站单一来源。
|
||||
* functions/version.txt 存放可读的版本字符串,后台、上传接口自检、页脚都读这里。
|
||||
*/
|
||||
if (!defined('TINYIMG_NAME')) {
|
||||
define('TINYIMG_NAME', 'TinyImg');
|
||||
}
|
||||
|
||||
if (!defined('TINYIMG_VERSION')) {
|
||||
$tinyimgVersionFile = __DIR__ . '/version.txt';
|
||||
$tinyimgVersion = @file_get_contents($tinyimgVersionFile);
|
||||
$tinyimgVersion = ($tinyimgVersion === false) ? '' : trim($tinyimgVersion);
|
||||
|
||||
define('TINYIMG_VERSION', $tinyimgVersion === '' ? 'v2.0.0' : $tinyimgVersion);
|
||||
unset($tinyimgVersionFile, $tinyimgVersion);
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1 @@
|
||||
v2.0.0(20261104)
|
||||
Reference in New Issue
Block a user