/
TimMixedUploaderHandler.php
313 lines (260 loc) · 9.4 KB
/
TimMixedUploaderHandler.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
namespace Ling\UploaderHandler;
/*
* LingTalfi 2016-01-11
*
*
*
* This MixedUploaderHandler uses a tim server to populate error/success messages.
* Also, certain parameters are expected, read below.
*
*
*
* Strategy
* -----------
*
* if the name of the file is passed, using $_FILE[file][name] or $_REQUEST[name] (post|get),
* then it is used, otherwise, a random name will be chosen by the server.
*
*
* ### Chunking
*
* Chunking is performed by the server by passing the following parameters via $_REQUEST:
*
* - chunks - the total number of chunks in the file
* - chunk - the ordinal number of the current chunk in the set (starts with zero)
* - ?name - the name of file (usually used to associate the chunk with the actual file)
*
*
* ### the ".part" suffix
*
* While the file is uploaded, it's suffixed with a suffix, which by default is ".part".
* For instance: /path/to/myfile.mp4.part is the file that contains the bits of the upload until the (theoretically )last bit
* of data has arrived. When the file's uploading is complete (either using chunking or regular upload),
* then the file /path/to/myfile.mp4.part is moved to /path/to/myfile.mp4.
*
*
* Note: this strategy is actually the original strategy used by the plupload jquery plugin.
*
*
* Also, beside of inheriting the MixedUploaderHandler's strategy (prepare and filter), this uploader emphasizes
* the fact that the file is first uploaded to a tmp dir (probably outside web server's root dir?),
* before it gets processed by the onFileAccepted callback (and possible moved to the web server's root directory).
* This is a security measure.
*
*
* webRootDir
* -------------
*
* By default, the tim success msg is the uploaded file path.
* However, if the webRootDir is set, then the tim's default success message is the url to the uploaded file,
* assuming that the dstDir (see class properties) is under the web root dir.
*
*
*
* Credits
* ----------
*
* This handler uses tim communication protocol (https://github.com/lingtalfi/Tim).
* It is based on a script provided by pluploader (http://plupload.com/).
*
*
*
*
*/
use Ling\Bat\FileSystemTool;
use Ling\Tim\TimServer\TimServerInterface;
class TimMixedUploaderHandler extends MixedUploaderHandler
{
private $tmpFileSuffix;
private $tmpDir; // folder while creating the file (should be we inaccessible for security)
private $dstDir; // the directory where the uploaded and checked file ends
private $webRootDir;
private $randomSeed;
// watch out for those two variables if you extend this class!, they are only defined inside THIS prepare method
private $isLastChunk;
private $fileName;
/**
* @var TimServerInterface
*/
protected $timServer;
public function __construct()
{
parent::__construct();
$this->tmpDir = '/tmp';
$this->dstDir = '/tmp';
$this->tmpFileSuffix = '.part';
$this->randomSeed = '_file__';
$this->isLastChunk = false;
}
public function setTmpDir($tmpDir)
{
$this->tmpDir = $tmpDir;
return $this;
}
public function setDstDir($dstDir)
{
$this->dstDir = $dstDir;
return $this;
}
public function setTimServer(TimServerInterface $timServer)
{
$this->timServer = $timServer;
return $this;
}
public function setTmpFileSuffix($tmpFileSuffix)
{
$this->tmpFileSuffix = $tmpFileSuffix;
return $this;
}
public function setRandomSeed($randomSeed)
{
$this->randomSeed = $randomSeed;
return $this;
}
//------------------------------------------------------------------------------/
//
//------------------------------------------------------------------------------/
protected function prepare(&$path, &$isChunk)
{
$this->isLastChunk = false;
if (false === parent::prepare($path, $isChunk)) {
return false;
}
if (null === $this->timServer) {
throw new \RuntimeException("Invalid object: timServer not set");
}
// Make sure file is not cached (as it happens for example on iOS devices)
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
@set_time_limit(5 * 60);
// Settings
$targetDir = realpath($this->tmpDir);
$cleanupTargetDir = true; // Remove old files
$maxFileAge = 5 * 3600; // Temp file age in seconds
// Create target dir
if (false === FileSystemTool::mkdir($targetDir, 0777, true)) {
$this->timServer->error("The target dir couldn't be created");
return false;
}
// Create destination dir
if (false === FileSystemTool::mkdir($this->dstDir, 0777, true)) {
$this->timServer->error("The destination dir couldn't be created");
return false;
}
// Get a file name
if (isset($_REQUEST["name"])) {
$fileName = $_REQUEST["name"];
}
elseif (isset($_FILES["file"]["name"])) {
$fileName = $_FILES["file"]["name"];
}
else {
$fileName = uniqid($this->randomSeed);
}
$fileName = preg_replace('!\.+!', '.', $fileName); // prevent parent access
$this->fileName = $fileName;
$filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
// Chunking might be enabled
$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
// Remove old temp files
if ($cleanupTargetDir) {
if (!$dir = opendir($targetDir)) {
$this->timServer->error("Failed to open temp directory.");
return false;
}
while (($file = readdir($dir)) !== false) {
$tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
// If temp file is current file proceed to the next
if ($tmpfilePath == "{$filePath}" . $this->tmpFileSuffix) {
continue;
}
// Remove temp file if it is older than the max age and is not the current file
if (preg_match('/' . preg_quote($this->tmpFileSuffix, '/') . '$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) {
@unlink($tmpfilePath);
}
}
closedir($dir);
}
// Open temp file
if (!$out = @fopen("{$filePath}" . $this->tmpFileSuffix, $chunks ? "ab" : "wb")) {
$this->timServer->error("Failed to open output stream.");
return false;
}
if (!empty($_FILES)) {
if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
$this->timServer->error("Failed to move uploaded file.");
return false;
}
// Read binary input stream and append it to temp file
if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
$this->timServer->error("Failed to open input stream.");
return false;
}
}
else {
if (!$in = @fopen("php://input", "rb")) {
$this->timServer->error("Failed to open input stream.");
return false;
}
}
while ($buff = fread($in, 4096)) {
fwrite($out, $buff);
}
@fclose($out);
@fclose($in);
// Check if file has been uploaded
if (!$chunks || $chunk == $chunks - 1) {
if (!$chunks) {
$isChunk = false;
}
else {
$isChunk = true;
$this->isLastChunk = true;
}
$path = $filePath . $this->tmpFileSuffix;
}
else {
// chunking, but not the last part
$isChunk = true;
$path = $filePath . $this->tmpFileSuffix;
}
}
protected function accept($path, $isChunk)
{
parent::accept($path, $isChunk);
$dst = $this->dstDir . "/" . $this->fileName;
if (false === $isChunk || true === $this->isLastChunk) {
/**
* if path and dst are on different drives,
* the rename might generate warnings, but from what I've tested, the file was renamed anyway,
* so I removed the warnings.
*/
@rename($path, $dst);
}
$this->onAccept($dst);
}
protected function refute($path, $isChunk, array $errors)
{
parent::refute($path, $isChunk, $errors);
// by default, just display the first error found
if ($errors) { // this condition shouldn't be checked actually, but just in case
$error = array_shift($errors);
$this->timServer->error($error);
}
}
protected function onAccept($dst)
{
if (null === $this->webRootDir) {
$this->timServer->success($dst);
}
else {
$url = str_replace(realpath($this->webRootDir), '', realpath($dst));
$this->timServer->success($url);
}
}
}