<?php
if (!defined('HOBBES')) { http_response_code(403); exit; }
require_role('editor');
$id = $route_params['id'] ?? '';
$meta = $id ? file_meta_load($id) : null;
if (!$meta || empty($meta['approved'])) {
flash('error', 'File not found.');
redirect('/browse');
}
$cats = categories_load();
// ── POST: save changes ────────────────────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
csrf_check();
$old_category = $meta['category'] ?? '';
$new_category = trim($_POST['category'] ?? '');
$old_path = file_physical_path($meta);
$meta['title'] = trim($_POST['title'] ?? $meta['title']);
$meta['description'] = trim($_POST['description'] ?? $meta['description']);
$meta['version'] = trim($_POST['version'] ?? '');
$meta['author'] = trim($_POST['author'] ?? '');
$meta['homepage'] = trim($_POST['homepage'] ?? '');
$meta['category'] = $new_category;
$meta['os2_version'] = trim($_POST['os2_version'] ?? '');
$meta['tags'] = trim($_POST['tags'] ?? '');
$meta['license'] = trim($_POST['license'] ?? '');
$meta['requirements'] = trim($_POST['requirements'] ?? '');
// Move physical file if category changed
if ($old_category !== $new_category && file_exists($old_path)) {
$new_cat_path = category_upload_path($new_category);
$new_dir = UPLOADS_DIR . '/' . $new_cat_path;
if (!is_dir($new_dir)) mkdir($new_dir, 0755, true);
$new_file = $new_dir . '/' . $meta['original_name'];
if (file_exists($new_file)) {
flash('error', 'A file named "' . h($meta['original_name']) . '" already exists in the target category. Category not changed.');
$meta['category'] = $old_category;
} elseif (@rename($old_path, $new_file)) {
$meta['stored_name'] = $new_cat_path . '/' . $meta['original_name'];
// Clean up legacy empty per-file id directory
if (!str_contains($old_path, UPLOADS_DIR . '/' . $old_category)) {
$old_dir = dirname($old_path);
if ($old_dir !== UPLOADS_DIR && is_dir($old_dir) && !glob($old_dir . '/*')) {
@rmdir($old_dir);
}
}
} else {
flash('error', 'Failed to move the file on disk. Check server permissions. Category not changed.');
$meta['category'] = $old_category;
}
}
file_meta_save($meta);
search_index_file($meta);
flash('success', 'File updated.');
redirect(file_url($meta));
}
// ── GET: show form ────────────────────────────────────────────────────────────
$page_title = 'Edit: ' . h($meta['original_name']);
$_page = 'browse';
include ROOT_DIR . '/templates/header.php';
?>
<div class="panel">
<div class="panel-title">Edit File — <?php echo h($meta['original_name']); ?></div>
<div class="panel-body">
<form method="post" action="/file/edit/<?php echo h($meta['id']); ?>" class="std">
<?php echo csrf_field(); ?>
<label>Title *
<input type="text" name="title" value="<?php echo h($meta['title'] ?? ''); ?>" required maxlength="300">
</label>
<label>Description
<textarea name="description"><?php echo h($meta['description'] ?? ''); ?></textarea>
</label>
<label>Category
<select name="category">
<option value="">-- Uncategorized --</option>
<?php render_cat_options(build_category_tree($cats), $meta['category'] ?? ''); ?>
</select>
</label>
<label>Version
<input type="text" name="version" value="<?php echo h($meta['version'] ?? ''); ?>" maxlength="100">
</label>
<label>Author / Vendor
<input type="text" name="author" value="<?php echo h($meta['author'] ?? ''); ?>" maxlength="200">
</label>
<label>Homepage
<input type="url" name="homepage" value="<?php echo h($meta['homepage'] ?? ''); ?>" maxlength="500">
</label>
<label>Compatible OS/2 Version(s)
<input type="text" name="os2_version" value="<?php echo h($meta['os2_version'] ?? ''); ?>" maxlength="100" placeholder="e.g. Warp 4, eCS 2.x, ArcaOS 5.x">
</label>
<label>License
<input type="text" name="license" value="<?php echo h($meta['license'] ?? ''); ?>" maxlength="100">
</label>
<label>Requirements / Dependencies
<input type="text" name="requirements" value="<?php echo h($meta['requirements'] ?? ''); ?>" maxlength="300">
</label>
<label>Tags <span class="muted">(comma-separated)</span>
<input type="text" name="tags" value="<?php echo h($meta['tags'] ?? ''); ?>" maxlength="300">
</label>
<div class="row">
<button type="submit" class="btn btn-primary">Save Changes</button>
<a href="<?php echo h(file_url($meta)); ?>" class="btn">Cancel</a>
</div>
</form>
<?php if (can('admin')): ?>
<hr style="margin:18px 0;">
<div>
<strong style="color:#8b0000;">Danger Zone</strong>
<form method="post" action="/admin/file-delete/<?php echo h($meta['id']); ?>"
style="display:inline;margin-left:12px;"
onsubmit="return confirm('Permanently delete "<?php echo addslashes($meta['original_name']); ?>" and all its metadata?\n\nThis cannot be undone.');">
<?php echo csrf_field(); ?>
<button type="submit" class="btn btn-danger">Delete This File</button>
</form>
<span class="muted" style="margin-left:8px;font-size:90%;">Admin only — removes file from disk and archive.</span>
</div>
<?php endif; ?>
</div>
</div>
<?php include ROOT_DIR . '/templates/footer.php'; ?>