blib/lib/Web/VWF/Display/index.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 183 CC 22 SCAR 59.2
CC
22
Cov
0%
html
506.0
BEGIN
2.0
BEGIN
2.0
CRAP 371.6
1 package Web::VWF::Display::index;
2  
3 # Display the index page.
4 # Certain variables are available to all templates,
5 # such as the stuff in the configuration file
6  
7 use strict;
8 use warnings;
9  
10 use version; our $VERSION = version->declare('0.004.3');
11  
12 use Carp;
13 use Web::VWF::Display;
14 use Web::VWF::Utils qw(create_disc_cache);
15 use Web::VWF::Mail;
16 use String::Random;
17  
18 our @ISA = ('Web::VWF::Display');
19  
20 sub html {
21 my $self = shift;
22 my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
23  
24 my $info = $self->{_info};
25 croak('Missing _info in object') unless $info;
26  
27 # Define allowed parameters (use state to avoid redeclaring in subsequent calls)
28 # state $allowed = {
29 my $allow = {
30 'person' => undef,
31 'action' => 'login',
32 'name' => undef,
33 'page' => 'index',
34 'password' => undef,
35 'lang' => qr/^[A-Z]{2}$/i,
36 'lint_content' => qr/^\d$/,
37 };
38  
39 my $config = $args{'config'};
40 my $logger = $args{'logger'};
41 my $params = $info->params({ allow => $allow });
42  
43 if(!defined($params)) {
44 # No parameters to process: display the main index page
45 return $self->SUPER::html();
46 }
47  
48 # Parameters to exclude from further processing
49 # my @exclude_keys = qw(page lint_content lang fbclid gclid);
50 # local delete @params{@exclude_keys};
51 delete local $params->{'page'};
52 delete local $params->{'lint_content'};
53 delete local $params->{'lang'};
54 delete local $params->{'fbclid'};
55 delete local $params->{'gclid'};
56  
57 # Database handle. A site without data files has none, and only the contact
58 # form needs it.
59 my $index = $args{'index'};
60  
61 # B::Lint reads `keys %{$params}` in scalar context as an implicit scalar
62 # context for a hash - a false positive, since the dereference is exactly
63 # what is meant - but CI runs B::Lint over every module and treats a finding
64 # as a failure, so the list is taken into an array before it is counted.
65 my @remaining = keys %{$params};
66 if(!@remaining) {
67 # No parameters to process: display the main index page
68 # When the contacts were last changed, if there are any: the handle
69 # opens its file only now, and dies if there is none
70 my $updated = $index ? eval { $index->updated() } : undef;
71 return $self->SUPER::html(defined($updated) ? (updated => $updated) : ());
72 }
73  
74 my $cache;
75 if(defined($params->{'action'})) {
76 $self->{'logindata'} ||= create_disc_cache(config => $config, logger => $logger, namespace => 'logindata', root_dir => $args{'cachedir'});
77 $cache = $self->{'logindata'};
78 if($params->{'action'} eq 'login') {
79 if((!defined($params->{'name'})) || !defined($params->{'password'})) {
80 return $self->SUPER::html({ error => 'Fill in name and password' });
81 }
82 # FIXME - read from configuration file
83 if(($params->{'name'} ne 'VWF') || ($params->{'password'} ne 'Password')) {
84 return $self->SUPER::html({ error => 'Incorrect name or password' });
85 }
86 my $rand = String::Random->new();
87 my $key;
88 do {
89 $key = $rand->randregex('\w\w\w\w\w\w\w\w');
90 } while($cache->get($key)); # FIXME: race condition
91 $self->set_cookie('session' => $key);
92 $self->{'logindata'}->set('VWF', $key, '1 day');
93 my $script_name = $info->script_name();
94 return "Location: $script_name?page=admin";
95 }
96 }
97 if(my $cookie = $info->get_cookie(cookie_name => 'session')) {
98 # No action was given, so the session store may not be open yet
99 $self->{'logindata'} ||= create_disc_cache(config => $config, logger => $logger, namespace => 'logindata', root_dir => $args{'cachedir'});
100 if(my $key = $self->{'logindata'}->get('VWF')) {
101 if($cookie eq $key) {
102 my $script_name = $info->script_name();
103 return "Location: $script_name?page=admin";
104 }
105 # A wrong cookie must not end the real session: anyone could
106 # send one, and so log the administrator out at will
107 }
108 return $self->SUPER::html({ error => 'You need to login to access the admin screen' });
109 }
110  
111 if(!defined($info->person())) {
112 return $self->SUPER::html({ error => 'Who do you want to contact?' });
113 }
114  
115 croak("Missing 'index' handle") unless($index);
116  
117 # Look in the index.db for the name given as the CGI argument and
118 # find their e-mail address
119 my $to = ($index->email({ entry => $info->person() }))[0];
120  
121 # Insert code here to error if $to isn't defined
122 if(!defined($to)) {
123 croak('No email entry assigned to ', $info->person());
124 }
125  
126 # Rate limit for sending e-mails
127 if(my $remote_addr = $ENV{'REMOTE_ADDR'}) {
128 if(($self->{'mailfrom'}->{$remote_addr} // 0) >= 2) {
129 $self->{_logger}->info("E-mail blocked to $to") if($self->{_logger});
130 return $self->SUPER::html({ error => 'You have reached your limit for sendmail e-mails' });
131 }
132 $self->{'mailfrom'}->{$remote_addr}++;
133 }
134  
135 # Send the email. The sender is in the site's own domain because that is
136 # the domain the configured key signs for: a From anywhere else would fail
137 # DMARC alignment at the receiver even though the signature itself verified.
138 my $from_email = 'webmaster@' . $info->domain_name();
139 my $mailer = Web::VWF::Mail->new(
140 from => $from_email,
141 domain => $info->domain_name(),
142 selector => $self->{_config}->{'DKIMSelector'},
143 keyfile => $self->{_config}->{'DKIMKey'},
144 );
145 $mailer->print("To: $to\n",
146 "From: $from_email\n",
147 "Subject: VWF sending an e-mail\n\n",
148 "Hello, world\n");
149  
150 if($mailer->close()) {
151 if($self->{_logger}) {
152 $self->{_logger}->trace("E-mail sent to $to");
153 }
154  
155 # Render the response that the email has been sent
156 return $self->SUPER::html({ action => 'sent', updated => $index->updated() });
157 }
158 return $self->SUPER::html({ error => "Can't send e-mail", updated => $index->updated() });
159 }
160  
161 1;
162  
163 =encoding utf8
164  
165 =head1 NAME
166  
167 Web::VWF::Display::index - The home page, with its contact form
168  
169 =head1 VERSION
170  
171 Version 0.004.3
172  
173 =head1 DESCRIPTION
174  
175 With no parameters it shows the F<index.tmpl> template. With C<?person=NAME>
176 it looks the address of C<NAME> up in the C<index> database (see
177 L<Web::VWF::Data::index>) and sends a message there with L<Web::VWF::Mail>,
178 signed with DKIM if the site has a key. Each client address may send two
179 messages in the life of the worker.
180  
181 C<action=login> with C<name> and C<password> starts an administrator session
182 (a cookie, kept in the C<logindata> cache for a day) and redirects to
183 C<?page=admin>. B<That login is a placeholder>: the credentials are fixed in
184 the code and it must be replaced before a site relies on it.
185  
186 =head1 METHODS
187  
188 =head2 html
189  
190 Takes C<config>, C<logger>, C<cachedir> and the C<index> data handle, which
191 L<Web::VWF> passes to every page. Croaks without C<_info>, when asked to
192 contact someone without the C<index> handle, or when the person named has no
193 address. The page itself needs no data. Returns the HTML, or a string
194 starting C<Location:> for a redirect.
195  
196 =head3 API SPECIFICATION
197  
198 =head4 INPUT
199  
200 {
201 index => { type => 'object', optional => 1 },
202 config => { type => 'object', optional => 1 },
203 logger => { type => 'object', optional => 1 },
204 cachedir => { type => 'string', optional => 1 },
205 }
206  
207 and the CGI parameters C<person>, C<action>, C<name>, C<password>, C<page>,
208 C<lang> and C<lint_content>.
209  
210 =head4 OUTPUT
211  
212 { type => 'string' }
213  
214 =head1 AUTHOR
215  
216 Nigel Horne, C<< <njh at nigelhorne.com> >>
217  
218 =head1 LICENCE AND COPYRIGHT
219  
220 Copyright 2010-2026 Nigel Horne.
221  
222 Usage is subject to the GPL2 licence terms.
223 If you use it,
224 please let me know.
225  
226 =cut