-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk-concat.php
95 lines (76 loc) · 4.3 KB
/
chunk-concat.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
<?php
// TODO: mkdir subfolders
// TODO: use json config files
//////////////////////////////////////////
////////// custom variables //////////////
$uploadPath = "elFinder" . DIRECTORY_SEPARATOR . "files";
//////////////////////////////////////////
$keys = ['REMOTE_ADDR','HTTP_X_FORWARDED_FOR','HTTP_CF_CONNECTING_IP','HTTP_X_REAL_IP']; // we can't tell if behind a proxy or if any of these are set
foreach ($keys as $key) { $remote_addr = isset($_SERVER[$key]) ? $_SERVER[$key] : '';}
$remote_country = isset($_SERVER["HTTP_CF_IPCOUNTRY"]) ? $_SERVER["HTTP_CF_IPCOUNTRY"] : '??';
// get variables
$fileId = $_GET['dzuuid'];
$chunkTotal = $_GET['dztotalchunkcount'];
// file path variables
$logfile = 'chunk-upload.log';
$targetPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $uploadPath . DIRECTORY_SEPARATOR;
// all the protections and cleanup below should also be done by the js client
$baseName = basename($_GET['fileName']);
// Remove anything which isn't a word, whitespace, number, or any of the following caracters: "-_~[]()."
// If you don't need to handle multi-byte characters
// you can use preg_replace rather than mb_ereg_replace
// $baseName = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\)\.])", '', $baseName);
$baseName = mb_ereg_replace("([^\w \d\-_~,;\[\]\(\)\.])", '', $baseName);
// Remove any runs of periods
$baseName = mb_ereg_replace("([\.]{2,})", '.', $baseName);
$fileExt = '.'.$_GET['fileExt'];
$fileExt = ($fileExt == '.') ? '' : $fileExt;
$fileName = $baseName.$fileExt;
// Custom special case just for you: next step is to load the dict off a json file and have various subdirectories+fileLists
$customFiles = array(
"nQ" => array('get-nQ.cmd','nQ.cmd')
);
foreach($customFiles as $customSubFolder => $arrCutomFiles)
{
if (in_array($fileName, $arrCutomFiles)) $targetPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $customSubFolder . DIRECTORY_SEPARATOR;
}
file_put_contents($logfile, date("Y-m-d H:i:s") .' , '. $remote_country .' , '. $remote_addr .' , chunk-concat: '. "fileName={$fileName}" .PHP_EOL, FILE_APPEND);
file_put_contents($logfile, date("Y-m-d H:i:s") .' , '. $remote_country .' , '. $remote_addr .' , chunk-concat: '. "targetPath={$targetPath}" .PHP_EOL, FILE_APPEND);
// unlink if exist, or file will be appended!!
@unlink("{$targetPath}{$fileName}");
file_put_contents($logfile, date("Y-m-d H:i:s") .' , '. $remote_country .' , '. $remote_addr .' , chunk-concat: '. "unlink {$fileName}" .PHP_EOL, FILE_APPEND);
/* ========================================
DEPENDENCY FUNCTIONS
======================================== */
// YOU CANNOT ADD file_put_contents($logfile) inside $returnResponse or uploads will fail completely
$returnResponse = function ($info = null, $filelink = null, $status = "ERROR") {
file_put_contents('chunk-upload-response.log', date("Y-m-d H:i:s") .' chunk-concat: '. $info .' '. $filelink .' '. $status .PHP_EOL, FILE_APPEND);
if ($status == "ERROR") die (json_encode( array(
"status" => $status,
"info" => $info,
"file_link" => $filelink
)));
};
/* ========================================
CONCATENATE UPLOADED FILES
======================================== */
// loop through temp files and grab the content
for ($i = 0; $i < $chunkTotal; $i++) {
// target temp file = 184e03c9-3b7b-4083-9185-647b87ba8872-1.ext
$temp_file_path = realpath("{$targetPath}{$fileId}-{$i}{$fileExt}") or $returnResponse("Your chunk was lost mid-upload.");
// copy chunk
$chunk = file_get_contents($temp_file_path);
if ( empty($chunk) ) $returnResponse("Chunks are uploading as empty strings.");
// add chunk to main file
file_put_contents($logfile, date("Y-m-d H:i:s") .' , '. $remote_country .' , '. $remote_addr .' , chunk-concat: '. "FILE_APPEND {$fileId}-{$i}{$fileExt} to {$baseName}{$fileExt}" .PHP_EOL, FILE_APPEND);
file_put_contents("{$targetPath}{$baseName}{$fileExt}", $chunk, FILE_APPEND | LOCK_EX);
// delete chunk
unlink($temp_file_path);
if ( file_exists($temp_file_path) ) $returnResponse("temp file could not be deleted", $temp_file_path);
}
$returnResponse("", $fileName.$fileExt, 'DONE');
/* ========== a bunch of steps I removed below here because they're irrelevant, but I described them anyway ========== */
// create FileMaker record
// run FileMaker script to populate container field with newly-created file
// unlink newly created file
// return success