Handle hostnames with upper-case letters
[webmin.git] / itsecur-firewall / import_times.cgi
1 #!/usr/bin/perl
2 # Actually do an import of time ranges
3
4 require './itsecur-lib.pl';
5 &can_edit_error("import");
6 &error_setup($text{'import_err'});
7 &ReadParseMime();
8
9 # Validate inputs
10 if (!$in{'src_def'}) {
11         -r $in{'src'} || &error_cleanup($text{'restore_esrc'});
12         $data = `cat $in{'src'}`;
13         }
14 else {
15         $in{'file'} || &error_cleanup($text{'restore_efile'});
16         $data = $in{'file'};
17         }
18
19 %times = map { $_->{'name'}, $_ } &list_times();
20 %daynum = ( "sun", 0, "mon", 1, "tue", 2, "wed", 3, "thu", 4, "fri", 5, "sat", 6 );
21
22 # Parse the CSV data
23 $data =~ s/\r//g;
24 $i = 0;
25 foreach $line (split(/\n/, $data)) {
26         # Split into columns
27         $oldline = $line;
28         $i++;
29         next if (!$line);
30         local @row;
31         while($line && $line =~ /^,?("([^"]*)"|([^,]*))(.*)$/) {
32                 push(@row, $2 || $3);
33                 $line = $4;
34                 }
35         @row >= 1 || &error(&text('import_erow', $i, $oldline));
36
37         # Create a service
38         $row[0] =~ /\S/ || &error(text('import_etimename', $i));
39         $times{$row[0]} && &error(text('import_etimeclash', $i, $row[0]));
40         $time = { 'name' => $row[0] };
41         if ($row[1]) {
42                 # Week days are given
43                 foreach $d (split(/[\s|,]+/, $row[1])) {
44                         local $dn = $daynum{lc($d)};
45                         defined($dn) || &error(text('import_etimeday', $i, $d));
46                         push(@days, $dn);
47                         }
48                 $time->{'days'} = join(",", @days);
49                 }
50         else {
51                 $time->{'days'} = '*';
52                 }
53         if ($row[2]) {
54                 # Time range is given
55                 $row[2] =~ /^(\d+):(\d+)\-(\d+):(\d+)$/ &&
56                         $1 >= 0 && $1 < 24 &&
57                         $2 >= 0 && $2 < 60 &&
58                         $3 >= 0 && $3 < 24 &&
59                         $4 >= 0 && $4 < 60 ||
60                         &error(&text('import_etimehour', $i, $row[2]));
61                 $time->{'hours'} = $row[2];
62                 }
63         else {
64                 $time->{'hours'} = '*';
65                 }
66         $time->{'days'} eq '*' && $time->{'hours'} eq '*' &&
67                 &error(text('import_etimenone', $i));
68         push(@newtimes, $time);
69         }
70
71 # Save the groups
72 &lock_itsecur_files();
73 @times = &list_times();
74 push(@times, @newtimes);
75 &automatic_backup();
76 &save_times(@times);
77 &unlock_itsecur_files();
78
79 # Tell the user
80 &header($text{'import_title'}, "",
81         undef, undef, undef, undef, &apply_button());
82 print "<hr>\n";
83
84 print "<p>",&text('import_done4', scalar(@newtimes)),"<p>\n";
85
86 print "<hr>\n";
87 &footer("", $text{'index_return'});
88 &remote_webmin_log("import", "times", $in{'src_def'} ? undef : $in{'src'});
89