index.cgi
changeset 17 bf0ff90e2cf5
parent 15 164da420a326
child 18 c250bcee5857
--- a/index.cgi	Wed Jul 06 09:33:50 2011 +0200
+++ b/index.cgi	Wed Jul 06 10:03:18 2011 +0200
@@ -20,8 +20,9 @@
 sub confirm($$);
 sub read_conf($);
 
-sub do_invite($);
-sub do_show($$);
+sub do_invite();
+sub page;
+sub mail;
 
 
 delete @ENV{grep /PATH$/ => keys %ENV};
@@ -57,53 +58,47 @@
 	print redirect(basename($ENV{SCRIPT_NAME}));
     }
 
-    # OK, let's start
-    print header(-charset => "UTF-8");
-    my $tt = Template->new(\%ttconfig);
 
     # ACCESS
     # Here we generate a link URL (sent via Mail) containing the
     # encrypted current timestamp. Accessing the form is only possible
     # using this link. Note: These links may not be unique!
     if (!path_info()) {
-	do_invite($tt);
+	do_invite();
 	exit 0;
     }
 
     # /show/(…)
     if (path_info() =~ /^\/?show\/(.*)$/) {
 	if ($1 ~~ [qw(info)]) {
-	    do_show($tt, $1);
+	    page("$1.tpl");
 	    exit 0;
 	}
-	$tt->process("html.denied.tpl");
+	page("html.denied.tpl");
 	exit 0;
     }
 
     # /user.<uuid>
     if (path_info() =~ /^\/?user\.(.*)$/) {
 	my $uuid = $1;
-	my $confirmed = param("confirm") eq "yes";
+
+	my $confirmed = param("confirm") eq "yes";   
 	my %data = confirm($uuid => $confirmed);
 
 	if ($data{error}) {
-	    $tt->process("html.denied.tpl");
+	    page("html.denied.tpl");
 	    exit 0;
 	}
 
-	open(my $sendmail, "|$SENDMAIL") or die "Can't open $SENDMAIL: $!\n";
-	$tt->process("mail.confirmed.tpl", {
+	mail("mail.confirmed.tpl", {
 	    to => $data{email},
 	    confirmed => $confirmed,
-	}, $sendmail)
-	or die $tt->error();
-	close($sendmail) or die "sendmail: $!\n";
+	});
 
-
-	$tt->process("html.confirmed.tpl", {
+	page("html.confirmed.tpl", {
 	    confirmed => $confirmed,
 	    error => delete $data{error},
-	    value => \%data}) or die $tt->error();
+	    value => \%data});
 	exit 0;
     }
 
@@ -118,7 +113,7 @@
 	    $time =~ /^\d+$/ or die "FORMAT ERROR";
 	    time() - $time < (60 * $EXPIRATION) or die "EXPIRED";
 	}; if ($@) {
-	    $tt->process("html.denied.tpl");
+	    page("html.denied.tpl");
 	    exit 0;
 	}
 
@@ -162,43 +157,37 @@
 	    if (!%warn) {
 		my %r = insert(%value);
 
-		open(my $sendmail => "|$SENDMAIL")
-		    or die "Can't open $SENDMAIL: $!\n";
-
-		$tt->process("mail.form-ack.tpl", {
+		mail("mail.form-ack.tpl", {
 		    to => $value{email},
 		    url => {
 			yes => "$SELF/user.$r{uuid}?confirm=yes",
 			no  => "$SELF/user.$r{uuid}?confirm=no",
 		    }
-		}, $sendmail)
-		or die $tt->error();
+		});
 
-		close($sendmail);
-
-		$tt->process("html.form-ack.tpl", {
+		page("html.form-ack.tpl", {
 		    value => \%value,
 		    created => $r{created},
 		    uuid => $r{uuid},
-		}) or die $tt->error();
+		});
 		exit 0;
 	    }
 	}
 
-	$tt->process("html.form.tpl", {
+	page("html.form.tpl", {
 	    warn => %warn ? \%warn : undef,
 	    value => {
 		givenname => scalar param("givenname"),
 		surname => scalar param("surname"),
 		email => scalar param("email"),
 	    },
-	} ) or die $tt->error();
+	} );
 	exit 0;
     }
 
-    $tt->process("html.denied.tpl", {
+    page("html.denied.tpl", {
 	    url => $SELF,
-    }) or die $tt->error();
+    });
     exit 0;
 }
 
@@ -231,6 +220,7 @@
 sub confirm($$) {
     my ($uuid, $confirmed) = @_;
     my %data;
+
     $DBH->begin_work;
 	
 	local $" = ", ";
@@ -268,7 +258,6 @@
 }
 
 sub do_invite() {
-	my ($tt) = @_;
 	my %warn;
 	my $sent;
 
@@ -281,22 +270,14 @@
 		$xxx =~ s/\+/-/g;
 		$xxx =~ s/\//_/g;
 
-		# send mail
-		open(my $sendmail => "|$SENDMAIL")
-		    or die "Can't open sendmail: $!\n";
-
-		$tt->process("mail.invitation.tpl", {
+		mail("mail.invitation.tpl", {
 		    to   => scalar(param("email")),
-		    url  => "$SELF/tmp.$xxx"}, $sendmail)
-		or die $tt->error();
-		close($sendmail)
-			or die "problem sending mail to "
-				. param("email");
+		    url  => "$SELF/tmp.$xxx"});
 
 		$sent = param("email");
 	    }
 	}
-	$tt->process("html.invitation.tpl", {
+	page("html.invitation.tpl", {
 	    sent => $sent,
 	    warn => %warn ? \%warn : undef,
 	    expires => $EXPIRATION,
@@ -304,7 +285,20 @@
 	});
 }
 
-sub do_show($$) {
-    my ($tt, $object) = @_;
-    $tt->process("$object.tpl");
+
+sub page {
+    state $tt = Template->new(\%ttconfig);
+    print header(-charset => "UTF-8");
+    $tt->process(@_);
 }
+
+sub mail {
+    state $tt = Template->new(\%ttconfig);
+    open(my $sendmail, "|$SENDMAIL") 
+	or die "Can't open $SENDMAIL: $!\n";
+    $tt->process(@_, $sendmail)
+    or die $tt->error();
+    close($sendmail) 
+	or die "SENDMAIL: $!\n";
+}
+