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