From: Moritz Bunkus Date: Wed, 12 Dec 2007 16:49:36 +0000 (+0000) Subject: Automatisches Erstellen von Arrays und Hashes in $form bei Verwendung spezieller... X-Git-Tag: release-2.6.0beta1~390 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=e5478aea0bc6d86ed99f41abed16dff8b3cb6094;p=kivitendo-erp.git Automatisches Erstellen von Arrays und Hashes in $form bei Verwendung spezieller Namen für Formularelemente: name[] kennzeichnet Arrays, name[+] ein neues Arrayelement, name.element kennzeichnet Hashes. --- diff --git a/SL/Form.pm b/SL/Form.pm index f43578bb6..d9d79a511 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -59,36 +59,71 @@ sub DESTROY { } } +sub _store_value { + $main::lxdebug->enter_sub(2); + + my $self = shift; + my $key = shift; + my $value = shift; + + my $curr = $self; + + while ($key =~ /\[\+?\]\.|\./) { + substr($key, 0, $+[0]) = ''; + + if ($& eq '.') { + $curr->{$`} ||= { }; + $curr = $curr->{$`}; + + } else { + $curr->{$`} ||= [ ]; + if (!scalar @{ $curr->{$`} } || $& eq '[+].') { + push @{ $curr->{$`} }, { }; + } + + $curr = $curr->{$`}->[-1]; + } + } + + $curr->{$key} = $value; + + $main::lxdebug->leave_sub(2); + + return \$curr->{$key}; +} + sub _input_to_hash { $main::lxdebug->enter_sub(2); - my $input = $_[0]; - my %in = (); + my $self = shift; + my $input = shift; + my @pairs = split(/&/, $input); foreach (@pairs) { - my ($name, $value) = split(/=/, $_, 2); - $in{$name} = unescape(undef, $value); + my ($key, $value) = split(/=/, $_, 2); + $self->_store_value($self->unescape($key), $self->unescape($value)); } $main::lxdebug->leave_sub(2); - - return %in; } sub _request_to_hash { $main::lxdebug->enter_sub(2); - my ($input) = @_; + my $self = shift; + my $input = shift; if (!$ENV{'CONTENT_TYPE'} || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) { + + $self->_input_to_hash($input); + $main::lxdebug->leave_sub(2); - return _input_to_hash($input); + return; } - my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr); - my %params; + my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr, $previous); my $boundary = '--' . $1; @@ -96,9 +131,9 @@ sub _request_to_hash { last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r")); if (($line eq $boundary) || ($line eq "$boundary\r")) { - $params{$name} =~ s|\r?\n$|| if $name; + ${ $previous } =~ s|\r?\n$|| if $previous; - undef $name; + undef $previous; undef $filename; $headers_done = 0; @@ -130,8 +165,8 @@ sub _request_to_hash { substr $line, $-[0], $+[0] - $-[0], ""; } - $params{$name} = ""; - $params{FILENAME} = $filename if ($filename); + $previous = $self->_store_value($name, ''); + $self->{FILENAME} = $filename if ($filename); next; } @@ -143,15 +178,14 @@ sub _request_to_hash { next; } - next unless $name; + next unless $previous; - $params{$name} .= "${line}\n"; + ${ $previous } .= "${line}\n"; } - $params{$name} =~ s|\r?\n$|| if $name; + ${ $previous } =~ s|\r?\n$|| if $previous; $main::lxdebug->leave_sub(2); - return %params; } sub new { @@ -176,17 +210,18 @@ sub new { $_ = $ARGV[0]; } - my %parameters = _request_to_hash($_); - map({ $self->{$_} = $parameters{$_}; } keys(%parameters)); + bless $self, $type; + + $self->_request_to_hash($_); - $self->{action} = lc $self->{action}; - $self->{action} =~ s/( |-|,|\#)/_/g; + $self->{action} = lc $self->{action}; + $self->{action} =~ s/( |-|,|\#)/_/g; - $self->{version} = "2.4.3"; + $self->{version} = "2.4.3"; $main::lxdebug->leave_sub(); - bless $self, $type; + return $self; } sub debug {