Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision e5478aea

Von Moritz Bunkus vor fast 17 Jahren hinzugefügt

  • ID e5478aea0bc6d86ed99f41abed16dff8b3cb6094
  • Vorgänger e309bf11
  • Nachfolger 777bf75c

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.

Unterschiede anzeigen:

SL/Form.pm
59 59
  }
60 60
}
61 61

  
62
sub _store_value {
63
  $main::lxdebug->enter_sub(2);
64

  
65
  my $self  = shift;
66
  my $key   = shift;
67
  my $value = shift;
68

  
69
  my $curr  = $self;
70

  
71
  while ($key =~ /\[\+?\]\.|\./) {
72
    substr($key, 0, $+[0]) = '';
73

  
74
    if ($& eq '.') {
75
      $curr->{$`} ||= { };
76
      $curr         = $curr->{$`};
77

  
78
    } else {
79
      $curr->{$`} ||= [ ];
80
      if (!scalar @{ $curr->{$`} } || $& eq '[+].') {
81
        push @{ $curr->{$`} }, { };
82
      }
83

  
84
      $curr = $curr->{$`}->[-1];
85
    }
86
  }
87

  
88
  $curr->{$key} = $value;
89

  
90
  $main::lxdebug->leave_sub(2);
91

  
92
  return \$curr->{$key};
93
}
94

  
62 95
sub _input_to_hash {
63 96
  $main::lxdebug->enter_sub(2);
64 97

  
65
  my $input = $_[0];
66
  my %in    = ();
98
  my $self  = shift;
99
  my $input = shift;
100

  
67 101
  my @pairs = split(/&/, $input);
68 102

  
69 103
  foreach (@pairs) {
70
    my ($name, $value) = split(/=/, $_, 2);
71
    $in{$name} = unescape(undef, $value);
104
    my ($key, $value) = split(/=/, $_, 2);
105
    $self->_store_value($self->unescape($key), $self->unescape($value));
72 106
  }
73 107

  
74 108
  $main::lxdebug->leave_sub(2);
75

  
76
  return %in;
77 109
}
78 110

  
79 111
sub _request_to_hash {
80 112
  $main::lxdebug->enter_sub(2);
81 113

  
82
  my ($input) = @_;
114
  my $self  = shift;
115
  my $input = shift;
83 116

  
84 117
  if (!$ENV{'CONTENT_TYPE'}
85 118
      || ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) {
119

  
120
    $self->_input_to_hash($input);
121

  
86 122
    $main::lxdebug->leave_sub(2);
87
    return _input_to_hash($input);
123
    return;
88 124
  }
89 125

  
90
  my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr);
91
  my %params;
126
  my ($name, $filename, $headers_done, $content_type, $boundary_found, $need_cr, $previous);
92 127

  
93 128
  my $boundary = '--' . $1;
94 129

  
......
96 131
    last if (($line eq "${boundary}--") || ($line eq "${boundary}--\r"));
97 132

  
98 133
    if (($line eq $boundary) || ($line eq "$boundary\r")) {
99
      $params{$name} =~ s|\r?\n$|| if $name;
134
      ${ $previous } =~ s|\r?\n$|| if $previous;
100 135

  
101
      undef $name;
136
      undef $previous;
102 137
      undef $filename;
103 138

  
104 139
      $headers_done   = 0;
......
130 165
          substr $line, $-[0], $+[0] - $-[0], "";
131 166
        }
132 167

  
133
        $params{$name}    = "";
134
        $params{FILENAME} = $filename if ($filename);
168
        $previous         = $self->_store_value($name, '');
169
        $self->{FILENAME} = $filename if ($filename);
135 170

  
136 171
        next;
137 172
      }
......
143 178
      next;
144 179
    }
145 180

  
146
    next unless $name;
181
    next unless $previous;
147 182

  
148
    $params{$name} .= "${line}\n";
183
    ${ $previous } .= "${line}\n";
149 184
  }
150 185

  
151
  $params{$name} =~ s|\r?\n$|| if $name;
186
  ${ $previous } =~ s|\r?\n$|| if $previous;
152 187

  
153 188
  $main::lxdebug->leave_sub(2);
154
  return %params;
155 189
}
156 190

  
157 191
sub new {
......
176 210
    $_ = $ARGV[0];
177 211
  }
178 212

  
179
  my %parameters = _request_to_hash($_);
180
  map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
213
  bless $self, $type;
214

  
215
  $self->_request_to_hash($_);
181 216

  
182
  $self->{action} = lc $self->{action};
183
  $self->{action} =~ s/( |-|,|\#)/_/g;
217
  $self->{action}  =  lc $self->{action};
218
  $self->{action}  =~ s/( |-|,|\#)/_/g;
184 219

  
185
  $self->{version}   = "2.4.3";
220
  $self->{version} =  "2.4.3";
186 221

  
187 222
  $main::lxdebug->leave_sub();
188 223

  
189
  bless $self, $type;
224
  return $self;
190 225
}
191 226

  
192 227
sub debug {

Auch abrufbar als: Unified diff