3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-27 06:19:25 +01:00
pbot/lib/PBot/Core/Utils/SafeFilename.pm

30 lines
684 B
Perl
Raw Normal View History

# File: SafeFilename.pm
#
# Purpose: for strings containing filenames, translates potentially unsafe
# characters into safe expansions; e.g. "foo/bar" becomes "foo&fslash;bar".
2023-02-21 06:31:52 +01:00
# SPDX-FileCopyrightText: 2017-2023 Pragmatic Software <pragma78@gmail.com>
2021-07-11 00:00:22 +02:00
# SPDX-License-Identifier: MIT
2021-07-24 04:22:25 +02:00
package PBot::Core::Utils::SafeFilename;
2020-02-15 23:38:32 +01:00
use PBot::Imports;
2017-09-05 04:06:31 +02:00
require Exporter;
2020-02-15 23:38:32 +01:00
our @ISA = qw/Exporter/;
2017-09-05 04:06:31 +02:00
our @EXPORT = qw/safe_filename/;
sub safe_filename($name) {
2020-02-15 23:38:32 +01:00
my $safe = '';
2017-09-05 04:06:31 +02:00
2020-02-15 23:38:32 +01:00
while ($name =~ m/(.)/gms) {
if ($1 eq '&') { $safe .= '&amp;'; }
elsif ($1 eq '/') { $safe .= '&fslash;'; }
else { $safe .= $1; }
2017-09-05 04:06:31 +02:00
}
2020-02-15 23:38:32 +01:00
return lc $safe;
2017-09-05 04:06:31 +02:00
}
1;