Fix XSS in date chooser
[webmin.git] / date_chooser.cgi
1 #!/usr/local/bin/perl
2 # date_chooser.cgi
3 # Display a table of days in the current month
4
5 $trust_unknown_referers = 1;
6 require './web-lib.pl';
7 require 'timelocal.pl';
8 &init_config();
9 &ReadParse();
10
11 @daysin = ( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
12 $daysin[1] = $in{'year'}%400 == 0 ? 29 :
13              $in{'year'}%100 == 0 ? 28 :
14              $in{'year'}%4 == 0 ? 29 : 28;
15
16 @tm = localtime(time());
17 if ($in{'day'} !~ /^\d+$/ || $in{'day'} < 1 || $in{'year'} !~ /^\d+$/) {
18         $in{'day'} = $tm[3];
19         $in{'month'} = $tm[4];
20         $in{'year'} = $tm[5]+1900;
21         }
22 if ($in{'day'} > $daysin[$in{'month'}]) {
23         $in{'day'} = $daysin[$in{'month'}];
24         }
25 $tm = timelocal(0, 0, 12, $in{'day'}, $in{'month'}, $in{'year'});
26
27 &popup_header($text{'chooser_date'});
28 $uday = &urlize($in{'day'});
29 $umonth = &urlize($in{'month'});
30 $uyear = &urlize($in{'year'});
31 print <<EOF;
32 <script>
33 function newmonth(m)
34 {
35 location = "date_chooser.cgi?day=$uday&month="+m.selectedIndex+"&year=$uyear";
36 }
37 function newyear(y)
38 {
39 location = "date_chooser.cgi?day=$uday&month=$umonth&year="+(y.selectedIndex+$in{'year'}-10);
40 }
41 function newday(d)
42 {
43 opener.dfield.value = d;
44 opener.mfield.selectedIndex = $umonth;
45 opener.yfield.value = $uyear;
46 close();
47 }
48 </script>
49 <form><table border width=100%>
50 <tr> <td colspan=7 align=center><select name=year onChange='newyear(this)'>
51 EOF
52 for($i=$in{'year'}-10; $i<=$in{'year'}+10; $i++) {
53         printf "<option %s>%s\n",
54                 $i == $in{'year'} ? 'selected' : '', $i;
55         }
56 print "</select> <select name=month onChange='newmonth(this)'>\n";
57 for($i=0; $i<12; $i++) {
58         printf "<option value=%s %s>%s\n",
59                 $i, $i == $in{'month'} ? 'selected' : '',
60                 $text{"month_".($i+1)};
61         }
62 print "</select></td> </tr>\n";
63
64 print "<tr>\n";
65 for($i=0; $i<7; $i++) {
66         print "<td><b>",$text{"sday_$i"},"</b></td>\n";
67         }
68 print "</tr>\n";
69
70 @first = localtime($tm - ($in{'day'}-1)*24*60*60);
71 $count = -$first[6] + 1;
72 for($y=0; $y<6; $y++) {
73         print "<tr>\n";
74         for($x=0; $x<7; $x++) {
75                 if ($count < 1 || $count > $daysin[$in{'month'}]) {
76                         print "<td align=center></td>\n";
77                         }
78                 else {
79                         printf "<td align=center %s><a href='' onClick='newday($count)'>%s</a></td>\n", $in{'day'} == $count ? $cb : '', $count;
80                         }
81                 $count++;
82                 }
83         print "</tr>\n";
84         }
85 print "</table></form>\n";
86 &popup_footer();
87