diff --git a/tinyfilemanager.php b/tinyfilemanager.php index a8c0f08..fbffb4c 100644 --- a/tinyfilemanager.php +++ b/tinyfilemanager.php @@ -458,6 +458,79 @@ unset($p, $use_auth, $iconv_input_encoding, $use_highlightjs, $highlightjs_style /*************************** ACTIONS ***************************/ +// file proxy +if (isset($_GET['proxy_file'])) { + header('Access-Control-Allow-Origin: *'); + header('Access-Control-Allow-Methods: GET'); + + function sanitizePath($path) { + if (substr($path, 0, 1) !== '/') { + die('Invalid file path.'); + } + if ($path === '/') + return '/'; + return realpath($path); + } + + // get file path + $filePath = isset($_GET['path'])?$_GET['path']:"/"; + + $filePath = sanitizePath($filePath); + + if ($filePath === false || !file_exists($filePath)) { + http_response_code(404); + die('File not found or inaccessible.'); + } + + if (is_dir($filePath)) { + // if it is dir,list the content + $fileList = getFileList($filePath); + echo generateDirectoryListing($filePath, $fileList); + exit; + } else { + // if it is image or vedio file ,return the immage file content + if (!is_readable($filePath)) { + http_response_code(403); + die("File is not readable."); + } + $mimeType = mime_content_type($filePath); + header('Content-Type: ' . $mimeType); + header('Content-Length: ' . filesize($filePath)); + readfile($filePath); + exit; + } +} + +// get file lists +function getFileList($dir) +{ + $files = array(); + $entries = scandir($dir); + foreach ($entries as $entry) { + if ($entry != "." && $entry != "..") { + $files[] = $entry; + } + } + return $files; +} +// create file lists HTML +function generateDirectoryListing($dir, $fileList) +{ + $html = "