Handle hostnames with upper-case letters
[webmin.git] / itsecur-firewall / save_time.cgi
1 #!/usr/bin/perl
2 # save_time.cgi
3 # Create, update or delete a time range
4
5 require './itsecur-lib.pl';
6 &can_edit_error("times");
7 &lock_itsecur_files();
8 &ReadParse();
9 @times = &list_times();
10 if (!$in{'new'}) {
11         $time = $times[$in{'idx'}];
12         }
13
14 if ($in{'delete'}) {
15         # Check if in use
16         &error_setup($text{'time_err2'});
17         @rules = &list_rules();
18         foreach $r (@rules) {
19                 &error($text{'time_einuse'})
20                         if ($r->{'time'} eq $time->{'name'});
21                 }
22
23         # Just delete this time
24         splice(@times, $in{'idx'}, 1);
25         &automatic_backup();
26         }
27 else {
28         # Validate inputs
29         &error_setup($text{'time_err'});
30         $in{'name'} =~ /^\S+$/ || &error($text{'time_ename'});
31         if ($in{'new'} || $in{'name'} ne $time->{'name'}) {
32                 # Check for clash
33                 ($clash) = grep { lc($_->{'name'}) eq lc($in{'name'}) } @times;
34                 $clash && &error($text{'time_eclash'});
35                 }
36         if (!$in{'hours_def'}) {
37                 foreach $t ('from', 'to') {
38                         $tm = $in{$t};
39                         $tm =~ /^(\d+):(\d+)$/ || &error($text{'time_e'.$t});
40                         $1 >= 0 && $1 < 24 || &error($text{'time_ehour'.$t});
41                         $2 >= 0 && $2 < 60 || &error($text{'time_emin'.$t});
42                         }
43                 }
44         if (!$in{'days_def'}) {
45                 @days = split(/\0/, $in{'days'});
46                 @days || &error($text{'time_edays'});
47                 }
48         $oldname = $time->{'name'};
49         $time->{'name'} = $in{'name'};
50         $time->{'hours'} = $in{'hours_def'} ? "*" :
51                                 $in{'from'}."-".$in{'to'};
52         $time->{'days'} = $in{'days_def'} ? "*" :
53                                 join(",", @days);
54
55         if ($in{'new'}) {
56                 push(@times, $time);
57                 }
58
59         &automatic_backup();
60         if (!$in{'new'} && $oldname ne $time->{'name'}) {
61                 # Has been re-named .. update all rules!
62                 @rules = &list_rules();
63                 foreach $r (@rules) {
64                         if ($r->{'time'} eq $oldname) {
65                                 $r->{'time'} = $time->{'name'};
66                                 }
67                         }
68                 &save_rules(@rules);
69                 }
70         }
71
72 &save_times(@times);
73 &unlock_itsecur_files();
74 &remote_webmin_log($in{'delete'} ? "delete" : $in{'new'} ? "create" : "update",
75             "time", $time->{'name'}, $time);
76 &redirect("list_times.cgi");
77