-
Notifications
You must be signed in to change notification settings - Fork 187
/
up.php
executable file
·199 lines (180 loc) · 7.18 KB
/
up.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/*
* @Author: yumusb
* @Date: 2020-03-27 14:45:07
* @LastEditors: yumusb
* @LastEditTime: 2020-03-27 14:45:34
* @Description:
*/
/*
URL https://github.com/yumusb/autoPicCdn
注意事项:
1. php中开启 Curl扩展
2. 如果使用github,则服务器需要能和https://api.github.com正常通信。(建议放到国外 http://renzhijia.com/buy/index/7/?yumu 美国免费空间推荐 优惠码 free2 )
3. 如果使用Gitee,请保证 上传的文件 遵循国内法律
4. 懒的搭建或者不会搭建,就直接用 http://chuibi.cn/
5. 本源码已经开启智能AI授权模式,请到 http://33.al/donate 打赏5元以后 再开始配置
*/
error_reporting(0);
header('Content-Type: text/html; charset=UTF-8');
date_default_timezone_set("PRC");
if(!is_callable('curl_init')){
$return['code'] = 500;
$return['msg'] = "服务器不支持Curl扩展";
$return['url'] = null;
die(json_encode($return));
}
//必选项
define("TYPE","GITHUB");//选择github
//define("TYPE","GITEE");//选择gitee,如果使用gitee,需要手动建立master分支,可以看这里 https://gitee.com/help/articles/4122
define("USER","pic-cdn");//你的GitHub/Gitee的用户名
define("REPO","cdn2");//必须是上面用户名下的 公开仓库
define("MAIL","[email protected]");//邮箱无所谓,随便写
define("TOKEN","213");
// Github 去这个页面 https://github.com/settings/tokens生成一个有写权限的token(repo:Full control of private repositories 和write:packages前打勾)
// gitee 去往这个页面 https://gitee.com/personal_access_tokens
//数据库配置文件
//请确保把当前目录下的 pic.sql 导入到你的数据库
$database = array(
'dbname' => 'YourDbName',//你的数据库名字
'host' => 'localhost',
'port' => 3306,
'user' => 'YourDbUser',//你的数据库用户名
'pass' => 'YourDbPass',//你的数据库用户名对应的密码
);
$table = 'remote_imgs'; //表名字
if(TYPE!=="GITHUB" && TYPE!=="GITEE"){
$return['code'] = 500;
$return['msg'] = "Baby,你要传到哪里呢?";
$return['url'] = null;
die(json_encode($return));
}
try {
$db = new PDO("mysql:dbname=" . $database['dbname'] . ";host=" . $database['host'] . ";" . "port=" . $database['port'] . ";", $database['user'], $database['pass'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
} catch (PDOException $e) {
$return['code'] = 500;
$return['msg'] = "数据库出错,请检查 up.php中的database配置项.<br> " . $e->getMessage();
$return['url'] = null;
die(json_encode($return));
}
function GetIP(){
if(getenv('HTTP_CLIENT_IP')&&strcasecmp(getenv('HTTP_CLIENT_IP'),'unknown'))
{
$ip=getenv('HTTP_CLIENT_IP');
}
elseif(getenv('HTTP_X_FORWARDED_FOR')&&strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),'unknown'))
{
$ip=getenv('HTTP_X_FORWARDED_FOR');
}
elseif(getenv('REMOTE_ADDR')&&strcasecmp(getenv('REMOTE_ADDR'),'unknown'))
{
$ip=getenv('REMOTE_ADDR');
}
elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],'unknown'))
{
$ip=$_SERVER['REMOTE_ADDR'];
}
$ip=addslashes(preg_replace("/^([\d\.]+).*/","\\1",$ip));
return $ip;
}
function upload_github($filename, $content)
{
$url = "https://api.github.com/repos/" . USER . "/" . REPO . "/contents/" . $filename;
$ch = curl_init();
$defaultOptions=[
CURLOPT_URL => $url,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST=>"PUT",
CURLOPT_POSTFIELDS=>json_encode([
"message"=>"upload By autoPicCdn",
"committer"=> [
"name"=> USER,
"email"=>MAIL,
],
"content"=> $content,
]),
CURLOPT_HTTPHEADER => [
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language:zh-CN,en-US;q=0.7,en;q=0.3",
"User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
'Authorization:token '.TOKEN,
],
];
curl_setopt_array($ch, $defaultOptions);
$chContents = curl_exec($ch);
curl_close($ch);
return $chContents;
}
function upload_gitee($filename, $content)
{
$url = "https://gitee.com/api/v5/repos/". USER ."/". REPO ."/contents/".$filename;
$ch = curl_init();
$defaultOptions=[
CURLOPT_URL => $url,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST=>"POST",
CURLOPT_POSTFIELDS=>[
"access_token"=>TOKEN,
"message"=>"upload By autoPicCdn",
"content"=> $content,
"owner"=>USER,
"repo"=>REPO,
"path"=>$filename,
"branch"=>"master"
],
CURLOPT_HTTPHEADER => [
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language:zh-CN,en-US;q=0.7,en;q=0.3",
"User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
],
];
curl_setopt_array($ch, $defaultOptions);
$chContents = curl_exec($ch);
curl_close($ch);
return $chContents;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_FILES["pic"]["error"] <= 0 && $_FILES["pic"]["size"] >100 ) {
$filename = date('Y') . '/' . date('m') . '/' . date('d') . '/' . md5(time().mt_rand(10,1000)) . ".png";
$tmpName = './tmp' . md5($filename);
move_uploaded_file($_FILES['pic']['tmp_name'], $tmpName);
$filemd5 = md5_file($tmpName);
$row = $db->query("SELECT `imgurl` FROM `{$table}` WHERE `imgmd5`= '{$filemd5}' ")->fetch(PDO::FETCH_ASSOC);
if($row){
$remoteimg=$row['imgurl'];
}else{
$content = base64_encode(file_get_contents($tmpName));
if(TYPE==="GITHUB"){
$res = json_decode(upload_github($filename, $content), true);
}
else{
$res = json_decode(upload_gitee($filename, $content), true);
}
if($res['content']['path'] != ""){
if(TYPE==="GITHUB"){
$remoteimg = 'https://cdn.jsdelivr.net/gh/' . USER . '/' . REPO . '@'.$res['commit']['sha'].'/' . $res['content']['path'];
}
else{
$remoteimg = $res['content']['download_url'];
}
$tmp = $db->prepare("INSERT INTO `{$table}`(`imgmd5`, `imguploadtime`, `imguploadip`,`imgurl`) VALUES (?,?,?,?)");
$tmp->execute(array($filemd5, time(), GetIP(), $remoteimg));
}
}
unlink($tmpName);
if ($remoteimg != "") {
$return['code'] = 'success';
$return['data']['url'] = $remoteimg;
$return['data']['filemd5'] = $filemd5;
} else {
$return['code'] = 500;
$return['msg'] = '上传失败,我们会尽快修复';
$return['url'] = null;
}
} else {
$return['code'] = 404;
$return['msg'] = '无法识别你的文件';
$return['url'] = null;
}
exit(json_encode($return));