Skip to content

Commit

Permalink
backport: fix: trim nasty characters from filenames
Browse files Browse the repository at this point in the history
Fix up makeEpisodeFilename() and makeUniqueFilename() so that characters
outside the alphanumeric range that aren't the period, dash, or
underscore are replaced with an underscore.

refs #691

(cherry picked from commit fc058e0)
  • Loading branch information
Chris Charabaruk committed Oct 30, 2022
1 parent 6739946 commit db3ed84
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion PodcastGenerator/core/episodes.php
Expand Up @@ -402,7 +402,7 @@ function compare_mtimes($a, $b)
*/
function makeEpisodeFilename($directory, $date, $filename)
{
$filename = strtolower(str_replace(' ', '_', $filename));
$filename = strtolower(trim(preg_replace('/[^a-zA-Z0-9._-]+/', '_', $title), '_'));
$targetfile = $directory . $date . '_' . $filename;

if (file_exists($targetfile)) {
Expand Down
3 changes: 2 additions & 1 deletion PodcastGenerator/core/misc/functions.php
Expand Up @@ -140,7 +140,8 @@ function makeUniqueFilename($path)
// make sure that we have a real directory path with filename attached
// just putting $path into realpath() will fail if the file doesn't exist
$pathinfo = pathinfo($path);
$realpath = realpath($pathinfo['dirname']) . '/' . $pathinfo['basename'];
$filename = strtolower(trim(preg_replace('/[^a-zA-Z0-9_-]+/', '_', $pathinfo['filename']), '_'));
$realpath = realpath($pathinfo['dirname']) . '/' . $filename . $pathinfo['extension'];

// if the existing path doesn't exist, we're unique!
if (!file_exists($realpath)) {
Expand Down

0 comments on commit db3ed84

Please sign in to comment.