blib/lib/QWizard/Generator/HTML.pm untested

filter: Statementstmt 0.0% 0 / 658 Branchbran 0.0% 0 / 234 Conditioncond 0.0% 0 / 98 MC/DCmcdc 0.0% 0 / 58 Subroutinesub 0.0% 0 / 74 Podpod 0.0% 0 / 64 totaltotal 0.0% 0 / 1186 CC 117 SCAR 33.5
CC
117
Cov
0%
do_multicheckbox
110.0
do_checkbox
72.0
do_entry
72.0
CRAP 28.5
1 package QWizard::Generator::HTML;
2  
3 #
4 # isprint() appears to be broken on some machine. This will determine if
5 # it's broken here and tell us how to proceed later.
6 #
7 my $use_np_isprint = 0;
8 if(isprint("abc\000abc") || isprint("abc\001abc") || !isprint("barra"))
9 {
10 $use_np_isprint = 1;
11 }
12  
13  
14 use strict;
15 our $VERSION = '3.15';
16 use CGI qw(escapeHTML);
17 use CGI::Cookie;
18 require Exporter;
19 use QWizard::Generator;
20 use QWizard::Storage::CGIParam;
21 use QWizard::Storage::CGICookie;
22 use IO::File;
23 use POSIX qw(isprint);
24  
25 my $defaultcss = "
26 body {
27 font-family:verdana, arial, helvetica, sans-serif;
28 background-color: white;
29 }
30  
31 span.centersection {
32 width: 100%;
33 }
34  
35 h1 {
36 /* top and bottom borders: 1px; left and right borders: 0px*/
37 border-width:1px;
38 border-color:black;
39 border-style:solid;
40 background-color: #9df;
41 padding-left: 5px;
42 }
43  
44 input,.qwcheckbox,select,.qwradio,.qwtext {
45 background-color: #cff;
46 }
47  
48 .qwnext {
49 margin-top: 15px;
50 background-color: #9df;
51 float: left;
52 }
53  
54 .qwcancel {
55 margin-top: 15px;
56 background-color: #9df;
57 float: right;
58 }
59  
60 p {
61 margin-left: 25px;
62 }
63  
64 h2 {
65 /* top and bottom borders: 1px; left and right borders: 0px*/
66 border-width:1px;
67 border-color:black;
68 border-style:solid;
69 background-color: #9df;
70 margin-left: 10px;
71 padding-left: 5px;
72 }
73  
74 #qwlabelpagenum {
75 font-weight: bold;
76 }
77  
78 #qwlabelpageauthor {
79 font-weight: bold;
80 }
81  
82 #qwparagraphstory {
83 font-family:verdana, arial, helvetica, sans-serif;
84 }
85  
86 #story {
87 /* top and bottom borders: 1px; left and right borders: 0px*/
88 border-width:1px 0px;
89 border-color:black;
90 border-style:solid;
91 background-color: #93d5ea;
92 }
93  
94 #qwtablewhatnext {
95 border-color:black;
96 border-style:solid;
97 background-color: #93d5ea;
98 width: 100%;
99 }
100  
101 #qwlabel {
102 align: left;
103 }
104  
105 #qwtablerowwhatnext {
106 align: left;
107 border-width:10px 0px;
108 }
109  
110 #qwtablewidget {
111 align: left;
112 border-width:10px 0px;
113 }
114  
115 ";
116  
117 @QWizard::Generator::HTML::ISA = qw(Exporter QWizard::Generator);
118  
119 our %defaults = (
120 form_name => 'qwform',
121 tmpdir => '/tmp',
122 one_pass => 1,
123 );
124  
125 our $have_gd_graph = eval { require GD::Graph::lines; };
126  
127 our $redo_screen_js =
128 "this.form.redo_screen.value=1; this.form.submit();";
129  
130 sub new {
131 my $type = shift;
132 my ($class) = ref($type) || $type;
133 my %self = %defaults;
134 for (my $i = 0; $i <= $#_; $i += 2) {
135 $self{$_[$i]} = $_[$i+1];
136 }
137 my $self = \%self;
138 bless($self, $class);
139 $self->add_handler('text',\&QWizard::Generator::HTML::do_entry,
140 [['single','name'],
141 ['default'],
142 ['forced','0'],
143 ['single','size'],
144 ['single','maxsize'],
145 ['single','submit'],
146 ['single','refresh_on_change']]);
147 $self->add_handler('hidetext',\&QWizard::Generator::HTML::do_entry,
148 [['single','name'],
149 ['default'],
150 ['forced','1'],
151 ['single','size'],
152 ['single','maxsize'],
153 ['single','submit'],
154 ['single','refresh_on_change']]);
155 $self->add_handler('textbox',\&QWizard::Generator::HTML::do_textbox,
156 [['default'],
157 ['single', 'width'],
158 ['single', 'size'],
159 ['single', 'height'],
160 ['single', 'submit'],
161 ['single','refresh_on_change']]);
162 $self->add_handler('checkbox',\&QWizard::Generator::HTML::do_checkbox,
163 [['multi','values'],
164 ['default'],
165 ['single', 'submit'],
166 ['single','refresh_on_change'],
167 ['single','button_label']]);
168 $self->add_handler('multi_checkbox',
169 \&QWizard::Generator::HTML::do_multicheckbox,
170 [['multi','default'],
171 ['values,labels'],
172 ['single','submit'],
173 ['single','refresh_on_change']]);
174 $self->add_handler('menu',
175 \&QWizard::Generator::HTML::do_menu,
176 [['values,labels', " "],
177 ['default'],
178 ['single','submit'],
179 ['single','refresh_on_change'],
180 ['single', 'name']]);
181 $self->add_handler('radio',
182 \&QWizard::Generator::HTML::do_radio,
183 [['values,labels'],
184 ['default'],
185 ['single','submit'],
186 ['single','refresh_on_change'],
187 ['single','name'],
188 ['single','icons'],
189 ['single','noiconpadding'],
190 ]);
191 $self->add_handler('label',
192 \&QWizard::Generator::HTML::do_label,
193 [['multi','values']]);
194 $self->add_handler('link',
195 \&QWizard::Generator::HTML::do_link,
196 [['single','linktext'],
197 ['single','url']]);
198 $self->add_handler('paragraph',
199 \&QWizard::Generator::HTML::do_paragraph,
200 [['multi','values'],
201 ['single','preformatted']]);
202 $self->add_handler('button',
203 \&QWizard::Generator::HTML::do_button,
204 [['single','values']]);
205 $self->add_handler('table',
206 \&QWizard::Generator::HTML::do_table,
207 [['norecurse','values'],
208 ['norecurse','headers']]);
209 $self->add_handler('bar',
210 \&QWizard::Generator::HTML::do_bar,
211 [['norecurse','values']]);
212 $self->add_handler('graph',
213 \&QWizard::Generator::HTML::do_graph,
214 [['norecurse','values'],
215 ['norecursemulti','graph_options']]);
216 $self->add_handler('image',
217 \&QWizard::Generator::HTML::do_image,
218 [['norecurse','imgdata'],
219 ['norecurse','image'],
220 ['single','imagealt'],
221 ['single', 'height'],
222 ['single', 'width']]);
223 $self->add_handler('fileupload',
224 \&QWizard::Generator::HTML::do_fileupload,
225 [['default','values']]);
226 $self->add_handler('filedownload',
227 \&QWizard::Generator::HTML::do_filedownload,
228 [['single','name'],
229 ['default'],
230 ['single','data'],
231 ['noexpand','datafn'],
232 ['single','extension'],
233 ['single','linktext']
234 ]);
235  
236 $self->add_handler('unknown',
237 \&QWizard::Generator::HTML::do_unknown,
238 []);
239  
240 $self->{'datastore'} = new QWizard::Storage::CGIParam;
241 $self->{'prefstore'} = new QWizard::Storage::CGICookie;
242  
243 return $self;
244 }
245  
246 sub init_cgi {
247 my $self = shift;
248 if (!exists($self->{'cgi'})) {
249 # we do this here late binding as possible for various reasons
250 $self->{'cgi'} = new CGI;
251 }
252 }
253  
254 sub init_screen {
255 my ($self, $wiz, $title) = @_;
256 $self->init_cgi();
257  
258 return if ($self->{'started'} || $wiz->{'started'});
259 $self->{'started'} = $wiz->{'started'} = $self->{'prefstore'}{'started'} =1;
260 $self->{'first_tree'} = 1;
261 my @otherargs;
262 if ($self->{'cssurl'}) {
263 push @otherargs, 'style', { src => $self->{'cssurl'}};
264 } elsif (!$self->{'nocss'}) {
265 push @otherargs, 'style', { code => $defaultcss };
266 }
267 print "Content-type: text/html\n\n" if (!$self->{'noheaders'} &&
268 !$wiz->{'noheaders'});
269 print $self->{'cgi'}->start_html(-title => escapeHTML($title),
270 -bgcolor => $self->{'bgcolor'}
271 || $wiz->{'bgcolor'} || "#ffffff",
272 @otherargs);
273  
274 if ($self->{'prefstore'}->{'immediate_out'} &&
275 $#{$self->{'prefstore'}->{'immediate_out'}} > -1) {
276 print @{$self->{'prefstore'}->{'immediate_out'}};
277 delete $self->{'prefstore'}->{'immediate_out'};
278 }
279 print $self->{'cgi'}->start_multipart_form(-name => $self->{'form_name'}),
280 "\n";
281 $self->{'wizard'} = $wiz;
282 }
283  
284 # html always waits
285 sub wait_for {
286 my ($self, $wiz, $next, $p) = @_;
287 print $self->{'cgi'}->end_form();
288 print "</tr>\n" if (exists($self->{'nocss'}));
289 $self->close_div_or_table(); # end for <div class="qwizard"> in start_primaries
290 return 1;
291 }
292  
293 sub do_css {
294 my ($self, $class, $name, $noidstr) = @_;
295 if (!exists($self->{'nocss'})) {
296 my $idstr = '';
297 $idstr = $class if (!$noidstr);
298 return " class=\"$class\" id=\"$idstr$name\" ";
299 }
300 return "";
301 }
302  
303 sub open_div_or_table {
304 my $self = shift;
305 if (!exists($self->{'nocss'})) {
306 print "<div class=\"" . $_[0] . "\">\n";
307 } else {
308 print "<table $_[1]>\n";
309 }
310 }
311  
312 sub close_div_or_table {
313 my $self = shift;
314 if (!exists($self->{'nocss'})) {
315 print "</div>\n";
316 } else {
317 print "</table>\n";
318 }
319 }
320  
321 sub open_div_or_tr {
322 my $self = shift;
323 if (!exists($self->{'nocss'})) {
324 print "<div class=\"" . $_[0] . "\">\n";
325 } else {
326 print "<tr $_[1]>\n";
327 }
328 }
329  
330 sub open_span_or_td {
331 my $self = shift;
332 if (!exists($self->{'nocss'})) {
333 print "<span class=\"" . $_[0] . "\">\n";
334 } else {
335 print "<td valign=\"top\" $_[1]>\n";
336 }
337 }
338  
339 sub close_span_or_td {
340 my $self = shift;
341 if (!exists($self->{'nocss'})) {
342 print "</span>\n";
343 } else {
344 print "</td>\n";
345 }
346 }
347  
348 sub close_div_or_tr {
349 my $self = shift;
350 if (!exists($self->{'nocss'})) {
351 print "</div>\n";
352 } else {
353 print "</tr>\n";
354 }
355 }
356  
357 sub get_image {
358 my ($self, $img, $css, $name) = @_;
359  
360 return "<img" . $self->do_css($css,$name) .
361 " src=\"" . $self->{'imgpath'} . escapeHTML($img) . "\"> ";
362 }
363  
364 sub do_question {
365 my ($self, $q, $wiz, $p, $text, $qcount) = @_;
366 return if (!$text && $q->{'type'} eq 'hidden');
367 my $padtext;
368 $padtext=" style=\"padding-left: 2em;\"" if ($q->{'indent'});
369 print " <tr" . $self->do_css('qwquestion',$q->{'name'}) . ">";
370 print "<td" . $self->do_css('qwquestiontext',$q->{'name'}) .
371 " valign=top $padtext>\n";
372 $text = QWizard::Generator::remove_accelerator($text);
373 if ($q->{'helptext'}) {
374 print $wiz->make_help_link($p, $qcount),
375 $self->maybe_escapeHTML($text, $q->{'noescape'}), "</a>\n";
376 } else {
377 print $self->maybe_escapeHTML($text, $q->{'noescape'});
378 }
379 if ($q->{'helpdesc'}) {
380  
381 #
382 # Get the actual help text, in case this is a subroutine.
383 #
384 my $helptext = $q->{'helpdesc'};
385 if (ref($helptext) eq "CODE") {
386 $helptext = $helptext->();
387 }
388 print "<br><small><i>" .
389 $self->maybe_escapeHTML($helptext, $q->{'noescape'}) . "</i></small>";
390 }
391 print "</td><td" . $self->do_css('qwquestion',$q->{'name'}, 1) . ">\n";
392 }
393  
394 sub do_question_end {
395 my ($self, $q, $wiz, $p, $qcount) = @_;
396  
397 #
398 # help text
399 #
400 return if (!$q->{'text'} && $q->{'type'} eq 'hidden');
401 print "</tr>\n";
402 }
403  
404 sub start_questions {
405 my ($self, $wiz, $p, $title, $intro) = @_;
406 # print "<td id=\"qwmain\" width=\"100%\">\n";
407 print "<span id=\"qwmain\">\n";
408 if ($title) {
409 print $self->{'cgi'}->h1(escapeHTML($title)),"\n";
410 }
411 if ($intro) {
412 $intro = $self->maybe_escapeHTML($intro, $p->{'noescape'});
413 $intro =~ s/\n\n/\n<p class=\"qwintroduction\">\n/g;
414 print "<p class=\"qwintroduction\">$intro\n<p class=\"qwintroduction\">\n";
415 }
416 print "<table class=\"qwquestions\">\n";
417 $self->{'intable'} = 1;
418 }
419  
420 sub end_questions {
421 my ($self, $wiz, $p) = @_;
422 print "</table>\n";
423 # print "</td>\n";
424 print "</span>\n";
425 #
426 # This focus() call should allow the user to type directly into the
427 # first text box without having to click there first.
428 #
429 print "<script>\n";
430 print "document.forms[0].elements[0].focus();\n";
431 print "</script>\n";
432  
433 $self->{'started'} = $wiz->{'started'} = 0;
434 delete($self->{'intable'});
435 }
436  
437 sub do_pass {
438 my ($self, $wiz, $name) = @_;
439 $self->do_hidden($wiz, $name, $self->qwparam($name))
440 if ($self->qwparam($name) ne '');
441 }
442  
443 ##################################################
444 # Bar support
445 ##################################################
446  
447 sub start_bar {
448 my ($self, $wiz, $name) = @_;
449 if ($self->{'intable'}) {
450 print "</table>\n";
451 }
452 print "<div " . $self->do_css('qwbar',$name) . ">\n";
453 }
454  
455 sub end_bar {
456 my ($self, $wiz, $name) = @_;
457 print "</div>\n";
458 if ($self->{'intable'}) {
459 print "<table class=\"qwquestions\">\n";
460 }
461 }
462  
463 sub do_bar {
464 my ($self, $q, $wiz, $p, $widgets) = @_;
465  
466 $self->start_bar($wiz, undef);
467 $self->do_a_table([$widgets], 0, $wiz, $q, $p);
468 $self->end_bar($wiz, 'Questions');
469 }
470  
471 sub do_top_bar {
472 my ($self, $q, $wiz, $p, $widgets) = @_;
473  
474 print "<tr><td colspan=\"10\">" if (exists($self->{'nocss'}));
475 $self->do_a_table([$widgets], 0, $wiz, $q, $p, 'topbar', 'topbar','topbar');
476 print "</td></tr>\n" if (exists($self->{'nocss'}));
477 }
478  
479 sub start_center_section {
480 my ($self) = @_;
481 $self->open_span_or_td("centersection");
482 }
483  
484 sub end_center_section {
485 my ($self, $wiz, $p, $next) = @_;
486 print "<div class=\"buttons\">\n";
487 print " <input class=qwnext type=submit value=\"" . escapeHTML(QWizard::Generator::remove_accelerator($next)) . "\">\n";
488 $self->do_hidden($wiz, "redo_screen", 0) if (!$self->qwparam('redo_screen'));
489 if ($self->qwparam('allow_refresh') || $p->{'allow_refresh'}) {
490 print "<input type=submit onclick=\"$redo_screen_js\" name=redo_screen_but value=\"Refresh Screen\">\n";
491 }
492 print " <input class=qwcancel name=\"qw_cancel\" type=submit value=\"Cancel\">\n" if (!$self->{'no_cancel'});
493 print "</div>\n";
494 $self->close_span_or_td();
495 }
496  
497 sub start_primaries {
498 my ($self) = @_;
499 # this is closed in wait_for()
500 $self->open_div_or_table("qwizard");
501 print "<tr>\n" if (exists($self->{'nocss'}));
502 }
503  
504 sub do_side {
505 my ($self, $spot, $q, $wiz, $p, $widgets) = @_;
506 $self->open_span_or_td($spot);
507  
508 my @tableinfo;
509 foreach my $w (@$widgets) {
510 next if (!$w);
511 if (ref($w) eq 'ARRAY') {
512 # special stand-alone side component
513 my $title = "";
514 $title = shift(@$w) if (ref($w->[0]) eq '');
515 my $id = $title;
516 $id =~ s/\W//;
517 print "<div id=\"side$id\" class=\"side\">\n";
518 print "<div id=\"sidetitle$id\" class=\"sidetitle\">" .
519 escapeHTML($title) . "</div>\n" if ($title);
520 print "<table id=\"sidecontent$id\" class=\"sidecontent\">\n";
521 foreach my $widget (@$w) {
522 # print "widget: $widget\n";
523 $wiz->ask_question($p, $widget);
524 }
525 print "</table>\n";
526 print "</div>\n";
527 } else {
528 # add to the default (bottom) table component
529 push @tableinfo, [$w];
530 }
531 }
532  
533 $self->do_a_table(\@tableinfo, 0, $wiz, $q, $p, $spot);
534 $self->close_span_or_td();
535 }
536  
537 sub do_left_side {
538 my $self = shift;
539 $self->do_side('leftside', @_);
540 }
541  
542 sub do_right_side {
543 my $self = shift;
544 $self->do_side('rightside', @_);
545 }
546  
547 ##################################################
548 # widgets
549 ##################################################
550  
551 sub do_button {
552 my ($self, $q, $wiz, $p, $vals) = @_;
553 print "<input" . $self->do_css('qwbutton',$q->{'name'}) . " type=submit name=\"$q->{'name'}\" value=\"" . QWizard::Generator::remove_accelerator($vals) . "\">\n";
554 }
555  
556 sub do_checkbox {
557 my ($self, $q, $wiz, $p, $vals, $def, $submit, $refresh_on_change,
558 $button_label) = @_;
559 $vals = [1, 0] if ($#$vals == -1);
560 my $otherstuff;
561 if ($def == $vals->[0]) {
562 $otherstuff .= " checked";
563 }
564 if ($#$vals > -1) {
565 $otherstuff .= " value=\"" . escapeHTML($vals->[0]) . "\"";
566 }
567 if ($submit) {
568 $otherstuff .= " onclick=\"this.form.submit()\"";
569 }
570 if ($refresh_on_change) {
571 $otherstuff .= " onclick=\"$redo_screen_js\"";
572 }
573 if ($button_label) {
574 print "<span " . $self->do_css('qwbuttonlabel',$q->{'name'}) . ">";
575 }
576 print "<input" . $self->do_css('qwcheckbox',$q->{'name'}) . " type=checkbox name=\"$q->{name}\"$otherstuff>";
577 if ($button_label) {
578 print " $button_label</span>\n";
579 }
580 }
581  
582 sub do_multicheckbox {
583 my ($self, $q, $wiz, $p, $defs, $vals, $labels,
584 $submit, $refresh_on_change) = @_;
585 print "<table>";
586 my $count = -1;
587 my ($startname, $endname);
588 foreach my $v (@$vals) {
589 $count++;
590 my $otherstuff;
591  
592 if ($wiz->qwparam('redoing_now')) {
593 $otherstuff .= "checked"
594 if ($wiz->qwparam($q->{'name'} . $v) eq $v);
595 } else {
596 $otherstuff .= "checked" if ($defs->[$count]);
597 }
598
599  
600 $otherstuff .= "checked" if ($defs->[$count]);
601 if ($submit) {
602 $otherstuff .= " onclick=\"this.form.submit()";
603 }
604 if ($refresh_on_change) {
605 $otherstuff .= " onclick=\"$redo_screen_js\"";
606 }
607 my $l = QWizard::Generator::remove_accelerator(($labels->{$v}) ? $labels->{$v} : "$v");
608 print "<tr><td>" . escapeHTML($l) . "</td>\n";
609 print "<td><input" .
610 $self->do_css('qwmulticheckbox',$q->{'name'}) .
611 " $otherstuff value=\"" .
612 escapeHTML($v) .
613 "\" type=checkbox name=\"" .
614 escapeHTML("$q->{name}$v") . "\"></td></tr>";
615 # XXX: hack:
616 push @{$wiz->{'passvars'}},$q->{'name'} . $v;
617 $startname = escapeHTML("$q->{name}$v") if ($count == 0);
618 $endname = escapeHTML("$q->{name}$v") if ($count == $#$vals);
619 }
620  
621 print "</table>";
622  
623 #Javascript for setting/unsetting/toggling buttons
624 print "
625  
626 <script language=\"JavaScript\">
627 function $q->{name}_setall() {
628 var doit = false;
629 for (i=0; i<document.qwform.elements.length; i++) {
630 if (document.qwform.elements[i].type == \"checkbox\") {
631 if (document.qwform.elements[i].name == \"$startname\") {
632 doit = true;
633 }
634 if (doit) {
635 document.qwform.elements[i].checked = true;
636 }
637 if (document.qwform.elements[i].name == \"$endname\") {
638 doit = false;
639 }
640 }
641 }
642 }
643  
644 function $q->{name}_unsetall() {
645 var doit = false;
646 for (i=0; i<document.qwform.elements.length; i++) {
647 if (document.qwform.elements[i].type == \"checkbox\") {
648 if (document.qwform.elements[i].name == \"$startname\") {
649 doit = true;
650 }
651 if (doit) {
652 document.qwform.elements[i].checked = false;
653 }
654 if (document.qwform.elements[i].name == \"$endname\") {
655 doit = false;
656 }
657 }
658 }
659 }
660  
661 function $q->{name}_toggleall() {
662 var doit = false;
663 for (i=0; i<document.qwform.elements.length; i++) {
664 if (document.qwform.elements[i].type == \"checkbox\") {
665 if (document.qwform.elements[i].name == \"$startname\") {
666 doit = true;
667 }
668 if (doit) {
669 if (document.qwform.elements[i].checked) {
670 document.qwform.elements[i].checked = false;
671 } else {
672 document.qwform.elements[i].checked = true;
673 }
674 }
675 if (document.qwform.elements[i].name == \"$endname\") {
676 doit = false;
677 }
678 }
679 }
680 }
681 ";
682 # foreach my $boxname (@boxnames) {
683 # print " document.qwform.try1.checked=true;\n";
684 # # print " document.qwform.\"$boxname\".checked=true;\n";
685 # }
686 print "
687 </script>
688  
689 <a href=\"javascript:$q->{name}_setall()\">[Set All]</a>
690 <a href=\"javascript:$q->{name}_unsetall()\">[Unset All]</a>
691 <a href=\"javascript:$q->{name}_toggleall()\">[Toggle All]</a>
692  
693 ";
694  
695 }
696  
697 sub do_radio {
698 my ($self, $q, $wiz, $p, $vals, $labels, $def,
699 $submit, $refresh_on_change, $name, $icons, $iconwidth) = @_;
700 my $stuff;
701 $stuff = " onclick=\"this.form.submit()\" " if ($submit);
702 $stuff = " onclick=\"$redo_screen_js\" " if ($refresh_on_change);
703  
704 # remove the key accelerators
705 my %passlabs = %$labels;
706 # remove key bindings specifiers
707 map {
708 $passlabs{$_} = QWizard::Generator::remove_accelerator($passlabs{$_});
709 } keys(%passlabs);
710  
711 # correct the ordering
712 my @passvals = reverse @$vals;
713  
714 foreach my $value (@passvals) {
715 print " <input type=\"radio\" name=\"$name\" value=\"" .
716 escapeHTML($value) . "\" $stuff " .
717 $self->do_css('qwradio',$q->{'name'}) . " />\n " .
718 (($icons->{$value}) ?
719 $self->get_image($icons->{$value}, 'qwradioimg', $q->{'name'}) :
720 (($iconwidth) ? "<span style=\"width: ${iconwidth};\" />" : "")).
721 $passlabs{$value} .
722 "<br />\n";
723 }
724 }
725  
726  
727 sub do_label {
728 my ($self, $q, $wiz, $p, $vals, $def) = @_;
729 if (defined ($vals)) {
730 my @labs = @$vals; # copy this so map doesn't modify the source
731 map { $_ = escapeHTML($_) } @labs;
732 print "<span" . $self->do_css('qwlabel',$q->{'name'}) . ">" .
733 join("<br>", @labs) . "</span>\n";
734 }
735 }
736  
737 sub do_link {
738 my ($self, $q, $wiz, $p, $text, $url) = @_;
739 print $self->{'cgi'}->a({href => $url,
740 id => $q->{'name'},
741 class => 'qwlink' . $q->{'name'}}, $text);
742 }
743  
744 sub do_paragraph {
745 my ($self, $q, $wiz, $p, $vals, $preformatted) = @_;
746 my @labs = @$vals; # copy this so map doesn't modify the source
747 map { $_ = escapeHTML($_) } @labs;
748 if ($preformatted) {
749 print "<pre" . $self->do_css('qwparagraph',$q->{'name'}) . ">\n",
750 @labs,"</pre>\n";
751 } else {
752 print "<span" . $self->do_css('qwparagraph',$q->{'name'}) . ">" .
753 join("<br><br>", @labs) . "</span>\n";
754 }
755 }
756  
757 sub do_menu {
758 my ($self, $q, $wiz, $p, $vals, $labels, $def,
759 $submit, $refresh_on_change, $name) = @_;
760 my @stuff;
761 push @stuff, -onchange, "this.form.submit()" if ($submit);
762 push @stuff, -onchange, "$redo_screen_js" if ($refresh_on_change);
763  
764 print $self->{'cgi'}->popup_menu(-name => $name,
765 -id => 'qwmenu' . $name,
766 -class => 'qwmenu',
767 -values => $vals,
768 -override => 1,
769 -labels => $labels,
770 -default => $def,
771 @stuff);
772 }
773  
774 sub do_fileupload {
775 my ($self, $q, $wiz, $p, $vals, $labels, $def) = @_;
776  
777 push @{$wiz->{'passvars'}}, $q->{'name'} . "_qwf";
778 print $self->{'cgi'}->filefield(-name => $q->{name},
779 -id => 'qwmenu' . $q->{'name'},
780 -class => 'qwmenu',
781 -override => 1,
782 -default => $def);
783 }
784  
785 sub qw_upload_file {
786 my ($self) = shift;
787 my ($it);
788 my $ret;
789 if (ref($self) =~ /QWizard/) {
790 $it = shift;
791 } else {
792 $it = $self;
793 }
794 if (!exists($self->{'cgi'})) {
795 $self->{'cgi'} = new CGI;
796 }
797  
798 my $fn;
799 if (!$self->qwparam($it . "_qwf")) {
800 # copy the file to a local qwizard copy of it
801  
802 # XXX: check error if undef; puts it in $self->{'cgi'}->cgi_error
803 my $fh = $self->{'cgi'}->upload($it);
804 $fn = $self->create_temp_file('.tmp', $fh);
805 $fn =~ s/(.*)\///;
806 $fn =~ s/$self->{'tmpdir'}\/+//;
807 $fn =~ s/\.tmp$//;
808 $self->qwparam($it . "_qwf", $fn);
809 } else {
810 $fn = $self->qwparam($it . "_qwf");
811 $fn =~ s/[^a-zA-Z0-9]//;
812 }
813  
814 $fn = $fn . ".tmp";
815 $fn = $self->{'tmpdir'} . "/" . $fn;
816 return $fn;
817 }
818  
819 sub qw_upload_fh {
820 my ($self) = shift;
821 my ($it);
822 my $ret;
823 if (ref($self) =~ /QWizard/) {
824 $it = shift;
825 } else {
826 $it = $self;
827 }
828 if (!exists($self->{'cgi'})) {
829 $self->{'cgi'} = new CGI;
830 }
831  
832 my $fn;
833 if (!$self->qwparam($it . "_qwf")) {
834 # copy the file to a local qwizard copy of it
835  
836 # XXX: check error if undef; puts it in $self->{'cgi'}->cgi_error
837 my $fh = $self->{'cgi'}->upload($it);
838 print STDERR "*" x 20 . ref($fh) . "++\n";
839 $fn = $self->create_temp_file('.tmp', $fh);
840 $fn =~ s/(.*)\///;
841 $fn =~ s/$self->{'tmpdir'}\/+//;
842 $fn =~ s/\.tmp$//;
843 $self->qwparam($it . "_qwf", $fn);
844 print STDERR "*" x 20 . " -> $it -> $fn -> " . ref($fh) . "++\n";
845 } else {
846 $fn = $self->qwparam($it . "_qwf");
847 $fn =~ s/[^a-zA-Z0-9]//;
848 print STDERR "*" x 80 . $fn,"\n";
849 }
850  
851 $fn = $fn . ".tmp";
852 $fn = $self->{'tmpdir'} . "/" . $fn;
853  
854 my $retfh = new IO::File;
855 $retfh->open("<$fn");
856  
857 return $retfh;
858 }
859  
860 sub do_entry {
861 my ($self, $q, $wiz, $p, $name, $def, $hide, $size, $maxsize,
862 $submit, $refresh_on_change) = @_;
863 my $otherinfo;
864 if ($size) {
865 $otherinfo .= " size=\"$size\"";
866 } else {
867 if ($maxsize) {
868 $otherinfo .= " size=\"$maxsize\"";
869 }
870 }
871 if ($maxsize) {
872 $otherinfo .= " maxlength=\"$maxsize\"";
873 }
874 if ($def ne '') {
875 $otherinfo .= " value=\"" . escapeHTML($def) . "\"";
876 }
877 if ($submit) {
878 $otherinfo .= " onchange=\"this.form.submit()\"";
879 }
880 if ($refresh_on_change) {
881 $otherinfo .= " onclick=\"$redo_screen_js\"";
882 }
883  
884 #
885 # If the hide flag was set, we'll treat this as unprintable text.
886 #
887 if ($hide) {
888 $otherinfo .= " type=\"password\"";
889 }
890  
891 print "<input" . $self->do_css('qwtext',$q->{'name'}) .
892 " name=\"$name\" $otherinfo>";
893 }
894  
895 sub do_textbox {
896 my ($self, $q, $wiz, $p, $def, $width, $size, $height, $submit, $refresh_on_change) = @_;
897 my $otherinfo;
898 if ($size || $width) {
899 $size = $size || $width;
900 $otherinfo .= " cols=\"$size\"";
901 }
902 if ($height) {
903 $otherinfo .= " rows=\"" . $height . "\"";
904 }
905 if ($submit) {
906 $otherinfo .= " onchange=\"this.form.submit()\"";
907 }
908 if ($refresh_on_change) {
909 $otherinfo .= " onclick=\"$redo_screen_js\"";
910 }
911 print "<textarea" . $self->do_css('qwtextbox',$q->{'name'}) .
912 " name=\"$q->{name}\" $otherinfo>" . escapeHTML($def) . "</textarea>";
913 }
914  
915 sub do_error {
916 my ($self, $q, $wiz, $p, $err) = @_;
917 my $name = ($q ? $q->{'name'} : '');
918 print "<tr" . $self->do_css('qwerrorrow',$name) . "><td" .
919 $self->do_css('qwerrorcol',$name) .
920 " colspan=3><font color=red>" . escapeHTML($err) .
921 "</font></td></tr>\n";
922 }
923  
924 sub do_separator {
925 my ($self, $q, $wiz, $p, $text) = @_;
926 if ($text eq "") {
927 $text = "&nbsp";
928 } else {
929 $text = escapeHTML($text);
930 }
931 my $name = (ref($q) eq 'HASH') ? $q->{'name'} : "";
932 print " <tr" . $self->do_css('qwseparatorrow',$name) .
933 "><td" . $self->do_css('qwseparatorcol',$name) .
934 " colspan=3>$text</td></tr>";
935 }
936  
937 sub do_hidden {
938 my ($self, $wiz, $name, $val) = @_;
939 print "<input type=hidden name=\"$name\" value=\"" .
940 escapeHTML($val) . "\">\n";
941 $self->qwparam($name,$val);
942 }
943  
944 sub do_unknown {
945 my ($self, $q, $wiz, $p) = @_;
946  
947 print "<font color=\"red\">Error: Unhandled question type '$q->{type}' in primary '$p->{module_name}'. It is highly likely that this page will not function properly after this point.</font>\n";
948 }
949  
950 ##################################################
951 # Display
952 ##################################################
953  
954 sub do_table {
955 my ($self, $q, $wiz, $p, $table, $headers) = @_;
956 my $color = $self->{'tablebgcolor'} || $self->{'bgcolor'};
957 print "<table" . $self->do_css('qwtable',$q->{'name'}) .
958 (!exists($self->{'nocss'}) ? "" : "bgcolor=$color border=1>") .
959 "\n";
960  
961 if ($headers) {
962 print " <tr " . $self->do_css('qwtableheaderrow',$q->{'name'}) .
963 "bgcolor=\"$self->{headerbgcolor}\">\n";
964 foreach my $column (@$headers) {
965 print "<th" . $self->do_css('qwtableheader',$q->{'name'}) .
966 ">" . ($column || "&nbsp;") . "</th> ";
967 }
968 print " </tr>\n";
969 }
970  
971 $self->do_a_table($table, 1, $wiz, $q, $p);
972 print "</table>\n";
973 }
974  
975 sub do_a_table {
976 my ($self, $table, $started, $wiz, $q, $p, $name) = @_;
977 $name = $q->{'name'} if (!$name);
978 print "<table" . $self->do_css('qwsubtable',$name) . ">"
979 if (!$started);
980 foreach my $row (@$table) {
981 print " <tr" . $self->do_css('qwtablerow',$name) . ">\n";
982 foreach my $column (@$row) {
983 print "<td>";
984 if (ref($column) eq "ARRAY") {
985 $self->do_a_table($column, 0, $wiz, $q, $p);
986 } elsif (ref($column) eq "HASH") {
987 print "<table" . $self->do_css('qwtablewidget',$name) .
988 ">\n";
989 my $param = $wiz->ask_question($p, $column);
990 push @{$wiz->{'passvars'}}, $param;
991 print "</table>\n";
992 } else {
993 my $val = $self->make_displayable($column);
994 print (defined($val) && $val ne "" ? $val : "&nbsp;");
995 }
996 print "</td>";
997 }
998 print " </tr>\n";
999 }
1000 print "</table>\n" if (!$started);
1001 }
1002  
1003 sub do_graph {
1004 my $self = shift;
1005 my ($q, $wiz, $p, $data, $gopts) = @_;
1006  
1007 if ($have_gd_graph) {
1008 my $file = $self->create_temp_file('.png', $self->do_graph_data(@_));
1009 $file =~ s/(.*)\///;
1010
1011 # XXX: net-policy specific hack!
1012 print "<img" . $self->do_css('qwgraph',$q->{'name'}) .
1013 " src=\"" . $self->{'imgpath'} . escapeHTML($file) . "\">\n";
1014 } else {
1015 print "graphs not supported without additional software\n";
1016 }
1017 }
1018  
1019 ########################################################################
1020 #
1021 sub do_image {
1022 my $self = shift;
1023 my ($q, $wiz, $p, $imgdata, $imgfile, $alt, $height, $width) = @_;
1024 my $image;
1025
1026 if ($imgdata) {
1027 # store the image in a temporary file
1028 $image = $self->create_temp_file('.png', $imgdata);
1029 $image =~ s/(.*)\///;
1030 } else {
1031 $image = $imgfile;
1032 }
1033 my $imagesrc = "src=\"" . $self->{'imgpath'} . escapeHTML($image) ."\"";
1034  
1035 #
1036 # If an alt tag was specified, create the alt image message.
1037 #
1038 my $altmsg = "alt=\"Broken Image - $image\"";
1039 if($alt ne "")
1040 {
1041 $altmsg = "alt=\"$alt\"";
1042 }
1043  
1044 #
1045 # If a height tag was specified, add the image height.
1046 #
1047 my $hmsg = " ";
1048 if($height ne "")
1049 {
1050 $hmsg = "height=\"$height\"";
1051 }
1052  
1053 #
1054 # If a width tag was specified, add the image width.
1055 #
1056 my $wmsg = " ";
1057 if($width ne "")
1058 {
1059 $wmsg = "width=\"$width\"";
1060 }
1061  
1062 print "<img" . $self->do_css('qwimage',$q->{'name'}) .
1063 " $imagesrc $altmsg $hmsg $wmsg border=1>\n";
1064 }
1065  
1066 sub do_filedownload {
1067 my ($self, $q, $wiz, $p, $name, $def, $data, $datafn, $extension,
1068 $linktext) = @_;
1069  
1070 # We simply always generate and save the file and make a link to it
1071 # XXX: this is not efficient and techinically should be generated on demand.
1072  
1073 my ($fh, $outputfile) = $self->create_temp_fh($extension || '.bin');
1074 $outputfile =~ s/.*\///;
1075  
1076 # print the passed in data
1077 print $fh $data if ($data);
1078  
1079 # if we have code to use for directly printing data, call it
1080 if ($datafn && ref($datafn) eq 'CODE') {
1081 # passed a generator function; call it
1082 $datafn->($fh, undef, $wiz, $p, $q, $outputfile);
1083 }
1084  
1085 # close it out
1086 $fh->close();
1087  
1088 # print the resulting html out
1089 print "<a href=\"" . $self->{'datapath'} . escapeHTML($outputfile) ."\">"
1090 . escapeHTML($linktext) . "</a>";
1091 }
1092  
1093  
1094  
1095 ##################################################
1096 #
1097 # Automatic updating for monitors.
1098 #
1099  
1100 sub do_autoupd
1101 {
1102 my ($self, $secs) = @_;
1103 my $msecs = $secs * 1000;
1104  
1105 if($secs eq "")
1106 {
1107 return;
1108 }
1109  
1110 # warn "\ndo_autoupd: sleeping for $secs seconds\n";
1111  
1112 #
1113 # Javascript for automatically updating the screen.
1114 #
1115 print <<EOF;
1116 <script language="JavaScript">
1117 function autoupd_$secs() {
1118 document.qwform.submit();
1119 }
1120  
1121 setTimeout("autoupd_$secs()",$msecs);
1122  
1123 </script>
1124 EOF
1125  
1126 }
1127  
1128 ##################################################
1129 # Trees
1130 ##################################################
1131  
1132 #TODO: Support passing in a hash for tree data (instead of just a function)
1133  
1134 sub do_tree {
1135 my ($self, $q, $wiz, $p, $labels, $expand_all, $def) = @_;
1136  
1137 my $treename = $q->{'name'} || 'tree';
1138  
1139 my $expanded = $self->qwparam("${treename}_expanded") || $q->{'root'};
1140 my @expand = split(/,/, $expanded);
1141 # redo_screen values:
1142 # 1: selects a label
1143 # 2: expands a branch
1144 # 3: collapses a branch
1145 my $redo = $self->qwparam("redoing_now");
1146  
1147 if ($redo == 2 && $self->qwparam("${treename}_collapse")) {
1148 push @expand, $self->qwparam("${treename}_collapse");
1149 } elsif ($redo == 3 && $self->qwparam("${treename}_collapse")) {
1150 @expand = grep(!($_ eq $self->qwparam("${treename}_collapse")),@expand);
1151 }
1152  
1153  
1154 my $selected = $self->qwparam($treename);
1155 if ($selected) {
1156 #if the selected node is hidden inside a collapsed branch, select the
1157 #closest visible node. Although it changes the selected node, this seems
1158 #better than the possibly-confusing situation of the selected node being
1159 #hidden beneath an unexpanded node.
1160 my $cur = $selected;
1161 until ($cur eq $q->{'root'}) {
1162 $cur = get_name($q->{'parent'}->($wiz, $cur) || return);
1163 my @tmp = grep($_ eq $cur, @expand);
1164 unless ($#tmp > -1) {
1165 $selected = $cur;
1166 }
1167 }
1168 } else { #ensure that the default is initially visible
1169 $selected = $def || $q->{'root'} || return;
1170 my $cur = $selected;
1171 until ($cur eq $q->{'root'}) {
1172 $cur = get_name($q->{'parent'}->($wiz, $cur) || return);
1173 push @expand, $cur;
1174 }
1175 }
1176  
1177 $expanded = join(',', @expand);
1178 $self->do_hidden($wiz, "${treename}_expanded", $expanded);
1179 if ($self->{'first_tree'}) { #only one hidden value for redo_screen
1180 $self->{'first_tree'} = 0;
1181 }
1182 $self->do_hidden($wiz, $treename, $selected);
1183  
1184 #holds the name of a node that needs to be collapsed or expanded
1185 $self->do_hidden($wiz, "${treename}_collapse", '');
1186  
1187 #Javascript for expanding/collapsing/selecting
1188 print <<EOF;
1189 <script language="JavaScript">
1190 function ${treename}_select(item, oper) {
1191 if (oper == 1) {
1192 document.qwform.${treename}.value=item;
1193 } else {
1194 document.qwform.${treename}_collapse.value=item;
1195 }
1196 document.qwform.redo_screen.value=oper;
1197 document.qwform.submit();
1198 }
1199 </script>
1200 EOF
1201  
1202 print "<div " . $self->do_css('qwtree',$treename) . ">\n";
1203 $self->print_branch($wiz, $q, $q->{'root'}, $selected, 0, $labels,
1204 \@expand, $expand_all);
1205 print "</div>\n";
1206 }
1207  
1208 sub get_name {
1209 my $node = shift;
1210  
1211 if (ref($node) eq 'HASH') {
1212 return $node->{'name'};
1213 } else {
1214 return $node;
1215 }
1216 }
1217  
1218 #recursively print out the tree
1219 sub print_branch {
1220 # XXX: css this
1221 my ($self, $wiz, $q, $cur, $selected, $nest, $labels,
1222 $expand, $expand_all) = @_;
1223  
1224 print "<br>" if $nest;
1225 for my $i (1 .. (5 * $nest)) { print "&nbsp;"; }
1226  
1227 my $children = $q->{'children'}->($wiz, get_name($cur));
1228 if ($#$children > -1) {
1229 my @ans = grep($_ eq get_name($cur), @$expand);
1230 if ($#ans > -1 || $expand_all > 0) { #is it expanded?
1231 $self->make_link('minus', 3, $cur, $selected, $q, $labels);
1232 foreach my $child (@$children) {
1233 $self->print_branch($wiz, $q, $child, $selected,
1234 $nest + 1, $labels, $expand, $expand_all-1);
1235 }
1236 } else {
1237 $self->make_link('plus', 2, $cur, $selected, $q, $labels);
1238 }
1239 } else {
1240 $self->make_link('blank', 0, $cur, $selected, $q, $labels);
1241 }
1242 }
1243  
1244 # prints a single node, and any required links, etc
1245 sub make_link {
1246 # XXX: css this
1247 my ($self, $imgtype, $oper, $cur, $selected, $q, $labels) = @_;
1248 my $name = get_name($cur);
1249 my $treename = $q->{'name'} || 'tree';
1250 print "<a href=\"javascript:${treename}_select('$name', $oper)\">" if $oper;
1251 print "<img src=\"$self->{'imgpath'}tree_$imgtype.png\" border=0>";
1252 print "</a>" if $oper;
1253 print "&nbsp;";
1254 my $label;
1255 if (ref($cur) eq 'HASH') {
1256 $label = $cur->{'label'};
1257 }
1258 $label = $label || $labels->{$name} || $name;
1259 if ($name eq $selected && $q->{'name'}) {
1260 print "<b>$label</b>";
1261 } else {
1262 print "<a href=\"javascript:${treename}_select('$name', 1);\">" if $q->{'name'};
1263 print $label;
1264 print "</a>" if $q->{'name'};
1265 print "\n";
1266 }
1267 }
1268  
1269  
1270  
1271 ##################################################
1272 # action confirm
1273 ##################################################
1274  
1275 sub start_confirm {
1276 my ($self, $wiz) = @_;
1277  
1278 print "<h1 class=\"qwconfirmtitle\">Wrapping up.</h1>\n";
1279 print $self->{'cgi'}->start_form(),"\n";
1280 print "<ul class=\"qwconfirmtop\">\n" .
1281 " <p>Do you want to commit the following changes:\n";
1282 print "<ul class=\"qwconfirmwrap\">\n";
1283 }
1284  
1285 sub end_confirm {
1286 my ($self, $wiz) = @_;
1287 print "</ul></ul>\n";
1288 # XXX: css these. id or class?
1289 print "<input type=submit name=wiz_confirmed value=\"" .
1290 ($wiz->qwparam('QWizard_commit') || "Commit") . "\">\n";
1291 print "<input type=submit name=wiz_canceled value=\"Cancel\">\n";
1292 print $self->{'cgi'}->end_form();
1293 $self->{'started'} = $wiz->{'started'} = 0;
1294 }
1295  
1296 sub do_confirm_message {
1297 my ($self, $wiz, $msg) = @_;
1298 print "<li class=\"confirmmsg\">" . $self->{'cgi'}->escapeHTML($msg) . "\n";
1299 }
1300  
1301 sub canceled_confirm {
1302 my ($self, $wiz) = @_;
1303 print $self->{'cgi'}->h1("canceled");
1304 print "<a href=\"$wiz->{top_location}\">Return to Top</a>\n";
1305 $self->{'started'} = $wiz->{'started'} = 0;
1306 }
1307  
1308 ##################################################
1309 # actions
1310 ##################################################
1311  
1312 sub start_actions {
1313 my ($self, $wiz) = @_;
1314 print $self->{'cgi'}->h1('Processing your request...');
1315 print "<div class=\"qwactions\">\n";
1316 # XXX: css pre or remove and style qwactions
1317 print "<pre>\n";
1318 }
1319  
1320 sub end_actions {
1321 my ($self, $wiz) = @_;
1322 print "</pre>\n";
1323 print "</div>\n";
1324 print $self->{'cgi'}->h2('Done!');
1325 print "<a href=\"$wiz->{top_location}\">" .
1326 ($wiz->qwparam('QWizard_finish') || "Return to Top") .
1327 "</a>\n";
1328 $self->{'started'} = $wiz->{'started'} = 0;
1329 }
1330  
1331 sub do_action_output {
1332 my ($self, $wiz, $action) = @_;
1333 print "<div class=\"qwaction\">" . escapeHTML($action) . "</div>\n";
1334 }
1335  
1336 sub do_action_error {
1337 my ($self, $wiz, $errstr) = @_;
1338 print "<font color=red size=+1><div class=\"qwactionerror\">ERROR: <b>" . escapeHTML($errstr) .
1339 "</b></div></font>\n";
1340 }
1341  
1342 sub make_displayable {
1343 my ($self, $str);
1344 if ($#_ > 0) {
1345 ($self, $str) = @_;
1346 } else {
1347 ($str) = @_;
1348 }
1349  
1350 my $transit = 0;
1351  
1352 #
1353 # If we have a broken isprint(), do the check ourselves. Otherwise,
1354 # use the builtin.
1355 #
1356 if($use_np_isprint == 1) {
1357 $transit = ($str =~ /[^\w\s!\@\#\$\%\^\&\*\(\)\.]/);
1358 }
1359 else {
1360 $transit = (!isprint($str));
1361 }
1362  
1363 #
1364 # If translation is required, convert the string to its hex equivalent.
1365 #
1366 if(length($str) != 0 && $transit == 1) {
1367 $str = "0x" . (unpack("H*", $str))[0];
1368 }
1369  
1370 # properly escape any html
1371 if (!$self || !exists($self->{'noescapehtml'})) {
1372 $str = escapeHTML($str);
1373 }
1374  
1375 return $str;
1376 }
1377  
1378 sub maybe_escapeHTML {
1379 my ($self, $text, $noescapeit) = @_;
1380 return $text if ($self->{'noescape'} || $noescapeit);
1381 return escapeHTML($text);
1382 }
1383  
1384  
1385 1;