-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
297 lines (266 loc) · 9.9 KB
/
index.php
File metadata and controls
297 lines (266 loc) · 9.9 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* Up : upload your file, and share it
* author : dmeloni
* source : drag and drop script (lehollandaisvolant)
*/
/**
* Verify if filename is correct
* @param string $titreDocument
* @return boolean
*/
function isValidName($titreDocument){
if(!(strlen($titreDocument) >= 3 && strlen($titreDocument) <= 100))
return false;
if($titreDocument == '.htaccess' || $titreDocument == '.htpassword' || $titreDocument == 'robots.txt'){
return false;
}
return preg_match('#^([&a-zA-Zéèàç\(\)0-9_\[\]\-\.: \?\=])+$#', $titreDocument);
}
/**
* Generate a random name
* @param int $car : number of character
* @return string
*/
function random($car) {
$string = "";
$chaine = "abcdefghijklmnpqrstuvwxy";
srand((double)microtime()*1000000);
for($i=0; $i<$car; $i++) {
$string .= $chaine[rand()%strlen($chaine)];
}
return $string;
}
$nbRandomCharacters = 10;
$serverMsg = '';
$folder = 'data';
$dataMappingFile = 'dataMapping.json'; // Simple array for mapping name of files
$dataMappingFilePath = $folder . '/' . $dataMappingFile;
/**
* Upload file on server
*/
if (is_writable($folder) && isset($_FILES['myfile'])) {
if(!isValidName($_FILES['myfile']['name'])){
return;
}
/*
* Data mapping recuperation
*/
if(!is_file($dataMappingFilePath)){
$dataMapping = array();
file_put_contents($dataMappingFilePath, json_encode($dataMapping));
}else{
$dataMapping = json_decode(file_get_contents($dataMappingFilePath), true);
}
$sFileName = $_FILES['myfile']['name'];
do{
$randomName = random(10);
}while($randomName === $dataMappingFile || is_file($folder . '/' . $randomName));
if(true === move_uploaded_file($_FILES['myfile']['tmp_name'], $folder . '/' . $randomName)){
$nbAvailableCopies = 1;
if(isset($_GET['copy']) && (int)$_GET['copy'] > 0){
$nbAvailableCopies = $_GET['copy'];
}
$dataMapping[$randomName] = array('name' => $_FILES['myfile']['name'], 'nb' => $nbAvailableCopies);
if(false !== file_put_contents($dataMappingFilePath, json_encode($dataMapping))){
echo $randomName;
return;
}
}
}
/**
* Download file
*/
if(isset($_GET['f']) && strlen($_GET['f']) == $nbRandomCharacters){
if(is_file($folder . '/' . $_GET['f'])){
/*
* Data mapping recuperation
*/
if(is_file($dataMappingFilePath)){
$dataMapping = json_decode(file_get_contents($dataMappingFilePath), true);
if(isset($dataMapping[$_GET['f']]) && $dataMapping[$_GET['f']]['nb'] > 0){
$file = $folder . '/' . $_GET['f'];
header('Content-type:application/octet-stream');
$size = filesize("./" . $file);
header("Content-Type: application/force-download; name=\"" . $dataMapping[$_GET['f']]['name'] . "\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $size");
header("Content-Disposition: attachment; filename=\"" . $dataMapping[$_GET['f']]['name'] . "\"");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
readfile("./" . $file);
$dataMapping[$_GET['f']]['nb']--;
if($dataMapping[$_GET['f']]['nb'] == 0){
// Remove the file && the mapping
// the @s doesn't corrupt data output if error
@unlink($file);
unset($dataMapping[$_GET['f']]);
}
@file_put_contents($dataMappingFilePath, json_encode($dataMapping));
return;
}else{
$serverMsg = 'File not available';
}
}
}else{
$serverMsg = 'File not found';
}
}
if(!(is_dir($folder) && is_writable($folder))){
$serverMsg = 'Data folder is not writable';
}
/*
* Get max upload size
*/
$max_upload = (int)(ini_get('upload_max_filesize'));
$max_post = (int)(ini_get('post_max_size'));
$memory_limit = (int)(ini_get('memory_limit'));
$upload_mb = min($max_upload, $max_post, $memory_limit);
$upload_b = $upload_mb * 1024 * 1024;
?><!DOCTYPE html><html lang="fr">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Up</title><meta charset="utf-8">
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, user-scalable=yes">
<link rel="apple-touch-icon" href="favicon.png">
<link rel="shortcut icon" href="favicon.ico">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<body>
<div id="content">
<section id="midle">
<div class="upload_form_cont">
<?php if(!empty($serverMsg)){?>
<div class="error"><?php echo $serverMsg;?></div>
<?php }else{?>
<div class="info">
<div id="upload-zone">Drop your files :-)</div>
</div>
<div>Number of available copies : <input type="text" id="upload-copy" value="1"></input></div>
<?php }?>
<div>Max upload size : <?php echo $upload_mb;?> Mb</div>
<div id="progress"></div>
</div>
</section>
<div class="progress"></div>
<script type="text/javascript">
// variables
var dropArea = document.documentElement; // drop area zone JS object
var progress = document.getElementById('progress'); // text zone where informations about uploaded files are displayed
var list = []; // file list
var nbDone = 0; // initialisation of nb files already uploaded during the process.
var nb=null;
var nbUploaded = null;
var oldColor = null;
var uploadError = false;
// main initialization
(function(){
// init handlers
function initHandlers() {
dropArea.addEventListener('drop', handleDrop, false);
dropArea.addEventListener('dragover', handleDragOver, false);
}
// drag over
function handleDragOver(event) {
event.stopPropagation();
event.preventDefault();
oldColor = dropArea.style.color;
dropArea.style.color='red';
//dropArea.className = 'hover';
}
// drag drop
function handleDrop(event) {
dropArea.style.color=oldColor;
event.stopPropagation();
event.preventDefault();
processFiles(event.dataTransfer.files);
}
// process bunch of files
function processFiles(filelist) {
if (!filelist || !filelist.length || list.length) return;
for (var i = 0; i < filelist.length && i < 500; i++) { // limit is 500 files (only for not having an infinite loop)
nbUploaded=filelist.length;
list.push(filelist[i]);
}
uploadNext();
}
// upload file
function uploadFile(file, status) {
// prepare XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.open('POST', 'index.php?copy='+document.getElementById('upload-copy').value);
xhr.onload = function() {
uploadNext();
nbUploaded--;
};
var totalTmp = 0;
if ( xhr.upload ) {
xhr.upload.onprogress = function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
totalTmp = e.total;
var pourcentage = Math.floor(done/total*1000)/10;
var progressMessage = "File : " + file['name'] + " ("+file['type']+") progress : " + pourcentage + "%" + " ("+done+"/"+total+" octets)";
var fileDiv = document.getElementById('file_'+nbDone+'');
fileDiv.textContent = progressMessage;
};
}
xhr.onreadystatechange = function() {
if(xhr.readyState == 4){
console.log(xhr);
var progressMessage = "File : " + file['name'] + " ("+file['type']+") progress : 100%" + " (" + file['size'] + " octets) : ";
urlMessage = '<a href="index.php?f='+xhr.responseText+'">'+xhr.responseText+'</a>';
var fileDiv = document.getElementById('file_'+nbDone+'');
fileDiv.innerHTML = progressMessage + urlMessage;
console.log(nbUploaded);
if(nbUploaded==1){
dropArea.style.color='#000';
}
}
};
xhr.onerror = function() {
var progressMessage = "File : " + file['name'] + " ("+file['type']+") upload error";
var fileDiv = document.getElementById('file_'+nbDone+'');
fileDiv.textContent = progressMessage;
uploadNext();
nbUploaded--;
if(nbUploaded==1){
dropArea.style.color='#000';
}
};
// prepare and send FormData
var formData = new FormData();
formData.append('myfile', file);
xhr.send(formData);
}
// upload next file
function uploadNext() {
if (list.length) {
nb = list.length - 1;
nbDone +=1;
var strTemp = '<div id="file_'+nbDone+'"></div>';
progress.innerHTML += strTemp;
var nextFile = list.shift();
var sizeMax = <?php echo $upload_b;?>;
if (nextFile.size >= sizeMax) { // 20Mb = generally the max file size on PHP hosts
var progressMessage = "File : " + nextFile['name'] + " ("+nextFile['type']+") File Too big (" + nextFile['size'] + " > "+sizeMax+")";
var fileDiv = document.getElementById('file_'+nbDone+'');
fileDiv.textContent = progressMessage;
uploadError = true;
uploadNext();
nbUploaded--;
if(nbUploaded==1){
dropArea.style.color='#000';
}
} else {
uploadFile(nextFile, status);
}
}
}
initHandlers();
})();
</script>
</div>
</body>
</html>