X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FForm.pm;h=a6ce5afc38c30dc120b8e0c33c9937164927e6d4;hb=c9a7e79e10591ae95b1523f2c7f249eae8439580;hp=0718a83ff6c84923bd6ee9a253cf6ef7a9fa1a55;hpb=e7191bc2818007bf58cec5e2167e977904f0ac44;p=kivitendo-erp.git diff --git a/SL/Form.pm b/SL/Form.pm index 0718a83ff..a6ce5afc3 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -38,91 +38,159 @@ package Form; use Data::Dumper; -use Cwd; -use HTML::Template; -use SL::Template; +use CGI; use CGI::Ajax; +use Cwd; +use List::Util qw(min max); +use SL::Auth; +use SL::Auth::DB; +use SL::Auth::LDAP; +use SL::AM; +use SL::Common; use SL::DBUtils; +use SL::Mailer; use SL::Menu; +use SL::Template; use SL::User; -use CGI; +use Template; +use List::Util qw(first max min sum); + +my $standard_dbh; + +sub DESTROY { + if ($standard_dbh) { + $standard_dbh->disconnect(); + undef $standard_dbh; + } +} + +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 ($i, $loc, $key, $val); - my (%ATTACH, $f, $header, $header_body, $len, $buf); - my ($boundary, @list, $size, $body, $x, $blah, $name); - - if ($ENV{'CONTENT_TYPE'} - && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) { - $boundary = quotemeta('--' . $1); - @list = split(/$boundary/, $input); - - # For some reason there are always 2 extra, that are empty - $size = @list - 2; - - for ($x = 1; $x <= $size; $x++) { - $header_body = $list[$x]; - $header_body =~ /\r\n\r\n|\n\n/; - - # Here we split the header and body - $header = $`; - $body = $'; #' - $body =~ s/\r\n$//; - - # Now we try to get the file name - $name = $header; - $name =~ /name=\"(.+)\"/; - ($name, $blah) = split(/\"/, $1); - - # If the form name is not attach, then we need to parse this like - # regular form data - if ($name ne "attach") { - $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg; - $ATTACH{$name} = $body; - - # Otherwise it is an attachment and we need to finish it up - } elsif ($name eq "attach") { - $header =~ /filename=\"(.+)\"/; - $ATTACH{'FILE_NAME'} = $1; - $ATTACH{'FILE_NAME'} =~ s/\"//g; - $ATTACH{'FILE_NAME'} =~ s/\s//g; - $ATTACH{'FILE_CONTENT'} = $body; - - for ($i = $x; $list[$i]; $i++) { - $list[$i] =~ s/^.+name=$//; - $list[$i] =~ /\"(\w+)\"/; - $ATTACH{$1} = $'; #' + 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; + } + + my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr, $previous); + + my $boundary = '--' . $1; + + foreach my $line (split m/\n/, $input) { + last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r")); + + if (($line eq $boundary) || ($line eq "$boundary\r")) { + ${ $previous } =~ s|\r?\n$|| if $previous; + + undef $previous; + undef $filename; + + $headers_done = 0; + $content_type = "text/plain"; + $boundary_found = 1; + $need_cr = 0; + + next; + } + + next unless $boundary_found; + + if (!$headers_done) { + $line =~ s/[\r\n]*$//; + + if (!$line) { + $headers_done = 1; + next; + } + + if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) { + if ($line =~ m|filename\s*=\s*"(.*?)"|i) { + $filename = $1; + substr $line, $-[0], $+[0] - $-[0], ""; + } + + if ($line =~ m|name\s*=\s*"(.*?)"|i) { + $name = $1; + substr $line, $-[0], $+[0] - $-[0], ""; } + + $previous = $self->_store_value($name, ''); + $self->{FILENAME} = $filename if ($filename); + + next; + } + + if ($line =~ m|^content-type\s*:\s*(.*?)$|i) { + $content_type = $1; } + + next; } - $main::lxdebug->leave_sub(2); - return %ATTACH; + next unless $previous; - } else { - $main::lxdebug->leave_sub(2); - return _input_to_hash($input); + ${ $previous } .= "${line}\n"; } + + ${ $previous } =~ s|\r?\n$|| if $previous; + + $main::lxdebug->leave_sub(2); } sub new { @@ -147,17 +215,86 @@ 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.2"; + $self->{version} = "2.4.3"; $main::lxdebug->leave_sub(); - bless $self, $type; + return $self; +} + +sub _flatten_variables_rec { + $main::lxdebug->enter_sub(2); + + my $self = shift; + my $curr = shift; + my $prefix = shift; + my $key = shift; + + my @result; + + if ('' eq ref $curr->{$key}) { + @result = ({ 'key' => $prefix . $key, 'value' => $curr->{$key} }); + + } elsif ('HASH' eq ref $curr->{$key}) { + foreach my $hash_key (sort keys %{ $curr->{$key} }) { + push @result, $self->_flatten_variables_rec($curr->{$key}, $prefix . $key . '.', $hash_key); + } + + } else { + foreach my $idx (0 .. scalar @{ $curr->{$key} } - 1) { + my $first_array_entry = 1; + + foreach my $hash_key (sort keys %{ $curr->{$key}->[$idx] }) { + push @result, $self->_flatten_variables_rec($curr->{$key}->[$idx], $prefix . $key . ($first_array_entry ? '[+].' : '[].'), $hash_key); + $first_array_entry = 0; + } + } + } + + $main::lxdebug->leave_sub(2); + + return @result; +} + +sub flatten_variables { + $main::lxdebug->enter_sub(2); + + my $self = shift; + my @keys = @_; + + my @variables; + + foreach (@keys) { + push @variables, $self->_flatten_variables_rec($self, '', $_); + } + + $main::lxdebug->leave_sub(2); + + return @variables; +} + +sub flatten_standard_variables { + $main::lxdebug->enter_sub(2); + + my $self = shift; + my %skip_keys = map { $_ => 1 } (qw(login password header stylesheet titlebar version), @_); + + my @variables; + + foreach (grep { ! $skip_keys{$_} } keys %{ $self }) { + push @variables, $self->_flatten_variables_rec($self, '', $_); + } + + $main::lxdebug->leave_sub(2); + + return @variables; } sub debug { @@ -172,6 +309,24 @@ sub debug { $main::lxdebug->leave_sub(); } +sub dumper { + $main::lxdebug->enter_sub(2); + + my $self = shift; + my $password = $self->{password}; + + $self->{password} = 'X' x 8; + + local $Data::Dumper::Sortkeys = 1; + my $output = Dumper($self); + + $self->{password} = $password; + + $main::lxdebug->leave_sub(2); + + return $output; +} + sub escape { $main::lxdebug->enter_sub(2); @@ -221,25 +376,6 @@ sub unquote { } -sub quote_html { - $main::lxdebug->enter_sub(2); - - my ($self, $str) = @_; - - my %replace = - ('order' => ['"', '<', '>'], - '<' => '<', - '>' => '>', - '"' => '"', - ); - - map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} }); - - $main::lxdebug->leave_sub(2); - - return $str; -} - sub hide_form { my $self = shift; @@ -257,6 +393,8 @@ sub hide_form { sub error { $main::lxdebug->enter_sub(); + $main::lxdebug->show_backtrace(); + my ($self, $msg) = @_; if ($ENV{HTTP_USER_AGENT}) { $msg =~ s/\n/
/g; @@ -264,11 +402,7 @@ sub error { } else { - if ($self->{error_function}) { - &{ $self->{error_function} }($msg); - } else { - die "Error: $msg\n"; - } + die "Error: $msg\n"; } $main::lxdebug->leave_sub(); @@ -305,20 +439,20 @@ sub info { $main::lxdebug->leave_sub(); } +# calculates the number of rows in a textarea based on the content and column number +# can be capped with maxrows sub numtextrows { $main::lxdebug->enter_sub(); + my ($self, $str, $cols, $maxrows, $minrows) = @_; - my ($self, $str, $cols, $maxrows) = @_; - - my $rows = 0; + $minrows ||= 1; - map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str; - - $maxrows = $rows unless defined $maxrows; + my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str; + $maxrows ||= $rows; $main::lxdebug->leave_sub(); - return ($rows > $maxrows) ? $maxrows : $rows; + return max(min($rows, $maxrows), $minrows); } sub dberror { @@ -336,12 +470,59 @@ sub isblank { my ($self, $name, $msg) = @_; - if ($self->{$name} =~ /^\s*$/) { - $self->error($msg); + my $curr = $self; + foreach my $part (split '.', $name) { + if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) { + $self->error($msg); + } + $curr = $curr->{$part}; + } + + $main::lxdebug->leave_sub(); +} + +sub create_http_response { + $main::lxdebug->enter_sub(); + + my $self = shift; + my %params = @_; + + my $cgi = $main::cgi; + $cgi ||= CGI->new(''); + + my $base_path; + + if ($ENV{HTTP_X_FORWARDED_FOR}) { + $base_path = $ENV{HTTP_REFERER}; + $base_path =~ s|^.*?://.*?/|/|; + } else { + $base_path = $ENV{REQUEST_URI}; } + $base_path =~ s|[^/]+$||; + $base_path =~ s|/$||; + + my $session_cookie; + if (defined $main::auth) { + my $session_cookie_value = $main::auth->get_session_id(); + $session_cookie_value ||= 'NO_SESSION'; + + $session_cookie = $cgi->cookie('-name' => $main::auth->get_session_cookie_name(), + '-value' => $session_cookie_value, + '-path' => $base_path); + } + + my %cgi_params = ('-type' => $params{content_type}); + $cgi_params{'-charset'} = $params{charset} if ($parmas{charset}); + + my $output = $cgi->header('-cookie' => $session_cookie, + %cgi_params); + $main::lxdebug->leave_sub(); + + return $output; } + sub header { $main::lxdebug->enter_sub(); @@ -352,14 +533,25 @@ sub header { return; } - my ($stylesheet, $favicon, $charset); + my ($stylesheet, $favicon); if ($ENV{HTTP_USER_AGENT}) { + my $doctype; - if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) { - $stylesheet = - qq| - |; + if ($ENV{'HTTP_USER_AGENT'} =~ m/MSIE\s+\d/) { + # Only set the DOCTYPE for Internet Explorer. Other browsers have problems displaying the menu otherwise. + $doctype = qq|\n|; + } + + my $stylesheets = "$self->{stylesheet} $self->{stylesheets}"; + + $stylesheets =~ s|^\s*||; + $stylesheets =~ s|\s*$||; + foreach my $file (split m/\s+/, $stylesheets) { + $file =~ s|.*/||; + next if (! -f "css/$file"); + + $stylesheet .= qq|\n|; } $self->{favicon} = "favicon.ico" unless $self->{favicon}; @@ -370,11 +562,8 @@ sub header { |; } - if ($self->{charset}) { - $charset = - qq| - |; - } + my $db_charset = $main::dbcharset ? $main::dbcharset : Common::DEFAULT_CHARSET; + if ($self->{landscape}) { $pagelayout = qq|