Handle hostnames with upper-case letters
[webmin.git] / apache / autoindex.pl
1 # autoindex.pl
2 # Directives in both mod_dir.pl and mod_autoindex.pl
3
4 @AddIcon_dirs = ("AddIcon", "AddIconByType", "AddIconByEncoding");
5 @AddIcon_descs = ("$text{'autoindex_fname'}", "$text{'autoindex_mime'}", "$text{'autoindex_enc'}");
6
7 sub edit_AddIcon_AddIconByType_AddIconByEncoding
8 {
9 local($rv, @all, $i, $j, $icon, $alt, $mode, $file);
10 $rv = "<table border>\n".
11 "<tr $tb> <td><b>$text{'autoindex_icon'}</b></td> <td><b>$text{'autoindex_alt'}</b></td>\n".
12 "<td><b>$text{'autoindex_match'}</b></td> <td><b>$text{'autoindex_fte'}</b></td></tr>\n";
13 @all = (@{$_[0]}, @{$_[1]}, @{$_[2]});
14 for($i=0; $i<=@all; $i++) {
15         $d = $all[$i];
16         if ($d) {
17                 if ($d->{'value'} =~ /^\((.*),(\S+)\)\s*(.*)$/) {
18                         $alt = $1; $icon = $2; $file = $3;
19                         }
20                 elsif ($d->{'value'} =~ /^(\S+)\s*(.*)$/) {
21                         $alt = ""; $icon = $1; $file = $2;
22                         }
23                 $mode = &indexof($d->{'name'}, @AddIcon_dirs);
24                 }
25         else { $alt = $icon = $file = ""; $mode = 0; }
26
27         $rv .="<tr $cb>\n";
28         $rv .="<td><input name=AddIcon_icon_$i size=25 value=\"$icon\"></td>\n";
29         $rv .="<td><input name=AddIcon_alt_$i size=10 value=\"$alt\"></td>\n";
30         $rv .="<td><select name=AddIcon_mode_$i>\n";
31         for($j=0; $j<@AddIcon_descs; $j++) {
32                 $rv .= sprintf "<option value=$j %s>%s\n",
33                         $mode == $j ? "selected" : "", $AddIcon_descs[$j];
34                 }
35         $rv .="</select></td>\n";
36         $rv .="<td><input name=AddIcon_file_$i size=20 value=\"$file\"></td>\n";
37         $rv .="</tr>\n";
38         }
39 $rv .= "</table>\n";
40 return (2, "$text{'autoindex_diricon'}", $rv);
41 }
42 sub save_AddIcon_AddIconByType_AddIconByEncoding
43 {
44 local($i, $icon, $alt, $file, $mode, $aref, @ai, @ait, @aie);
45 for($i=0; defined($in{"AddIcon_icon_$i"}); $i++) {
46         $icon = $in{"AddIcon_icon_$i"}; $alt = $in{"AddIcon_alt_$i"};
47         $mode = $in{"AddIcon_mode_$i"}; $file = $in{"AddIcon_file_$i"};
48         if ($icon !~ /\S/ && $file !~ /\S/) { next; }
49         $icon =~ /^\S+$/ || &error(&text('autoindex_eiconurl', $icon));
50         $file =~ /\S/ || &error(&text('autoindex_emiss', $AddIcon_descs[$mode], $icon));
51         $aref = $mode==0 ? \@ai : $mode==1 ? \@ait : \@aie;
52         if ($alt) { push(@$aref, "($alt,$icon) $file"); }
53         else { push(@$aref, "$icon $file"); }
54         }
55 return ( \@ai, \@ait, \@aie );
56 }
57
58 sub edit_DefaultIcon
59 {
60 return (1, "$text{'autoindex_deficon'}",
61         &opt_input($_[0]->{'value'}, "DefaultIcon", "$text{'autoindex_default'}", 20));
62 }
63 sub save_DefaultIcon
64 {
65 return &parse_opt("DefaultIcon", '^\S+$', "$text{'autoindex_edeficon'}");
66 }
67
68 @AddAlt_dirs = ("AddAlt", "AddAltByType", "AddAltByEncoding");
69 @AddAlt_descs = ("Filename", "MIME type", "Encoding");
70
71 sub edit_AddAlt_AddAltByType_AddAltByEncoding
72 {
73 local($rv, @all, $i, $j, $alt, $mode, $file);
74 $rv = "<table border>\n".
75 "<tr $tb> <td><b>$text{'autoindex_alt'}</b></td>\n".
76 "<td><b>$text{'autoindex_match'}</b></td> <td><b>$text{'autoindex_fte'}</b></td></tr>\n";
77 @all = (@{$_[0]}, @{$_[1]}, @{$_[2]});
78 for($i=0; $i<=@all; $i++) {
79         $d = $all[$i];
80         if ($d->{'value'}) {
81                 $alt = $d->{'words'}->[0];
82                 @w = @{$d->{'words'}};
83                 $file = join(' ', @w[1..$#w]);
84                 $mode = &indexof($d->{'name'}, @AddAlt_dirs);
85                 }
86         else { $alt = $file = ""; $mode = 0; }
87
88         $rv .="<tr $cb>\n";
89         $rv .="<td><input name=AddAlt_alt_$i size=20 value=\"$alt\"></td>\n";
90         $rv .="<td><select name=AddAlt_mode_$i>\n";
91         for($j=0; $j<@AddAlt_descs; $j++) {
92                 $rv .= sprintf "<option value=$j %s>%s\n",
93                         $mode == $j ? "selected" : "", $AddAlt_descs[$j];
94                 }
95         $rv .="</select></td>\n";
96         $rv .="<td><input name=AddAlt_file_$i size=20 value=\"$file\"></td>\n";
97         $rv .="</tr>\n";
98         }
99 $rv .= "</table>\n";
100 return (2, "$text{'autoindex_diralt'}", $rv);
101 }
102 sub save_AddAlt_AddAltByType_AddAltByEncoding
103 {
104 local($i, $alt, $file, $mode, $aref, @ai, @ait, @aie);
105 for($i=0; defined($alt = $in{"AddAlt_alt_$i"}); $i++) {
106         $mode = $in{"AddAlt_mode_$i"}; $file = $in{"AddAlt_file_$i"};
107         if ($alt !~ /\S/ && $file !~ /\S/) { next; }
108         $file =~ /\S/ || &error(&text('autoindex_emissquot', $AddAlt_descs[$mode], $alt));
109         $aref = $mode==0 ? \@ai : $mode==1 ? \@ait : \@aie;
110         push(@$aref, "\"$alt\" $file");
111         }
112 return ( \@ai, \@ait, \@aie );
113 }
114
115 sub edit_AddDescription
116 {
117 local($rv, $i, $desc, $file);
118 $rv = "<table border>\n".
119       "<tr $tb> <td><b>$text{'autoindex_desc'}</b></td> <td><b>$text{'autoindex_fnames'}</b></td> </tr>\n";
120 for($i=0; $i<=@{$_[0]}; $i++) {
121         if ($_[0]->[$i] && $_[0]->[$i]->{'value'} =~ /^"(.*)"\s*(.*)$/)
122                 { $desc = $1; $file = $2; }
123         else { $desc = $file = ""; }
124         $rv .= "<tr $cb> <td><input size=40 name=AddDescription_desc_$i ".
125                "value=\"$desc\"></td>\n";
126         $rv .= "<td><input size=20 name=AddDescription_file_$i ".
127                "value=\"$file\"></td> </tr>\n";
128         }
129 $rv .= "</table>\n";
130 return (2, "$text{'autoindex_dirdesc'}", $rv);
131 }
132 sub save_AddDescription
133 {
134 local($i, $desc, $file, @rv);
135 for($i=0; defined($in{"AddDescription_desc_$i"}); $i++) {
136         $desc = $in{"AddDescription_desc_$i"};
137         $file = $in{"AddDescription_file_$i"};
138         if ($desc !~ /\S/ && $file !~ /\S/) { next; }
139         $desc =~ /\S/ || &error(&text('autoindex_enodesc', $file));
140         $file =~ /\S/ || &error(&text('autoindex_enofile', $desc));
141         push(@rv, "\"$desc\" $file");
142         }
143 return ( \@rv );
144 }
145
146 @IndexOptions_v =
147         ( "FancyIndexing", "ScanHTMLTitles", "IconHeight", "IconWidth",
148           "SuppressColumnSorting", "SuppressDescription",
149           "SuppressHTMLPreamble", "SuppressLastModified", "SuppressSize",
150           "IconsAreLinks", "NameWidth", "DescriptionWidth", "FoldersFirst",
151           "HTMLTable", "IgnoreClient", "SuppressIcon", "SuppressRules",
152           "TrackModified", "VersionSort" );
153 @IndexOptions_d =
154         ( $text{'autoindex_fancy'},
155           $text{'autoindex_htmltitle'},
156           $text{'autoindex_iheight'},
157           $text{'autoindex_iwidth'},
158           $text{'autoindex_sort'},
159           $text{'autoindex_fildesc'},
160           $text{'autoindex_htags'},
161           $text{'autoindex_mtime'},
162           $text{'autoindex_size'},
163           $text{'autoindex_iconlink'},
164           $text{'autoindex_fwidth'},
165           $text{'autoindex_dwidth'},
166           $text{'autoindex_dirfirst'},
167           $text{'autoindex_html'},
168           $text{'autoindex_client'},
169           $text{'autoindex_sicon'},
170           $text{'autoindex_srules'},
171           $text{'autoindex_track'},
172           $text{'autoindex_version'} );
173 @IndexOptions_i = ( 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 );
174 @IndexOptions_n = ( 0, 0, 1.3, 1.3,
175                     1.3, 0,
176                     1.3, 0, 0,
177                     1.302, 1.302, 1.310, 1.310,
178                     2.023, 2.023, 2.023, 2.023,
179                     2.023, 2.0 );
180
181 sub edit_IndexOptions_FancyIndexing
182 {
183 local($o, %opts, $i, $rv);
184 $rv = &choice_input($_[0] || $_[1] ? 0 : 1, "IndexOptions_def", 1,
185                     "$text{'autoindex_default2'},1", "$text{'autoindex_select'},0");
186 foreach $o (split(/\s+/, $_[0]->{'value'})) {
187         if ($o =~ /^(\S+)=(\S+)$/) { $opts{$1} = $2; }
188         else { $opts{$o} = -1; }
189         }
190 if ($_[1]->{'value'} eq "on") { $opts{'FancyIndexing'} = -1; }
191 $rv .= "<table border><tr><td><table cellpadding=0>\n";
192 local $sw = 0;
193 for($i=0; $i<@IndexOptions_v; $i++) {
194         $o = $IndexOptions_v[$i];
195         next if ($_[2]->{'version'} < $IndexOptions_n[$i]);
196
197         $rv .= "<tr>\n" if (!$sw);
198         if ($IndexOptions_i[$i]) { $opts{$o} = -$opts{$o}-1; }
199         $rv .= sprintf "<td><input type=checkbox name=Index_$o value=1 %s> %s\n",
200                 $opts{$o} ? "checked" : "", $IndexOptions_d[$i];
201         if ($o =~ /IconWidth|IconHeight/) {
202                 $rv .= "&nbsp;";
203                 $rv .= sprintf 
204                         "<input type=radio name=Index_${o}_def value=1 %s> $text{'autoindex_default3'}\n",
205                         $opts{$o} < 0 ? "checked" : "";
206                 $rv .= sprintf
207                         "&nbsp;<input type=radio name=Index_${o}_def value=0 %s>\n",
208                         $opts{$o} < 0 ? "" : "checked";
209                 $rv .= sprintf
210                         "<input name=Index_${o}_wh size=5 value=\"%s\"> $text{'autoindex_pixels'}\n",
211                         $opts{$o} < 0 ? "" : $opts{$o};
212                 }
213         elsif ($o =~ /NameWidth|DescriptionWidth/) {
214                 $rv .= "&nbsp;";
215                 $rv .= sprintf
216                         "<input name=Index_${o}_w size=5 value=\"%s\"> $text{'autoindex_chars'}\n",
217                         $opts{$o} < 0 ? "" : $opts{$o};
218                 }
219         $rv .= "</td>";
220         $rv .= "</tr>\n" if ($sw);
221         $sw = !$sw;
222         }
223 $rv .= "</table></td></tr></table>\n";
224 return (2, "$text{'autoindex_diropt'}", $rv);
225 }
226 sub save_IndexOptions_FancyIndexing
227 {
228 local($i, $o, @rv, %opts);
229 if ($in{'IndexOptions_def'}) { return ( [ ], [ ] ); }
230 for($i=0; $i<@IndexOptions_v; $i++) {
231         $o = $IndexOptions_v[$i];
232         next if ($_[0]->{'version'} < $IndexOptions_n[$i]);
233
234         if ($in{"Index_$o"}) { $opts{$o} = -1; }
235         if ($o =~ /IconWidth|IconHeight/ &&
236             $in{"Index_${o}"} && !$in{"Index_${o}_def"}) {
237                 $in{"Index_${o}_wh"} =~ /^[1-9]\d*$/ ||
238                         &error(&text('autoindex_eiconsize', $in{"Index_${o}_wh"}));
239                 $opts{$o} = $in{"Index_${o}_wh"};
240                 }
241         elsif ($o =~ /NameWidth|DescriptionWidth/ && $in{"Index_${o}"}) {
242                 $in{"Index_${o}_w"} =~ /^[1-9]\d*$/ ||
243                     $in{"Index_${o}_w"} eq '*' ||
244                         &error(&text('autoindex_ewidth', $in{"Index_$(o)_h"}));
245                 $opts{$o} = $in{"Index_${o}_w"};
246                 }
247         if ($IndexOptions_i[$i]) { $opts{$o} = -$opts{$o}-1; }
248         if ($opts{$o} < 0) { push(@rv, "$o"); }
249         elsif ($opts{$o} > 0) { push(@rv, "$o=$opts{$o}"); }
250         }
251 return ( [ join(' ', @rv) ], [ ] );
252 }
253
254 sub edit_HeaderName
255 {
256 return (1, "$text{'autoindex_dirhead'}",
257         &opt_input($_[0]->{'value'}, "HeaderName", "$text{'autoindex_default4'}", 20));
258 }
259 sub save_HeaderName
260 {
261 return &parse_opt("HeaderName", '^\S+$', "$text{'autoindex_edirhead'}");
262 }
263
264 sub edit_ReadmeName
265 {
266 return (1, "$text{'autoindex_dirfoot'}",
267         &opt_input($_[0]->{'value'}, "ReadmeName", "$text{'autoindex_default4'}", 20));
268 }
269 sub save_ReadmeName
270 {
271 return &parse_opt("ReadmeName", '^\S+$', "$text{'autoindex_edirfoot'}");
272 }
273
274 sub edit_IndexIgnore
275 {
276 local($rv, $i, @ii);
277 foreach $i (@{$_[0]}) { push(@ii, split(/\s+/, $i->{'value'})); }
278 $rv = join("\n", @ii);
279 return (1, "$text{'autoindex_ignore'}",
280         "<textarea name=IndexIgnore rows=5 cols=20>$rv</textarea>");
281 }
282 sub save_IndexIgnore
283 {
284 local(@rv); @rv = split(/\s+/, $in{'IndexIgnore'});
285 if (!@rv) { return ( [ ] ); }
286 else { return ( [ join(' ', @rv) ] ); }
287 }
288
289 1;
290