further refinement

fix a regexp injection hole
master
joey 2007-03-07 18:42:41 +00:00
parent 355494b78b
commit cfb2da268b
1 changed files with 15 additions and 19 deletions

View File

@ -7,29 +7,25 @@ use strict;
use IkiWiki; use IkiWiki;
use IkiWiki::Plugin::mdwn; use IkiWiki::Plugin::mdwn;
my %defaults = (
data => undef,
file => undef,
format => 'auto',
sep_char => {
'csv' => ',',
'dsv' => '\|',
},
class => undef,
header => 1,
);
sub import { #{{{ sub import { #{{{
hook(type => "preprocess", id => "table", call => \&preprocess); hook(type => "preprocess", id => "table", call => \&preprocess);
} # }}} } # }}}
sub preprocess (@) { #{{{ sub preprocess (@) { #{{{
my %params = (%defaults, @_); my %params =(
format => 'auto',
header => 'yes',
sep_char => {
'csv' => ',',
'dsv' => '|',
},
@_
);
if (defined $params{delimiter}) { if (exists $params{delimiter}) {
$params{sep_char}->{$params{format}} = $params{delimiter}; $params{sep_char}->{$params{format}} = $params{delimiter};
} }
if (defined $params{file}) { if (exists $params{file}) {
if (! $pagesources{$params{file}}) { if (! $pagesources{$params{file}}) {
return "[[table ".gettext("cannot find file")."]]"; return "[[table ".gettext("cannot find file")."]]";
} }
@ -40,7 +36,7 @@ sub preprocess (@) { #{{{
# first try the more simple format # first try the more simple format
if (is_dsv_data($params{data})) { if (is_dsv_data($params{data})) {
$params{format} = 'dsv'; $params{format} = 'dsv';
$params{sep_char}->{dsv} = '\|'; $params{sep_char}->{dsv} = '|';
} }
else { else {
$params{format} = 'csv'; $params{format} = 'csv';
@ -60,7 +56,7 @@ sub preprocess (@) { #{{{
} }
my $header; my $header;
if ($params{header} != 1) { if (lc($params{header}) eq "yes") {
$header=shift @data; $header=shift @data;
} }
if (! @data) { if (! @data) {
@ -71,7 +67,7 @@ sub preprocess (@) { #{{{
build_rows(\%params, @data), build_rows(\%params, @data),
close_table(\%params, $header)); close_table(\%params, $header));
if (defined $params{file}) { if (exists $params{file}) {
return $html."\n\n". return $html."\n\n".
htmllink($params{page}, $params{destpage}, $params{file}, htmllink($params{page}, $params{destpage}, $params{file},
linktext => gettext('Direct data download')); linktext => gettext('Direct data download'));
@ -134,7 +130,7 @@ sub read_dsv ($) { #{{{
my @text_lines = split(/\n/, $params->{data}); my @text_lines = split(/\n/, $params->{data});
my @data; my @data;
my $splitter = qr{$params->{sep_char}->{dsv}}; my $splitter = qr{\Q$params->{sep_char}->{dsv}\E};
foreach my $line (@text_lines) { foreach my $line (@text_lines) {
push @data, [ split($splitter, $line) ]; push @data, [ split($splitter, $line) ];
} }