From ec562cef57ff5cccc8e0a728484f6de06ca1a17e Mon Sep 17 00:00:00 2001 From: Floorb <132411956+Neetpone@users.noreply.github.com> Date: Mon, 27 Feb 2023 05:58:30 -0500 Subject: [PATCH] fix: download filenames can sometimes be pretty weird. --- includes/functions.php | 12 ++++++++++++ public/paste.php | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/includes/functions.php b/includes/functions.php index f5e639d..f1d62b0 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -298,3 +298,15 @@ function paginate(int $current_page, int $per_page, int $total_records) : string return $html; } + +function pp_filename_escape(string $filename, string $extension) : string { + /* Remove NTFS invalid characters */ + $filename = preg_replace('#[<>:"/|?*]#', '-', $filename); + + /* Windows MAX_PATH limit */ + if (strlen($filename . $extension) > 255) { + $filename = substr($filename, 0, 255 - strlen($extension)); + } + + return $filename . $extension; +} \ No newline at end of file diff --git a/public/paste.php b/public/paste.php index 708eacf..ef7faee 100644 --- a/public/paste.php +++ b/public/paste.php @@ -155,8 +155,9 @@ $op_content = trim(htmlspecialchars_decode($p_content)); // Download the paste if (isset($_GET['download'])) { + $filename = pp_filename_escape($paste->title . '_' . $paste->user->username, '.txt'); header('Content-Type: text/plain'); - header('Content-Disposition: attachment; filename="' . $paste->id . '_' . pp_html_escape($paste->title) . '_' . pp_html_escape($paste->user->username) . '.txt"'); + header('Content-Disposition: attachment; filename="' . $filename . '"'); echo $op_content; exit(); }