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
|
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/../config.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/utils.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/thumbnails.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/file.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/alert.php';
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
generate_alert(
'/',
"Method not allowed",
405,
null
);
exit;
}
if (!is_dir(FILE_UPLOAD_DIRECTORY) && !mkdir(FILE_UPLOAD_DIRECTORY, 0777, true)) {
generate_alert(
'/',
"Failed to create a directory for user files",
500,
null
);
exit();
}
try {
$preserve_original_name = boolval($_POST['preserve_original_name'] ?? '0');
$title = str_safe($_POST['title'] ?? '', FILE_TITLE_MAX_LENGTH);
if (empty(trim($title))) {
$title = null;
}
$url = isset($_POST['url']) ? $_POST['url'] ?: null : null;
$file = isset($_FILES['file']) ? $_FILES['file'] ?: null : null;
if (empty($file['tmp_name'])) {
$file = null;
}
$paste = isset($_POST['paste']) ? $_POST['paste'] ?: null : null;
$file_data = null;
if (!(isset($file) ^ isset($url) ^ isset($paste)) || (isset($file) && isset($url) && isset($paste))) {
throw new RuntimeException('You can upload only one type of content: file, URL or text');
}
if (FILEEXT_ENABLED && isset($url) && !empty($url)) {
$output = [];
exec('yt-dlp -f "worst" --get-filename -o "%(filesize_approx)s %(ext)s %(duration)s" ' . escapeshellarg($url) . '', $output);
if (empty($output)) {
throw new RuntimeException('Bad URL');
}
$output = explode(' ', $output[0]);
// TODO: some videos don't have duration
$duration = intval($output[2]);
if ($duration > FILEEXT_MAX_DURATION) {
throw new RuntimeException(sprintf("File must be under %d minutes", FILEEXT_MAX_DURATION / 60));
}
if (!array_key_exists($output[1], FILE_ACCEPTED_MIME_TYPES)) {
throw new RuntimeException("Unsupported extension: {$output[1]}");
}
$file_data = [
'size' => intval($output[0]),
'mime' => FILE_ACCEPTED_MIME_TYPES[$output[1]],
'extension' => $output[1]
];
} else if (isset($paste)) {
$file_data = [
'size' => strlen($paste),
'mime' => 'text/plain',
'extension' => 'txt'
];
} else if (isset($file)) {
if (
!isset($file['error']) ||
is_array($file['error'])
) {
throw new RuntimeException('Invalid parameters.');
}
// checking file error
switch ($file['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
// checking file mimetype
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $file_ext = array_search($finfo->file($file['tmp_name']), FILE_ACCEPTED_MIME_TYPES, true)) {
throw new RuntimeException("Invalid file format.");
}
// verifying file mimetype
$file_mime = FILE_ACCEPTED_MIME_TYPES[$file_ext];
$is_media = str_starts_with($file_mime, 'image/') || str_starts_with($file_mime, 'video/') || str_starts_with($file_mime, 'audio/');
if (FILE_VERIFY_MIMETYPE && $is_media && !verify_mimetype($file['tmp_name'], $file_mime)) {
throw new RuntimeException('Invalid file format.');
}
// striping exif data
if (FILE_STRIP_EXIF && $is_media && !strip_exif($file['tmp_name'])) {
throw new RuntimeException('Failed to strip EXIF tags.');
}
$file_data = [
'size' => $file['size'],
'mime' => $file_mime,
'extension' => $file_ext
];
}
if (!$file_data) {
throw new RuntimeException('No URL or file specified');
}
$file_id_length = FILE_ID_LENGTH;
$file_id_gen_attempts = 0;
do {
$file_id = FILE_ID_PREFIX . generate_random_char_sequence(FILE_ID_CHARACTERS, $file_id_length);
if ($file_id_gen_attempts > 20) {
$file_id_length++;
$file_id_gen_attempts = 0;
}
$file_id_gen_attempts++;
} while (is_file(FILE_UPLOAD_DIRECTORY . "/{$file_id}.{$file_data['extension']}"));
$file_data['id'] = $file_id;
if (isset($url)) {
$result = 0;
$output = [];
$file_path = FILE_UPLOAD_DIRECTORY . "/$file_id.{$file_data['extension']}";
exec(sprintf(
'yt-dlp -f "worst" -o "%s" %s 2>&1',
$file_path,
escapeshellarg($url)
), $output, $result);
if ($result != 0) {
error_log(sprintf("Failed to download a file (URL: %s): %s", $url, implode('\n', $output)));
throw new RuntimeException('Failed to download a file! Try again later.');
}
// verifying file mime type
$file_mime = $file_data['mime'];
$is_media = str_starts_with($file_mime, 'image/') || str_starts_with($file_mime, 'video/') || str_starts_with($file_mime, 'audio/');
if (FILE_VERIFY_MIMETYPE && $is_media && !verify_mimetype($file_path, $file_mime)) {
delete_file($file_id, $file_data['extension']);
throw new RuntimeException('Invalid file format.');
}
} else if (isset($paste) && !file_put_contents(FILE_UPLOAD_DIRECTORY . sprintf('/%s.%s', $file_id, $file_data['extension']), $paste)) {
throw new RuntimeException('Failed to paste a text! Try again later.');
} else if (isset($file) && !move_uploaded_file($file['tmp_name'], FILE_UPLOAD_DIRECTORY . sprintf('/%s.%s', $file_id, $file_data['extension']))) {
throw new RuntimeException("Failed to save the file. Try again later.");
}
if (FILE_THUMBNAILS && !is_dir(FILE_THUMBNAIL_DIRECTORY) && !mkdir(FILE_THUMBNAIL_DIRECTORY, 0777, true)) {
throw new RuntimeException('Failed to create a directory for thumbnails');
}
if (
FILE_THUMBNAILS && (
(
str_starts_with($file_data['mime'], 'image/') &&
$thumbnail_error = generate_image_thumbnail(
FILE_UPLOAD_DIRECTORY . "/{$file_id}.{$file_data['extension']}",
FILE_THUMBNAIL_DIRECTORY . "/{$file_id}.webp",
FILE_THUMBNAIL_SIZE[0],
FILE_THUMBNAIL_SIZE[1]
)
) ||
(
str_starts_with($file_data['mime'], 'video/') &&
$thumbnail_error = generate_video_thumbnail(
FILE_UPLOAD_DIRECTORY . "/{$file_id}.{$file_data['extension']}",
FILE_THUMBNAIL_DIRECTORY . "/{$file_id}",
FILE_THUMBNAIL_DIRECTORY . "/{$file_id}.webp",
FILE_THUMBNAIL_SIZE[0],
FILE_THUMBNAIL_SIZE[1]
)
)
)
) {
throw new RuntimeException("Failed to create a thumbnail (Error code {$thumbnail_error})");
}
$file_data['urls'] = [
'download_url' => INSTANCE_URL . "/{$file_data['id']}.{$file_data['extension']}"
];
if (FILE_METADATA && FILE_DELETION) {
$file_data['password'] = $_POST['password'] ?? generate_random_char_sequence(FILE_ID_CHARACTERS, FILE_DELETION_KEY_LENGTH);
$file_data['urls']['deletion_url'] = INSTANCE_URL . "/delete.php?f={$file_data['id']}.{$file_data['extension']}&key={$file_data['password']}";
}
generate_alert(
"/{$file_data['id']}.{$file_data['extension']}",
null,
201,
$file_data
);
if (FILE_METADATA) {
unset($file_data['urls']);
$file_data['password'] = password_hash($file_data['password'], PASSWORD_DEFAULT);
$file_data['views'] = 0;
$file_data['uploaded_at'] = time();
if ($title) {
$file_data['original_name'] = $title;
}
if ($preserve_original_name && !$title) {
if ($file && !empty($file['name'])) {
$file_data['original_name'] = $file['name'];
} else if ($url) {
$file_data['original_name'] = $url;
}
}
if (!is_dir(FILE_METADATA_DIRECTORY) && !mkdir(FILE_METADATA_DIRECTORY, 0777, true)) {
throw new RuntimeException('Failed to create a folder for file metadata');
}
if (!file_put_contents(FILE_METADATA_DIRECTORY . "/{$file_data['id']}.metadata.json", json_encode($file_data, JSON_UNESCAPED_SLASHES))) {
throw new RuntimeException('Failed to create a file metadata');
}
}
} catch (RuntimeException $e) {
generate_alert(
"/",
$e->getMessage(),
400
);
}
|