-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathFilePond.class.php
293 lines (232 loc) · 8.53 KB
/
FilePond.class.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
<?php
namespace FilePond;
require_once(__DIR__ . '/Helper/Transfer.class.php');
require_once(__DIR__ . '/Helper/Post.class.php');
require_once(__DIR__ . '/Helper/ServerExceptions.php');
function fetch($url) {
try {
// create temp file
$out = tmpfile();
// go!
$ch = curl_init(str_replace(' ','%20',$url));
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
if (!curl_exec($ch)) throw new \Exception(curl_error($ch), curl_errno($ch));
$type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$length = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
return array(
'tmp_name' => stream_get_meta_data($out)['uri'],
'name' => sanitize_filename(pathinfo($url)['basename']),
'type' => $type,
'length' => $length,
'error' => $code >= 200 && $code < 300 ? 0 : $code,
'ref' => $out, // need this so the file is not automatically removed
);
}
catch(Exception $e) {
return false;
}
}
function sanitize_filename($filename) {
$info = pathinfo($filename);
$name = sanitize_filename_part($info['filename']);
$extension = sanitize_filename_part($info['extension']);
return (strlen($name) > 0 ? $name : '_') . '.' . $extension;
}
function sanitize_filename_part($str) {
return preg_replace("/[^a-zA-Z0-9\_\s]/", "", $str);
}
function remove_directory($path) {
if (!is_dir($path)) {return;}
$files = glob($path . DIRECTORY_SEPARATOR . '{.,}*', GLOB_BRACE);
@array_map('unlink', $files);
@rmdir($path);
}
function remove_transfer_directory($path, $id) {
// don't remove anything if the transfer id is not valid (just a security precaution)
if (!is_valid_transfer_id($id)) return;
remove_directory($path . DIRECTORY_SEPARATOR . $id . DIRECTORY_SEPARATOR . VARIANTS_DIR);
remove_directory($path . DIRECTORY_SEPARATOR . $id);
}
function create_directory($path) {
if (!is_dir($path)) {
mkdir($path, 0755, true);
return true;
}
return false;
}
function secure_directory($path) {
$content = '# Don\'t list directory contents
IndexIgnore *
# Disable script execution
AddHandler cgi-script .php .pl .jsp .asp .sh .cgi
Options -ExecCGI -Indexes';
file_put_contents($path . DIRECTORY_SEPARATOR . '.htaccess', $content);
}
function create_secure_directory($path) {
$created = create_directory($path);
if ($created) {
secure_directory($path);
}
}
function write_file($path, $data, $filename) {
$handle = fopen($path . DIRECTORY_SEPARATOR . $filename, 'w');
fwrite($handle, $data);
fclose($handle);
}
function is_url($str) {
if (!filter_var($str, FILTER_VALIDATE_URL)) return false;
return in_array(parse_url($str, PHP_URL_SCHEME),['http', 'https', 'ftp']);
}
function echo_file($file) {
// read file object
if (is_string($file)) $file = read_file($file);
// something went wrong while reading the file
if (!$file) http_response_code(500);
// Allow to read Content Disposition (so we can read the file name on the client side)
header('Access-Control-Expose-Headers: Content-Disposition, Content-Length, X-Content-Transfer-Id');
header('Content-Type: ' . $file['type']);
header('Content-Length: ' . $file['length']);
header('Content-Disposition: inline; filename="' . $file['name'] . '"');
echo isset($file['content']) ? $file['content'] : read_file_contents($file['tmp_name']);
}
function read_file_contents($filename) {
$file = read_file($filename);
if (!$file) return false;
return $file['content'];
}
function read_file($filename) {
$handle = fopen($filename, 'r');
if (!$handle) return false;
$content = fread($handle, filesize($filename));
fclose($handle);
if (!$content) return false;
return array(
'tmp_name' => $filename,
'name' => basename($filename),
'content' => $content,
'type' => mime_content_type($filename),
'length' => filesize($filename),
'error' => 0
);
}
function move_temp_file($file, $path) {
move_uploaded_file($file['tmp_name'], $path . DIRECTORY_SEPARATOR . sanitize_filename($file['name']));
}
function move_file($file, $path) {
if (is_uploaded_file($file['tmp_name'])) {
return move_temp_file($file, $path);
}
return rename($file['tmp_name'], $path . DIRECTORY_SEPARATOR . sanitize_filename($file['name']));
}
function store_transfer($path, $transfer) {
// create transfer directory
$path = $path . DIRECTORY_SEPARATOR . $transfer->getId();
create_secure_directory($path);
// store metadata
if ($transfer->getMetadata()) {
write_file($path, @json_encode($transfer->getMetadata()), METADATA_FILENAME);
}
// store main file if set (if not set, we expect to receive chunks in the near future)
$files = $transfer->getFiles();
if ($files === null) return;
$file = $files[0];
move_file($file, $path);
// store variants
if (count($transfer->getFiles()) > 1) {
$files = array_slice($files, 1);
$variants = $path . DIRECTORY_SEPARATOR . VARIANTS_DIR;
create_secure_directory($variants);
foreach($files as $file) {
move_file($file, $variants);
}
}
}
function get_files($path, $pattern) {
$results = [];
$files = glob($path . DIRECTORY_SEPARATOR . $pattern);
foreach($files as $file) {
array_push($results, create_file_object($file));
}
return $results;
}
function get_file($path, $pattern) {
$result = get_files($path, $pattern);
if (count($result) > 0) {
return $result[0];
}
return;
}
function create_file_object($filename) {
return array(
'tmp_name' => $filename,
'name' => basename($filename),
'type' => mime_content_type($filename),
'length' => filesize($filename),
'error' => 0
);
}
function is_valid_transfer_id($id) {
return preg_match('/^[0-9a-fA-F]{32}$/', $id);
}
function get_transfer($path, $id) {
if (!is_valid_transfer_id($id)) return false;
$transfer = new Transfer($id);
$path = $path . DIRECTORY_SEPARATOR . $id;
$file = get_file($path, '*.*');
$metadata = get_file($path, METADATA_FILENAME);
$variants = get_files($path . DIRECTORY_SEPARATOR . VARIANTS_DIR, '*.*');
$transfer->restore($file, $variants, null, $metadata);
return $transfer;
}
function get_post($entry) {
return isset($_FILES[$entry]) || isset($_POST[$entry]) ? new Post($entry) : false;
}
function route_form_post($entries, $routes) {
// if a singly field entry is supplied, turn it into an array
if (is_string($entries)) $entries = array($entries);
foreach ($entries as $entry) {
$post = get_post($entry);
if (!$post) continue;
if (!isset($routes[$post->getFormat()])) continue;
call_user_func($routes[$post->getFormat()], $post->getValues());
}
}
function route_api_request($entries, $routes) {
// if a singly field entry is supplied, turn it into an array
if (is_string($entries)) $entries = array($entries);
// get the request method so we don't have to use $_SERVER each time
$request_method = $_SERVER['REQUEST_METHOD'];
// loop over all set entry fields to find posted values
foreach ($entries as $entry) {
// post new files
if ($request_method === 'POST') {
$post = get_post($entry);
if (!$post) continue;
$transfer = new Transfer();
$transfer->populate($entry);
return call_user_func($routes['FILE_TRANSFER'], $transfer);
}
// revert existing transfer
if ($request_method === 'DELETE') {
return call_user_func($routes['REVERT_FILE_TRANSFER'], file_get_contents('php://input'));
}
// fetch, load, restore
if ($request_method === 'GET' || $request_method === 'HEAD' || $request_method === 'PATCH') {
$handlers = array(
'fetch' => 'FETCH_REMOTE_FILE',
'restore' => 'RESTORE_FILE_TRANSFER',
'load' => 'LOAD_LOCAL_FILE',
'patch' => 'PATCH_FILE_TRANSFER'
);
foreach ($handlers as $param => $handler) {
if (isset($_GET[$param])) {
return call_user_func($routes[$handler], $_GET[$param], $entry);
}
}
}
}
}