Handle hostnames with upper-case letters
[webmin.git] / syslog-ng / edit_filter.cgi
1 #!/usr/local/bin/perl
2 # Show a form for editing or creating a log filter
3
4 require './syslog-ng-lib.pl';
5 &ReadParse();
6
7 # Show title and get the filter
8 $conf = &get_config();
9 if ($in{'new'}) {
10         &ui_print_header(undef, $text{'filter_title1'}, "");
11         }
12 else {
13         &ui_print_header(undef, $text{'filter_title2'}, "");
14         @filters = &find("filter", $conf);
15         ($filter) = grep { $_->{'value'} eq $in{'name'} } @filters;
16         $filter || &error($text{'filter_egone'});
17         }
18
19 # Form header
20 print &ui_form_start("save_filter.cgi", "post");
21 print &ui_hidden("new", $in{'new'}),"\n";
22 print &ui_hidden("old", $in{'name'}),"\n";
23 print &ui_table_start($text{'filter_header'}, undef, 2);
24
25 # Filter name
26 print &ui_table_row($text{'filter_name'},
27                     &ui_textbox("name", $filter->{'value'}, 20));
28
29 # See if the conditions are simple (1 or more of facility, level, matches
30 # and program, and separated)
31 $simple = 1;
32 foreach $m (@{$filter->{'members'}}) {
33         if (ref($m)) {
34                 if ($simple{$m->{'name'}}) {
35                         # Already done this type .. not simple any more
36                         $simple = 0;
37                         }
38                 else {
39                         $simple{$m->{'name'}} = $m;
40                         }
41                 }
42         elsif ($m eq "and") {
43                 # 'and' separator .. we can handle this
44                 }
45         else {
46                 $simple = 0;
47                 }
48         }
49
50 # Build table for simple mode
51 %simple = ( ) if (!$simple);
52 $stable = "<table>\n";
53
54 # Priority
55 $p = $simple{'priority'};
56 @pris = &list_priorities();
57 if ($p) {
58         if ($p->{'values'}->[1] eq "..") {
59                 $i1 = &indexof(@pris, $p->{'values'}->[0]);
60                 $i2 = &indexof(@pris, $p->{'values'}->[2]);
61                 if ($i1 >= 0 && $i2 >= 0) {
62                         %gotpris = map { $pris[$_], 1 } ($i1 .. $i2);
63                         }
64                 }
65         else {
66                 %gotpris = map { $_, 1 } grep { $_ ne "," } @{$p->{'values'}};
67                 }
68         }
69 $stable .= "<tr> <td><b>".&ui_checkbox("priority", 1, $text{'filter_priority'},
70                                        $p ? 1 : 0)."</b></td>\n";
71 $stable .= "<td>";
72 foreach $p (@pris) {
73         $stable .= &ui_checkbox("pri", $p, $p, $gotpris{$p})."\n";
74         }
75 $stable .= "</td> </tr>\n";
76
77 # Facility
78 $p = $simple{'facility'};
79 @facs = &list_facilities();
80 if ($p) {
81         %gotfacs = map { $_, 1 } grep { $_ ne "," } @{$p->{'values'}};
82         }
83 $stable .= "<tr> <td valign=top><b>".
84   &ui_checkbox("facility", 1, $text{'filter_facility'}, $p ? 1 : 0).
85   "</b></td>\n";
86 $stable .= "<td><table>";
87 $i = 0;
88 foreach $f (@facs) {
89         $stable .= "<tr>\n" if ($i % 8 == 0);
90         $stable .= "<td>".&ui_checkbox("fac", $f, $f, $gotfacs{$f})."</td>\n";
91         $stable .= "<tr>\n" if ($i % 8 == 7);
92         $i++;
93         }
94 $stable .= "</table></td> </tr>\n";
95
96 # Program
97 $p = $simple{'program'};
98 $stable .= "<tr> <td><b>".&ui_checkbox("program", 1, $text{'filter_program'},
99                                        $p ? 1 : 0)."</b></td>\n";
100 $stable .= "<td>".&ui_textbox("prog", $p ? $p->{'value'} : undef, 30).
101            "</td> </tr>\n";
102
103 # Match RE
104 $p = $simple{'match'};
105 $stable .= "<tr> <td><b>".&ui_checkbox("match", 1, $text{'filter_match'},
106                                        $p ? 1 : 0)."</b></td>\n";
107 $stable .= "<td>".&ui_textbox("re", $p ? $p->{'value'} : undef, 50).
108            "</td> </tr>\n";
109
110 # Sending hostname
111 $p = $simple{'host'};
112 $stable .= "<tr> <td><b>".&ui_checkbox("host", 1, $text{'filter_host'},
113                                        $p ? 1 : 0)."</b></td>\n";
114 $stable .= "<td>".&ui_textbox("hn", $p ? $p->{'value'} : undef, 30).
115            "</td> </tr>\n";
116
117 # Sending IP
118 $p = $simple{'netmask'};
119 if ($p) {
120       ($net, $mask) = split(/\//, $p->{'value'});
121       }
122 $stable .= "<tr> <td><b>".&ui_checkbox("netmask", 1, $text{'filter_netmask'},
123                                        $p ? 1 : 0)."</b></td>\n";
124 $stable .= "<td>".&ui_textbox("net", $net, 15)."/".
125                   &ui_textbox("mask", $mask, 15)."</td> </tr>\n";
126
127 $stable .= "</table>\n";
128
129 # Build text area for complex mode
130 local @w;
131 foreach $m (@{$filter->{'members'}}) {
132         if (ref($m)) {
133                 local ($line) = &directive_lines($m);
134                 $line =~ s/\s*;\s*$//;
135                 push(@w, $line);
136                 }
137         else {
138                 push(@w, $m);
139                 }
140         }
141 $ctable = &ui_textarea("bool", join(" ", @w), 5, 80);
142
143 # Show the two modes
144 print "<tr> <td colspan=2>\n";
145 print &ui_oneradio("mode", 0, "<b>$text{'filter_mode0'}</b>", $simple),"<br>\n";
146 print $stable,"<br>\n";
147 print &ui_oneradio("mode", 1, "<b>$text{'filter_mode1'}</b>",!$simple),"<br>\n";
148 print $ctable,"<br>\n";
149 print "</td> </tr>\n";
150
151 # Form footer and buttons
152 print &ui_table_end();
153 if ($in{'new'}) {
154         print &ui_form_end([ [ "create", $text{'create'} ] ]);
155         }
156 else {
157         print &ui_form_end([ [ "save", $text{'save'} ],
158                              [ "delete", $text{'delete'} ] ]);
159         }
160 &ui_print_footer("list_filters.cgi", $text{'filters_return'});