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