Handle hostnames with upper-case letters
[webmin.git] / apache / mod_mime.pl
1 # mod_mime.pl
2 # Defines MIME module directives
3
4 sub mod_mime_directives
5 {
6 local $rv;
7 $rv = [ [ 'AddType', 1, 6, 'virtual directory htaccess', undef, 10 ],
8         [ 'AddHandler', 1, 6, 'virtual directory htaccess', undef, 6 ],
9         [ 'AddOutputFilter', 1, 18, 'virtual directory htaccess', 2.0 ],
10         [ 'AddInputFilter', 1, 18, 'virtual directory htaccess', 2.0 ],
11         [ 'DefaultLanguage', 0, 19, 'virtual directory htaccess', 1.304 ],
12         [ 'RemoveHandler', 1, 6, 'directory htaccess', 1.304, 5 ],
13         [ 'RemoveType', 1, 6, 'directory htaccess', 1.313, 5 ],
14         [ 'RemoveEncoding', 1, 6, 'directory htaccess', 1.313, 5 ],
15         [ 'AddLanguage', 1, 19, 'virtual directory htaccess', undef, 4 ],
16         [ 'AddEncoding', 1, 6, 'virtual directory htaccess', undef, 8 ],
17         [ 'AddCharset', 1, 19, 'virtual directory htaccess', 1.310, 2 ],
18         [ 'ForceType', 0, 6, 'directory htaccess', -2.0 ],
19         [ 'SetHandler', 0, 6, 'directory htaccess' ],
20         [ 'TypesConfig', 0, 6, 'global' ] ];
21 return &make_directives($rv, $_[0], "mod_mime");
22 }
23
24 # extmap_input(directive, title, size, desc, list)
25 sub extmap_input
26 {
27 local($rv, $i, $type, $exts);
28 $rv = "<table border>\n".
29       "<tr $tb> <td><b>$_[1]</b></td> <td><b>$text{'mod_mime_ext'}</b></td>\n".
30       "<td><b>$_[1]</b></td> <td><b>$text{'mod_mime_ext'}</b></td> </tr>\n";
31 $len = int(@{$_[4]}/2)*2 + 2;
32 for($i=0; $i<$len; $i++) {
33         if ($i%2 == 0) { $rv .= "<tr $cb>\n"; }
34         if ($_[4]->[$i]) {
35                 $_[4]->[$i]->{'value'} =~ /^(\S+)\s*(.*)$/;
36                 $type = $1; $exts = $2;
37                 }
38         else { $type = $exts = ""; }
39         $rv .= "<td>";
40         if ($_[2] eq "h") { $rv .= &handler_input($type, "$_[0]_type_$i"); }
41         else { $rv .= "<input name=$_[0]_type_$i size=$_[2] value=$type>"; }
42         $rv .= "</td>\n";
43         $rv .= "<td><input name=$_[0]_exts_$i size=10 value=\"$exts\"></td>\n";
44         if ($i%2 == 1) { $rv .= "</tr>\n"; }
45         }
46 $rv .= "</table>\n";
47 return (2, $_[3], $rv);
48 }
49
50 # parse_extmap(directive, regexp, desc)
51 sub parse_extmap
52 {
53 local($i, $type, $exts, @rv, $re); $re = $_[1];
54 for($i=0; defined($in{"$_[0]_type_$i"}); $i++) {
55         $type = $in{"$_[0]_type_$i"}; $exts = $in{"$_[0]_exts_$i"};
56         if ($type !~ /\S/ && $exts !~ /\S/) { next; }
57         if ($type !~ /$re/) { &error(&text('mod_mime_einvalid', $type, $_[2])); }
58         if ($exts !~ /\S/) { &error(&text('mod_mime_eext', $_[2], $type)); }
59         push(@rv, "$type $exts");
60         }
61 return ( \@rv );
62 }
63
64 sub edit_AddType
65 {
66 &extmap_input("AddType", $text{'mod_mime_type'}, 20, $text{'mod_mime_xtype'}, $_[0]);
67 }
68 sub save_AddType
69 {
70 return &parse_extmap("AddType", '^(\S+)\/(\S+)$',
71                      $text{'mod_mime_mtype'});
72 }
73
74 sub edit_AddHandler
75 {
76 &extmap_input("AddHandler", $text{'mod_mime_handler'}, "h", $text{'mod_mime_chandl'}, $_[0]);
77 }
78 sub save_AddHandler
79 {
80 return &parse_extmap("AddHandler");
81 }
82
83 sub edit_AddEncoding
84 {
85 &extmap_input("AddEncoding", $text{'mod_mime_cenc'}, 20, $text{'mod_mime_cencs'}, $_[0]);
86 }
87 sub save_AddEncoding
88 {
89 return &parse_extmap("AddEncoding", '^(\S+)$',
90                      $text{'mod_mime_cenc'});
91 }
92
93 sub edit_AddLanguage
94 {
95 &extmap_input("AddLanguage", $text{'mod_mime_lang'}, 5, $text{'mod_mime_clangs'}, $_[0]);
96 }
97 sub save_AddLanguage
98 {
99 return &parse_extmap("AddLanguage", '^\S+$', $text{'mod_mime_clang'});
100 }
101
102 sub edit_ForceType
103 {
104 return (1, $text{'mod_mime_defmime'},
105         &opt_input($_[0]->{'value'}, "ForceType", $text{'mod_mime_real'}, 15));
106 }
107 sub save_ForceType
108 {
109 return &parse_opt("ForceType", '^\S+\/\S+$', $text{'mod_mime_etype'});
110 }
111
112 sub edit_SetHandler
113 {
114 return (1, $text{'mod_mime_pass'},
115         &handler_input($_[0]->{'value'}, "SetHandler"));
116 }
117 sub save_SetHandler
118 {
119 return &parse_handler("SetHandler");
120 }
121
122 sub edit_TypesConfig
123 {
124 return (2,
125         $text{'mod_mime_file'},
126         &opt_input($_[0]->{'value'}, "TypesConfig", $text{'mod_mime_default'}, 40).
127         &file_chooser_button("TypesConfig", 0));
128 }
129 sub save_TypesConfig
130 {
131 return &parse_opt("TypesConfig");
132 }
133
134 sub edit_RemoveHandler
135 {
136 local @list;
137 foreach $v (@{$_[0]}) {
138         push(@list, @{$v->{'words'}});
139         }
140 return (2, "$text{'mod_mime_ignhand'}",
141         &opt_input(@list ? join(" ", @list) : undef,
142                    "RemoveHandler", "$text{'mod_mime_none'}", 40));
143 }
144 sub save_RemoveHandler
145 {
146 return &parse_opt("RemoveHandler");
147 }
148
149 sub edit_RemoveType
150 {
151 local @list;
152 foreach $v (@{$_[0]}) {
153         push(@list, @{$v->{'words'}});
154         }
155 return (2, "$text{'mod_mime_igntype'}",
156         &opt_input(@list ? join(" ", @list) : undef,
157                    "RemoveType", "$text{'mod_mime_none'}", 40));
158 }
159 sub save_RemoveType
160 {
161 return &parse_opt("RemoveType");
162 }
163
164 sub edit_RemoveEncoding
165 {
166 local @list;
167 foreach $v (@{$_[0]}) {
168         push(@list, @{$v->{'words'}});
169         }
170 return (2, "$text{'mod_mime_ignenc'}",
171         &opt_input(@list ? join(" ", @list) : undef,
172                    "RemoveEncoding", "$text{'mod_mime_none'}", 40));
173 }
174 sub save_RemoveEncoding
175 {
176 return &parse_opt("RemoveEncoding");
177 }
178
179 sub edit_AddCharset
180 {
181 &extmap_input("AddCharset", "$text{'mod_mime_chars'}", 12, "$text{'mod_mime_xchars'}", $_[0]);
182 }
183 sub save_AddCharset
184 {
185 return &parse_extmap("AddCharset");
186 }
187
188 sub edit_DefaultLanguage
189 {
190 return (1, $text{'mod_mime_deflang'},
191         &opt_input($_[0]->{'value'}, "DefaultLanguage", $text{'mod_mime_none'},
192                    10));
193 }
194 sub save_DefaultLanguage
195 {
196 return &parse_opt("DefaultLanguage", '\S', $text{'mod_mime_edeflang'});
197 }
198
199 sub edit_AddOutputFilter
200 {
201 return (2, $text{'mod_mime_outfilter'},
202         &filtmap_input("AddOutputFilter", $_[0]));
203 }
204 sub save_AddOutputFilter
205 {
206 return &parse_filtmap("AddOutputFilter");
207 }
208
209 sub edit_AddInputFilter
210 {
211 return (2, $text{'mod_mime_infilter'},
212         &filtmap_input("AddInputFilter", $_[0]));
213 }
214 sub save_AddInputFilter
215 {
216 return &parse_filtmap("AddInputFilter");
217 }
218
219 # filtmap_input(name, &values)
220 sub filtmap_input
221 {
222 local $rv = "<table border>\n".
223             "<tr $tb> <td><b>$text{'mod_mime_filters'}</b></td>\n".
224             "<td><b>$text{'mod_mime_ext'}</b></td> </tr>\n";
225 local ($d, $i = 0);
226 foreach $d (@{$_[1]}, { }) {
227         local @w = @{$d->{'words'}};
228         local @v = split(/;/, shift(@w));
229         $rv .= "<tr $cb>\n";
230         $rv .= "<td>".&filters_input(\@v, $_[0]."_f_$i")."</td>\n";
231         $rv .= "<td><input name=$_[0]_e_$i size=30 value='".
232                 join(" ", @w)."'></td>\n";
233         $rv .= "</tr>\n";
234         $i++;
235         }
236 $rv .= "</table>\n";
237 return $rv;
238 }
239 # parse_filtmap(name)
240 sub parse_filtmap
241 {
242 local ($i, @rv);
243 for($i=0; defined($in{"$_[0]_e_$i"}); $i++) {
244         local @f = split(/\0/, $in{"$_[0]_f_$i"});
245         next if (!@f);
246         local $exts = $in{"$_[0]_e_$i"};
247         $exts || &error(&text('mod_mime_efext', join(" ", @f)));
248         push(@rv, join(";", @f)." ".$exts);
249         }
250 return ( \@rv );
251 }
252
253 1;
254