repository

BlowfishCMS

A small content management system for OpenBSD

version 0.45.6 · ISC · 2026-12-03 · releases

61 lines · 4.1 KB · download this file

package BlowfishCMS;
# Contact: contact form delivery through Postmark.
# Every BlowfishCMS/*.pm file adds subs to the one BlowfishCMS package; request state
# is shared through the package variables below and reset by BlowfishCMS::handle_request.
use v5.36;
use utf8;
use JSON::PP qw(encode_json decode_json);
use Digest::SHA qw(hmac_sha256_hex sha256_hex);
use Encode qw(decode encode FB_CROAK);
use Fcntl qw(:flock :DEFAULT);
use Unicode::Normalize qw(NFKC);
use POSIX qw(strftime);
use File::Basename qw(basename);
our %instance;
our ($base,$data,$store,$cdata,$root,$db,$q,$route,$admin,$contact,$comments_route,$method,$cookie_header,$csrf);

# The contact service's own folder: its log, rate limits and mail settings.
sub contact_dir { $base.$instance{contact} }
# Adds a line to the contact log (never the message itself).
sub contact_log ($message) {open my $fh,'>>',contact_dir().'/contact.log' or return;print {$fh} now()." $message\n";close $fh}
# /contact/send, in the contact service (the only one that can read the
# Postmark token). Step by step:
#   - only POST, and only from this site's own pages;
#   - only while a page that's on has a contact form;
#   - a filled-in "company" trap field means a robot: pretend success;
#   - name, email and message must be there; line breaks are removed from
#     the name and subject so they can't add extra email headers;
#   - rate limits, kept in rate.json under a file lock (flock) so two
#     requests can't both count wrong: 5 messages an hour per address (by a
#     hash of the address), 50 an hour in all;
#   - the message is sent through Postmark to the receiving address, with
#     Reply-To set to the visitor, so replying answers them.
# Either way the browser goes back to /contact/ with ?sent=1 or ?sent=0.
sub contact_form {
    if ($method ne 'POST') {header(405,'text/plain; charset=UTF-8');print 'Method Not Allowed';return}
    my $origin=$ENV{HTTP_ORIGIN}//'';
    if ($origin ne '' && !own_origin($origin)) {header(403);print 'Forbidden';return}
    $db //= attempt {database()};
    unless ($db && contact_form_enabled()) {header(404,'text/plain; charset=UTF-8');print 'Not Found';return}
    redirect('/contact/?sent=1') if param('company',120) ne '';
    my ($name,$email,$subject,$message)=(param('name',120),param('email',190),param('subject',160)||'Website contact form',param('message',8000));
    my $form=param('form_name',60);
    redirect('/contact/?sent=0') unless $name && $email =~ /\A[^\s\@\r\n]+\@[^\s\@\r\n]+\.[^\s\@\r\n]+\z/ && $message && (!$form || ($form eq 'contact'));
    $name =~ s/[\r\n]+/ /g;$subject =~ s/[\r\n]+/ /g;
    my $token=mail_token();die "Postmark token unavailable\n" unless length $token;
    my $ip=$ENV{REMOTE_ADDR}//'unknown';
    open my $rate,'+>>',contact_dir().'/rate.json' or die "Quota unavailable\n";
    flock($rate,LOCK_EX) or die "Quota lock unavailable\n";seek($rate,0,0);
    my $raw=do{local $/;<$rate>};my $state=length($raw)?decode_json($raw):{};my $hour=int(time/3600);
    $state={hour=>$hour,total=>0,addresses=>{}} if ($state->{hour}//0)!=$hour;
    my $key=sha256_hex($ip);
    if (($state->{addresses}{$key}//0)>=5 || $state->{total}>=50) {close $rate;redirect('/contact/?sent=0')}
    $state->{addresses}{$key}++;$state->{total}++;seek($rate,0,0);truncate($rate,0) or die "Quota unavailable\n";print {$rate} encode_json($state) or die "Quota unavailable\n";close $rate or die "Quota unavailable\n";
    my $to=mail_config()->{to};die "No receiving address is set\n" unless $to =~ /\A[^\s\@<>]+\@[^\s\@<>]+\z/;
    my $text="New contact form submission from $instance{domain}\n\nName: $name\nEmail: $email\nSubject: $subject\nIP: $ip\n\nMessage:\n$message\n";
    my ($status,$result)=postmark_send($token,To=>$to,ReplyTo=>$email,Subject=>"[$instance{domain}] $subject",TextBody=>$text);
    if ($status==200 && defined($result->{ErrorCode}) && $result->{ErrorCode}==0) {contact_log('Postmark accepted message '.($result->{MessageID}//''));redirect('/contact/?sent=1')}
    contact_log('Postmark request failed; HTTP='.$status.' errorCode='.($result->{ErrorCode}//-1));redirect('/contact/?sent=0');
}

1;