From f16b3b0b0f6dcfd00528e85abcc89ee51bf8978e Mon Sep 17 00:00:00 2001 From: Al Beano Date: Thu, 15 Sep 2016 22:04:29 +0100 Subject: [PATCH] unsubscribe button --- tormon/notify.pl | 4 +++- tormon/tormon.fcgi | 27 +++++++++++++++++++++++++++ tormon/unsubscribe.tt | 2 ++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tormon/unsubscribe.tt diff --git a/tormon/notify.pl b/tormon/notify.pl index dffa836..2427dd3 100755 --- a/tormon/notify.pl +++ b/tormon/notify.pl @@ -79,7 +79,9 @@ SUB: while (my $sub = $sth->fetchrow_hashref) { ], body => "Hi,\n\nThe Tor node with the fingerprint '" . $sub->{"fp"} . "' " . - ($status == 1 ? "is down." : "has disappeared from the Tor network!"), + ($status == 1 ? "is down." : "has disappeared from the Tor network!") + . "\n\n----------\nTo unsubscribe, click this link:\n" + . $config->{"baseurl"}."/unsubscribe?id=$sub->{id}&s=$sub->{secret}", ); my $trans = new Email::Sender::Transport::SMTPS ( host => $config->{mail}->{host}, diff --git a/tormon/tormon.fcgi b/tormon/tormon.fcgi index 6f98962..5d17551 100755 --- a/tormon/tormon.fcgi +++ b/tormon/tormon.fcgi @@ -166,6 +166,33 @@ while ($request->Accept() <= 0) { $content = read_file("$Bin/confirm.tt"); } } + case (/^\/unsubscribe\?id=[0-9]+&s=[a-z]{16}$/) { + if ($ENV{REQUEST_URI} =~ /^\/unsubscribe\?id=([0-9]+)&s=([a-z]{16})$/) { + my $id = $1; + my $secret = $2; + my $q = $dbh->prepare("select * from users where id=? and secret=?"); + $q->bind_param(1, $id); + $q->bind_param(2, $secret); + $q->execute; + my $href = $q->fetchrow_hashref; + + if ($q->rows != 1) { + $code = "Status: 403 Forbidden"; + my $tt = read_file("$Bin/error.tt"); + $content = ${ $tmpl->render($tt, {err => 403}) }; + last; + } + + $q->finish; + $q = $dbh->prepare("delete from users where id=? and secret=?"); + $q->bind_param(1, $id); + $q->bind_param(2, $secret); + $q->execute; + $q->finish; + + $content = read_file("$Bin/unsubscribe.tt"); + } + } else { my $tt = read_file("$Bin/error.tt"); $content = ${ $tmpl->render($tt, {err => 404}) }; diff --git a/tormon/unsubscribe.tt b/tormon/unsubscribe.tt new file mode 100644 index 0000000..3bd9abb --- /dev/null +++ b/tormon/unsubscribe.tt @@ -0,0 +1,2 @@ +

Thank you

+You are no longer subscribed.