Handle hostnames with upper-case letters
[webmin.git] / apache / mod_expires.pl.broken
1 # mod_expires.pl
2 # Defines editors for setting expires times
3
4 sub mod_expires_directives
5 {
6 local($rv);
7 $rv = [ [ 'ExpiresActive ExpiresByType ExpiresDefault', 1, 5,
8           'virtual directory htaccess' ] ];
9 return &make_directives($rv, $_[0], "mod_expires");
10 }
11
12 sub edit_ExpiresActive_ExpiresByType_ExpiresDefault
13 {
14 local($rv, $max, $i);
15 $rv = "Generate Expires headers?\n".
16       &choice_input($_[0]->[0]->{'value'}, "ExpiresActive", "off",
17       "Yes,on", "No,off")."<br>\n";
18 $rv .= "Default expiry time?\n";
19 $rv .= sprintf "<input type=radio name=ExpireDefault_def value=1 %s> None\n",
20         $_[2]->[0] ? "" : "checked";
21 $rv .= sprintf "&nbsp; <input type=radio name=ExpireDefault_def value=2 %s>\n",
22         $_[2]->[0] ? "checked" : "";
23 $rv .= &expires_input("ExpiresDefault", $_[2]->[0]->{'value'})."<br>\n";
24 $rv .= "<table border>\n".
25        "<tr $tb> <td><b>MIME type</b></td> <td><b>Expiry time</b></td> </tr>\n";
26 $max = @{$_[1]} + 1;
27 for($i=0; $i<$max; $i++) {
28         if ($_[1]->[$i]->{'value'} =~ /^(\S+)\s+(.*)$/) {
29                 $type = $1; $when = $2;
30                 }
31         else { $type = $when = ""; }
32         $rv .= "<tr $cb>\n";
33         $rv .= "<td><input name=Expires_type_$i size=15 value=\"$type\"></td>\n";
34         $rv .= "<td>".&expires_input("Expires_when_$i", $when)."</td>\n";
35         $rv .= "</tr>\n";
36         }
37 $rv .= "</table>\n";
38 return (2, "Expires headers", $rv);
39 }
40 sub save_Expires
41 {
42 local($i, $type, @rv);
43 for($i=0; defined($type = $in{"Expires_type_$i"}); $i++) {
44         if ($type !~ /\S/) { next; }
45         $type =~ /^(\S+)\/(\S+)$/ || &error("'$type' is not a valid MIME type");
46         push(@rv, "$type ".&parse_expires("Expires_when_$i"));
47         }
48 return ( &parse_choice("ExpiresActive", "off"),
49          \@rv,
50          $in{'ExpiresDefault_def'} ? [ ] : &parse_expires("ExpiresDefault") );
51 }
52
53 @Expires_units = (60, 60, 24, 30, 365);
54 @Expires_words = ('second', 'minute', 'hour', 'day', 'week', 'month', 'year');
55
56 # expires_input(name, value)
57 sub expires_input
58 {
59 local(@tm, @w, $i, $rv, $from, $secs);
60 if ($_[1] =~ /^"(\S+)\s+(plus\s+)?(.*)"$/) {
61         @w = split(/\s+/, $3);
62         $from = ($1 =~ /access|now/ ? "A" : "M");
63         @tm = (0, 0, 0, 0, 0, 0, 0);
64         for($i=0; $i<@w; $i+=2) {
65                 $w[$i+1] =~ s/s$//g;
66                 $tm[&indexof($w[$i+1], @Expires_words)] = $w[$i];
67                 }
68         }
69 elsif ($_[1] =~ /^(A|M)(\d+)$/) {
70         $from = $1;
71         $secs = $2;
72         for($i=0; $i<@Expires_units; $i++) {
73                 push(@tm, $secs % $Expires_units[$i]);
74                 $secs = int($secs / $Expires_units[$i]);
75                 }
76         push(@tm, $secs);
77         splice(@tm, 4, 1, 0);
78         }
79 else {
80         $from = "A";
81         @tm = (0, 0, 0, 0, 0, 0, 0);
82         }
83 $rv = "<select name=$_[0]_from>\n";
84 $rv .= sprintf "<option value=M %s>Modification\n",
85         $from eq "M" ? "checked" : "";
86 $rv .= sprintf "<option value=A %s>Access\n",
87         $from eq "A" ? "checked" : "";
88 $rv .= "</select> time plus&nbsp;\n";
89 for($i=0; $i<7; $i++) {
90         $rv .= "<input size=3 name=$_[0]_$i value=\"$tm[$i]\">".
91                 substr($Expires_words[$i],0,1)."\n";
92         }
93 return $rv;
94 }
95
96 # parse_expires(name)
97 sub parse_expires
98 {
99 local($rv, $i, $v);
100 $rv = $in{"$_[0]_from"} eq "A" ? "access" : "modification";
101 $rv .= " plus";
102 for($i=0; $i<7; $i++) {
103         $v = $in{"$_[0]_$i"};
104         if ($v !~ /^\d*$/) { &error("'$v' is not a valid number of ".
105                                     "$Expires_words[$i]s"); }
106         if ($v) { $rv .= " $v $Expires_words[$i]s"; }
107         }
108 if ($rv =~ /plus$/) { $rv .= " 0 seconds"; }
109 return "\"$rv\"";
110 }
111
112 1;
113