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