-
Notifications
You must be signed in to change notification settings - Fork 16
/
upload.php
221 lines (203 loc) · 7.04 KB
/
upload.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
require_once("auth.php");
class Upload{
private $policy;
private $signKey;
private $errMsg;
private $fileSize;
private $fileName;
private $originName;
private $filePath;
private $frontPath;
private $auth;
private $chunkData;
private $showMsg;
public function __construct($msg=true){
header("Access-Control-Allow-Origin: *");
$this->showMsg = $msg;
$this->policy = isset($_POST["token"]) ? $_POST["token"] : $this->quit();
$this->policy = empty($this->policy) ? $_SERVER['HTTP_AUTHORIZATION'] :$this->policy;
}
public function chunkInit(){
header("access-control-allow-headers:authorization,content-type");
header("access-control-allow-methods:OPTIONS, HEAD, POST");
$policyInfo = explode(":", $this->policy);
$this->signKey = $policyInfo[0];
$this->policy = json_decode(base64_decode($policyInfo[1]),true);
$this->auth = new Auth($this->signKey);
if(!$this->auth->check($this->policy,"UPLOAD") && $_SERVER['REQUEST_METHOD'] !="OPTIONS"){
self::setError("auth failed.");
}
if($_SERVER['REQUEST_METHOD'] !="OPTIONS"){
$this->chunkData = file_get_contents("php://input");
$this->fileSize = strlen($this->chunkData);
if(!$this->validCheck(true)){
self::setError($this->errMsg);
}
$this->saveChunk();
}
}
public function init(){
header("Content-Type:text/json");
$policyInfo = explode(":", $this->policy);
$this->signKey = $policyInfo[0];
$this->policy = json_decode(base64_decode($policyInfo[1]),true);
$this->frontPath = isset($_POST["path"]) ? $_POST["path"] : "";
$this->auth = new Auth($this->signKey);
if(!$this->auth->check($this->policy,"UPLOAD")){
self::setError("auth failed.");
}
$this->fileSize = $_FILES["file"]["size"];
if(!$this->validCheck(false)){
self::setError($this->errMsg);
}
$this->saveFile();
}
private function quit(){
if($this->showMsg){
die ("Cloudreve Remote Server");
}
}
private function validCheck($chunk=false){
if($chunk && $this->fileSize > 4194350){
$this->errMsg = "File is to large.";
return false;
}
if($this->fileSize > $this->policy["fsizeLimit"]){
$this->errMsg = "文件太大";
return false;
}
return true;
}
public function saveChunk(){
$this->fileName=md5(uniqid());
if(!is_dir("chunks/" . $this->policy["uid"])){
mkdir("chunks/" . $this->policy["uid"],0777,true);
}
if(function_exists("scandir")){
$chunkList = scandir("chunks/" . $this->policy["uid"]);
if(count($chunkList)*4194304 >= CHUNK_BUFFER_TIMES*$this->policy["fsizeLimit"]){
self::setError("分片缓冲区已满,请等待系统回收");
}
}
if(!file_put_contents("chunks/" .$this->policy["uid"] ."/".$this->fileName,$this->chunkData)){
self::setError("文件转移失败");
};
echo json_encode(["ctx" => $this->fileName]);
}
public function saveFile(){
$this->fileName = str_replace('$(fname)', $_FILES["file"]["name"], $this->policy["saveKey"]);
if (file_exists("uploads/" . $this->fileName)){
self::setError("文件冲突");
}
if(!is_dir("uploads/" . dirname($this->fileName))){
mkdir("uploads/" . dirname($this->fileName),0777,true);
}
$this->originName = $_FILES["file"]["name"];
if(!move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/" .$this->fileName)){
self::setError("文件转移失败");
};
$this->filePath = "uploads/" .$this->fileName;
$this->sendCallback();
}
public static function setError($msg){
header("HTTP/1.1 401 Unauthorized");
die(json_encode(["error"=> $msg]));
}
static function urlsafe_b64decode($string) {
$data = str_replace(array('-','_'),array('+','/'),$string);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
return base64_decode($data);
}
public function mkfile(){
$policyInfo = explode(":", $this->policy);
$this->signKey = $policyInfo[0];
$this->policy = json_decode(base64_decode($policyInfo[1]),true);
$this->auth = new Auth($this->signKey);
if(!$this->auth->check($this->policy,"UPLOAD")){
self::setError("auth failed.");
}
$this->frontPath = isset($_GET["path"]) ? self::urlsafe_b64decode($_GET["path"]) : "";
$this->originName = self::urlsafe_b64decode($_GET["fname"]);
$chunkList = explode(",",file_get_contents("php://input"));
$this->combineChunk($chunkList);
}
public function combineChunk($chunkList){
$fileName = "file_".md5(uniqid());
$fileObj=fopen ('chunks/'.$this->policy["uid"]."/".$fileName,"a+");
foreach ($chunkList as $key => $value) {
$chunkObj = fopen('chunks/'.$this->policy["uid"]."/".$value, "rb");
if(!$fileObj || !$chunkObj){
self::setError("文件创建失败");
}
$content = fread($chunkObj, 4195304);
fwrite($fileObj, $content, 4195304);
unset($content);
fclose($chunkObj);
unlink('chunks/'.$this->policy["uid"]."/".$value);
}
fclose($fileObj);
$this->generateFile($fileName);
}
private function generateFile($fileName){
$this->fileSize = filesize('chunks/'.$this->policy["uid"]."/".$fileName);
if(!$this->validCheck(false)){
unlink('chunks/'.$this->policy["uid"]."/".$fileName);
self::setError($this->errMsg);
}
$this->fileName = str_replace('$(fname)', $this->originName, $this->policy["saveKey"]);
if (file_exists("uploads/" . $this->fileName)){
unlink('chunks/'.$this->policy["uid"]."/".$fileName);
self::setError("文件冲突");
}
if(!is_dir("uploads/" . dirname($this->fileName))){
mkdir("uploads/" . dirname($this->fileName),0777,true);
}
if(!@rename('chunks/'.$this->policy["uid"]."/".$fileName,"uploads/" .$this->fileName)){
@unlink('chunks/'.$this->policy["uid"]."/".$fileName);
self::setError("文件转移失败");
};
$this->filePath = "uploads/" .$this->fileName;
$this->sendCallback();
}
public function sendCallback(){
@list($width, $height, $img_type, $attr) = getimagesize($this->filePath);
$picInfo = empty($width)?" ":$width.",".$height;
$callbackBody = [
"fname" => $this->originName,
"objname" => $this->fileName,
"path" => $this->frontPath,
"fsize" => $this->fileSize,
"callbackkey" => $this->policy["callbackKey"],
"picinfo" => $picInfo,
];
$session = curl_init($this->policy["callbackUrl"]);
$headers = array();
$headers[] = "Authorization: " . $this->auth->sign($callbackBody);
curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($session, CURLOPT_POST, 1);
curl_setopt($session, CURLOPT_POSTFIELDS, base64_encode(json_encode($callbackBody)));
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($session, CURLOPT_SSL_VERIFYHOST, false);
$server_output = curl_exec($session);
$httpCode = curl_getinfo($session,CURLINFO_HTTP_CODE);
curl_close ($session);
if(!$server_output){
unlink("uploads/" .$this->fileName);
self::setError("回调无法发起,错误代码:".curl_errno($session));
}
if($httpCode != 200){
$resutltData = json_decode($server_output,true);
$errorMsg = isset($resutltData["error"]) ? $resutltData["error"] : "回调失败,未知错误";
unlink("uploads/" .$this->fileName);
self::setError($errorMsg);
}else{
echo '{"key":"success"}';
}
}
}
?>