mirror of
https://github.com/webmin/webmin.git
synced 2026-03-20 08:40:24 +00:00
Add limit when reading file contents
This commit is contained in:
@@ -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'}, "");
|
||||
|
||||
2
lang/en
2
lang/en
@@ -380,4 +380,6 @@ nice_size_b=bytes
|
||||
|
||||
langauto_include=Include machine translations
|
||||
|
||||
edit_truncated=truncated $1 of data
|
||||
|
||||
__norefs=1
|
||||
|
||||
@@ -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 = <FILE>;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user