fix: download filenames can sometimes be pretty weird.

This commit is contained in:
Floorb 2023-02-27 05:58:30 -05:00
parent c19139ed6e
commit ec562cef57
2 changed files with 14 additions and 1 deletions

View file

@ -298,3 +298,15 @@ function paginate(int $current_page, int $per_page, int $total_records) : string
return $html; 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;
}

View file

@ -155,8 +155,9 @@ $op_content = trim(htmlspecialchars_decode($p_content));
// Download the paste // Download the paste
if (isset($_GET['download'])) { if (isset($_GET['download'])) {
$filename = pp_filename_escape($paste->title . '_' . $paste->user->username, '.txt');
header('Content-Type: text/plain'); 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; echo $op_content;
exit(); exit();
} }