Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 48abd6c9

Von Sven Schöling vor fast 13 Jahren hinzugefügt

  • ID 48abd6c981f62e880b94e1ad9659d0a4d406912b
  • Vorgänger 5c695f5d
  • Nachfolger 0ab92915

Request Handling aus Form ausgelagert.

Unterschiede anzeigen:

SL/Form.pm
58 58
use SL::IS;
59 59
use SL::Mailer;
60 60
use SL::Menu;
61
use SL::MoreCommon qw(uri_encode uri_decode);
61 62
use SL::OE;
63
use SL::Request;
62 64
use SL::Template;
63 65
use SL::User;
64 66
use SL::X;
......
81 83
  undef $standard_dbh;
82 84
}
83 85

  
84
sub _store_value {
85
  $main::lxdebug->enter_sub(2);
86

  
87
  my $self  = shift;
88
  my $key   = shift;
89
  my $value = shift;
90

  
91
  my @tokens = split /((?:\[\+?\])?(?:\.|$))/, $key;
92

  
93
  my $curr;
94

  
95
  if (scalar @tokens) {
96
     $curr = \ $self->{ shift @tokens };
97
  }
98

  
99
  while (@tokens) {
100
    my $sep = shift @tokens;
101
    my $key = shift @tokens;
102

  
103
    $curr = \ $$curr->[++$#$$curr], next if $sep eq '[]';
104
    $curr = \ $$curr->[max 0, $#$$curr]  if $sep eq '[].';
105
    $curr = \ $$curr->[++$#$$curr]       if $sep eq '[+].';
106
    $curr = \ $$curr->{$key}
107
  }
108

  
109
  $$curr = $value;
110

  
111
  $main::lxdebug->leave_sub(2);
112

  
113
  return $curr;
114
}
115

  
116
sub _input_to_hash {
117
  $main::lxdebug->enter_sub(2);
118

  
119
  my $self  = shift;
120
  my $input = shift;
121

  
122
  my @pairs = split(/&/, $input);
123

  
124
  foreach (@pairs) {
125
    my ($key, $value) = split(/=/, $_, 2);
126
    $self->_store_value($self->unescape($key), $self->unescape($value)) if ($key);
127
  }
128

  
129
  $main::lxdebug->leave_sub(2);
130
}
131

  
132
sub _request_to_hash {
133
  $main::lxdebug->enter_sub(2);
134

  
135
  my $self  = shift;
136
  my $input = shift;
137
  my $uploads = {};
138

  
139
  if (!$ENV{'CONTENT_TYPE'}
140
      || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
141

  
142
    $self->_input_to_hash($input);
143

  
144
    $main::lxdebug->leave_sub(2);
145
    return $uploads;
146
  }
147

  
148
  my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr, $previous);
149

  
150
  my $boundary = '--' . $1;
151

  
152
  foreach my $line (split m/\n/, $input) {
153
    last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
154

  
155
    if (($line eq $boundary) || ($line eq "$boundary\r")) {
156
      ${ $previous } =~ s|\r?\n$|| if $previous;
157

  
158
      undef $previous;
159
      undef $filename;
160

  
161
      $headers_done   = 0;
162
      $content_type   = "text/plain";
163
      $boundary_found = 1;
164
      $need_cr        = 0;
165

  
166
      next;
167
    }
168

  
169
    next unless $boundary_found;
170

  
171
    if (!$headers_done) {
172
      $line =~ s/[\r\n]*$//;
173

  
174
      if (!$line) {
175
        $headers_done = 1;
176
        next;
177
      }
178

  
179
      if ($line =~ m|^content-disposition\s*:.*?form-data\s*;|i) {
180
        if ($line =~ m|filename\s*=\s*"(.*?)"|i) {
181
          $filename = $1;
182
          substr $line, $-[0], $+[0] - $-[0], "";
183
        }
184

  
185
        if ($line =~ m|name\s*=\s*"(.*?)"|i) {
186
          $name = $1;
187
          substr $line, $-[0], $+[0] - $-[0], "";
188
        }
189

  
190
        $previous         = _store_value($uploads, $name, '') if ($name);
191
        $self->{FILENAME} = $filename if ($filename);
192

  
193
        next;
194
      }
195

  
196
      if ($line =~ m|^content-type\s*:\s*(.*?)$|i) {
197
        $content_type = $1;
198
      }
199

  
200
      next;
201
    }
202

  
203
    next unless $previous;
204

  
205
    ${ $previous } .= "${line}\n";
206
  }
207

  
208
  ${ $previous } =~ s|\r?\n$|| if $previous;
209

  
210
  $main::lxdebug->leave_sub(2);
211

  
212
  return $uploads;
213
}
214

  
215
sub _recode_recursively {
216
  $main::lxdebug->enter_sub();
217
  my ($iconv, $param) = @_;
218

  
219
  if (any { ref $param eq $_ } qw(Form HASH)) {
220
    foreach my $key (keys %{ $param }) {
221
      if (!ref $param->{$key}) {
222
        # Workaround for a bug: converting $param->{$key} directly
223
        # leads to 'undef'. I don't know why. Converting a copy works,
224
        # though.
225
        $param->{$key} = $iconv->convert("" . $param->{$key});
226
      } else {
227
        _recode_recursively($iconv, $param->{$key});
228
      }
229
    }
230

  
231
  } elsif (ref $param eq 'ARRAY') {
232
    foreach my $idx (0 .. scalar(@{ $param }) - 1) {
233
      if (!ref $param->[$idx]) {
234
        # Workaround for a bug: converting $param->[$idx] directly
235
        # leads to 'undef'. I don't know why. Converting a copy works,
236
        # though.
237
        $param->[$idx] = $iconv->convert("" . $param->[$idx]);
238
      } else {
239
        _recode_recursively($iconv, $param->[$idx]);
240
      }
241
    }
242
  }
243
  $main::lxdebug->leave_sub();
244
}
245

  
246 86
sub new {
247 87
  $main::lxdebug->enter_sub();
248 88

  
......
258 98

  
259 99
  bless $self, $type;
260 100

  
261
  $main::lxdebug->leave_sub();
262

  
263
  return $self;
264
}
265

  
266
sub read_cgi_input {
267
  $main::lxdebug->enter_sub();
268

  
269
  my ($self) = @_;
270

  
271
  $self->_input_to_hash($ENV{QUERY_STRING}) if $ENV{QUERY_STRING};
272
  $self->_input_to_hash($ARGV[0])           if @ARGV && $ARGV[0];
273

  
274
  my $uploads;
275
  if ($ENV{CONTENT_LENGTH}) {
276
    my $content;
277
    read STDIN, $content, $ENV{CONTENT_LENGTH};
278
    $uploads = $self->_request_to_hash($content);
279
  }
280

  
281
  if ($self->{RESTORE_FORM_FROM_SESSION_ID}) {
282
    my %temp_form;
283
    $::auth->restore_form_from_session(delete $self->{RESTORE_FORM_FROM_SESSION_ID}, form => \%temp_form);
284
    $self->_input_to_hash(join '&', map { $self->escape($_) . '=' . $self->escape($temp_form{$_}) } keys %temp_form);
285
  }
286

  
287
  my $db_charset   = $::lx_office_conf{system}->{dbcharset};
288
  $db_charset    ||= Common::DEFAULT_CHARSET;
289

  
290
  my $encoding     = $self->{INPUT_ENCODING} || $db_charset;
291
  delete $self->{INPUT_ENCODING};
292

  
293
  _recode_recursively(SL::Iconv->new($encoding, $db_charset), $self);
294

  
295
  map { $self->{$_} = $uploads->{$_} } keys %{ $uploads } if $uploads;
296

  
297
  #$self->{version} =  "2.6.1";                 # Old hardcoded but secure style
298 101
  open VERSION_FILE, "VERSION";                 # New but flexible code reads version from VERSION-file
299 102
  $self->{version} =  <VERSION_FILE>;
300 103
  close VERSION_FILE;
......
305 108
  return $self;
306 109
}
307 110

  
111
sub read_cgi_input {
112
  my ($self) = @_;
113
  SL::Request::read_cgi_input($self);
114
}
115

  
308 116
sub _flatten_variables_rec {
309 117
  $main::lxdebug->enter_sub(2);
310 118

  
......
404 212
}
405 213

  
406 214
sub escape {
407
  $main::lxdebug->enter_sub(2);
408

  
409 215
  my ($self, $str) = @_;
410 216

  
411
  $str =  Encode::encode('utf-8-strict', $str) if $::locale->is_utf8;
412
  $str =~ s/([^a-zA-Z0-9_.:-])/sprintf("%%%02x", ord($1))/ge;
413

  
414
  $main::lxdebug->leave_sub(2);
415

  
416
  return $str;
217
  return uri_encode($str);
417 218
}
418 219

  
419 220
sub unescape {
420
  $main::lxdebug->enter_sub(2);
421

  
422 221
  my ($self, $str) = @_;
423 222

  
424
  $str =~ tr/+/ /;
425
  $str =~ s/\\$//;
426

  
427
  $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
428
  $str =  Encode::decode('utf-8-strict', $str) if $::locale->is_utf8;
429

  
430
  $main::lxdebug->leave_sub(2);
431

  
432
  return $str;
223
  return uri_decode($str);
433 224
}
434 225

  
435 226
sub quote {
......
3790 3581

  
3791 3582
=head1 SPECIAL FUNCTIONS
3792 3583

  
3793
=head2 C<_store_value()>
3794

  
3795
parses a complex var name, and stores it in the form.
3796

  
3797
syntax:
3798
  $form->_store_value($key, $value);
3799

  
3800
keys must start with a string, and can contain various tokens.
3801
supported key structures are:
3802

  
3803
1. simple access
3804
  simple key strings work as expected
3805

  
3806
  id => $form->{id}
3807

  
3808
2. hash access.
3809
  separating two keys by a dot (.) will result in a hash lookup for the inner value
3810
  this is similar to the behaviour of java and templating mechanisms.
3811

  
3812
  filter.description => $form->{filter}->{description}
3813

  
3814
3. array+hashref access
3815

  
3816
  adding brackets ([]) before the dot will cause the next hash to be put into an array.
3817
  using [+] instead of [] will force a new array index. this is useful for recurring
3818
  data structures like part lists. put a [+] into the first varname, and use [] on the
3819
  following ones.
3820

  
3821
  repeating these names in your template:
3822

  
3823
    invoice.items[+].id
3824
    invoice.items[].parts_id
3825

  
3826
  will result in:
3827

  
3828
    $form->{invoice}->{items}->[
3829
      {
3830
        id       => ...
3831
        parts_id => ...
3832
      },
3833
      {
3834
        id       => ...
3835
        parts_id => ...
3836
      }
3837
      ...
3838
    ]
3839

  
3840
4. arrays
3841

  
3842
  using brackets at the end of a name will result in a pure array to be created.
3843
  note that you mustn't use [+], which is reserved for array+hash access and will
3844
  result in undefined behaviour in array context.
3845

  
3846
  filter.status[]  => $form->{status}->[ val1, val2, ... ]
3847

  
3848 3584
=head2 C<update_business> PARAMS
3849 3585

  
3850 3586
PARAMS (not named):

Auch abrufbar als: Unified diff