-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin-attachments.php
363 lines (328 loc) · 11.9 KB
/
admin-attachments.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
require "lib/function.php";
admincheck();
$_GET['id'] = filter_int($_GET['id']);
$_GET['action'] = filter_string($_GET['action']);
$_POST['page'] = filter_int($_POST['page']);
$_GET['r'] = filter_int($_GET['r']);
if ($_GET['action'] == 'edit') {
if (isset($_POST['submit']) || isset($_POST['submit2'])) {
if ($_GET['id'] != -1 && !$sql->resultq("SELECT COUNT(*) FROM attachments WHERE id = {$_GET['id']}"))
errorpage("This attachment does not exist.");
$_POST['assign'] = filter_int($_POST['assign']);
// Determine if the ID is assigned to a post or PM
if (filter_int($_POST['type'])) {
$redir = "showprivate.php";
$post = 0;
$pm = $_POST['assign'];
$valid = (!$pm || $sql->resultq("SELECT COUNT(*) FROM pm_posts WHERE id = $pm"));
} else {
$redir = "thread.php";
$pm = 0;
$post = $_POST['assign'];
$valid = (!$post || $sql->resultq("SELECT COUNT(*) FROM posts WHERE id = $post"));
}
if (!$valid)
errorpage("Invalid post ID specified.");
// Upload / reupload
$file = filter_array($_FILES['upload']);
if (upload_error($file, true))
$file = NULL;
// If no file name is specified, use the one from the uploaded file.
if (!isset($_POST['filename'])) {
if (!$file) { // unless no new file is being uploaded
errorpage("No file name specified.");
}
$_POST['filename'] = str_replace(array("\r", "\n"), '', $file['name']);
}
if ($_GET['id'] == -1) {
if (!$file)
errorpage("No file selected.");
// Go through normal post procedure.
$key = "aac{$loguser['id']}";
$user = filter_int($_POST['user']);
$post = $_POST['assign'];
$flags = ($pm ? ATTACH_PM : 0);
upload_attachment($file, $key, $user, $post, $flags);
$ids = confirm_attachments($key, $user, $post, $flags);
$_GET['id'] = $ids[0]; // redirect to upload edit
$in = array(
'filename' => filter_string($_POST['filename']),
'post' => $post,
'pm' => $pm,
'user' => filter_int($_POST['user']),
);
} else {
if ($file) {
// We already know the ID of the file, so do an instant replace
$path = attachment_name($_GET['id']);
// Move it right away
move_uploaded_file($file['tmp_name'], $path);
$_POST['mime'] = mime_content_type($path);
$_POST['size'] = $file['size'];
list($width, $height) = getimagesize($path);
$_POST['is_image'] = ($width && $height);
}
$in = array(
'filename' => filter_string($_POST['filename']),
'post' => $post,
'pm' => $pm,
'mime' => filter_string($_POST['mime']),
'views' => filter_int($_POST['views']),
'size' => filter_int($_POST['size']),
'is_image' => filter_int($_POST['is_image']),
'user' => filter_int($_POST['user']),
);
}
$sql->queryp("UPDATE attachments SET ".mysql::setplaceholders($in)." WHERE id = {$_GET['id']}", $in);
if (isset($_POST['submit2'])) {
if ($_GET['r']) {
return header("Location: {$redir}?pid={$_POST['assign']}#{$_POST['assign']}");
} else {
return header("Location: ?");
}
} else {
return header("Location: ?id={$_GET['id']}&r={$_GET['r']}&action=edit");
}
}
$attach = $sql->fetchq("SELECT * FROM attachments WHERE id = {$_GET['id']}");
if (!$attach) {
$title = "Creating a new attachment";
$attach = array(
'filename' => '',
'post' => 0,
'pm' => 0,
'mime' => '', // 'text/plain',
'views' => 0,
'size' => 0,
'is_image' => 0,
'user' => 0,
);
$_GET['id'] = -1;
} else {
$title = "Editing an attachment";
}
pageheader($title);
print adminlinkbar();
?>
<form method="POST" action="?action=edit&id=<?= $_GET['id'] ?>&r=<?= $_GET['r'] ?>" enctype="multipart/form-data">
<table class="table">
<tr><td class="tdbgh center b" colspan="2"><?= $title ?></td></tr>
<tr>
<td class="tdbg1 center b">File name:</td>
<td class="tdbg2">
<input type="text" name="filename" style="width: 500px" value="<?= htmlspecialchars($attach['filename']) ?>">
<span class="fonts"> if (re)uploading the file, you can leave this blank to use the new file's name</span>
</td>
</tr>
<tr>
<td class="tdbg1 center b">Assigned to:</td>
<td class="tdbg2">
<label><input type="radio" name="type" value=0<?= $attach['post'] != 0 ? " checked" : ""?>> Post</label>
<label><input type="radio" name="type" value=1<?= $attach['pm'] != 0 ? " checked" : ""?>> PM</label>
<input type="text" name="assign" style="width: 100px" value="<?= max($attach['post'], $attach['pm']) ?>">
</td>
</tr>
<tr>
<td class="tdbg1 center b">Uploaded by:</td>
<td class="tdbg2"><?= user_select('user', $attach['user']) ?></td>
</tr>
<?php if ($_GET['id'] != -1) { ?>
<tr>
<td class="tdbg1 center b">MIME type:</td>
<td class="tdbg2"><?= mime_select('mime', $attach['mime']) ?></td>
</tr>
<tr>
<td class="tdbg1 center b">File size (bytes):</td>
<td class="tdbg2"><input type="text" name="size" style="width: 150px" value="<?= $attach['size'] ?>"></td>
</tr>
<tr>
<td class="tdbg1 center b">Downloads:</td>
<td class="tdbg2"><input type="text" name="views" style="width: 150px" value="<?= $attach['views'] ?>"></td>
</tr>
<tr>
<td class="tdbg1 center b">Options:</td>
<td class="tdbg2">
<label><input type="checkbox" name="is_image" value=1<?= $attach['is_image'] ? " checked" : ""?>> Display thumbnail</label>
</td>
</tr>
<?php } ?>
<tr>
<td class="tdbg1 center b">(Re)upload:</td>
<td class="tdbg2"><input type="file" name="upload"></td>
</tr>
<tr>
<td class="tdbg1 center b"></td>
<td class="tdbg2"><input type="submit" name="submit" value="Save and continue"> - <input type="submit" name="submit2" value="Save and close"></td>
</tr>
</table>
</form>
<?php
} else if ($_GET['action'] == 'delete') {
$data = $sql->fetchq("SELECT pm, post FROM attachments WHERE id = {$_GET['id']}");
if ($_GET['r']) {
$redirurl = ($data['pm'] ? "showprivate.php?pid={$data['pm']}#{$data['pm']}" : "thread.php?pid={$data['post']}#{$data['post']}");
$redirpage = ($data['pm'] ? "the private message" : "the post");
} else {
$redirurl = "?";
$redirpage = "the attachments page";
}
$message = "Are you sure you want to permanently <b>DELETE</b> this attachment?";
$form_link = "?action=delete&id={$_GET['id']}&r={$_GET['r']}";
$buttons = array(
0 => ["Yes"],
1 => ["No", $redirurl]
);
if (confirmpage($message, $form_link, $buttons)) {
remove_attachments(array($_GET['id']));
errorpage("The attachment has been deleted!", $redirurl, $redirpage);
}
} else {
pageheader("Attachments");
print adminlinkbar();
$_POST['type'] = filter_int($_POST['type']);
$_POST['filename'] = filter_string($_POST['filename']);
$_POST['mime'] = filter_string($_POST['mime']);
$_POST['thumb'] = filter_int($_POST['thumb']);
$ppp = 50;
$min = $_POST['page'] * $ppp;
$where = array();
$values = array();
switch ($_POST['type']) {
case 1: $where[] = "a.post != 0"; break;
case 2: $where[] = "a.pm != 0"; break;
case 3: $where[] = "(a.pm = 0 AND a.post = 0)"; break;
}
if ($_POST['filename']) {
$where[] = "a.filename LIKE ?";
$values[] = str_replace('*', '%', $_POST['filename']);
}
if ($_POST['mime']) {
$where[] = "a.mime LIKE ?";
$values[] = str_replace('*', '%', $_POST['mime']);
}
switch ($_POST['thumb']) {
case 1: $where[] = "a.is_image != 0"; break;
case 2: $where[] = "a.is_image = 0"; break;
}
if ($where)
$qwhere = "WHERE ".implode(' AND ', $where);
else
$qwhere = "";
$total = $sql->resultp("SELECT COUNT(*) FROM attachments a {$qwhere}", $values);
if ($min > $total) {
$_POST['page'] = floor($total / $ppp);
$min = ($ppp * $_POST['page']);
}
$pagelinks = page_select($total, $ppp);
$attachments = $sql->queryp("
SELECT a.*, $userfields uid
FROM attachments a
LEFT JOIN users u ON a.user = u.id
{$qwhere}
ORDER BY a.id ASC
LIMIT {$min},{$ppp}
", $values);
$txt = "";
$i = 0;
while ($x = $sql->fetch($attachments)) {
$cell = (++$i)%2+1;
if ($x['post']) {
$type = "<a href='thread.php?pid={$x['post']}#{$x['post']}'>Post</a>";
$typelink = "";
} else if ($x['pm']) {
$type = "<a href='showprivate.php?pid={$x['pm']}#{$x['pm']}'>PM</a>";
$typelink = "&pm";
} else {
$type = "-";
$typelink = "";
}
$txt .= "<tr>
<td class='tdbg{$cell} center fonts nobr'><a href='?id={$x['id']}&action=edit'>Edit</a> - <a href='?id={$x['id']}&action=delete'>Delete</a></td>
<td class='tdbg{$cell} center'>{$x['id']}</td>
<td class='tdbg{$cell} center'><a href='download.php?id={$x['id']}{$typelink}' target='_blank'>".htmlspecialchars($x['filename'])."</a></td>
<td class='tdbg{$cell} center'>{$x['mime']}</td>
<td class='tdbg{$cell} center'>".sizeunits($x['size'])."</td>
<td class='tdbg{$cell} center'>{$x['views']}</td>
<td class='tdbg{$cell} center'>".($x['is_image'] ? "Yes" : "No")."</td>
<td class='tdbg{$cell} center'>{$type}</td>
</tr>";
}
$sel_type[$_POST['type']] = ' checked';
$sel_thumb[$_POST['thumb']] = ' checked';
?>
<form method="POST" action="?">
<table class="table">
<tr><td class="tdbgh center b" colspan=2>Attachments</td></tr>
<tr>
<td class="tdbg1 center b">View from:</td>
<td class="tdbg2">
<label><input type="radio" name="type" value="0" <?= filter_string($sel_type[0]) ?>> All</label>
<label><input type="radio" name="type" value="1" <?= filter_string($sel_type[1]) ?>> Posts</label>
<label><input type="radio" name="type" value="2" <?= filter_string($sel_type[2]) ?>> PMs</label>
<label><input type="radio" name="type" value="3" <?= filter_string($sel_type[3]) ?>> Unassigned</label>
</td>
</tr>
<tr>
<td class="tdbg1 center b">File name:</td>
<td class="tdbg2">
<input type="text" name="filename" value="<?= htmlspecialchars($_POST['filename']) ?>" style="width: 450px">
<span class="fonts">use * as wildcard</span>
</td>
</tr>
<tr>
<td class="tdbg1 center b">MIME type:</td>
<td class="tdbg2"><?= mime_select('mime', $_POST['mime']) ?></td>
</tr>
<tr>
<td class="tdbg1 center b">Thumbnail option:</td>
<td class="tdbg2">
<label><input type="radio" name="thumb" value="0" <?= filter_string($sel_thumb[0]) ?>> All</label>
<label><input type="radio" name="thumb" value="1" <?= filter_string($sel_thumb[1]) ?>> With thumbnail</label>
<label><input type="radio" name="thumb" value="2" <?= filter_string($sel_thumb[2]) ?>> Without thumbnail</label>
</td>
</tr>
<tr>
<td class="tdbg1 center b">Page:</td>
<td class="tdbg2"><?= $pagelinks ?></td>
</tr>
<tr>
<td class="tdbg1 center b"></td>
<td class="tdbg2">
<input type="submit" name="submit" value="Submit">
</td>
</tr>
</table>
</form>
<br>
<table class="table">
<tr><td class='tdbgc center'><a href='?action=edit&id=-1'>< Upload a new attachment ></a></td></tr>
</table>
<br>
<table class="table">
<tr>
<td class='tdbgh center'></td>
<td class='tdbgh center'>#</td>
<td class='tdbgh center'>Filename</td>
<td class='tdbgh center'>MIME type</td>
<td class='tdbgh center'>Size</td>
<td class='tdbgh center'>Downloads</td>
<td class='tdbgh center'>Thumbnail</td>
<td class='tdbgh center'>Type</td>
</tr>
<?= $txt ?>
</table>
<?php
}
pagefooter();
function mime_select($name, $sel = "") {
$datalist = "";
$h = fopen('mime.types', 'r');
while (($line = fgets($h)) !== false) {
if ($line[0] != '#' && preg_match("/(.*?)\s/", $line, $match)) {
$datalist .= "<option value=\"{$match[1]}\">\n";
}
}
return "<input type='text' name='{$name}' list='{$name}list' style='width: 300px' value=\"". htmlspecialchars($sel) ."\">\n".
"<datalist id='{$name}list'>{$datalist}</datalist>";
}