blib/lib/GetWeb/UnformatForm.pm untested

filter: Statementstmt 0.0% 0 / 148 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 286 CC 14 SCAR 19.7
CC
14
Cov
0%
input
20.0
input_checkbox
12.0
input_checkbox_start
12.0
CRAP 7.1
1 require GetWeb::FormatAnnotated;
2  
3 package GetWeb::UnformatForm;
4  
5 use URI::URL;
6 use GetWeb::Util;
7 use URI::Escape;
8  
9 @ISA = qw( GetWeb::FormatAnnotated );
10  
11 use Carp;
12 use strict;
13  
14 sub new
15 {
16 my $type = shift;
17 my $pBody = shift;
18  
19 my $self = $type -> SUPER::new (@_);
20 $$self{formCount} = 'A';
21 $$self{pBody} = $pBody;
22 bless($self,$type);
23 $self;
24 }
25  
26 sub form_start
27 {
28 my $self = shift;
29 my ($elem) = @_;
30  
31 # jfjf support POST, mailto correctly
32  
33 $elem -> {phQuery} = {};
34  
35 my $letter = $self -> {formCount};
36 my $pBody = $self -> {pBody};
37  
38 $$pBody =~ s/.*?\<GETWEB: +FORM +$letter\>//
39 or $self -> myDie("form $letter not found\n");
40  
41 $self -> SUPER::form_start(@_);
42 }
43  
44 sub formDie
45 {
46 my ($self, $text) = @_;
47 defined $text or $text = "died during form processing\n";
48  
49 my $form = $self -> {current_form};
50 defined $form or $self -> myDie("no current form, died from $text");
51  
52 return if defined $form -> {formDieText};
53 $form -> {formDieText} = $text;
54 die $text;
55 }
56  
57  
58 sub myDie
59 {
60 my ($self, $text) = @_;
61 defined $text or $text = "died during form processing\n";
62  
63 return if defined $self -> {myDieText};
64 $self -> {myDieText} = $text;
65 die $text;
66 }
67  
68 sub addNameValue
69 {
70 my ($self, $elem, $unique) = @_;
71  
72 my $name = $elem -> attr('name');
73 my $value = $elem -> attr('value');
74  
75 $self -> addKeyVal($name,$value,$unique);
76 }
77  
78 sub addKeyVal
79 {
80 my ($self, $key, $val, $unique) = @_;
81  
82 # print "(key val is $key $val)\n";
83
84 my $phQuery = $self -> {current_form} -> {phQuery};
85  
86 my $paVal = $phQuery -> {$key};
87 if (! defined $paVal)
88 {
89 $paVal = [];
90 $phQuery -> {$key} = $paVal;
91 }
92 else
93 {
94 $unique and
95 $self -> formDie("SYNTAX_ERROR: key '$key' selected more than once: '$val' and '"
96 . $$paVal[0] . "'\n\n" .
97 "Please select only one '$key' attribute.\n");
98 }
99
100 push(@$paVal,$val);
101 }
102  
103 sub input_hidden_start
104 {
105 my $self = shift;
106 my ($elem) = @_;
107  
108 $self -> addNameValue($elem);
109 $self -> SUPER::input_hidden_start(@_);
110 }
111  
112 # jfjf implement escaping
113  
114 sub endsInSlash
115 {
116 my $self = shift;
117 my $text = shift;
118  
119 $text =~ s/\\(.)/$1/g;
120  
121 # print "text is $text.\n";
122  
123 $text =~ /\\$/;
124 }
125  
126 sub input
127 {
128 my $self = shift;
129  
130 my $pBody = $self -> {pBody};
131  
132 # jfjf make escaping slashes more consistent
133  
134 while (1)
135 {
136 while (1)
137 {
138 $$pBody =~ s/(.*?)\[// or
139 $self -> myDie("CORRUPT: could not find next left bracket\n");
140  
141 last unless $self -> endsInSlash($1);
142 }
143
144 my $input = "";
145 while (1)
146 {
147 $$pBody =~ s/(.*?)\]// or
148 $self -> myDie("CORRUPT: could not find next right bracket\n");
149 $input .= $1;
150  
151 last unless $self -> endsInSlash($input);
152 $input .= "]";
153 }
154  
155 $input =~ s/\n/ /g;
156 # next if $input eq 'IMAGE';
157 next if $input =~ /\d/ and $input =~ /^[\dX]+$/i; # href, ignore
158 $input =~ s/\\(.)/$1/g;
159  
160 # print "found $input\n";
161 return $input;
162 }
163 }
164  
165 sub input_line
166 {
167 my $self = shift;
168
169 my $input = $self -> input;
170 $input =~ s/__+$/_/g;
171 $input =~ s/(?!\\)(.)_$/$1/;
172 $input =~ s/^_+//;
173  
174 # repeat same statement to get all unescaped intermediate underscores
175 $input =~ s/(?!\\)(.)_/$1 /g;
176 $input =~ s/(?!\\)(.)_/$1 /g;
177  
178 $input =~ s/\\(.)/$1/g;
179
180 $input;
181 }
182  
183 # jfjf escape numbers in forms
184  
185 sub input_checkbox
186 {
187 my $self = shift;
188  
189 my $input = $self -> input;
190  
191 return 1 if $input =~ /X/i and $input =~ /^[\sX]+$/i;
192 return 0 if $input =~ /^\s*$/i;
193 $self -> formDie("SYNTAX_ERROR: illegal checkbox input: $input\n");
194 }
195  
196 sub input_text_start
197 {
198 my $self = shift;
199 my ($elem) = @_;
200  
201 my $line = $self -> input_line;
202 my $name = $elem -> attr('name');
203 $self -> addKeyVal($name,$line);
204  
205 $self -> SUPER::input_text_start(@_);
206 }
207  
208 sub input_submit_start
209 {
210 my $self = shift;
211 my ($elem) = @_;
212  
213 #print "submit here\n";
214  
215 my $check = $self -> input_checkbox;
216
217 # sanity check
218 my $pBody = $self -> {pBody};
219 $$pBody =~ /.+?check preceding box/
220 or $self -> myDie("CORRUPT: parsed past submit query\n");
221 my $between = $&;
222 $between =~ /\[/
223 and $` !~ /\\$/
224 and $self -> myDie("CORRUPT: submit not aligned with submit query: $between\n");
225  
226 if ($check)
227 {
228 my $form = $self -> {current_form};
229 my $formDieText = $form -> {formDieText};
230 defined $formDieText and
231 $self -> myDie($formDieText);
232  
233 $form -> {getweb_submit} = 1;
234  
235 # jfjf check on if this key-value is correct
236 my $name = $elem -> attr('name');
237
238 defined $name and
239 $self -> addKeyVal($name,$elem -> attr('value'));
240 }
241 $self -> SUPER::input_submit_start;
242 }
243  
244 # jfj eliminate all form letters from code
245  
246 sub input_checkbox_start
247 {
248 my $self = shift;
249 my ($elem) = @_;
250  
251 my $check = $self -> input_checkbox;
252 if ($check)
253 {
254 my $unique = (lc $elem -> attr('type')) eq 'checkbox'?0:1;
255 my $key = $elem -> attr('name');
256 my $val = $elem -> attr('value');
257 $val eq '' and $val = 'on';
258 $self -> addKeyVal($key,$val,$unique);
259 }
260  
261 $self -> SUPER::input_checkbox_start(@_);
262 }
263  
264 sub option_start
265 {
266 my $self = shift;
267 my ($elem) = @_;
268  
269 my $check = $self -> input_checkbox;
270 if ($check)
271 {
272 my $select = $self -> {current_select};
273 my $unique = ! $select -> attr('multiple');
274 my $val = $elem -> attr('value');
275 my $key = $self -> {select_name};
276  
277 $self -> addKeyVal($key,$val,$unique);
278 }
279  
280 $self -> SUPER::option_start(@_);
281 }
282  
283 sub textarea_start
284 {
285 my $self = shift;
286 my ($elem) = @_;
287  
288 # jfj figure out correct newline behavior in textarea
289  
290 my $text = "";
291  
292 my $rows = $elem -> attr('rows');
293 my $i = $rows;
294 while ($i--)
295 {
296 my $line = $self -> input_line;
297 if ($line ne "")
298 {
299 $text .= "$line\n";
300 }
301 }
302  
303 my $name = $elem -> attr('name');
304 $self -> addKeyVal($name,$text);
305  
306 $self -> SUPER::textarea_start(@_);
307 }
308  
309 1;