f5ef9c66d4ac81f414f2fd38a5301b8ad981d8b0
[clearscm.git] / maps / bin / processaction.cgi
1 #!/usr/bin/perl
2 #################################################################################
3 #
4 # File:         $RCSfile: processaction.cgi,v $
5 # Revision:     $Revision: 1.1 $
6 # Description:  Process the action
7 # Author:       Andrew@DeFaria.com
8 # Created:      Mon Jan 16 20:25:32 PST 2006
9 # Modified:     $Date: 2013/06/12 14:05:47 $
10 # Language:     Perl
11 #
12 # (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
13 #
14 ################################################################################
15 use strict;
16 use warnings;
17
18 use FindBin;
19 $0 = $FindBin::Script;
20
21 use lib $FindBin::Bin;
22
23 use MAPS;
24 use MAPSWeb;
25
26 use CGI qw (:standard *table start_Tr end_Tr);
27 use CGI::Carp 'fatalsToBrowser';
28
29 my $type   = param 'type';
30 my $action = param 'action';
31 my $next   = param 'next';
32 my $userid = cookie 'MAPSUser';
33 my $lines;
34 my $total;
35 my $table_name = 'list';
36
37 my @scripts = ('ListActions.js');
38
39 sub ReturnSequenceNbrs {
40   my @names = param;
41   my @sequence_nbrs;
42
43   foreach (@names) {
44     if (/action(\d+)/) {
45       push @sequence_nbrs, $1;
46     } # if
47   } # foreach
48
49   return @sequence_nbrs;
50 } # ReturnSequenceNbrs
51
52 sub DeleteEntries {
53   my ($type) = @_;
54
55   my @sequence_nbrs = ReturnSequenceNbrs;
56
57   my $count;
58
59   foreach (@sequence_nbrs) {
60     $count += DeleteList $type, $_;
61   } # foreach
62
63   if ($count == 0) {
64     DisplayError 'Nothing to delete!';
65   } else {
66     ResequenceList $userid, $type;
67
68     if ($count == 1) {
69       print redirect ("/maps/php/list.php?type=$type&next=$next&message=Deleted entry");
70     } else {
71       print redirect ("/maps/php/list.php?type=$type&next=$next&message=Deleted $count entries");
72     } # if
73   } # if
74
75   return $count;
76 } # DeleteEntries
77
78 sub PrintInputLine ($$$$$) {
79   my ($nextseq, $email_nbr, $leftclass, $dataclass, $rightclass) = @_;
80
81   my $email     = '';
82   my $pattern   = '';
83   my $domain    = '';
84   my $hit_count = 0;
85
86   if (defined $email_nbr && $email_nbr ne '') {
87     $email = param "email$email_nbr";
88     if ($email && $email ne '') {
89       ($pattern, $domain) = split /\@/, $email;
90     } # if
91
92     $hit_count = CountMsg $email;
93   } # if
94
95   print Tr [
96     td {-class      => $leftclass,
97         -align      => 'center'}, "$nextseq",
98     td {-class      => $dataclass,
99         -align      => 'right'},
100       (textfield {-class     => 'inputfield',
101                   -style     => 'width:100%',
102                   -align     => 'right',
103                   -size      => 25,
104                   -maxlength => '255',
105                   -name      => "pattern$nextseq",
106                   -value     => $pattern}),
107     td {-class      => $dataclass,
108         -align      => 'center'}, '@',
109     td {-class      => $dataclass},
110       (textfield {-class      => 'inputfield',
111                   -style      => 'width:100%',
112                   -align      => 'left',
113                   -size       => 25,
114                   -maxlength  => '255',
115                   -name       => "domain$nextseq",
116                   -value      => $domain}),
117     td {-class      => $dataclass},
118       (textfield {-class     => 'inputfield',
119                   -style     => 'width:100%',
120                   -align     => 'left',
121                   -size      => 25,
122                   -maxlength => '255',
123                   -name      => "comment$nextseq",
124                   -value     => ''}),
125     td {-class      => $rightclass},
126       (textfield {-class     => 'inputfield',
127                   -style     => 'width:100%',
128                   -align     => 'left',
129                   -size      => 25,
130                   -maxlength => '255',
131                   -name      => "hit_count$nextseq",
132                   -value     => $hit_count}),
133   ];
134
135   return;
136 } # PrintInputLine
137
138 sub AddNewEntry {
139   my ($type, @selected)  = @_;
140
141   # First display the last page and add the appropriate number of
142   # empty, editable entries (possibly filled in) for the user to add
143   # the new entry
144   my $selected = @selected;
145   my $nextseq = MAPSDB::GetNextSequenceNo $userid, $type;
146   my $next = ($nextseq - $lines) + $selected - 1;
147
148   $next = 0
149     if $next < 0;
150
151   my $Type = ucfirst $type;
152
153   Heading (
154     'getcookie',
155     '',
156     "Add to $Type List",
157     "Add to $Type List",
158     '',
159     $table_name,
160     @scripts
161   );
162
163   NavigationBar $userid;
164
165   # Now display table and new entry
166   print start_form {
167     -method => 'post',
168     -action => 'add2' . $type . 'list.cgi',
169     -name   => 'list'
170   };
171
172   print start_table {-align       => 'center',
173                      -id          => $table_name,
174                      -border      => 0,
175                      -cellspacing => 0,
176                      -cellpadding => 4,
177                      -width       => '100%'};
178   print Tr [
179     th {-class => 'tableleftend'},  'Seq',
180     th {-class => 'tableheader'},   'Username',
181     th {-class => 'tableheader'},   '@',
182     th {-class => 'tableheader'},   'Domain',
183     th {-class => 'tableheader'},   'Comments',
184     th {-class => 'tablerightend'}, 'Hit Count'
185   ];
186
187   my @list = ReturnList $type, $next, $lines;
188   my %record;
189   my $i = 1;
190
191   foreach (@list) {
192     $i++;
193
194     %record = %{$_};
195
196     # Normalize fields
197     my $sequence  = $record{sequence};
198     my $pattern   = $record{pattern}   ? $record{pattern}   : '&nbsp;';
199     my $domain    = $record{domain}    ? $record{domain}    : '&nbsp;';
200     my $comment   = $record{comment}   ? $record{comment}   : '&nbsp;';
201     my $hit_count = $record{hit_count} ? $record{hit_count} : '&nbsp;';
202
203     print Tr [
204       td {-class  => 'tableleftdata',
205           -align  => 'center'}, $sequence,
206       td {-class  => 'tabledata',
207           -align  => 'right'}, $pattern,
208       td {-class  => 'tabledata',
209           -align  => 'center'}, '@',
210       td {-class  => 'tabledata',
211           -align  => 'left'}, $domain,
212       td {-class  => 'tabledata',
213           -align  => 'left'}, $comment,
214       td {-class  => 'tablerightdata',
215           -align  => 'right'}, $hit_count,
216     ];
217   } # foreach
218
219   # Now the input line(s)
220   if (@selected == 0) {
221     PrintInputLine $nextseq, undef, 'tablebottomleft', 'tablebottomdata',
222                                     'tablebottomright';
223   } else {
224     foreach (@selected) {
225       my $leftclass  = $i == $lines ? 'tablebottomleft'  : 'tableleftdata';
226       my $dataclass  = $i == $lines ? 'tablebottomdata'  : 'tabledata';
227       my $rightclass = $i == $lines ? 'tablebottomright' : 'tablerightdata';
228       $i++;
229       PrintInputLine $nextseq++, $_, $leftclass, $dataclass, $rightclass;
230     } # foreach
231   } # for
232
233   print end_table;
234   print br,
235     '<center>',
236       submit ({-name    => 'update',
237                -value   => 'Update',
238                -onClick => 'return CheckEntry (document.list);'}),
239       submit ({-name    => 'Reset',
240                -value   => 'Reset',
241                -onClick => 'history.back(); return false'}),
242     '</center>';
243   print end_form;
244
245   return;
246 } # AddNewEntry
247
248 sub ModifyEntries {
249   my ($type) = @_;
250
251   my @selected = ReturnSequenceNbrs;
252
253   my $Type = ucfirst $type;
254
255   Heading (
256     'getcookie',
257     '',
258     "Modify $Type List",
259     "Modify $Type List",
260     '',
261     $table_name,
262     @scripts
263   );
264
265   NavigationBar $userid;
266
267   # Redisplay the page but open up the lines that are getting modified
268   print start_form {
269     -method  => 'post',
270     -action  => 'modifyentries.cgi',
271     -name    => 'list'
272   };
273
274   # Print some hidden fields to pass along
275   print
276     hidden ({-name    => 'type',
277              -default => $type}),
278     hidden ({-name    => 'next',
279              -default => $next});
280
281   print start_table {-align       => 'center',
282                      -id          => $table_name,
283                      -border      => 0,
284                      -cellspacing => 0,
285                      -cellpadding => 4,
286                      -width       => '100%'};
287   print Tr [
288     th {-class => 'tableleftend'},  'Seq',
289     th {-class => 'tableheader'},   'Username',
290     th {-class => 'tableheader'},   '@',
291     th {-class => 'tableheader'},   'Domain',
292     th {-class => 'tableheader'},   'Comments',
293     th {-class => 'tablerightend'}, 'Hit Count',
294   ];
295
296   my @list = ReturnList $type, $next, $lines;
297   my %record;
298   my $s = 0;
299   my $i = 1;
300
301   foreach (@list) {
302     %record = %{$_};
303
304     my $sequence = $record{sequence};
305     my $leftclass  = ($i eq $lines || $sequence eq $total) ?
306       'tablebottomleft'  : 'tableleftdata';
307     my $dataclass  = ($i eq $lines || $sequence eq $total) ?
308       'tablebottomdata'  : 'tabledata';
309     my $rightclass = ($i eq $lines || $sequence eq $total) ?
310       'tablebottomright' : 'tablerightdata';
311
312     $i++;
313
314     print start_Tr,
315       td {-class  => $leftclass,
316           -align  => 'center'}, $record{sequence};
317
318     if ($record{sequence} eq $selected[$s]) {
319       $s++;
320       # Normalize fields
321       my $pattern   = $record{pattern}   ? $record{pattern}   : '';
322       my $domain    = $record{domain}    ? $record{domain}    : '';
323       my $comment   = $record{comment}   ? $record{comment}   : '';
324       my $hit_count = $record{hit_count} ? $record{hit_count} : '';
325
326       print
327         td {-class      => $dataclass,
328             -align      => 'right'},
329           (textfield {-class     => 'inputfield',
330                       -style     => 'width:100%',
331                       -align     => 'right',
332                       -size      => 25,
333                       -maxlength => '255',
334                       -name      => "pattern$sequence",
335                       -value     => $pattern}),
336         td {-class      => $dataclass,
337             -align      => 'center'}, '@',
338         td {-class      => $dataclass},
339           (textfield {-class     => 'inputfield',
340                       -style     => 'width:100%',
341                       -align     => 'left',
342                       -size      => 25,
343                       -maxlength => '255',
344                       -name      => "domain$sequence",
345                       -value     => $domain}),
346         td {-class      => $dataclass},
347            (textfield {-class     => 'inputfield',
348                        -style     => 'width:100%',
349                        -align     => 'left',
350                        -size      => 25,
351                        -maxlength => '255',
352                        -name      => "comment$sequence",
353                        -value     => $comment}),
354         td {-class      => $rightclass},
355            (textfield {-class     => 'inputfield',
356                        -style     => 'width:100%',
357                        -align     => 'left',
358                        -size      => 25,
359                        -maxlength => '255',
360                        -name      => "hit_count$sequence",
361                        -value     => $hit_count});
362     } else {
363       # Put in '&nbsp;' for undefined fields
364       my $pattern   = $record{pattern}   ? $record{pattern}   : '&nbsp;';
365       my $domain    = $record{domain}    ? $record{domain}    : '&nbsp;';
366       my $comment   = $record{comment}   ? $record{comment}   : '&nbsp;';
367       my $hit_count = $record{hit_count} ? $record{hit_count} : '&nbsp;';
368
369       print
370         td {-class => $dataclass,
371             -align => 'right'}, $pattern,
372         td {-class => $dataclass,
373             -align => 'center'}, '@',
374         td {-class => $dataclass,
375             -align => 'left'}, $domain,
376         td {-class => $dataclass,
377             -align => 'left'}, $comment,
378         td {-class => $rightclass,
379             -align => 'left'}, $hit_count;
380     } # if
381
382     print end_Tr;
383   } # foreach
384
385   print end_table;
386   print br,
387     '<center>',
388       submit ({-name    => 'update',
389                -value   => 'Update',
390                -onClick => 'return CheckEntry (document.list);'}),
391       submit ({-name    => 'Reset',
392                -value   => 'Reset',
393                -onClick => 'history.back(); return false'}),
394     '</center>';
395   print end_form;
396
397   return;
398 } # ModifyEntries
399
400 sub WhitelistMarked {
401   AddNewEntry 'white', ReturnSequenceNbrs;
402 } # WhitelistMarked
403
404 sub BlacklistMarked {
405   AddNewEntry 'black', ReturnSequenceNbrs;
406 } # BlacklistMarked
407
408 sub NulllistMarked {
409   AddNewEntry 'null', ReturnSequenceNbrs;
410 } # NulllistMarked
411
412 # Main
413 $userid ||= $ENV{USER};
414
415 SetContext $userid;
416
417 my %options = GetUserOptions $userid;
418
419 $lines = $options{'Page'};
420
421 $total = MAPSDB::count 'list', "userid = \"$userid\" and type = \"$type\""
422   if $type;
423
424 if ($action eq 'Add New Entry') {
425   AddNewEntry $type;
426 } elsif ($action eq 'Delete Marked') {
427   DeleteEntries $type;
428 } elsif ($action eq 'Modify Marked') {
429   ModifyEntries $type;
430 } elsif ($action eq 'Whitelist Marked') {
431   WhitelistMarked;
432 } elsif ($action eq 'Blacklist Marked') {
433   BlacklistMarked;
434 } elsif ($action eq 'Nulllist Marked') {
435   NulllistMarked;
436 } else {
437   Heading (
438     'getcookie',
439     '',
440     "Unknown action ($action)",
441     "Unknown action ($action)"
442   );
443
444   NavigationBar $userid;
445   DisplayError "Unknown action encountered ($action)";
446 } # if
447
448 Footing $table_name;
449
450 exit;