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