| 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.003.0'); | ||
| 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 | ||
| 58 | my $index = $args{'index'}; | ||
| 59 | croak("Missing 'index' handle") unless($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 | return $self->SUPER::html(updated => $index->updated()); | ||
| 69 | } | ||
| 70 | |||
| 71 | my $cache; | ||
| 72 | if(defined($params->{'action'})) { | ||
| 73 | $self->{'logindata'} ||= create_disc_cache(config => $config, logger => $logger, namespace => 'logindata', root_dir => $args{'cachedir'}); | ||
| 74 | $cache = $self->{'logindata'}; | ||
| 75 | if($params->{'action'} eq 'login') { | ||
| 76 | if((!defined($params->{'name'})) || !defined($params->{'password'})) { | ||
| 77 | return $self->SUPER::html({ error => 'Fill in name and password' }); | ||
| 78 | } | ||
| 79 | # FIXME - read from configuration file | ||
| 80 | if(($params->{'name'} ne 'VWF') || ($params->{'password'} ne 'Password')) { | ||
| 81 | return $self->SUPER::html({ error => 'Incorrect name or password' }); | ||
| 82 | } | ||
| 83 | my $rand = String::Random->new(); | ||
| 84 | my $key; | ||
| 85 | do { | ||
| 86 | $key = $rand->randregex('\w\w\w\w\w\w\w\w'); | ||
| 87 | } while($cache->get($key)); # FIXME: race condition | ||
| 88 | $self->set_cookie('session' => $key); | ||
| 89 | $self->{'logindata'}->set('VWF', $key, '1 day'); | ||
| 90 | my $script_name = $info->script_name(); | ||
| 91 | return "Location: $script_name?page=admin"; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | if(my $cookie = $info->get_cookie(cookie_name => 'session')) { | ||
| 95 | # No action was given, so the session store may not be open yet | ||
| 96 | $self->{'logindata'} ||= create_disc_cache(config => $config, logger => $logger, namespace => 'logindata', root_dir => $args{'cachedir'}); | ||
| 97 | if(my $key = $self->{'logindata'}->get('VWF')) { | ||
| 98 | if($cookie eq $key) { | ||
| 99 | my $script_name = $info->script_name(); | ||
| 100 | return "Location: $script_name?page=admin"; | ||
| 101 | } | ||
| 102 | # A wrong cookie must not end the real session: anyone could | ||
| 103 | # send one, and so log the administrator out at will | ||
| 104 | } | ||
| 105 | return $self->SUPER::html({ error => 'You need to login to access the admin screen' }); | ||
| 106 | } | ||
| 107 | |||
| 108 | if(!defined($info->person())) { | ||
| 109 | return $self->SUPER::html({ error => 'Who do you want to contact?' }); | ||
| 110 | } | ||
| 111 | |||
| 112 | # Look in the index.db for the name given as the CGI argument and | ||
| 113 | # find their e-mail address | ||
| 114 | my $to = ($index->email({ entry => $info->person() }))[0]; | ||
| 115 | |||
| 116 | # Insert code here to error if $to isn't defined | ||
| 117 | if(!defined($to)) { | ||
| 118 | croak('No email entry assigned to ', $info->person()); | ||
| 119 | } | ||
| 120 | |||
| 121 | # Rate limit for sending e-mails | ||
| 122 | if(my $remote_addr = $ENV{'REMOTE_ADDR'}) { | ||
| 123 | if(($self->{'mailfrom'}->{$remote_addr} // 0) >= 2) { | ||
| 124 | $self->{_logger}->info("E-mail blocked to $to") if($self->{_logger}); | ||
| 125 | return $self->SUPER::html({ error => 'You have reached your limit for sendmail e-mails' }); | ||
| 126 | } | ||
| 127 | $self->{'mailfrom'}->{$remote_addr}++; | ||
| 128 | } | ||
| 129 | |||
| 130 | # Send the email. The sender is in the site's own domain because that is | ||
| 131 | # the domain the configured key signs for: a From anywhere else would fail | ||
| 132 | # DMARC alignment at the receiver even though the signature itself verified. | ||
| 133 | my $from_email = 'webmaster@' . $info->domain_name(); | ||
| 134 | my $mailer = Web::VWF::Mail->new( | ||
| 135 | from => $from_email, | ||
| 136 | domain => $info->domain_name(), | ||
| 137 | selector => $self->{_config}->{'DKIMSelector'}, | ||
| 138 | keyfile => $self->{_config}->{'DKIMKey'}, | ||
| 139 | ); | ||
| 140 | $mailer->print("To: $to\n", | ||
| 141 | "From: $from_email\n", | ||
| 142 | "Subject: VWF sending an e-mail\n\n", | ||
| 143 | "Hello, world\n"); | ||
| 144 | |||
| 145 | if($mailer->close()) { | ||
| 146 | if($self->{_logger}) { | ||
| 147 | $self->{_logger}->trace("E-mail sent to $to"); | ||
| 148 | } | ||
| 149 | |||
| 150 | # Render the response that the email has been sent | ||
| 151 | return $self->SUPER::html({ action => 'sent', updated => $index->updated() }); | ||
| 152 | } | ||
| 153 | return $self->SUPER::html({ error => "Can't send e-mail", updated => $index->updated() }); | ||
| 154 | } | ||
| 155 | |||
| 156 | 1; | ||
| 157 | |||
| 158 | =encoding utf8 | ||
| 159 | |||
| 160 | =head1 NAME | ||
| 161 | |||
| 162 | Web::VWF::Display::index - The home page, with its contact form | ||
| 163 | |||
| 164 | =head1 VERSION | ||
| 165 | |||
| 166 | Version 0.003.0 | ||
| 167 | |||
| 168 | =head1 DESCRIPTION | ||
| 169 | |||
| 170 | With no parameters it shows the F<index.tmpl> template. With C<?person=NAME> | ||
| 171 | it looks the address of C<NAME> up in the C<index> database (see | ||
| 172 | L<Web::VWF::Data::index>) and sends a message there with L<Web::VWF::Mail>, | ||
| 173 | signed with DKIM if the site has a key. Each client address may send two | ||
| 174 | messages in the life of the worker. | ||
| 175 | |||
| 176 | C<action=login> with C<name> and C<password> starts an administrator session | ||
| 177 | (a cookie, kept in the C<logindata> cache for a day) and redirects to | ||
| 178 | C<?page=admin>. B<That login is a placeholder>: the credentials are fixed in | ||
| 179 | the code and it must be replaced before a site relies on it. | ||
| 180 | |||
| 181 | =head1 METHODS | ||
| 182 | |||
| 183 | =head2 html | ||
| 184 | |||
| 185 | Takes C<config>, C<logger>, C<cachedir> and the C<index> data handle, which | ||
| 186 | L<Web::VWF> passes to every page. Croaks without C<_info> or the C<index> | ||
| 187 | handle, or when the person named has no address. Returns the HTML, or a string | ||
| 188 | starting C<Location:> for a redirect. | ||
| 189 | |||
| 190 | =head3 API SPECIFICATION | ||
| 191 | |||
| 192 | =head4 INPUT | ||
| 193 | |||
| 194 | { | ||
| 195 | index => { type => 'object' }, | ||
| 196 | config => { type => 'object', optional => 1 }, | ||
| 197 | logger => { type => 'object', optional => 1 }, | ||
| 198 | cachedir => { type => 'string', optional => 1 }, | ||
| 199 | } | ||
| 200 | |||
| 201 | and the CGI parameters C<person>, C<action>, C<name>, C<password>, C<page>, | ||
| 202 | C<lang> and C<lint_content>. | ||
| 203 | |||
| 204 | =head4 OUTPUT | ||
| 205 | |||
| 206 | { type => 'string' } | ||
| 207 | |||
| 208 | =head1 AUTHOR | ||
| 209 | |||
| 210 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||
| 211 | |||
| 212 | =head1 LICENCE AND COPYRIGHT | ||
| 213 | |||
| 214 | Copyright 2010-2026 Nigel Horne. | ||
| 215 | |||
| 216 | Usage is subject to the GPL2 licence terms. | ||
| 217 | If you use it, | ||
| 218 | please let me know. | ||
| 219 | |||
| 220 | =cut |