]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/hosts.cgi
72d66b35982e756a6d394639586b6d27dbaa72f4
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)
33 my %mainsettings = ();
34 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
35 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
41 #Settings2 for editing the multi-line list
43 $settings{'EN'} = ''; # reuse for dummy field in position zero
45 $settings{'HOST'} = '';
46 $settings{'DOM'} = '';
47 my @nosaved=('EN','IP','HOST','DOM'); # List here ALL setting2 fields. Mandatory
49 $settings{'ACTION'} = ''; # add/edit/remove
50 $settings{'KEY1'} = ''; # point record for ACTION
52 #Define each field that can be used to sort columns
53 my $sortstring='^IP|^HOST|^DOM';
54 $settings{'SORT_HOSTSLIST'} = 'HOST';
55 my $errormessage = '';
58 &Header
::showhttpheaders
();
61 &Header
::getcgihash
(\
%settings);
65 if (open(FILE
, "$datafile")) {
70 ## Settings1 Box not used...
71 &General
::readhash
("${General::swroot}/main/settings", \
%settings);
74 ## Now manipulate the multi-line list with Settings2
76 # toggle the check box
77 # add/update a new line
78 # begin editing a line
82 # Toggle enable/disable field. Field is in second position
83 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
85 chomp(@current[$settings{'KEY1'}]);
86 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
88 $temp[0] = $temp[0] ne '' ?
'' : 'on'; # Toggle the field
89 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
90 $settings{'KEY1'} = ''; # End edit mode
92 &General
::log($Lang::tr
{'hosts config changed'});
95 open(FILE
, ">$datafile") or die 'hosts datafile error';
99 # Rebuild configuration file
103 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
105 unless(&General
::validip
($settings{'IP'})) {
106 $errormessage = $Lang::tr
{'invalid fixed ip address'};
109 unless(&General
::validhostname
($settings{'HOST'})) {
110 $errormessage = $Lang::tr
{'invalid hostname'};
113 if ($settings{'DOM'} && ! &General
::validdomainname
($settings{'DOM'})) {
114 $errormessage = $Lang::tr
{'invalid domain name'};
118 unless ($errormessage) {
119 if ($settings{'KEY1'} eq '') { #add or edit ?
120 unshift (@current, "$settings{'EN'},$settings{'IP'},$settings{'HOST'},$settings{'DOM'}\n");
121 &General
::log($Lang::tr
{'hosts config added'});
123 @current[$settings{'KEY1'}] = "$settings{'EN'},$settings{'IP'},$settings{'HOST'},$settings{'DOM'}\n";
124 $settings{'KEY1'} = ''; # End edit mode
125 &General
::log($Lang::tr
{'hosts config changed'});
128 # Write changes to config file.
129 &SortDataFile
; # sort newly added/modified entry
130 &BuildConfiguration
; # then re-build new host
132 #map ($settings{$_}='' ,@nosaved); # Clear fields
136 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
138 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
140 my @temp = split(/\,/, $line);
141 $settings{'EN'}=$temp[0]; # Prepare the screen for editing
142 $settings{'IP'}=$temp[1];
143 $settings{'HOST'}=$temp[2];
144 $settings{'DOM'}=$temp[3];
147 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
148 splice (@current,$settings{'KEY1'},1); # Delete line
149 open(FILE
, ">$datafile") or die 'hosts datafile error';
152 $settings{'KEY1'} = ''; # End remove mode
153 &General
::log($Lang::tr
{'hosts config changed'});
155 &BuildConfiguration
; # then re-build conf which use new data
160 ## Check if sorting is asked
161 # If same column clicked, reverse the sort.
162 if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
163 my $newsort=$ENV{'QUERY_STRING'};
164 my $actual=$settings{'SORT_HOSTSLIST'};
165 #Reverse actual sort ?
166 if ($actual =~ $newsort) {
168 if ($actual !~ 'Rev') {
173 $settings{'SORT_HOSTSLIST'}=$newsort;
174 map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
175 &General
::writehash
($setting, \
%settings);
177 $settings{'ACTION'} = 'SORT'; # Create an 'ACTION'
178 map ($settings{$_} = '' ,@nosaved,'KEY1'); # and reinit vars to empty
181 if ($settings{'ACTION'} eq '' ) { # First launch from GUI
182 # Place here default value when nothing is initialized
183 $settings{'EN'} = 'on';
184 $settings{'DOM'} = $settings{'DOMAINNAME'};
187 &Header
::openpage
($Lang::tr
{'hostname'}, 1, '');
188 &Header
::openbigbox
('100%', 'left', '', $errormessage);
189 my %checked=(); # Checkbox manipulations
192 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
193 print "<font class='base'>$errormessage </font>";
198 # Remove if no Setting1 needed
201 # $warnmessage = "<font color=${Header::colourred}><b>$Lang::tr{'capswarning'}</b></font>: $warnmessage";
203 #&Header::openbox('100%', 'left', $Lang::tr{'settings'});
204 #print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
206 #<table width='100%'>
208 # <td class='base'>$Lang::tr{'domain name'} : $settings{'DOMAINNAME'}</td>
215 #<table width='100%'>
218 # <td class='base' width='25%'><!--<img src='/blob.gif' align='top' alt='*' /> $Lang::tr{'this field may be blank'}</td>-->
219 # <td class='base' width='25%'>$warnmessage</td>
220 # <td width='50%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' disabled='disabled' /></td>
226 #&Header::closebox(); # end of Settings1
230 # Second check box is for editing the list
232 $checked{'EN'}{'on'} = ($settings{'EN'} eq '' ) ?
'' : "checked='checked'";
234 my $buttontext = $Lang::tr
{'add'};
235 if ($settings{'KEY1'} ne '') {
236 $buttontext = $Lang::tr
{'update'};
237 &Header
::openbox
('100%', 'left', $Lang::tr
{'edit an existing host'});
239 &Header
::openbox
('100%', 'left', $Lang::tr
{'add a host'});
242 #Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
244 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
245 <input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
248 <td class='base'>$Lang::tr{'host ip'}: </td>
249 <td><input type='text' name='IP' value='$settings{'IP'}' /></td>
250 <td class='base'>$Lang::tr{'hostname'}:</td>
251 <td><input type='text' name='HOST' value='$settings{'HOST'}' /></td>
253 <td class='base'>$Lang::tr{'domain name'}: <img src='/blob.gif' alt='*' /></td>
254 <td><input type='text' name='DOM' value='$settings{'DOM'}' /></td>
255 <td class='base'>$Lang::tr{'enabled'}</td>
256 <td><input type='checkbox' name='EN' $checked{'EN'}{'on'} /></td>
262 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' /> $Lang::tr{'this field may be blank'}</td>
263 <td width='50%' align='center'><input type='hidden' name='ACTION' value='$Lang::tr{'add'}' /><input type='submit' name='SUBMIT' value='$buttontext' /></td>
272 # Third box shows the list, in columns
274 # Columns headers may content a link. In this case it must be named in $sortstring
276 &Header
::openbox
('100%', 'left', $Lang::tr
{'current hosts'});
280 <td width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?IP'><b>$Lang::tr{'host ip'}</b></a></td>
281 <td width='20%' align='center'><a href='$ENV{'SCRIPT_NAME'}?HOST'><b>$Lang::tr{'hostname'}</b></a></td>
282 <td width='50%' align='center'><a href='$ENV{'SCRIPT_NAME'}?DOM'><b>$Lang::tr{'domain name'}</b></a></td>
283 <td width='10%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></td>
289 # Print each line of @current list
293 foreach my $line (@current) {
294 chomp($line); # remove newline
295 my @temp=split(/\,/,$line);
296 $temp[3] ='' unless defined $temp[3]; # not always populated
298 #Choose icon for checkbox
301 if ($temp[0] ne '' ) {
303 $gdesc = $Lang::tr
{'click to disable'};
306 $gdesc = $Lang::tr
{'click to enable'};
310 if ($settings{'KEY1'} eq $key) {
311 print "<tr bgcolor='${Header::colouryellow}'>";
313 print "<tr bgcolor='$color{'color22'}'>";
315 print "<tr bgcolor='$color{'color20'}'>";
318 <td align='center'>$temp[1]</td>
319 <td align='center'>$temp[2]</td>
320 <td align='center'>$temp[3]</td>
322 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
323 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
324 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
325 <input type='hidden' name='KEY1' value='$key' />
330 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
331 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
332 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
333 <input type='hidden' name='KEY1' value='$key' />
338 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
339 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
340 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
341 <input type='hidden' name='KEY1' value='$key' />
351 # If table contains entries, print 'Key to action icons'
356 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
357 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
358 <td class='base'>$Lang::tr{'click to disable'}</td>
359 <td> </td>
360 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
361 <td class='base'>$Lang::tr{'click to enable'}</td>
362 <td> </td>
363 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
364 <td class='base'>$Lang::tr{'edit'}</td>
365 <td> </td>
366 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
367 <td class='base'>$Lang::tr{'remove'}</td>
375 &Header
::closebigbox
();
376 &Header
::closepage
();
378 ## Ouf it's the end !
380 # Sort the "current" array according to choices
385 # Sort pair of record received in $a $b special vars.
386 # When IP is specified use numeric sort else alpha.
387 # If sortname ends with 'Rev', do reverse sort.
390 my $qs=''; # The sort field specified minus 'Rev'
391 if (rindex ($settings{'SORT_HOSTSLIST'},'Rev') != -1) {
392 $qs=substr ($settings{'SORT_HOSTSLIST'},0,length($settings{'SORT_HOSTSLIST'})-3);
394 my @a = split(/\./,$entries{$a}->{$qs});
395 my @b = split(/\./,$entries{$b}->{$qs});
401 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
403 } else { #not reverse
404 $qs=$settings{'SORT_HOSTSLIST'};
406 my @a = split(/\./,$entries{$a}->{$qs});
407 my @b = split(/\./,$entries{$b}->{$qs});
413 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
418 #Use an associative array (%entries)
420 foreach my $line (@current) {
421 chomp( $line); #remove newline because can be on field 5 or 6 (addition of REMARK)
422 my @temp = ( '','','', '');
423 @temp = split (',',$line);
425 # Build a pair 'Field Name',value for each of the data dataline.
426 # Each SORTABLE field must have is pair.
427 # Other data fields (non sortable) can be grouped in one
429 my @record = ('KEY',$key++,'EN',$temp[0],'IP',$temp[1],'HOST',$temp[2],'DOM',$temp[3]);
430 my $record = {}; # create a reference to empty hash
431 %{$record} = @record; # populate that hash with @record
432 $entries{$record->{KEY
}} = $record; # add this to a hash of hashes
435 open(FILE
, ">$datafile") or die 'hosts datafile error';
437 # Each field value is printed , with the newline ! Don't forget separator and order of them.
438 foreach my $entry (sort fixedleasesort
keys %entries) {
439 print FILE
"$entries{$entry}->{EN},$entries{$entry}->{IP},$entries{$entry}->{HOST},$entries{$entry}->{DOM}\n";
443 # Reload sorted @current
444 open (FILE
, "$datafile");
450 # Build the configuration file
452 sub BuildConfiguration
{
453 system '/usr/local/bin/rebuildhosts';