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
|
<?php
namespace handler\admin;
use csrf;
use RedirectResponse;
use Response;
// So it's 2022 outside, and it's PHP 8.1 already, which is actually so cool comparing to 5.x and even 7.4, but...
// ...class names are still case-insensitive?!! And I can't import \uploads because it's the same as Uploads?!!
//
// PHP, what the fuck is wrong with you?!
class Uploads extends AdminRequestHandler {
public function get(): Response {
list($error) = $this->input('error');
$uploads = \uploads::getAll();
$this->skin->title = ($this->lang)('blog_upload');
return $this->skin->renderPage('admin/uploads',
error: $error,
uploads: $uploads);
}
public function post(): Response {
csrf::check('addupl');
list($custom_name, $note) = $this->input('name, note');
if (!isset($_FILES['files']))
return new RedirectResponse('/uploads/?error='.urlencode('no file'));
$files = [];
for ($i = 0; $i < count($_FILES['files']['name']); $i++) {
$files[] = [
'name' => $_FILES['files']['name'][$i],
'type' => $_FILES['files']['type'][$i],
'tmp_name' => $_FILES['files']['tmp_name'][$i],
'error' => $_FILES['files']['error'][$i],
'size' => $_FILES['files']['size'][$i],
];
}
if (count($files) > 1) {
$note = '';
$custom_name = '';
}
foreach ($files as $f) {
if ($f['error'])
return new RedirectResponse('/uploads/?error='.urlencode('error code '.$f['error']));
if (!$f['size'])
return new RedirectResponse('/uploads/?error='.urlencode('received empty file'));
$ext = extension($f['name']);
if (!\uploads::isExtensionAllowed($ext))
return new RedirectResponse('/uploads/?error='.urlencode('extension not allowed'));
$upload_id = \uploads::add(
$f['tmp_name'],
$custom_name ?: $f['name'],
$note);
if (!$upload_id)
return new RedirectResponse('/uploads/?error='.urlencode('failed to create upload'));
}
return new RedirectResponse('/uploads/');
}
}
|