Handle hostnames with upper-case letters
[webmin.git] / bind8 / edit_recs.cgi
1 #!/usr/local/bin/perl
2 # edit_recs.cgi
3 # Display records of some type from some domain
4
5 require './bind8-lib.pl';
6 &ReadParse();
7 $zone = &get_zone_name($in{'index'}, $in{'view'});
8 $dom = $zone->{'name'};
9 &can_edit_zone($zone) ||
10         &error($text{'recs_ecannot'});
11 &can_edit_type($in{'type'}, \%access) ||
12         &error($text{'recs_ecannottype'});
13 $desc = &text('recs_header', &ip6int_to_net(&arpa_to_ip($dom)));
14 $typedesc = $text{"recs_$in{'type'}"} || $in{'type'};
15 &ui_print_header($desc, &text('recs_title', $typedesc), "",
16                  undef, undef, undef, undef, &restart_links($zone));
17
18 # Show form for adding a record
19 $type = $zone->{'type'};
20 $file = $zone->{'file'};
21 $form = 0;
22 if (!$access{'ro'} && $type eq 'master' && $in{'type'} ne 'ALL') {
23         &record_input($in{'index'}, $in{'view'}, $in{'type'}, $file, $dom,
24                       undef, undef, $in{'newname'}, $in{'newvalue'});
25         $form++;
26         $shown_create_form = 1;
27         }
28
29 if ($config{'largezones'}) {
30         # Show search form
31         print &ui_form_start("edit_recs.cgi");
32         print &ui_hidden("index", $in{'index'}),"\n";
33         print &ui_hidden("view", $in{'view'}),"\n";
34         print &ui_hidden("type", $in{'type'}),"\n";
35         print "<b>$text{'recs_find'}</b>\n";
36         print &ui_textbox("search", $in{'search'}, 20),"\n";
37         print &ui_submit($text{'recs_search'}),"\n";
38         print &ui_form_end();
39         $form++;
40         }
41
42 if (!$config{'largezones'} || $in{'search'}) {
43         # Get all records
44         @allrecs = grep { !$_->{'generate'} && !$_->{'defttl'} }
45                      &read_zone_file($file, $dom);
46         if ($in{'search'}) {
47                 # Limit to records matching some search
48                 foreach $r (@allrecs) {
49                         if ($r->{'name'} =~ /\Q$in{'search'}\E/i) {
50                                 push(@recs, $r);
51                                 }
52                         else {
53                                 foreach $v (@{$r->{'values'}}) {
54                                         if ($v =~ /\Q$in{'search'}\E/i) {
55                                                 push(@recs, $r);
56                                                 last;
57                                                 }
58                                         }
59                                 }
60                         }
61                 }
62         else {
63                 # Show them all
64                 @recs = @allrecs;
65                 }
66         }
67
68 # Actually show the records
69 if ($in{'type'} eq "ALL") {
70         @recs = grep { $_->{'type'} ne "SOA" } @recs
71         }
72 else {
73         @recs = grep { $_->{'type'} eq $in{'type'} } @recs
74         }
75 if (@recs) {
76         @recs = &sort_records(@recs);
77         foreach $v (keys %text) {
78                 if ($v =~ /^value_([A-Z]+)(\d+)/) {
79                         $hmap{$1}->[$2-1] = $text{$v};
80                         }
81                 }
82         @links = ( );
83         if (!$access{'ro'} && $type eq 'master') {
84                 print &ui_form_start("delete_recs.cgi", "post");
85                 print &ui_hidden("index", $in{'index'}),"\n";
86                 print &ui_hidden("view", $in{'view'}),"\n";
87                 print &ui_hidden("type", $in{'type'}),"\n";
88                 print &ui_hidden("sort", $in{'sort'}),"\n";
89                 @links = ( &select_all_link("d", $form),
90                            &select_invert_link("d", $form) );
91                 }
92         print &ui_links_row(\@links);
93         if ($in{'type'} =~ /HINFO|WKS|RP|KEY|LOC|SPF/ ||
94             $config{'allow_comments'}) {
95                 # One-column table
96                 print &recs_table(@recs);
97                 }
98         else {
99                 # Two-column table
100                 $mid = int((@recs+1)/2);
101                 @grid = ( );
102                 push(@grid, &recs_table(@recs[0 .. $mid-1]));
103                 if ($mid < @recs) {
104                         push(@grid, &recs_table(@recs[$mid .. $#recs]));
105                         }
106                 print &ui_grid_table(\@grid, 2, 100,
107                         [ "width=50%", "width=50%" ]);
108                 }
109         print &ui_links_row(\@links);
110         if (!$access{'ro'} && $type eq 'master') {
111                 print &ui_submit($text{'recs_delete'}),"\n";
112                 if ($in{'type'} eq 'A' || $in{'type'} eq 'AAAA') {
113                         print &ui_checkbox("rev", 1, $text{'recs_drev'},
114                                            $config{'rev_def'} != 1),"\n";
115                         }
116                 print &ui_form_end();
117                 }
118         }
119 elsif (!$shown_create_form) {
120         # Show error message
121         print "<b>",&text('recs_none', $typedesc),"</b><p>\n";
122         }
123
124 &ui_print_footer("", $text{'index_return'},
125         "edit_$type.cgi?index=$in{'index'}&view=$in{'view'}",
126         $text{'recs_return'});
127
128 sub recs_table
129 {
130 my ($r, $i, $j, $k, $h);
131 my $rv;
132
133 # Generate header, with correct columns for record type
134 local (@hcols, @tds);
135 if (!$access{'ro'} && $type eq 'master') {
136         push(@hcols, "");
137         push(@tds, "width=5");
138         }
139 push(@hcols, "<a href='edit_recs.cgi?index=$in{'index'}&view=$in{'view'}&type=$in{'type'}&sort=1'>".($in{'type'} eq "PTR" ? $text{'recs_addr'} : $text{'recs_name'})."</a>");
140 push(@hcols, "<a href='edit_recs.cgi?index=$in{'index'}&view=$in{'view'}&type=$in{'type'}&sort=5'>$text{'recs_type'}</a>") if ($in{'type'} eq "ALL");
141 push(@hcols, $text{'recs_ttl'});
142 @hmap = @{$hmap{$in{'type'}}};
143 foreach $h (@hmap) {
144         push(@hcols, "<a href='edit_recs.cgi?index=$in{'index'}&view=$in{'view'}&type=$in{'type'}&sort=2'>$h</a>");
145         }
146 if ($in{'type'} eq "ALL" || $is_extra{$in{'type'}}) {
147         push(@hcols, $text{'recs_vals'});
148         }
149 if ($config{'allow_comments'} && $in{'type'} ne "WKS") {
150         push(@hcols, "<a href='edit_recs.cgi?index=$in{'index'}&view=$in{'view'}&type=$in{'type'}&sort=4'>$text{'recs_comment'}</a>");
151         }
152 $rv .= &ui_columns_start(\@hcols, 100);
153
154 # Show the actual records
155 for($i=0; $i<@_; $i++) {
156         $r = $_[$i];
157         if ($in{'type'} eq "PTR") {
158                 $name = &ip6int_to_net(&arpa_to_ip($r->{'name'}));
159                 }
160         elsif ($in{'type'} eq "SRV") {
161                 $name = $r->{'name'};
162                 $name =~ s/^_//;
163                 $name =~ s/\._/\./;
164                 }
165         else {
166                 $name = $r->{'name'};
167                 }
168         local @cols;
169         $name = &html_escape($name);
170         $id = &record_id($r);
171         if (!$access{'ro'} && $type eq 'master') {
172                 push(@cols, 
173                       "<a href=\"edit_record.cgi?index=".
174                       "$in{'index'}&id=".&urlize($id)."&num=$r->{'num'}&".
175                       "type=$in{'type'}&sort=$in{'sort'}&view=$in{'view'}\">".
176                       "$name</a>");
177                 }
178         else {
179                 push(@cols, $name);
180                 }
181         if ($in{'type'} eq 'ALL') {
182                 push(@cols, $r->{'type'});
183                 }
184         if ($r->{'ttl'} =~ /(\d+)([SMHDW]?)/i) {
185                 $r->{'ttl'} =~ s/S//i;
186                 if ($r->{'ttl'} =~ s/M//i) { $r->{'ttl'} *= 60; }
187                 if ($r->{'ttl'} =~ s/H//i) { $r->{'ttl'} *= 3600; }
188                 if ($r->{'ttl'} =~ s/D//i) { $r->{'ttl'} *= 86400; }
189                 if ($r->{'ttl'} =~ s/W//i) { $r->{'ttl'} *= 604800; }
190                 }
191         push(@cols, $r->{'ttl'} ? &html_escape($r->{'ttl'}) : $text{'default'});
192         for($j=0; $j<@hmap; $j++) {
193                 local $v;
194                 if ($in{'type'} eq "RP" && $j == 0) {
195                         $v .= &convert_illegal(
196                                 &dotted_to_email($r->{'values'}->[$j]));
197                         }
198                 elsif ($in{'type'} eq "WKS" && $j == @hmap-1) {
199                         for($k=$j; $r->{'values'}->[$k]; $k++) {
200                                 $v .= &convert_illegal($r->{'values'}->[$k]);
201                                 $v .= ' ';
202                                 }
203                         }
204                 elsif ($in{'type'} eq "LOC") {
205                         $v = &convert_illegal(join(" ", @{$r->{'values'}}));
206                         }
207                 elsif ($in{'type'} eq "KEY" && $j == 3) {
208                         $v = substr($r->{'values'}->[$j], 0, 20)."...";
209                         }
210                 else {
211                         $v = &convert_illegal($r->{'values'}->[$j]);
212                         }
213                 if (length($v) > 80) {
214                         $v = substr($v, 0, 80)." ...";
215                         }
216                 push(@cols, $v);
217                 }
218         if ($in{'type'} eq "ALL" || $is_extra{$in{'type'}}) {
219                 $joined = join(" ", @{$r->{'values'}});
220                 if (length($joined) > 80) {
221                         $joined = substr($joined, 0, 80)." ...";
222                         }
223                 push(@cols, $joined);
224                 }
225         if ($config{'allow_comments'} && $in{'type'} ne "WKS") {
226                 push(@cols, &html_escape($r->{'comment'}));
227                 }
228         if (!$access{'ro'} && $type eq 'master') {
229                 $rv .= &ui_checked_columns_row(\@cols, \@tds,
230                                               "d", $r->{'num'}."/".$id);
231                 }
232         else {
233                 $rv .= &ui_columns_row(\@cols, \@tds);
234                 }
235         }
236 $rv .= &ui_columns_end();
237 return $rv;
238 }
239