-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_file.php
More file actions
210 lines (192 loc) · 9.52 KB
/
Copy pathedit_file.php
File metadata and controls
210 lines (192 loc) · 9.52 KB
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
<?php
require_once __DIR__ . '/includes/auth.php';
init_session();
require_login();
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/access_control.php';
require_once __DIR__ . '/includes/settings_loader.php';
$settings = datadock_load_settings();
require_once __DIR__ . '/includes/read_only.php';
datadock_block_if_read_only($settings);
$autoDeleteDurations = [
'1_minute' => '+1 minute',
'30_minutes' => '+30 minutes',
'1_hour' => '+1 hour',
'6_hours' => '+6 hours',
'1_day' => '+1 day',
'1_week' => '+1 week',
'1_month' => '+1 month',
'1_year' => '+1 year',
'never' => null,
];
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
$_SESSION['flash_error'][] = "❌ Invalid file.";
header("Location: dashboard.php");
exit;
}
$fileId = (int) $_GET['id'];
$userId = $_SESSION['user_id'];
$isAdmin = ($_SESSION['role'] ?? '') === 'admin';
if ($isAdmin) {
$stmt = $pdo->prepare("SELECT * FROM files WHERE id = ? AND deleted_at IS NULL");
$stmt->execute([$fileId]);
} else {
$stmt = $pdo->prepare("SELECT * FROM files WHERE id = ? AND user_id = ? AND deleted_at IS NULL");
$stmt->execute([$fileId, $userId]);
}
$file = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$file) {
$_SESSION['flash_error'][] = "❌ File not found or permission denied.";
header("Location: dashboard.php");
exit;
}
$tagsEnabled = !isset($settings['tags_enabled']) || !empty($settings['tags_enabled']);
$ipAllowDisplay = '';
if (!empty($file['ip_allowlist'])) {
$decoded = json_decode((string) $file['ip_allowlist'], true);
if (is_array($decoded)) {
$ipAllowDisplay = implode("\n", $decoded);
}
}
$fileTagsStr = '';
if ($tagsEnabled) {
$stmt = $pdo->prepare('SELECT t.name FROM tags t INNER JOIN file_tags ft ON ft.tag_id = t.id WHERE ft.file_id = ? ORDER BY t.name');
$stmt->execute([$fileId]);
$fileTagsStr = implode(', ', $stmt->fetchAll(PDO::FETCH_COLUMN));
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$originalName = trim($_POST['original_name'] ?? '');
$description = trim($_POST['description'] ?? '');
$duration = $_POST['duration'] ?? 'never';
if ($originalName === '') {
$_SESSION['flash_error'][] = "❌ Filename cannot be empty.";
} else {
if (strlen($originalName) > 255) {
$_SESSION['flash_error'][] = "❌ Filename is too long.";
}
if (strlen($description) > 500) {
$_SESSION['flash_error'][] = "❌ Description is too long (max 500 characters).";
}
}
if ($duration === 'keep' || !isset($autoDeleteDurations[$duration])) {
if ($duration !== 'keep') {
$duration = 'never';
}
}
$expiryDate = null;
if ($duration === 'keep') {
$expiryDate = $file['expiry_date'];
} elseif ($autoDeleteDurations[$duration] !== null) {
$nowUTC = new DateTime('now', new DateTimeZone('UTC'));
$expiryDate = (clone $nowUTC)->modify($autoDeleteDurations[$duration])->format('Y-m-d H:i:s');
}
$clearAccessPw = !empty($_POST['clear_access_password']);
$newAccessPw = (string) ($_POST['access_password_new'] ?? '');
$ipAllowText = (string) ($_POST['ip_allowlist'] ?? '');
$accessPasswordHash = $file['access_password_hash'] ?? null;
if ($clearAccessPw) {
$accessPasswordHash = null;
} elseif ($newAccessPw !== '') {
if (strlen($newAccessPw) < 4) {
$_SESSION['flash_error'][] = '❌ Download password must be at least 4 characters (or leave blank to keep unchanged).';
} else {
$accessPasswordHash = password_hash($newAccessPw, PASSWORD_DEFAULT);
}
}
$ipAllowJson = datadock_parse_ip_allowlist_text($ipAllowText);
if (strlen($ipAllowJson) > 8000) {
$_SESSION['flash_error'][] = '❌ IP allow list is too long.';
}
if (empty($_SESSION['flash_error'])) {
if ($isAdmin) {
$stmt = $pdo->prepare("UPDATE files SET original_name = ?, description = ?, expiry_date = ?, access_password_hash = ?, ip_allowlist = ? WHERE id = ?");
$stmt->execute([$originalName, $description === '' ? null : $description, $expiryDate, $accessPasswordHash, $ipAllowJson === '[]' ? null : $ipAllowJson, $fileId]);
} else {
$stmt = $pdo->prepare("UPDATE files SET original_name = ?, description = ?, expiry_date = ?, access_password_hash = ?, ip_allowlist = ? WHERE id = ? AND user_id = ?");
$stmt->execute([$originalName, $description === '' ? null : $description, $expiryDate, $accessPasswordHash, $ipAllowJson === '[]' ? null : $ipAllowJson, $fileId, $userId]);
}
if ($tagsEnabled) {
$tagOwner = (int) ($file['user_id'] ?? $userId);
if ($tagOwner > 0) {
datadock_sync_file_tags($pdo, $fileId, $tagOwner, (string) ($_POST['tags'] ?? ''));
}
}
$_SESSION['flash_success'][] = "✅ File metadata updated.";
header("Location: dashboard.php");
exit;
}
}
$pageTitle = "Edit file";
require_once __DIR__ . '/includes/header.php';
?>
<div class="page-section">
<h2 class="page-title">Edit file</h2>
<p class="page-description">Change display name, description, or expiry. The file on disk is not modified.</p>
<form method="post" action="edit_file.php?id=<?= (int)$fileId ?>" class="settings-card" style="max-width:32rem;">
<div class="settings-card-body">
<div class="form-group">
<label for="original_name">Display name</label>
<input type="text" name="original_name" id="original_name" value="<?= sanitize_data($file['original_name'] ?? '') ?>" maxlength="255" required>
</div>
<div class="form-group">
<label for="description">Description <span class="label-optional">(optional, max 500 characters)</span></label>
<textarea name="description" id="description" rows="3" maxlength="500"><?= sanitize_data($file['description'] ?? '') ?></textarea>
</div>
<?php if ($tagsEnabled && !empty($file['user_id'])): ?>
<div class="form-group">
<label for="tags">Tags <span class="label-optional">(optional, comma-separated)</span></label>
<input type="text" name="tags" id="tags" value="<?= sanitize_data($fileTagsStr) ?>" maxlength="2000" placeholder="e.g. work, reference">
</div>
<?php endif; ?>
<div class="form-group">
<label>Download password <span class="label-optional">(optional gate for public / shared links)</span></label>
<?php if (!empty($file['access_password_hash'])): ?>
<p class="form-hint">A password is set. Enter a new password below to replace it, or check "Remove download password".</p>
<?php endif; ?>
<input type="password" name="access_password_new" id="access_password_new" autocomplete="new-password" maxlength="128" placeholder="<?= !empty($file['access_password_hash']) ? 'New password (optional)' : 'Leave empty to keep unchanged' ?>">
<div class="settings-row-checkbox" style="margin-top:0.5rem;">
<label>
<input type="checkbox" name="clear_access_password" value="1" id="clear_access_password">
Remove download password
</label>
</div>
</div>
<div class="form-group">
<label for="ip_allowlist">Restrict download by IP <span class="label-optional">(optional, one IPv4/IPv6 or CIDR per line)</span></label>
<textarea name="ip_allowlist" id="ip_allowlist" rows="4" maxlength="8000" placeholder="e.g. 203.0.113.10 192.168.0.0/24"><?= sanitize_data($ipAllowDisplay) ?></textarea>
<small class="form-hint">Applies to anonymous downloads (public listing, token links, signed links). Logged-in owners are not restricted. Empty = no IP restriction.</small>
</div>
<div class="form-group">
<label for="duration">Expires</label>
<select name="duration" id="duration">
<option value="keep"<?= $file['expiry_date'] !== null ? ' selected' : '' ?>>Keep current</option>
<?php foreach ($autoDeleteDurations as $key => $offset): ?>
<option value="<?= sanitize_data($key) ?>"<?= $file['expiry_date'] === null && $key === 'never' ? ' selected' : '' ?>>
<?= ucwords(str_replace('_', ' ', $key)) ?>
</option>
<?php endforeach; ?>
</select>
<?php if ($file['expiry_date']): ?>
<small class="form-hint">Current expiry: <span class="utc-datetime" data-utc="<?= sanitize_data($file['expiry_date']) ?>"></span></small>
<?php endif; ?>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save changes</button>
<a href="dashboard.php" class="btn btn-small">Cancel</a>
</div>
</div>
</form>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('.utc-datetime').forEach(el => {
const utc = el.dataset.utc;
if (utc) {
const local = new Date(utc + ' UTC');
el.textContent = local.toLocaleString();
}
});
});
</script>
<?php require_once __DIR__ . '/includes/footer.php'; ?>