From b59464667915cb08ea4913d7688de22b8969b9e9 Mon Sep 17 00:00:00 2001 From: Ilia Rostovtsev Date: Sat, 12 Dec 2020 00:20:10 +0300 Subject: [PATCH] Add limit when reading file contents --- filemin/edit_file.cgi | 27 +++++++++++++++------------ lang/en | 2 ++ web-lib-funcs.pl | 20 ++++++++++++++++++-- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/filemin/edit_file.cgi b/filemin/edit_file.cgi index c2705e413..11f8ab5af 100755 --- a/filemin/edit_file.cgi +++ b/filemin/edit_file.cgi @@ -7,21 +7,24 @@ get_paths(); my $file = &simplify_path($cwd . '/' . $in{'file'}); &check_allowed_path($file); -my $data = &read_file_contents($file); +my $data = &read_file_contents($file, $in{'limit'}); my $encoding_name; -eval "use Encode::Detect::Detector;"; -if (!$@) { - $encoding_name = Encode::Detect::Detector::detect($data); -} -my $forced = ($data =~ /(.*\n)(.*\n)(.*\n)/); -$forced = (($1 . $2 . $3) =~ /coding[=:]\s*([-\w.]+)/); -if ((lc(get_charset()) eq "utf-8" && ($encoding_name && lc($encoding_name) ne "utf-8")) || $forced) { - if ($forced) { - $encoding_name = "$1"; +if ($in{'binary'}) { + $data =~ s/[^[:print:]\n\r\t]/ /g; +} else { + eval "use Encode::Detect::Detector;"; + if (!$@) { + $encoding_name = Encode::Detect::Detector::detect($data); + } + my $forced = ($data =~ /(.*\n)(.*\n)(.*\n)/); + $forced = (($1 . $2 . $3) =~ /coding[=:]\s*([-\w.]+)/); + if ((lc(get_charset()) eq "utf-8" && ($encoding_name && lc($encoding_name) ne "utf-8")) || $forced) { + if ($forced) { + $encoding_name = "$1"; + } + eval {$data = Encode::encode('utf-8', Encode::decode($encoding_name, $data))}; } - use Encode qw( encode decode ); - eval {$data = Encode::encode('utf-8', Encode::decode($encoding_name, $data))}; } &ui_print_header(undef, $text{'edit_file'}, ""); diff --git a/lang/en b/lang/en index 10b60d5f9..a388d3bea 100644 --- a/lang/en +++ b/lang/en @@ -380,4 +380,6 @@ nice_size_b=bytes langauto_include=Include machine translations +edit_truncated=truncated $1 of data + __norefs=1 diff --git a/web-lib-funcs.pl b/web-lib-funcs.pl index f325a6e3d..5ba866bc2 100755 --- a/web-lib-funcs.pl +++ b/web-lib-funcs.pl @@ -10219,7 +10219,7 @@ else { } } -=head2 read_file_contents(file) +=head2 read_file_contents(file, limit) Given a filename, returns its complete contents as a string. Effectively the same as the Perl construct `cat file`. @@ -10227,11 +10227,27 @@ the same as the Perl construct `cat file`. =cut sub read_file_contents { -my ($file) = @_; +my ($file, $limit) = @_; &open_readfile(FILE, $file) || return undef; local $/ = undef; my $rv = ; close(FILE); +if ($limit) { + my ($a, $b, $c, $s, $e, $f, $g, $h); + $a = $rv =~ tr/\n//; + $h = length($rv); + $f = int($limit); + if ($f && $h + $a > $f) { + $b = "\n\n\n[--- @{[&text('edit_truncated', &nice_size($h - $f))]} ---]\n\n\n"; + $g = int(($f / 2) + (length($b) / 2)); + $s = substr($rv, 0, $g); + $s =~ s/\n$//; + $s = $s . $b; + $e = substr($rv, ($g * -1)); + $e =~ s/^\n//; + $rv = $s . $e; + } + } return $rv; }