]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/hosts.cgi
Am Pakfire weitergearbeitet.
[people/pmueller/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
986e08d9 20require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
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
f2fdd0c1
CS
32my %color = ();
33my %mainsettings = ();
34&General::readhash("${General::swroot}/main/settings", \%mainsettings);
35&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
36
ac1cfefa
MT
37our %settings = ();
38#Settings1
39# removed
40
41#Settings2 for editing the multi-line list
42#Must not be saved !
43$settings{'EN'} = ''; # reuse for dummy field in position zero
44$settings{'IP'} = '';
45$settings{'HOST'} = '';
46$settings{'DOM'} = '';
47my @nosaved=('EN','IP','HOST','DOM'); # List here ALL setting2 fields. Mandatory
48
49$settings{'ACTION'} = ''; # add/edit/remove
50$settings{'KEY1'} = ''; # point record for ACTION
51
52#Define each field that can be used to sort columns
53my $sortstring='^IP|^HOST|^DOM';
54$settings{'SORT_HOSTSLIST'} = 'HOST';
55my $errormessage = '';
56my $warnmessage = '';
57
58&Header::showhttpheaders();
59
60#Get GUI values
61&Header::getcgihash(\%settings);
62
63# Load multiline data
64our @current = ();
65if (open(FILE, "$datafile")) {
66 @current = <FILE>;
67 close (FILE);
68}
69
70## Settings1 Box not used...
71&General::readhash("${General::swroot}/main/settings", \%settings);
72
73
74## Now manipulate the multi-line list with Settings2
75# Basic actions are:
76# toggle the check box
77# add/update a new line
78# begin editing a line
79# remove a line
80
81
82# Toggle enable/disable field. Field is in second position
83if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
84 #move out new line
85 chomp(@current[$settings{'KEY1'}]);
86 my @temp = split(/\,/,@current[$settings{'KEY1'}]);
87
88 $temp[0] = $temp[0] ne '' ? '' : 'on'; # Toggle the field
89 @current[$settings{'KEY1'}] = join (',',@temp)."\n";
90 $settings{'KEY1'} = ''; # End edit mode
91
92 &General::log($Lang::tr{'hosts config changed'});
93
94 #Save current
95 open(FILE, ">$datafile") or die 'hosts datafile error';
96 print FILE @current;
97 close(FILE);
98
99 # Rebuild configuration file
100 &BuildConfiguration;
101}
102
103if ($settings{'ACTION'} eq $Lang::tr{'add'}) {
104 # Validate inputs
105 unless(&General::validip($settings{'IP'})) {
106 $errormessage = $Lang::tr{'invalid fixed ip address'};
107 }
108
109 unless(&General::validhostname($settings{'HOST'})) {
110 $errormessage = $Lang::tr{'invalid hostname'};
111 }
112
113 if ($settings{'DOM'} && ! &General::validdomainname($settings{'DOM'})) {
114 $errormessage = $Lang::tr{'invalid domain name'};
115 }
116
117
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'});
122 } else {
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'});
126 }
127
128 # Write changes to config file.
129 &SortDataFile; # sort newly added/modified entry
130 &BuildConfiguration; # then re-build new host
131
132 #map ($settings{$_}='' ,@nosaved); # Clear fields
133 }
134}
135
136if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
137 #move out new line
138 my $line = @current[$settings{'KEY1'}]; # KEY1 is the index in current
139 chomp($line);
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];
145}
146
147if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
148 splice (@current,$settings{'KEY1'},1); # Delete line
149 open(FILE, ">$datafile") or die 'hosts datafile error';
150 print FILE @current;
151 close(FILE);
152 $settings{'KEY1'} = ''; # End remove mode
153 &General::log($Lang::tr{'hosts config changed'});
154
155 &BuildConfiguration; # then re-build conf which use new data
156}
157
158
159
160## Check if sorting is asked
161# If same column clicked, reverse the sort.
162if ($ENV{'QUERY_STRING'} =~ /$sortstring/ ) {
163 my $newsort=$ENV{'QUERY_STRING'};
164 my $actual=$settings{'SORT_HOSTSLIST'};
165 #Reverse actual sort ?
166 if ($actual =~ $newsort) {
167 my $Rev='';
168 if ($actual !~ 'Rev') {
169 $Rev='Rev';
170 }
171 $newsort.=$Rev;
172 }
173 $settings{'SORT_HOSTSLIST'}=$newsort;
174 map (delete ($settings{$_}) ,(@nosaved,'ACTION','KEY1'));# Must never be saved
175 &General::writehash($setting, \%settings);
176 &SortDataFile;
177 $settings{'ACTION'} = 'SORT'; # Create an 'ACTION'
178 map ($settings{$_} = '' ,@nosaved,'KEY1'); # and reinit vars to empty
179}
180
181if ($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'};
185}
186
187&Header::openpage($Lang::tr{'hostname'}, 1, '');
188&Header::openbigbox('100%', 'left', '', $errormessage);
189my %checked=(); # Checkbox manipulations
190
191if ($errormessage) {
192 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
193 print "<font class='base'>$errormessage&nbsp;</font>";
194 &Header::closebox();
195}
196
197#
198# Remove if no Setting1 needed
199#
200#if ($warnmessage) {
201# $warnmessage = "<font color=${Header::colourred}><b>$Lang::tr{'capswarning'}</b></font>: $warnmessage";
202#}
203#&Header::openbox('100%', 'left', $Lang::tr{'settings'});
204#print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>";
205#print <<END
206#<table width='100%'>
207#<tr>
208# <td class='base'>$Lang::tr{'domain name'} : $settings{'DOMAINNAME'}</td>
209#</table>
210#
211#END
212#;
213#
214#print <<END
215#<table width='100%'>
216#<hr />
217#<tr>
218# <td class='base' width='25%'><!--<img src='/blob.gif' align='top' alt='*' />&nbsp;$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>
221#</tr>
222#</table>
223#</form>
224#END
225#;
226#&Header::closebox(); # end of Settings1
227
228
229#
230# Second check box is for editing the list
231#
232$checked{'EN'}{'on'} = ($settings{'EN'} eq '' ) ? '' : "checked='checked'";
233
234my $buttontext = $Lang::tr{'add'};
235if ($settings{'KEY1'} ne '') {
236 $buttontext = $Lang::tr{'update'};
237 &Header::openbox('100%', 'left', $Lang::tr{'edit an existing host'});
238} else {
239 &Header::openbox('100%', 'left', $Lang::tr{'add a host'});
240}
241
242#Edited line number (KEY1) passed until cleared by 'save' or 'remove' or 'new sort order'
243print <<END
244<form method='post' action='$ENV{'SCRIPT_NAME'}'>
245<input type='hidden' name='KEY1' value='$settings{'KEY1'}' />
246<table width='100%'>
247<tr>
248 <td class='base'>$Lang::tr{'host ip'}:&nbsp;</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>
252</tr><tr>
253 <td class='base'>$Lang::tr{'domain name'}:&nbsp;<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>
257</tr>
258</table>
259<hr />
260<table width='100%'>
261<tr>
262 <td class='base' width='50%'><img src='/blob.gif' align='top' alt='*' />&nbsp;$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>
264</tr>
265</table>
266</form>
267END
268;
269&Header::closebox();
270
271#
272# Third box shows the list, in columns
273#
274# Columns headers may content a link. In this case it must be named in $sortstring
275#
276&Header::openbox('100%', 'left', $Lang::tr{'current hosts'});
277print <<END
278<table width='100%'>
279<tr>
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>
284</tr>
285END
286;
287
288#
289# Print each line of @current list
290#
291
292my $key = 0;
293foreach my $line (@current) {
294 chomp($line); # remove newline
295 my @temp=split(/\,/,$line);
296 $temp[3] ='' unless defined $temp[3]; # not always populated
297
298 #Choose icon for checkbox
299 my $gif = '';
300 my $gdesc = '';
301 if ($temp[0] ne '' ) {
302 $gif = 'on.gif';
303 $gdesc = $Lang::tr{'click to disable'};
304 } else {
305 $gif = 'off.gif';
306 $gdesc = $Lang::tr{'click to enable'};
307 }
308
309 #Colorize each line
310 if ($settings{'KEY1'} eq $key) {
311 print "<tr bgcolor='${Header::colouryellow}'>";
312 } elsif ($key % 2) {
f2fdd0c1 313 print "<tr bgcolor='$color{'color22'}'>";
ac1cfefa 314 } else {
f2fdd0c1 315 print "<tr bgcolor='$color{'color20'}'>";
ac1cfefa
MT
316 }
317 print <<END
318<td align='center'>$temp[1]</td>
319<td align='center'>$temp[2]</td>
320<td align='center'>$temp[3]</td>
321<td align='center'>
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' />
326</form>
327</td>
328
329<td align='center'>
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' />
334</form>
335</td>
336
337<td align='center'>
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' />
342</form>
343</td>
344</tr>
345END
346;
347 $key++;
348}
349print "</table>";
350
351# If table contains entries, print 'Key to action icons'
352if ($key) {
353print <<END
354<table>
355<tr>
356 <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</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>&nbsp;&nbsp;</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>&nbsp;&nbsp;</td>
363 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
364 <td class='base'>$Lang::tr{'edit'}</td>
365 <td>&nbsp;&nbsp;</td>
366 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
367 <td class='base'>$Lang::tr{'remove'}</td>
368</tr>
369</table>
370END
371;
372}
373
374&Header::closebox();
375&Header::closebigbox();
376&Header::closepage();
377
378## Ouf it's the end !
379
380# Sort the "current" array according to choices
381sub SortDataFile
382{
383 our %entries = ();
384
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.
388 #
389 sub fixedleasesort {
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);
393 if ($qs eq 'IP') {
394 my @a = split(/\./,$entries{$a}->{$qs});
395 my @b = split(/\./,$entries{$b}->{$qs});
396 ($b[0]<=>$a[0]) ||
397 ($b[1]<=>$a[1]) ||
398 ($b[2]<=>$a[2]) ||
399 ($b[3]<=>$a[3]);
400 } else {
401 $entries{$b}->{$qs} cmp $entries{$a}->{$qs};
402 }
403 } else { #not reverse
404 $qs=$settings{'SORT_HOSTSLIST'};
405 if ($qs eq 'IP') {
406 my @a = split(/\./,$entries{$a}->{$qs});
407 my @b = split(/\./,$entries{$b}->{$qs});
408 ($a[0]<=>$b[0]) ||
409 ($a[1]<=>$b[1]) ||
410 ($a[2]<=>$b[2]) ||
411 ($a[3]<=>$b[3]);
412 } else {
413 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
414 }
415 }
416 }
417
418 #Use an associative array (%entries)
419 my $key = 0;
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);
424
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
428
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
433 }
434
435 open(FILE, ">$datafile") or die 'hosts datafile error';
436
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";
440 }
441
442 close(FILE);
443 # Reload sorted @current
444 open (FILE, "$datafile");
445 @current = <FILE>;
446 close (FILE);
447}
448
449#
450# Build the configuration file
451#
452sub BuildConfiguration {
453 system '/usr/local/bin/rebuildhosts';
454}