/* ============================================ ACTIES (POST) ============================================ */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['fm_action'])) { if (!csrf_check(isset($_POST['csrf_token']) ? $_POST['csrf_token'] : '')) { fm_error("Ongeldige CSRF‑token."); } $action = $_POST['fm_action']; $targets = isset($_POST['targets']) ? $_POST['targets'] : []; if (!is_array($targets)) $targets = [$targets]; /* UPLOAD */ if ($action === 'upload' && !empty($perms['write'])) { if (!empty($_FILES['upload_file']['name'])) { $file = $_FILES['upload_file']; if ($file['size'] <= 0) fm_error("Bestand ongeldig of leeg."); if ($file['size'] > $MAX_UPLOAD_BYTES) fm_error("Bestand is te groot."); $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); if (!in_array($ext, $ALLOWED_UPLOAD_EXT, true)) fm_error("Extensie niet toegestaan."); if (preg_match('/\.(php|phtml|php3|php4|php5|phar)$/i', $file['name'])) { fm_error("PHP‑bestanden zijn niet toegestaan."); } $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $file['tmp_name']); finfo_close($finfo); if (!in_array($mime, $ALLOWED_MIME, true)) { fm_error("MIME‑type niet toegestaan: ".$mime); } $cleanName = preg_replace('/[^A-Za-z0-9._-]/', '_', $file['name']); $dest = $currentPathReal . DS . $cleanName; if (!file_exists($dest)) { if (!move_uploaded_file($file['tmp_name'], $dest)) { fm_error("Upload mislukt."); } fm_log($user." upload: ".$dest." (".$mime.", ".$ext.")"); } } header("Location: ?root=$currentRootKey&path=".urlencode(str_replace('\\','/',$relPath))); exit; } /* NIEUWE MAP */ if ($action === 'mkdir' && !empty($perms['write'])) { $name = trim($_POST['new_dir_name'] ?? ''); if ($name === '') fm_error("Geen mapnaam opgegeven."); if (preg_match('/[\/\\\\<>:\"|?*]/', $name)) fm_error("Mapnaam bevat ongeldige tekens."); $newDir = $currentPathReal . DS . $name; if (!@mkdir($newDir, 0777, true)) { fm_error("Map aanmaken mislukt."); } fm_log($user." mkdir: ".$newDir); header("Location: ?root=$currentRootKey&path=".urlencode(str_replace('\\','/',$relPath))); exit; } /* VERWIJDEREN */ if ($action === 'delete') { if (empty($targets)) fm_error("Geen items geselecteerd om te verwijderen."); $resolved = resolve_targets($targets, $rootPath); if (empty($resolved)) fm_error("Geen geldige items gevonden om te verwijderen."); foreach ($resolved as $t) { $real = $t['real']; $itemPerms = get_permissions($real); if (empty($itemPerms['delete'])) { fm_error("Geen rechten om te verwijderen: ".$t['rel']); } if (!rrmdir_safe($real)) { fm_error("Verwijderen mislukt: ".$t['rel']); } fm_log($user." delete: ".$real); } header("Location: ?root=$currentRootKey&path=".urlencode(str_replace('\\','/',$relPath))); exit; } /* HERNOEMEN */ if ($action === 'rename' && !empty($perms['rename'])) { if (count($targets) !== 1) fm_error("Selecteer precies één item om te hernoemen."); $resolved = resolve_targets($targets, $rootPath); if (empty($resolved)) fm_error("Ongeldig item geselecteerd."); $targetFullReal = $resolved[0]['real']; $newName = trim($_POST['new_name'] ?? ''); if ($newName === '') fm_error("Geen nieuwe naam opgegeven."); if (preg_match('/[\/\\\\<>:\"|?*]/', $newName)) fm_error("Naam bevat verboden tekens."); $forbidden = ['CON','PRN','AUX','NUL','COM1','LPT1']; if (in_array(strtoupper($newName), $forbidden, true)) fm_error("Ongeldige bestandsnaam."); $newPath = dirname($targetFullReal) . DS . $newName; if (!@rename($targetFullReal, $newPath)) { fm_error("Hernoemen mislukt."); } fm_log($user." rename: ".$targetFullReal." -> ".$newPath); header("Location: ?root=$currentRootKey&path=".urlencode(str_replace('\\','/',$relPath))); exit; } /* COPY */ if ($action === 'copy' && !empty($perms['read'])) { $resolved = resolve_targets($targets, $rootPath); if (empty($resolved)) fm_error("Geen items geselecteerd om te kopiëren."); $_SESSION['clipboard'] = [ 'mode' => 'copy', 'items' => array_column($resolved, 'rel'), 'root' => $currentRootKey ]; header("Location: ?root=$currentRootKey&path=".urlencode(str_replace('\\','/',$relPath))); exit; } /* CUT */ if ($action === 'cut' && !empty($perms['rename'])) { $resolved = resolve_targets($targets, $rootPath); if (empty($resolved)) fm_error("Geen items geselecteerd om te knippen."); $_SESSION['clipboard'] = [ 'mode' => 'cut', 'items' => array_column($resolved, 'rel'), 'root' => $currentRootKey ]; header("Location: ?root=$currentRootKey&path=".urlencode(str_replace('\\','/',$relPath))); exit; } /* PASTE */ if ($action === 'paste' && !empty($perms['write'])) { $clip = $_SESSION['clipboard']; if (empty($clip['mode']) || empty($clip['items']) || empty($clip['root'])) { fm_error("Er is niets om te plakken."); } $srcRootKey = $clip['root']; $srcRoots = get_user_roots(); if (!isset($srcRoots[$srcRootKey])) { fm_error("Klembord ongeldig."); } $srcRootPath = $srcRoots[$srcRootKey]; foreach ($clip['items'] as $rel) { $srcFull = normalize_path($srcRootPath . DS . $rel); $srcReal = realpath($srcFull); if (!$srcReal || !ensure_within_root($srcReal)) continue; $baseName = basename($srcReal); $destFull = normalize_path($currentPathReal . DS . $baseName); if ($clip['mode'] === 'copy') { if (!rcopy_safe($srcReal, $destFull)) { fm_error("Kopiëren mislukt: ".$rel); } fm_log($user." copy: ".$srcReal." -> ".$destFull); } else { if (!@rename($srcReal, $destFull)) { if (!rcopy_safe($srcReal, $destFull) || !rrmdir_safe($srcReal)) { fm_error("Knippen mislukt: ".$rel); } } fm_log($user." move: ".$srcReal." -> ".$destFull); } } $_SESSION['clipboard'] = ['mode'=>null,'items'=>[],'root'=>null]; header("Location: ?root=$currentRootKey&path=".urlencode(str_replace('\\','/',$relPath))); exit; }