c9f0f58f7b1932721b6114ad23c71be5c29e4991
[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/../lib";
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   for (@names) {
44     if (/action(\d+)/) {
45       push @sequence_nbrs, $1;
46     } # if
47   } # for
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 = 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   for (@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   } # for
218
219   # Now the input line(s)
220   if (@selected == 0) {
221     PrintInputLine($nextseq, undef, 'tablebottomleft', 'tablebottomdata',
222                                     'tablebottomright');
223   } else {
224     for (@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     } # for
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   # Corner case: If on the first page (i.e. $next=0) then being zero based, we
297   # will actually get 21 entries in @list (i.e. $next=0, $lines=20 - 21 $entries
298   # are retrieved). So in that case, and that case only, we will change $lines
299   # to $lines - 1.
300   --$lines if $next == 0;
301
302   my @list = ReturnList($type, $next, $lines);
303   my %record;
304   my $s = 0;
305   my $i = 1;
306
307   for (@list) {
308     %record = %{$_};
309
310     my $sequence = $record{sequence};
311     my $leftclass  = ($i == @list || $sequence eq $total) ?
312       'tablebottomleft'  : 'tableleftdata';
313     my $dataclass  = ($i == @list || $sequence eq $total) ?
314       'tablebottomdata'  : 'tabledata';
315     my $rightclass = ($i == @list || $sequence eq $total) ?
316       'tablebottomright' : 'tablerightdata';
317
318     $i++;
319
320     print start_Tr,
321       td {-class  => $leftclass,
322           -align  => 'center'}, $record{sequence};
323
324     if ($record{sequence} eq $selected[$s]) {
325       $s++;
326       # Normalize fields
327       my $pattern   = $record{pattern}   ? $record{pattern}   : '';
328       my $domain    = $record{domain}    ? $record{domain}    : '';
329       my $comment   = $record{comment}   ? $record{comment}   : '';
330       my $hit_count = $record{hit_count} ? $record{hit_count} : '';
331
332       print
333         td {-class               => $dataclass,
334             -align               => 'right'},
335           (textfield {-class     => 'inputfield',
336                       -style     => 'width:100%',
337                       -align     => 'right',
338                       -size      => 25,
339                       -maxlength => '255',
340                       -name      => "pattern$sequence",
341                       -value     => $pattern}),
342         td {-class               => $dataclass,
343             -align               => 'center'}, '@',
344         td {-class               => $dataclass},
345           (textfield {-class     => 'inputfield',
346                       -style     => 'width:100%',
347                       -align     => 'left',
348                       -size      => 25,
349                       -maxlength => '255',
350                       -name      => "domain$sequence",
351                       -value     => $domain}),
352         td {-class               => $dataclass},
353           (textfield {-class     => 'inputfield',
354                       -style     => 'width:100%',
355                       -align     => 'left',
356                       -size      => 25,
357                       -maxlength => '255',
358                       -name      => "comment$sequence",
359                       -value     => $comment}),
360         td {-class               => $rightclass},
361            (textfield {-class     => 'inputfield',
362                        -style     => 'width:100%',
363                        -align     => 'left',
364                        -size      => 25,
365                        -maxlength => '255',
366                        -name      => "hit_count$sequence",
367                        -value     => $hit_count});
368     } else {
369       # Put in '&nbsp;' for undefined fields
370       my $pattern   = $record{pattern}   ? $record{pattern}   : '&nbsp;';
371       my $domain    = $record{domain}    ? $record{domain}    : '&nbsp;';
372       my $comment   = $record{comment}   ? $record{comment}   : '&nbsp;';
373       my $hit_count = $record{hit_count} ? $record{hit_count} : '&nbsp;';
374
375       print
376         td {-class => $dataclass,
377             -align => 'right'}, $pattern,
378         td {-class => $dataclass,
379             -align => 'center'}, '@',
380         td {-class => $dataclass,
381             -align => 'left'}, $domain,
382         td {-class => $dataclass,
383             -align => 'left'}, $comment,
384         td {-class => $rightclass,
385             -align => 'left'}, $hit_count;
386     } # if
387
388     print end_Tr;
389   } # for
390
391   print end_table;
392   print br,
393     '<center>',
394       submit ({-name    => 'update',
395                -value   => 'Update',
396                -onClick => 'return CheckEntry (document.list);'}),
397       submit ({-name    => 'Reset',
398                -value   => 'Reset',
399                -onClick => 'history.back(); return false'}),
400     '</center>';
401   print end_form;
402
403   return;
404 } # ModifyEntries
405
406 sub WhitelistMarked {
407   AddNewEntry('white', ReturnSequenceNbrs);
408 } # WhitelistMarked
409
410 sub BlacklistMarked {
411   AddNewEntry('black', ReturnSequenceNbrs);
412 } # BlacklistMarked
413
414 sub NulllistMarked {
415   AddNewEntry('null', ReturnSequenceNbrs);
416 } # NulllistMarked
417
418 # Main
419 $userid ||= $ENV{USER};
420
421 SetContext($userid);
422
423 my %options = GetUserOptions($userid);
424
425 $lines = $options{'Page'};
426
427 $total = count('list', "userid = \"$userid\" and type = \"$type\"") if $type;
428
429 if ($action eq 'Add New Entry') {
430   AddNewEntry($type);
431 } elsif ($action eq 'Delete Marked') {
432   DeleteEntries($type);
433 } elsif ($action eq 'Modify Marked') {
434   ModifyEntries($type);
435 } elsif ($action eq 'Whitelist Marked') {
436   WhitelistMarked;
437 } elsif ($action eq 'Blacklist Marked') {
438   BlacklistMarked;
439 } elsif ($action eq 'Nulllist Marked') {
440   NulllistMarked;
441 } else {
442   Heading(
443     'getcookie',
444     '',
445     "Unknown action ($action)",
446     "Unknown action ($action)"
447   );
448
449   NavigationBar($userid);
450   DisplayError("Unknown action encountered ($action)");
451 } # if
452
453 Footing($table_name);
454
455 exit;