]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/hosts.cgi
5 # This code is distributed under the terms of the GPL
7 # (c) Alan Hourihane <alanh@fairlite.demon.co.uk>
9 # $Id: hosts.cgi,v 1.4.2.20 2005/11/05 15:46:25 gespinasse Exp $
12 # use dhcp.cgi model to rewrite this code
16 # enable only the following on debugging purpose
18 #use CGI::Carp 'fatalsToBrowser';
20 require '/var/ipfire/general-functions.pl';
21 require "${General::swroot}/lang.pl";
22 require "${General::swroot}/header.pl";
24 #workaround to suppress a warning when a variable is used only once
25 my @dummy = ( ${Header
::colouryellow
} );
29 my $setting = "${General::swroot}/main/settings";
30 our $datafile = "${General::swroot}/main/hosts"; #(our: used in subroutine)
36 #Settings2 for editing the multi-line list
38 $settings{'EN'} = ''; # reuse for dummy field in position zero
40 $settings{'HOST'} = '';
41 $settings{'DOM'} = '';
42 my @nosaved=('EN','IP','HOST','DOM'); # List here ALL setting2 fields. Mandatory
44 $settings{'ACTION'} = ''; # add/edit/remove
45 $settings{'KEY1'} = ''; # point record for ACTION
47 #Define each field that can be used to sort columns
48 my $sortstring='^IP|^HOST|^DOM';
49 $settings{'SORT_HOSTSLIST'} = 'HOST';
50 my $errormessage = '';
53 &Header
::showhttpheaders
();
56 &Header
::getcgihash
(\
%settings);
60 if (open(FILE
, "$datafile")) {
65 ## Settings1 Box not used...
66 &General
::readhash
("${General::swroot}/main/settings", \
%settings);
69 ## Now manipulate the multi-line list with Settings2
71 # toggle the check box
72 # add/update a new line
73 # begin editing a line
77 # Toggle enable/disable field. Field is in second position
78 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
80 chomp(@current[$settings{'KEY1'}]);
81 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
83 $temp[0] = $temp[0] ne '' ?
'' : 'on'; # Toggle the field
84 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
85 $settings{'KEY1'} = ''; # End edit mode
87 &General
::log($Lang::tr
{'hosts config changed'});
90 open(FILE
, ">$datafile") or die 'hosts datafile error';
94 # Rebuild configuration file
98 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
100 unless(&General
::validip
($settings{'IP'})) {
101 $errormessage = $Lang::tr
{'invalid fixed ip address'};
104 unless(&General
::validhostname
($settings{'HOST'})) {
105 $errormessage = $Lang::tr
{'invalid hostname'};
108 if ($settings{'DOM'} && ! &General
::validdomainname
($settings{'DOM'})) {
109 $errormessage = $Lang::tr
{'invalid domain name'};
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'});
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'});
123 # Write changes to config file.
124 &SortDataFile
; # sort newly added/modified entry
125 &BuildConfiguration
; # then re-build new host
127 #map ($settings{$_}='' ,@nosaved); # Clear fields
131 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
133 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
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];
142 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
143 splice (@current,$settings{'KEY1'},1); # Delete line
144 open(FILE
, ">$datafile") or die 'hosts datafile error';
147 $settings{'KEY1'} = ''; # End remove mode
148 &General
::log($Lang::tr
{'hosts config changed'});
150 &BuildConfiguration
; # then re-build conf which use new data
155 ## Check if sorting is asked
156 # If same column clicked, reverse the sort.
157 if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
158 my $newsort=$ENV{'QUERY_STRING'};
159 my $actual=$settings{'SORT_HOSTSLIST'};
160 #Reverse actual sort ?
161 if ($actual =~ $newsort) {
163 if ($actual !~ 'Rev') {
168 $settings{'SORT_HOSTSLIST'}=$newsort;
169 map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
170 &General
::writehash
($setting, \
%settings);
172 $settings{'ACTION'} = 'SORT'; # Create an 'ACTION'
173 map ($settings{$_} = '' ,@nosaved,'KEY1'); # and reinit vars to empty
176 if ($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'};
182 &Header
::openpage
($Lang::tr
{'hostname'}, 1, '');
183 &Header
::openbigbox
('100%', 'left', '', $errormessage);
184 my %checked=(); # Checkbox manipulations
187 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
188 print "<font class='base'>$errormessage </font>";
193 # Remove if no Setting1 needed
196 # $warnmessage = "<font color=${Header::colourred}><b>$Lang::tr{'capswarning'}</b></font>: $warnmessage";
198 #&Header::openbox('100%', 'left', $Lang::tr{'settings'});
199 #print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
201 #<table width='100%'>
203 # <td class='base'>$Lang::tr{'domain name'} : $settings{'DOMAINNAME'}</td>
210 #<table width='100%'>
213 # <td class='base' width='25%'><!--<img src='/blob.gif' align='top' alt='*' /> $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>
221 #&Header::closebox(); # end of Settings1
225 # Second check box is for editing the list
227 $checked{'EN'}{'on'} = ($settings{'EN'} eq '' ) ?
'' : "checked='checked'";
229 my $buttontext = $Lang::tr
{'add'};
230 if ($settings{'KEY1'} ne '') {
231 $buttontext = $Lang::tr
{'update'};
232 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
234 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
237 #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
239 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
240 <input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
243 <td class='base'>$Lang::tr{'host ip'}: </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>
248 <td class='base'>$Lang::tr{'domain name'}: <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>
257 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' /> $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>
267 # Third box shows the list, in columns
269 # Columns headers may content a link. In this case it must be named in $sortstring
271 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
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>
284 # Print each line of @current list
288 foreach my $line (@current) {
289 chomp($line); # remove newline
290 my @temp=split(/\,/,$line);
291 $temp[3] ='' unless defined $temp[3]; # not always populated
293 #Choose icon for checkbox
296 if ($temp[0] ne '' ) {
298 $gdesc = $Lang::tr
{'click to disable'};
301 $gdesc = $Lang::tr
{'click to enable'};
305 if ($settings{'KEY1'} eq $key) {
306 print "<tr bgcolor='${Header::colouryellow}'>";
308 print "<tr bgcolor='${Header::table2colour}'>";
310 print "<tr bgcolor='${Header::table1colour}'>";
313 <td align='center'>$temp[1]</td>
314 <td align='center'>$temp[2]</td>
315 <td align='center'>$temp[3]</td>
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' />
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' />
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' />
346 # If table contains entries, print 'Key to action icons'
351 <td class='boldbase'> <b>$Lang::tr{'legend'}: </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> </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> </td>
358 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
359 <td class='base'>$Lang::tr{'edit'}</td>
360 <td> </td>
361 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
362 <td class='base'>$Lang::tr{'remove'}</td>
370 &Header
::closebigbox
();
371 &Header
::closepage
();
373 ## Ouf it's the end !
375 # Sort the "current" array according to choices
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.
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);
389 my @a = split(/\./,$entries{$a}->{$qs});
390 my @b = split(/\./,$entries{$b}->{$qs});
396 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
398 } else { #not reverse
399 $qs=$settings{'SORT_HOSTSLIST'};
401 my @a = split(/\./,$entries{$a}->{$qs});
402 my @b = split(/\./,$entries{$b}->{$qs});
408 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
413 #Use an associative array (%entries)
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);
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
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
430 open(FILE
, ">$datafile") or die 'hosts datafile error';
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";
438 # Reload sorted @current
439 open (FILE
, "$datafile");
445 # Build the configuration file
447 sub BuildConfiguration
{
448 system '/usr/local/bin/rebuildhosts';