Gro├čes Update:
[ipfire-2.x.git] / html / cgi-bin / hosts.cgi
CommitLineData
ac1cfefa
MT
1#!/usr/bin/perl
2#
78331e30 3# IPFire CGIs
ac1cfefa
MT
4#
5# This code is distributed under the terms of the GPL
6#
7# (c) Alan Hourihane <alanh@fairlite.demon.co.uk>
8#
9# $Id: hosts.cgi,v 1.4.2.20 2005/11/05 15:46:25 gespinasse Exp $
10#
11# Franck
12# use dhcp.cgi model to rewrite this code
13
14use strict;
15
16# enable only the following on debugging purpose
17#use warnings;
18#use CGI::Carp 'fatalsToBrowser';
19
20require 'CONFIG_ROOT/general-functions.pl';
21require "${General::swroot}/lang.pl";
22require "${General::swroot}/header.pl";
23
24#workaround to suppress a warning when a variable is used only once
25my @dummy = ( ${Header::colouryellow} );
26undef (@dummy);
27
28# Files used
29my $setting = "${General::swroot}/main/settings";
30our $datafile = "${General::swroot}/main/hosts"; #(our: used in subroutine)
31
32our %settings = ();
33#Settings1
34# removed
35
36#Settings2 for editing the multi-line list
37#Must not be saved !
38$settings{'EN'} = ''; # reuse for dummy field in position zero
39$settings{'IP'} = '';
40$settings{'HOST'} = '';
41$settings{'DOM'} = '';
42my @nosaved=('EN','IP','HOST','DOM'); # List here ALL setting2 fields. Mandatory
43
44$settings{'ACTION'} = ''; # add/edit/remove
45$settings{'KEY1'} = ''; # point record for ACTION
46
47#Define each field that can be used to sort columns
48my $sortstring='^IP|^HOST|^DOM';
49$settings{'SORT_HOSTSLIST'} = 'HOST';
50my $errormessage = '';
51my $warnmessage = '';
52
53&Header::showhttpheaders();
54
55#Get GUI values
56&Header::getcgihash(\%settings);
57
58# Load multiline data
59our @current = ();
60if (open(FILE, "$datafile")) {
61 @current = <FILE>;
62 close (FILE);
63}
64
65## Settings1 Box not used...
66&General::readhash("${General::swroot}/main/settings", \%settings);
67
68
69## Now manipulate the multi-line list with Settings2
70# Basic actions are:
71# toggle the check box
72# add/update a new line
73# begin editing a line
74# remove a line
75
76
77# Toggle enable/disable field. Field is in second position
78if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
79 #move out new line
80 chomp(@current[$settings{'KEY1'}]);
81 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
82
83 $temp[0] = $temp[0] ne '' ? '' : 'on'; # Toggle the field
84 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
85 $settings{'KEY1'} = ''; # End edit mode
86
87 &General::log($Lang::tr{'hosts config changed'});
88
89 #Save current
90 open(FILE, ">$datafile") or die 'hosts datafile error';
91 print FILE @current;
92 close(FILE);
93
94 # Rebuild configuration file
95 &BuildConfiguration;
96}
97
98if ($settings{'ACTION'} eq $Lang::tr{'add'}) {
99 # Validate inputs
100 unless(&General::validip($settings{'IP'})) {
101 $errormessage = $Lang::tr{'invalid fixed ip address'};
102 }
103
104 unless(&General::validhostname($settings{'HOST'})) {
105 $errormessage = $Lang::tr{'invalid hostname'};
106 }
107
108 if ($settings{'DOM'} && ! &General::validdomainname($settings{'DOM'})) {
109 $errormessage = $Lang::tr{'invalid domain name'};
110 }
111
112
113 unless ($errormessage) {
114 if ($settings{'KEY1'} eq '') { #add or edit ?
115 unshift (@current, "$settings{'EN'},$settings{'IP'},$settings{'HOST'},$settings{'DOM'}\n");
116 &General::log($Lang::tr{'hosts config added'});
117 } else {
118 @current[$settings{'KEY1'}] = "$settings{'EN'},$settings{'IP'},$settings{'HOST'},$settings{'DOM'}\n";
119 $settings{'KEY1'} = ''; # End edit mode
120 &General::log($Lang::tr{'hosts config changed'});
121 }
122
123 # Write changes to config file.
124 &SortDataFile; # sort newly added/modified entry
125 &BuildConfiguration; # then re-build new host
126
127 #map ($settings{$_}='' ,@nosaved); # Clear fields
128 }
129}
130
131if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
132 #move out new line
133 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
134 chomp($line);
135 my @temp = split(/\,/, $line);
136 $settings{'EN'}=$temp[0]; # Prepare the screen for editing
137 $settings{'IP'}=$temp[1];
138 $settings{'HOST'}=$temp[2];
139 $settings{'DOM'}=$temp[3];
140}
141
142if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
143 splice (@current,$settings{'KEY1'},1); # Delete line
144 open(FILE, ">$datafile") or die 'hosts datafile error';
145 print FILE @current;
146 close(FILE);
147 $settings{'KEY1'} = ''; # End remove mode
148 &General::log($Lang::tr{'hosts config changed'});
149
150 &BuildConfiguration; # then re-build conf which use new data
151}
152
153
154
155## Check if sorting is asked
156# If same column clicked, reverse the sort.
157if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
158 my $newsort=$ENV{'QUERY_STRING'};
159 my $actual=$settings{'SORT_HOSTSLIST'};
160 #Reverse actual sort ?
161 if ($actual =~ $newsort) {
162 my $Rev='';
163 if ($actual !~ 'Rev') {
164 $Rev='Rev';
165 }
166 $newsort.=$Rev;
167 }
168 $settings{'SORT_HOSTSLIST'}=$newsort;
169 map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
170 &General::writehash($setting, \%settings);
171 &SortDataFile;
172 $settings{'ACTION'} = 'SORT'; # Create an 'ACTION'
173 map ($settings{$_} = '' ,@nosaved,'KEY1'); # and reinit vars to empty
174}
175
176if ($settings{'ACTION'} eq '' ) { # First launch from GUI
177 # Place here default value when nothing is initialized
178 $settings{'EN'} = 'on';
179 $settings{'DOM'} = $settings{'DOMAINNAME'};
180}
181
182&Header::openpage($Lang::tr{'hostname'}, 1, '');
183&Header::openbigbox('100%', 'left', '', $errormessage);
184my %checked=(); # Checkbox manipulations
185
186if ($errormessage) {
187 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
188 print "<font class='base'>$errormessage&nbsp;</font>";
189 &Header::closebox();
190}
191
192#
193# Remove if no Setting1 needed
194#
195#if ($warnmessage) {
196# $warnmessage = "<font color=${Header::colourred}><b>$Lang::tr{'capswarning'}</b></font>: $warnmessage";
197#}
198#&Header::openbox('100%', 'left', $Lang::tr{'settings'});
199#print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
200#print <<END
201#<table width='100%'>
202#<tr>
203# <td class='base'>$Lang::tr{'domain name'} : $settings{'DOMAINNAME'}</td>
204#</table>
205#
206#END
207#;
208#
209#print <<END
210#<table width='100%'>
211#<hr />
212#<tr>
213# <td class='base' width='25%'><!--<img src='/blob.gif' align='top' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td>-->
214# <td class='base' width='25%'>$warnmessage</td>
215# <td width='50%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' disabled='disabled' /></td>
216#</tr>
217#</table>
218#</form>
219#END
220#;
221#&Header::closebox(); # end of Settings1
222
223
224#
225# Second check box is for editing the list
226#
227$checked{'EN'}{'on'} = ($settings{'EN'} eq '' ) ? '' : "checked='checked'";
228
229my $buttontext = $Lang::tr{'add'};
230if ($settings{'KEY1'} ne '') {
231 $buttontext = $Lang::tr{'update'};
232 &Header::openbox('100%', 'left', $Lang::tr{'edit an existing host'});
233} else {
234 &Header::openbox('100%', 'left', $Lang::tr{'add a host'});
235}
236
237#Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
238print <<END
239<form method='post' action='$ENV{'SCRIPT_NAME'}'>
240<input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
241<table width='100%'>
242<tr>
243 <td class='base'>$Lang::tr{'host ip'}:&nbsp;</td>
244 <td><input type='text' name='IP' value='$settings{'IP'}' /></td>
245 <td class='base'>$Lang::tr{'hostname'}:</td>
246 <td><input type='text' name='HOST' value='$settings{'HOST'}' /></td>
247</tr><tr>
248 <td class='base'>$Lang::tr{'domain name'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
249 <td><input type='text' name='DOM' value='$settings{'DOM'}' /></td>
250 <td class='base'>$Lang::tr{'enabled'}</td>
251 <td><input type='checkbox' name='EN' $checked{'EN'}{'on'} /></td>
252</tr>
253</table>
254<hr />
255<table width='100%'>
256<tr>
257 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td>
258 <td width='50%' align='center'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
259</tr>
260</table>
261</form>
262END
263;
264&Header::closebox();
265
266#
267# Third box shows the list, in columns
268#
269# Columns headers may content a link. In this case it must be named in $sortstring
270#
271&Header::openbox('100%', 'left', $Lang::tr{'current hosts'});
272print <<END
273<table width='100%'>
274<tr>
275 <td width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'host ip'}</b></a></td>
276 <td width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?HOST'><b>$Lang::tr{'hostname'}</b></a></td>
277 <td width='50%' align='center'><a href='$ENV{'SCRIPT_NAME'}?DOM'><b>$Lang::tr{'domain name'}</b></a></td>
278 <td width='10%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></td>
279</tr>
280END
281;
282
283#
284# Print each line of @current list
285#
286
287my $key = 0;
288foreach my $line (@current) {
289 chomp($line); # remove newline
290 my @temp=split(/\,/,$line);
291 $temp[3] ='' unless defined $temp[3]; # not always populated
292
293 #Choose icon for checkbox
294 my $gif = '';
295 my $gdesc = '';
296 if ($temp[0] ne '' ) {
297 $gif = 'on.gif';
298 $gdesc = $Lang::tr{'click to disable'};
299 } else {
300 $gif = 'off.gif';
301 $gdesc = $Lang::tr{'click to enable'};
302 }
303
304 #Colorize each line
305 if ($settings{'KEY1'} eq $key) {
306 print "<tr bgcolor='${Header::colouryellow}'>";
307 } elsif ($key % 2) {
308 print "<tr bgcolor='${Header::table2colour}'>";
309 } else {
310 print "<tr bgcolor='${Header::table1colour}'>";
311 }
312 print <<END
313<td align='center'>$temp[1]</td>
314<td align='center'>$temp[2]</td>
315<td align='center'>$temp[3]</td>
316<td align='center'>
317<form method='post' action='$ENV{'SCRIPT_NAME'}'>
318<input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
319<input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
320<input type='hidden' name='KEY1' value='$key' />
321</form>
322</td>
323
324<td align='center'>
325<form method='post' action='$ENV{'SCRIPT_NAME'}'>
326<input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
327<input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
328<input type='hidden' name='KEY1' value='$key' />
329</form>
330</td>
331
332<td align='center'>
333<form method='post' action='$ENV{'SCRIPT_NAME'}'>
334<input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
335<input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
336<input type='hidden' name='KEY1' value='$key' />
337</form>
338</td>
339</tr>
340END
341;
342 $key++;
343}
344print "</table>";
345
346# If table contains entries, print 'Key to action icons'
347if ($key) {
348print <<END
349<table>
350<tr>
351 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
352 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
353 <td class='base'>$Lang::tr{'click to disable'}</td>
354 <td>&nbsp;&nbsp;</td>
355 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
356 <td class='base'>$Lang::tr{'click to enable'}</td>
357 <td>&nbsp;&nbsp;</td>
358 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
359 <td class='base'>$Lang::tr{'edit'}</td>
360 <td>&nbsp;&nbsp;</td>
361 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
362 <td class='base'>$Lang::tr{'remove'}</td>
363</tr>
364</table>
365END
366;
367}
368
369&Header::closebox();
370&Header::closebigbox();
371&Header::closepage();
372
373## Ouf it's the end !
374
375# Sort the "current" array according to choices
376sub SortDataFile
377{
378 our %entries = ();
379
380 # Sort pair of record received in $a $b special vars.
381 # When IP is specified use numeric sort else alpha.
382 # If sortname ends with 'Rev', do reverse sort.
383 #
384 sub fixedleasesort {
385 my $qs=''; # The sort field specified minus 'Rev'
386 if (rindex ($settings{'SORT_HOSTSLIST'},'Rev') != -1) {
387 $qs=substr ($settings{'SORT_HOSTSLIST'},0,length($settings{'SORT_HOSTSLIST'})-3);
388 if ($qs eq 'IP') {
389 my @a = split(/\./,$entries{$a}->{$qs});
390 my @b = split(/\./,$entries{$b}->{$qs});
391 ($b[0]<=>$a[0]) ||
392 ($b[1]<=>$a[1]) ||
393 ($b[2]<=>$a[2]) ||
394 ($b[3]<=>$a[3]);
395 } else {
396 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
397 }
398 } else { #not reverse
399 $qs=$settings{'SORT_HOSTSLIST'};
400 if ($qs eq 'IP') {
401 my @a = split(/\./,$entries{$a}->{$qs});
402 my @b = split(/\./,$entries{$b}->{$qs});
403 ($a[0]<=>$b[0]) ||
404 ($a[1]<=>$b[1]) ||
405 ($a[2]<=>$b[2]) ||
406 ($a[3]<=>$b[3]);
407 } else {
408 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
409 }
410 }
411 }
412
413 #Use an associative array (%entries)
414 my $key = 0;
415 foreach my $line (@current) {
416 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
417 my @temp = ( '','','', '');
418 @temp = split (',',$line);
419
420 # Build a pair 'Field Name',value for each of the data dataline.
421 # Each SORTABLE field must have is pair.
422 # Other data fields (non sortable) can be grouped in one
423
424 my @record = ('KEY',$key++,'EN',$temp[0],'IP',$temp[1],'HOST',$temp[2],'DOM',$temp[3]);
425 my $record = {}; # create a reference to empty hash
426 %{$record} = @record; # populate that hash with @record
427 $entries{$record->{KEY}} = $record; # add this to a hash of hashes
428 }
429
430 open(FILE, ">$datafile") or die 'hosts datafile error';
431
432 # Each field value is printed , with the newline ! Don't forget separator and order of them.
433 foreach my $entry (sort fixedleasesort keys %entries) {
434 print FILE "$entries{$entry}->{EN},$entries{$entry}->{IP},$entries{$entry}->{HOST},$entries{$entry}->{DOM}\n";
435 }
436
437 close(FILE);
438 # Reload sorted @current
439 open (FILE, "$datafile");
440 @current = <FILE>;
441 close (FILE);
442}
443
444#
445# Build the configuration file
446#
447sub BuildConfiguration {
448 system '/usr/local/bin/rebuildhosts';
449}