feat: Option „Ganzes System (ab /)“
- Neue Einstellung zeigt zusätzlich zu /mnt/user das Dateisystem ab / (Bestätigungsdialog beim Aktivieren, U_NAV_ROOTS hat Vorrang). - Geschützt vor Löschen/Verschieben/Rechteänderung: Wurzeln, Ordner direkt unter /, Mountpunkte unter /mnt. - /proc, /sys, /dev, /run: nur anzeigen; keine Jobs, Downloads, Vorschau oder Rekursion hinein. - Größe von Systemordnern nur auf Knopfdruck (weckt sonst alle Disks). - Pfade unter / ohne doppelten Schrägstrich; kaputte Symlinks erzeugen keine PHP-Warnungen mehr in der Antwort. - Paket 2026.11.21.r005. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a7e8325853
commit
0592a02c8e
17 files changed
+196
-43
No files matched your search
@@ -4,6 +4,9 @@ const UNAV_DEFAULT_ROOT = '/mnt/user';
|
||||
const UNAV_JOB_DIR = '/tmp/u-navigator/jobs';
|
||||
const UNAV_SETTINGS_FILE = '/boot/config/plugins/u-navigator/settings.json';
|
||||
const UNAV_MAX_FAVORITES = 50;
|
||||
const UNAV_SYSTEM_ROOT = '/';
|
||||
// Kernel/virtual filesystems: never walked recursively (endless or device files).
|
||||
const UNAV_PSEUDO_FS = ['/proc', '/sys', '/dev', '/run'];
|
||||
|
||||
function unav_settings_file(): string {
|
||||
return getenv('U_NAV_SETTINGS_FILE') ?: UNAV_SETTINGS_FILE;
|
||||
@@ -23,6 +26,7 @@ function unav_settings(): array {
|
||||
'readOnly' => true,
|
||||
'debug' => false,
|
||||
'thumbnails' => true,
|
||||
'systemRoot' => false,
|
||||
'favorites' => [],
|
||||
];
|
||||
$file = unav_settings_file();
|
||||
@@ -37,6 +41,7 @@ function unav_settings(): array {
|
||||
'readOnly' => filter_var($settings['readOnly'] ?? $defaults['readOnly'], FILTER_VALIDATE_BOOLEAN),
|
||||
'debug' => filter_var($settings['debug'] ?? $defaults['debug'], FILTER_VALIDATE_BOOLEAN),
|
||||
'thumbnails' => filter_var($settings['thumbnails'] ?? $defaults['thumbnails'], FILTER_VALIDATE_BOOLEAN),
|
||||
'systemRoot' => filter_var($settings['systemRoot'] ?? $defaults['systemRoot'], FILTER_VALIDATE_BOOLEAN),
|
||||
'favorites' => unav_clean_favorites($settings['favorites'] ?? [], false),
|
||||
]);
|
||||
}
|
||||
@@ -84,6 +89,7 @@ function unav_settings_write(array $settings): array {
|
||||
'readOnly' => filter_var($settings['readOnly'] ?? $current['readOnly'], FILTER_VALIDATE_BOOLEAN),
|
||||
'debug' => filter_var($settings['debug'] ?? $current['debug'], FILTER_VALIDATE_BOOLEAN),
|
||||
'thumbnails' => filter_var($settings['thumbnails'] ?? $current['thumbnails'], FILTER_VALIDATE_BOOLEAN),
|
||||
'systemRoot' => filter_var($settings['systemRoot'] ?? $current['systemRoot'], FILTER_VALIDATE_BOOLEAN),
|
||||
'favorites' => array_key_exists('favorites', $settings)
|
||||
? unav_clean_favorites($settings['favorites'], true)
|
||||
: $current['favorites'],
|
||||
@@ -94,14 +100,39 @@ function unav_settings_write(array $settings): array {
|
||||
unav_error(500, 'Could not create settings directory');
|
||||
}
|
||||
file_put_contents($file, json_encode($next, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), LOCK_EX);
|
||||
unav_roots(true);
|
||||
return $next;
|
||||
}
|
||||
|
||||
function unav_roots(): array {
|
||||
$configured = getenv('U_NAV_ROOTS') ?: UNAV_DEFAULT_ROOT;
|
||||
$roots = array_values(array_filter(array_map('trim', explode(',', $configured))));
|
||||
function unav_default_root(): string {
|
||||
return getenv('U_NAV_DEFAULT_ROOT') ?: UNAV_DEFAULT_ROOT;
|
||||
}
|
||||
|
||||
// U_NAV_ROOTS (if set) fixes the roots; otherwise the user shares, plus "/" when the
|
||||
// "whole system" setting is enabled.
|
||||
function unav_roots_from_env(): bool {
|
||||
return (string)getenv('U_NAV_ROOTS') !== '';
|
||||
}
|
||||
|
||||
function unav_roots(bool $reset = false): array {
|
||||
static $cache = null;
|
||||
if ($reset) {
|
||||
$cache = null;
|
||||
return [];
|
||||
}
|
||||
if ($cache !== null) {
|
||||
return $cache;
|
||||
}
|
||||
if (unav_roots_from_env()) {
|
||||
$roots = array_values(array_filter(array_map('trim', explode(',', (string)getenv('U_NAV_ROOTS')))));
|
||||
} else {
|
||||
$roots = [unav_default_root()];
|
||||
if (unav_settings()['systemRoot']) {
|
||||
$roots[] = UNAV_SYSTEM_ROOT;
|
||||
}
|
||||
}
|
||||
if (!$roots) {
|
||||
$roots = [UNAV_DEFAULT_ROOT];
|
||||
$roots = [unav_default_root()];
|
||||
}
|
||||
|
||||
$resolved = [];
|
||||
@@ -119,9 +150,45 @@ function unav_roots(): array {
|
||||
if (!$resolved) {
|
||||
unav_error(500, 'No usable U-Navigator root found');
|
||||
}
|
||||
$cache = $resolved;
|
||||
return $resolved;
|
||||
}
|
||||
|
||||
// Joins a directory and a name without producing "//name" when the directory is "/".
|
||||
function unav_join(string $dir, string $name): string {
|
||||
return rtrim($dir, '/') . '/' . ltrim($name, '/');
|
||||
}
|
||||
|
||||
function unav_is_pseudo_fs(string $path): bool {
|
||||
foreach (UNAV_PSEUDO_FS as $pseudo) {
|
||||
if ($path === $pseudo || str_starts_with($path, $pseudo . '/')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Reading files below /proc, /sys, /dev or /run can block forever or return endless data.
|
||||
function unav_assert_not_pseudo_fs(string $path): void {
|
||||
if (unav_is_pseudo_fs($path)) {
|
||||
unav_error(400, 'Files in /proc, /sys, /dev and /run cannot be opened here');
|
||||
}
|
||||
}
|
||||
|
||||
// Roots, top-level system folders (/boot, /etc, /usr, ...) and Unraid mount points
|
||||
// (/mnt/user, /mnt/disk1, /mnt/cache, ...) must never be moved, deleted or re-permissioned.
|
||||
function unav_is_protected(string $real): bool {
|
||||
if (unav_is_root($real)) {
|
||||
return true;
|
||||
}
|
||||
$trimmed = rtrim($real, '/');
|
||||
if ($trimmed === '' || dirname($trimmed) === '/') {
|
||||
return true;
|
||||
}
|
||||
$mnt = unav_mnt_dir();
|
||||
return $trimmed === $mnt || dirname($trimmed) === $mnt || unav_is_pseudo_fs($trimmed);
|
||||
}
|
||||
|
||||
function unav_read_only(): bool {
|
||||
$env = getenv('U_NAV_READ_ONLY');
|
||||
if ($env === '1') {
|
||||
@@ -170,8 +237,8 @@ function unav_is_root(string $real): bool {
|
||||
|
||||
// Destructive operations must never act on a configured root itself (e.g. /mnt/user).
|
||||
function unav_assert_not_root(string $real): void {
|
||||
if (unav_is_root($real)) {
|
||||
unav_error(403, 'Root folders cannot be moved, deleted or changed');
|
||||
if (unav_is_protected($real)) {
|
||||
unav_error(403, 'Wurzel- und Systemordner können nicht verschoben, gelöscht oder in den Rechten geändert werden');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,7 +273,7 @@ function unav_destination_path(?string $input): array {
|
||||
unav_error(404, 'Destination parent not found');
|
||||
}
|
||||
$root = unav_root_for_real($parentReal);
|
||||
return ['root' => $root, 'path' => $parentReal . '/' . basename($candidate)];
|
||||
return ['root' => $root, 'path' => unav_join($parentReal, basename($candidate))];
|
||||
}
|
||||
|
||||
function unav_assert_writable(): void {
|
||||
@@ -243,7 +310,7 @@ function unav_copy_recursive(string $source, string $destination): void {
|
||||
if ($entry === '.' || $entry === '..') {
|
||||
continue;
|
||||
}
|
||||
unav_copy_recursive($source . '/' . $entry, $destination . '/' . $entry);
|
||||
unav_copy_recursive(unav_join($source, $entry), unav_join($destination, $entry));
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -267,7 +334,7 @@ function unav_delete_recursive(string $path): void {
|
||||
if ($entry === '.' || $entry === '..') {
|
||||
continue;
|
||||
}
|
||||
unav_delete_recursive($path . '/' . $entry);
|
||||
unav_delete_recursive(unav_join($path, $entry));
|
||||
}
|
||||
if (!rmdir($path)) {
|
||||
unav_error(500, 'Could not remove source directory after move');
|
||||
@@ -379,7 +446,7 @@ function unav_unique_path(string $path): string {
|
||||
// "Datei (2).txt" continues with "Datei (3).txt" instead of "Datei (2) (2).txt".
|
||||
$stem = preg_replace('/ \(\d+\)$/', '', $stem);
|
||||
for ($i = 2; $i < 10000; $i++) {
|
||||
$candidate = $dir . '/' . $stem . ' (' . $i . ')' . $ext;
|
||||
$candidate = rtrim($dir, '/') . '/' . $stem . ' (' . $i . ')' . $ext;
|
||||
if (!file_exists($candidate) && !is_link($candidate)) {
|
||||
return $candidate;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ function unav_config_payload(array $settings): array {
|
||||
'readOnly' => unav_read_only(),
|
||||
'debug' => $settings['debug'],
|
||||
'thumbnails' => $settings['thumbnails'],
|
||||
'systemRoot' => $settings['systemRoot'],
|
||||
'rootsFromEnv' => unav_roots_from_env(),
|
||||
'favorites' => $settings['favorites'],
|
||||
'roots' => unav_roots(),
|
||||
'thumbnailSupport' => function_exists('imagecreatefromstring'),
|
||||
|
||||
@@ -4,7 +4,9 @@ require_once __DIR__ . '/common.php';
|
||||
$paths = unav_download_paths();
|
||||
$items = [];
|
||||
foreach ($paths as $path) {
|
||||
$items[] = unav_existing_path($path);
|
||||
$item = unav_existing_path($path);
|
||||
unav_assert_not_pseudo_fs($item['path']);
|
||||
$items[] = $item;
|
||||
}
|
||||
|
||||
if (count($items) === 1 && is_file($items[0]['path']) && !is_link($items[0]['path'])) {
|
||||
@@ -94,7 +96,7 @@ function unav_zip_add_path(ZipArchive $zip, string $path, string $zipPath): void
|
||||
if ($entry === '.' || $entry === '..') {
|
||||
continue;
|
||||
}
|
||||
unav_zip_add_path($zip, $real . '/' . $entry, $zipPath . '/' . unav_zip_safe_name($entry));
|
||||
unav_zip_add_path($zip, unav_join($real, $entry), $zipPath . '/' . unav_zip_safe_name($entry));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,6 +58,11 @@ if ($action === 'compress') {
|
||||
if (in_array($action, ['move', 'delete', 'permissions', 'fixperms'], true)) {
|
||||
unav_assert_not_root($job['source']);
|
||||
}
|
||||
foreach ($job['sources'] ?? [$job['source']] as $path) {
|
||||
if (unav_is_pseudo_fs($path)) {
|
||||
unav_error(400, 'System pseudo filesystems (/proc, /sys, /dev, /run) are not supported for this action');
|
||||
}
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
case 'move':
|
||||
|
||||
@@ -107,7 +107,7 @@ function unav_worker_guard(array $job): void {
|
||||
throw new RuntimeException('Destination parent not found');
|
||||
}
|
||||
unav_worker_assert_inside_root($parentReal);
|
||||
$destinationPath = $parentReal . '/' . basename($destination);
|
||||
$destinationPath = unav_join($parentReal, basename($destination));
|
||||
$exists = file_exists($destinationPath) || is_link($destinationPath);
|
||||
if ($exists && ($job['conflict'] ?? 'fail') !== 'overwrite') {
|
||||
throw new RuntimeException('Destination already exists');
|
||||
@@ -136,8 +136,8 @@ function unav_worker_guard_source(string $source): string {
|
||||
|
||||
function unav_worker_assert_not_root(string $path): void {
|
||||
$real = realpath($path);
|
||||
if ($real !== false && unav_is_root($real)) {
|
||||
throw new RuntimeException('Root folders cannot be moved, deleted or changed');
|
||||
if ($real !== false && unav_is_protected($real)) {
|
||||
throw new RuntimeException('Wurzel- und Systemordner können nicht verschoben, gelöscht oder in den Rechten geändert werden');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,6 +210,9 @@ function unav_worker_copy(string $source, string $destination, array &$job, stri
|
||||
if (is_link($source)) {
|
||||
throw new RuntimeException('Copying symlinks is not allowed');
|
||||
}
|
||||
if (unav_is_pseudo_fs($source)) {
|
||||
return;
|
||||
}
|
||||
if (is_dir($source)) {
|
||||
if (!mkdir($destination, 0777, true) && !is_dir($destination)) {
|
||||
throw new RuntimeException('Could not create destination directory');
|
||||
@@ -218,7 +221,7 @@ function unav_worker_copy(string $source, string $destination, array &$job, stri
|
||||
if ($entry === '.' || $entry === '..') {
|
||||
continue;
|
||||
}
|
||||
unav_worker_copy($source . '/' . $entry, $destination . '/' . $entry, $job, $label);
|
||||
unav_worker_copy(unav_join($source, $entry), unav_join($destination, $entry), $job, $label);
|
||||
}
|
||||
@chmod($destination, fileperms($source) & 07777);
|
||||
@touch($destination, filemtime($source) ?: time());
|
||||
@@ -282,7 +285,7 @@ function unav_worker_delete(string $path, ?array &$job = null): void {
|
||||
if ($entry === '.' || $entry === '..') {
|
||||
continue;
|
||||
}
|
||||
unav_worker_delete($path . '/' . $entry, $job);
|
||||
unav_worker_delete(unav_join($path, $entry), $job);
|
||||
}
|
||||
if (!rmdir($path)) {
|
||||
throw new RuntimeException('Could not delete directory ' . basename($path));
|
||||
@@ -291,7 +294,7 @@ function unav_worker_delete(string $path, ?array &$job = null): void {
|
||||
}
|
||||
|
||||
function unav_worker_size_recursive(string $path): array {
|
||||
if (is_link($path)) {
|
||||
if (is_link($path) || unav_is_pseudo_fs($path)) {
|
||||
return ['size' => 0, 'files' => 0, 'directories' => 0];
|
||||
}
|
||||
if (is_file($path)) {
|
||||
@@ -306,7 +309,7 @@ function unav_worker_size_recursive(string $path): array {
|
||||
if ($entry === '.' || $entry === '..') {
|
||||
continue;
|
||||
}
|
||||
$child = unav_worker_size_recursive($path . '/' . $entry);
|
||||
$child = unav_worker_size_recursive(unav_join($path, $entry));
|
||||
$total['size'] += $child['size'];
|
||||
$total['files'] += $child['files'];
|
||||
$total['directories'] += $child['directories'];
|
||||
@@ -358,7 +361,7 @@ function unav_worker_permissions_apply(string $path, ?string $mode, ?string $own
|
||||
if ($entry === '.' || $entry === '..') {
|
||||
continue;
|
||||
}
|
||||
unav_worker_permissions_apply($path . '/' . $entry, $mode, $owner, $group, true, $job, $count);
|
||||
unav_worker_permissions_apply(unav_join($path, $entry), $mode, $owner, $group, true, $job, $count);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -377,7 +380,7 @@ function unav_worker_fixperms(array &$job): void {
|
||||
}
|
||||
foreach (scandir($path) ?: [] as $entry) {
|
||||
if ($entry !== '.' && $entry !== '..') {
|
||||
$walk($path . '/' . $entry);
|
||||
$walk(unav_join($path, $entry));
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -413,7 +416,7 @@ function unav_worker_search(array &$job): void {
|
||||
if ($name === '.' || $name === '..') {
|
||||
continue;
|
||||
}
|
||||
$path = $dir . '/' . $name;
|
||||
$path = unav_join($dir, $name);
|
||||
$isLink = is_link($path);
|
||||
$isDir = !$isLink && is_dir($path);
|
||||
$job['result']['scanned']++;
|
||||
@@ -430,7 +433,7 @@ function unav_worker_search(array &$job): void {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if ($isDir) {
|
||||
if ($isDir && !unav_is_pseudo_fs($path)) {
|
||||
$stack[] = $path;
|
||||
}
|
||||
}
|
||||
@@ -512,7 +515,7 @@ function unav_worker_extract(array &$job): void {
|
||||
throw new RuntimeException('Destination parent not found');
|
||||
}
|
||||
unav_worker_assert_inside_root($parent);
|
||||
$destination = $parent . '/' . basename($destination);
|
||||
$destination = unav_join($parent, basename($destination));
|
||||
if (file_exists($destination) || is_link($destination)) {
|
||||
throw new RuntimeException('Destination already exists');
|
||||
}
|
||||
@@ -652,7 +655,7 @@ function unav_worker_compress(array &$job): void {
|
||||
throw new RuntimeException('Destination parent not found');
|
||||
}
|
||||
unav_worker_assert_inside_root($parent);
|
||||
$destination = $parent . '/' . basename($destination);
|
||||
$destination = unav_join($parent, basename($destination));
|
||||
if (file_exists($destination)) {
|
||||
throw new RuntimeException('Destination already exists');
|
||||
}
|
||||
@@ -708,14 +711,14 @@ function unav_worker_compress(array &$job): void {
|
||||
}
|
||||
|
||||
function unav_worker_zip_add(ZipArchive $zip, string $path, string $name, array &$job): void {
|
||||
if (is_link($path)) {
|
||||
if (is_link($path) || unav_is_pseudo_fs($path)) {
|
||||
return;
|
||||
}
|
||||
if (is_dir($path)) {
|
||||
$zip->addEmptyDir($name);
|
||||
foreach (scandir($path) ?: [] as $entry) {
|
||||
if ($entry !== '.' && $entry !== '..') {
|
||||
unav_worker_zip_add($zip, $path . '/' . $entry, $name . '/' . $entry, $job);
|
||||
unav_worker_zip_add($zip, unav_join($path, $entry), $name . '/' . $entry, $job);
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
||||
@@ -11,17 +11,17 @@ foreach (scandir($resolved['path']) ?: [] as $name) {
|
||||
if ($name === '.' || $name === '..') {
|
||||
continue;
|
||||
}
|
||||
$full = $resolved['path'] . '/' . $name;
|
||||
$full = unav_join($resolved['path'], $name);
|
||||
$type = is_link($full) ? 'symlink' : (is_dir($full) ? 'directory' : (is_file($full) ? 'file' : 'other'));
|
||||
$ownerId = fileowner($full);
|
||||
$groupId = filegroup($full);
|
||||
$perms = fileperms($full);
|
||||
$ownerId = @fileowner($full);
|
||||
$groupId = @filegroup($full);
|
||||
$perms = @fileperms($full);
|
||||
$entries[] = [
|
||||
'name' => $name,
|
||||
'path' => $full,
|
||||
'type' => $type,
|
||||
'size' => is_file($full) ? filesize($full) : 0,
|
||||
'modifiedAt' => date(DATE_ATOM, filemtime($full) ?: time()),
|
||||
'size' => is_file($full) ? (@filesize($full) ?: 0) : 0,
|
||||
'modifiedAt' => date(DATE_ATOM, @filemtime($full) ?: (@lstat($full)['mtime'] ?? time())),
|
||||
'owner' => unav_owner_name($ownerId),
|
||||
'ownerId' => $ownerId,
|
||||
'group' => unav_group_name($groupId),
|
||||
|
||||
@@ -14,7 +14,7 @@ $name = trim((string)($data['name'] ?? ''));
|
||||
if (!unav_valid_name($name)) {
|
||||
unav_error(400, 'Ungültiger Ordnername');
|
||||
}
|
||||
$path = $parent['path'] . '/' . $name;
|
||||
$path = unav_join($parent['path'], $name);
|
||||
if (file_exists($path) || is_link($path)) {
|
||||
unav_error(409, 'Destination already exists');
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ require_once __DIR__ . '/common.php';
|
||||
const UNAV_TEXT_PREVIEW_MAX_BYTES = 1048576;
|
||||
|
||||
$resolved = unav_existing_path($_GET['path'] ?? null);
|
||||
unav_assert_not_pseudo_fs($resolved['path']);
|
||||
if (!is_file($resolved['path'])) {
|
||||
unav_error(400, 'Preview target must be a file');
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ const UNAV_THUMB_MAX_BYTES = 52428800;
|
||||
|
||||
$resolved = unav_existing_path($_GET['path'] ?? null);
|
||||
$path = $resolved['path'];
|
||||
unav_assert_not_pseudo_fs($path);
|
||||
$size = max(32, min(512, (int)($_GET['size'] ?? 192)));
|
||||
if (!is_file($path) || !preg_match('/\.(jpe?g|png|gif|webp)$/i', $path)) {
|
||||
unav_error(415, 'No thumbnail for this file type');
|
||||
|
||||
@@ -105,13 +105,13 @@ function unav_prepare_upload_destination(string $targetPath, string $relativeNam
|
||||
$safeName = unav_upload_safe_relative_path($relativeName);
|
||||
$relativeParent = dirname($safeName);
|
||||
if ($relativeParent !== '.') {
|
||||
$parentCandidate = $targetPath . '/' . $relativeParent;
|
||||
$parentCandidate = unav_join($targetPath, $relativeParent);
|
||||
unav_root_for_real($targetPath);
|
||||
if (!is_dir($parentCandidate) && !mkdir($parentCandidate, 0777, true) && !is_dir($parentCandidate)) {
|
||||
unav_error(500, 'Could not create upload directory');
|
||||
}
|
||||
}
|
||||
$destination = unav_destination_path($targetPath . '/' . $safeName);
|
||||
$destination = unav_destination_path(unav_join($targetPath, $safeName));
|
||||
if (file_exists($destination['path'])) {
|
||||
if (!$overwrite) {
|
||||
unav_error(409, 'Destination already exists');
|
||||
|
||||
Reference in new issue
Block a user