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
|
<?php
class uploads {
protected static $allowedExtensions = [
'jpg', 'png', 'git', 'mp4', 'mp3', 'ogg', 'diff', 'txt', 'gz', 'tar',
'icc', 'icm', 'patch', 'zip', 'brd', 'pdf', 'lua', 'xpi', 'rar', '7z',
'tgz', 'bin', 'py', 'pac',
];
public static function getCount(): int {
$db = getDb();
return (int)$db->result($db->query("SELECT COUNT(*) FROM uploads"));
}
public static function isExtensionAllowed(string $ext): bool {
return in_array($ext, self::$allowedExtensions);
}
public static function add(string $tmp_name, string $name, string $note): ?int {
global $config;
$name = sanitize_filename($name);
if (!$name)
$name = 'file';
$random_id = self::getNewRandomId();
$size = filesize($tmp_name);
$is_image = detect_image_type($tmp_name) !== false;
$image_w = 0;
$image_h = 0;
if ($is_image) {
list($image_w, $image_h) = getimagesize($tmp_name);
}
$db = getDb();
if (!$db->insert('uploads', [
'random_id' => $random_id,
'ts' => time(),
'name' => $name,
'size' => $size,
'image' => (int)$is_image,
'image_w' => $image_w,
'image_h' => $image_h,
'note' => $note,
'downloads' => 0,
])) {
return null;
}
$id = $db->insertId();
$dir = $config['uploads_dir'].'/'.$random_id;
$path = $dir.'/'.$name;
mkdir($dir);
chmod($dir, 0775); // g+w
rename($tmp_name, $path);
chmod($path, 0664); // g+w
return $id;
}
public static function delete(int $id): bool {
$upload = self::get($id);
if (!$upload)
return false;
$db = getDb();
$db->query("DELETE FROM uploads WHERE id=?", $id);
rrmdir($upload->getDirectory());
return true;
}
/**
* @return Upload[]
*/
public static function getAll(): array {
$db = getDb();
$q = $db->query("SELECT * FROM uploads ORDER BY id DESC");
return array_map('Upload::create_instance', $db->fetchAll($q));
}
public static function get(int $id): ?Upload {
$db = getDb();
$q = $db->query("SELECT * FROM uploads WHERE id=?", $id);
if ($db->numRows($q)) {
return new Upload($db->fetch($q));
} else {
return null;
}
}
/**
* @param string[] $ids
* @param bool $flat
* @return Upload[]
*/
public static function getUploadsByRandomId(array $ids, bool $flat = false): array {
if (empty($ids)) {
return [];
}
$db = getDb();
$uploads = array_fill_keys($ids, null);
$q = $db->query("SELECT * FROM uploads WHERE random_id IN('".implode('\',\'', array_map([$db, 'escape'], $ids))."')");
while ($row = $db->fetch($q)) {
$uploads[$row['random_id']] = new Upload($row);
}
if ($flat) {
$list = [];
foreach ($ids as $id) {
$list[] = $uploads[$id];
}
unset($uploads);
return $list;
}
return $uploads;
}
public static function getByRandomId(string $random_id): ?Upload {
$db = getDb();
$q = $db->query("SELECT * FROM uploads WHERE random_id=? LIMIT 1", $random_id);
if ($db->numRows($q)) {
return new Upload($db->fetch($q));
} else {
return null;
}
}
protected static function getNewRandomId(): string {
$db = getDb();
do {
$random_id = strgen(8);
} while ($db->numRows($db->query("SELECT id FROM uploads WHERE random_id=?", $random_id)) > 0);
return $random_id;
}
}
|