diff --git a/tinyfilemanager.php b/tinyfilemanager.php index 3b111b4..2936599 100644 --- a/tinyfilemanager.php +++ b/tinyfilemanager.php @@ -121,6 +121,13 @@ $max_upload_size_bytes = 5000000000; // size 5,000,000,000 bytes (~5GB) // eg. decrease to 1MB if nginx reports problem 413 entity too large $upload_chunk_size_bytes = 2000000; // chunk size 2,000,000 bytes (~2MB) +// Handle file uploads where the file exists +// PROMPT => ask the user to resolve specific conflicts, or leave the temporary file +// NEW => newly uploaded file is renamed with a timestamp appended +// OLD => old file will be renamed with a timestamp appended +// REPLACE => old file will be deleted +$upload_name_conflict_handling = 'PROMPT'; + // Possible rules are 'OFF', 'AND' or 'OR' // OFF => Don't check connection IP, defaults to OFF // AND => Connection must be on the whitelist, and not on the blacklist @@ -667,6 +674,46 @@ if ((isset($_SESSION[FM_SESSION_ID]['logged'], $auth_users[$_SESSION[FM_SESSION_ event_callback(array("fail" => $err)); } } + + // Upload Conflict Resolution + if (isset($_POST['type']) && $_POST['type'] == 'conflict') { + $path = $_POST['path']; + $filename = $_POST['filename']; + $resolution = $_POST['resoultion']; + + + $fullPath = $path . '/' . $filename; + $tempPath = "{$fullPath}.part"; + + if (!file_exists($tempPath)) { + echo json_encode(['status' => 'error', 'info' => "Temporary file not found. $tempPath"]); + exit; + } + + $ext = pathinfo($filename, PATHINFO_FILENAME) != '' ? strtolower(pathinfo($filename, PATHINFO_EXTENSION)) : ''; + $fullPathTarget = $fullPath; + $ext_1 = $ext ? '.' . $ext : ''; + $datedPath = $path . '/' . basename($filename, $ext_1) . '_' . date('ymdHis') . $ext_1; + + if (fm_is_excluded($filename, $path)) { + $fullPathTarget = $datedPath; + } else switch ($_POST['resolution']) { + case 'OLD': + fm_rename($fullPath, $datedPath); + break; + case 'REPLACE': + if (fm_rdelete($fullPath)) break; + case 'NEW': + default: + $fullPathTarget = $datedPath; + } + fm_rename($tempPath, $fullPathTarget); + + echo json_encode(['status' => 'resolved', + 'info' => "RESOLVED WITH: $path , $filename, $resolution"]); + exit; + } + exit(); } @@ -1045,13 +1092,33 @@ if (!empty($_FILES) && !FM_READONLY) { } if ($chunkIndex == $chunkTotal - 1) { + $fullPathTarget = $fullPath; + $ext_1 = $ext ? '.' . $ext : ''; + $datedPath = $path . '/' . basename($fullPathInput, $ext_1) . '_' . date('ymdHis') . $ext_1; if (file_exists($fullPath)) { - $ext_1 = $ext ? '.' . $ext : ''; - $fullPathTarget = $path . '/' . basename($fullPathInput, $ext_1) . '_' . date('ymdHis') . $ext_1; - } else { - $fullPathTarget = $fullPath; + if (fm_is_excluded($fullPathInput, $path)) { + $fullPathTarget = $datedPath; // excluded items should not be replaced, renamed, or prompted + } else switch ($upload_name_conflict_handling) { + case 'PROMPT': + $response = array( + 'status' => 'conflict', + 'info' => "file upload successful with conflict", + 'filename' => $fullPathInput, + 'path' => $path + ); + echo json_encode($response); + exit(); + case 'OLD': + fm_rename($fullPath,$datedPath); + break; + case 'REPLACE': + if (fm_rdelete($fullPath)) break; + case 'NEW': + default: + $fullPathTarget = $datedPath; + } } - rename("{$fullPath}.part", $fullPathTarget); + fm_rename("{$fullPath}.part", $fullPathTarget); } } else if (move_uploaded_file($tmp_name, $fullPath)) { // Be sure that the file has been uploaded @@ -1334,7 +1401,7 @@ $objects = is_readable($path) ? scandir($path) : array(); $folders = array(); $files = array(); $current_path = array_slice(explode("/", $path), -1)[0]; -if (is_array($objects) && fm_is_exclude_items($current_path, $path)) { +if (is_array($objects) && !fm_is_excluded($current_path, $path)) { foreach ($objects as $file) { if ($file == '.' || $file == '..') { continue; @@ -1343,9 +1410,9 @@ if (is_array($objects) && fm_is_exclude_items($current_path, $path)) { continue; } $new_path = $path . '/' . $file; - if (@is_file($new_path) && fm_is_exclude_items($file, $new_path)) { + if (@is_file($new_path) && !fm_is_excluded($file, $new_path)) { $files[] = $file; - } elseif (@is_dir($new_path) && $file != '.' && $file != '..' && fm_is_exclude_items($file, $new_path)) { + } elseif (@is_dir($new_path) && $file != '.' && $file != '..' && !fm_is_excluded($file, $new_path)) { $folders[] = $file; } } @@ -1421,6 +1488,36 @@ if (isset($_GET['upload']) && !FM_READONLY) { + +