Initial commit
[clearscm.git] / maps / bin / MAPSWeb.pm
1 #################################################################################
2 #
3 # File:         $RCSfile: MAPSWeb.pm,v $
4 # Revision:     $Revision: 1.1 $
5 # Description:  Routines for generating portions of MAPSWeb
6 # Author:       Andrew@DeFaria.com
7 # Created:      Fri Nov 29 14:17:21  2002
8 # Modified:     $Date: 2013/06/12 14:05:47 $
9 # Language:     perl
10 #
11 # (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
12 #
13 ################################################################################
14 package MAPSWeb;
15
16 use strict;
17
18 use FindBin;
19
20 use lib $FindBin::Bin;
21
22 use MAPS;
23 use MAPSLog;
24 use MAPSUtil;
25
26 use CGI qw (:standard *table start_Tr end_Tr start_div end_div);
27 use vars qw (@ISA @EXPORT);
28
29 use Exporter;
30
31 @ISA = qw (Exporter);
32
33 @EXPORT = qw (
34   Debug
35   DisplayError
36   Footing
37   Heading
38   NavigationBar
39 );
40
41 sub getquickstats {
42   my $date = shift;
43
44   my %dates = GetStats (1, $date);
45
46   foreach (@MAPSLog::Types) {
47     $dates{$date}{processed} += $dates{$date}{$_};
48   } # foreach
49
50   return %dates;
51 } # getquickstats
52
53 sub displayquickstats {
54   # Quick stats are today only.
55   my $today = Today2SQLDatetime;
56   my $time  = substr $today, 11;
57   my $date  = substr $today, 0, 10;
58   my %dates = getquickstats $date;
59
60   print start_div {-class => 'quickstats'};
61   print h4 {-class      => 'header',
62             -align      => 'center'},
63     'Today\'s Activity';
64   print p {-align       => 'center'},
65     b ('as of ' . FormatTime ($time));
66   print start_table {
67     -align              => 'center',
68     -border             => 0,
69     -cellspacing        => 0,
70     -cellpadding        => 2};
71   print start_Tr {-align => 'right'};
72   print
73     td {-class  => 'smalllabel',
74         -align  => 'right'},
75       'Processed';
76   print
77     td {-class  => 'smallnumber',
78         -align  => 'right'},
79       $dates{$date}{'processed'};
80   print
81     td {-class  => 'smallnumber',
82         -align  => 'right'},
83       'n/a';
84   print end_Tr;
85
86   foreach (@MAPSLog::Types) {
87     print start_Tr {-align => 'right'};
88
89     my $value = $dates{$date}{$_};
90     my $percent;
91     if ($_ eq 'mailloop' || $_ eq 'registered') {
92       $percent = 'n/a';
93     } else {
94       $percent = $dates{$date}{processed} == 0 ?
95         0 : $dates{$date}{$_} / $dates{$date}{processed} * 100;
96       $percent = sprintf '%5.1f%s', $percent, '%';
97     } # if
98     my $stat = $value == 0 ?
99       0 : a {-href => "detail.cgi?type=$_;date=$date"}, $value;
100     print
101       td {-class        => 'smalllabel'}, ucfirst ($_);
102     print
103       td {-class        => 'smallnumber'}, $stat;
104     print
105       td {-class        => 'smallnumber'}, $percent;
106     print end_Tr;
107   } # foreach
108   print end_table;
109   print end_div;
110 } # displayquickstats
111
112 sub Footing (;$) {
113   my ($table_name) = @_;
114
115   # General footing (copyright). Note we calculate the current year
116   # so that the copyright automatically extends itself.
117   my $year = substr ((scalar (localtime)), 20, 4);
118
119   print start_div {-class => "copyright"};
120   print "Copyright © 2001-$year - All rights reserved";
121   print br (
122     a ({-href => 'http://defaria.com'},
123       'Andrew DeFaria'),
124     a ({-href => 'mailto:Andrew@DeFaria.com'},
125       '<Andrew@DeFaria.com>'));
126   print end_div;
127
128   print end_div; # This div ends "content" which was started in Heading
129   print "<script language='JavaScript1.2'>AdjustTableWidth (\"$table_name\");</script>"
130     if $table_name;
131   print end_html;
132 } # Footing
133
134 sub Debug ($) {
135   my ($msg) = @_;
136
137   print br, font ({ -class => 'error' }, 'DEBUG: '), $msg;
138 } # Debug
139
140 sub DisplayError ($) {
141   my ($errmsg) = @_;
142
143   print h3 ({-class => 'error',
144              -align => 'center'}, 'ERROR: ' . $errmsg);
145
146   Footing;
147
148   exit 1;
149 } # DisplayError
150
151 # This subroutine puts out the header for web pages. It is called by
152 # various cgi scripts thus has a few parameters.
153 sub Heading ($$$$;$$@) {
154   my ($action,          # One of getcookie, setcookie, unsetcookie
155       $userid,          # User id (if setting a cookie)
156       $title,           # Title string
157       $h1,              # H1 header
158       $h2,              # H2 header (optional)
159       $table_name,      # Name of table in page, if any
160       @scripts) = @_;   # Array of JavaScript scripts to include
161
162   my @java_scripts;
163   my $cookie;
164
165   # Since CheckAddress appears on all pages (well except for the login
166   # page) include it by default along with MAPSUtils.js
167   push @java_scripts, [
168     {-language  => 'JavaScript1.2',
169      -src       => '/maps/JavaScript/MAPSUtils.js'},
170     {-language  => 'JavaScript1.2',
171      -src       => '/maps/JavaScript/CheckAddress.js'}
172   ];
173
174   # Add on any additional JavaScripts that the caller wants. Note the
175   # odd single element array of hashes but that's what CGI requires!
176   # Build up scripts from array
177   foreach (@scripts) {
178     push @{$java_scripts[0]},
179       {-language        => 'JavaScript1.2',
180        -src             => "/maps/JavaScript/$_"}
181   } # foreach
182
183   # Since Heading is called from various scripts we sometimes need to
184   # set a cookie, other times delete a cookie but most times return the
185   # cookie.
186   if ($action eq 'getcookie') {
187     # Get userid from cookie
188     $userid = cookie ('MAPSUser');
189   } elsif ($action eq 'setcookie') {
190     $cookie = cookie (
191        -name    => 'MAPSUser',
192        -value   => $userid,
193        -expires => '+1y',
194        -path    => '/maps'
195     );
196   } elsif ($action eq 'unsetcookie') {
197     $cookie = cookie (
198        -name    => 'MAPSUser',
199        -value   => '',
200        -expires => '-1d',
201        -path    => '/maps'
202     );
203   } # if
204
205   print
206     header     (-title  => "MAPS: $title",
207                 -cookie => $cookie);
208
209   if (defined $table_name) {
210     print
211       start_html (-title        => "MAPS: $title",
212                   -author       => 'Andrew\@DeFaria.com',
213                   -style        => {-src        => '/maps/css/MAPSStyle.css'},
214                   -onResize     => "AdjustTableWidth (\"$table_name\");",
215                   -head         => [
216                     Link ({-rel  => 'icon',
217                            -href => '/maps/MAPS.png',
218                            -type => 'image/png'}),
219                     Link ({-rel  => 'shortcut icon',
220                            -href => '/maps/favicon.ico'})
221                   ],
222                   -script       => @java_scripts);
223   } else {
224     print
225       start_html (-title        => "MAPS: $title",
226                   -author       => 'Andrew\@DeFaria.com',
227                   -style        => {-src        => '/maps/css/MAPSStyle.css'},
228                   -head         => [
229                      Link ({-rel  => 'icon',
230                             -href => '/maps/MAPS.png',
231                             -type => 'image/png'}),
232                      Link ({-rel  => 'shortcut icon',
233                             -href => '/maps/favicon.ico'})],
234                   -script       => @java_scripts);
235   } # if
236
237   print start_div {class => 'heading'};
238   print h2 {-align      => 'center',
239             -class      => 'header'},
240     font ({-class       => 'standout'}, 'MAPS'),
241       $h1;
242
243   if (defined $h2 && $h2 ne '') {
244     print h3 {-align    => 'center',
245               -class    => 'header'},
246       $h2;
247   } # if
248   print end_div;
249
250   # Start body content
251   print start_div {-class => 'content'};
252
253   return $userid
254 } # Heading
255
256 sub NavigationBar {
257   my $userid = shift;
258
259   print start_div {-id => 'leftbar'};
260
261   if (!defined $userid) {
262     print div ({-class  => 'username'}, 'Welcome to MAPS');
263     print div ({-class  => 'menu'},
264       (a {-href => '/maps/doc/'},
265         'What is MAPS?<br>'),
266       (a {-href => '/maps/doc/SPAM.html'},
267         'What is SPAM?<br>'),
268       (a {-href => '/maps/doc/Requirements.html'},
269         'Requirements<br>'),
270       (a {-href => '/maps/SignupForm.html'},
271         'Signup<br>'),
272       (a {-href => '/maps/doc/Using.html'},
273         'Using MAPS<br>'),
274       (a {-href => '/maps/doc/'},
275         'Help<br>'),
276     );
277   } else {
278     print div ({-class  => 'username'}, 'Welcome '. ucfirst $userid);
279     print div ({-class  => 'menu'},
280       (a {-href => '/maps/'},
281         'MAPS Home<br>'),
282       (a {-href => '/maps/bin/stats.cgi'},
283         'Statistics<br>'),
284       (a {-href => '/maps/bin/editprofile.cgi'},
285         'Edit Profile<br>'),
286       (a {-href => '/maps/php/Reports.php'},
287         'Reports<br>'),
288       (a {-href => '/maps/php/list.php?type=white'},
289         'White List<br>'),
290       (a {-href => '/maps/php/list.php?type=black'},
291         'Black List<br>'),
292       (a {-href => '/maps/php/list.php?type=null'},
293         'Null List<br>'),
294       (a {-href => '/maps/doc/'},
295         'Help<br>'),
296       (a {-href => '/maps/adm/'},
297         'MAPS Admin<br>'),
298       (a {-href => '/maps/?logout=yes'},
299         'Logout'),
300     );
301     print start_div {-class => 'search'};
302     print start_form {-method   => 'get',
303                       -action   => '/maps/bin/search.cgi',
304                       -name     => 'search'};
305     print 'Search Sender/Subject',
306       textfield {-class         => 'searchfield',
307                  -id            => 'searchfield',
308                  -name          => 'str',
309                  -size          => 20,
310                  -maxlength     => 255,
311                  -value         => '',
312                  -onclick       => "document.search.str.value = '';"};
313     print end_form;
314     print end_div;
315
316     displayquickstats;
317
318     print start_div {-class => 'search'};
319     print start_form {-method   => 'post',
320                       -action   => 'javascript://',
321                       -name     => 'address',
322                       -onsubmit => 'checkaddress(this);'};
323     print 'Check Email Address',
324       textfield {-class         => 'searchfield',
325                  -id            => 'searchfield',
326                  -name          => 'email',
327                  -size          => 20,
328                  -maxlength     => 255,
329                  -value         => '',
330                  -onclick       => "document.address.email.value = '';"};
331     print end_form;
332     print end_div;
333   } # if
334
335   print end_div;
336 } # NavigationBar
337
338 1;