+(* COCANWIKI - a wiki written in Objective CAML.
+ * Written by Richard W.M. Jones <rich@merjis.com>.
+ * Copyright (C) 2004 Merjis Ltd.
+ * $Id: page_email_send.ml,v 1.1 2004/09/24 15:53:57 rich Exp $
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *)
+
+open Apache
+open Registry
+open Cgi
+open Printf
+
+open Cocanwiki
+open Cocanwiki_ok
+open Cocanwiki_template
+open Cocanwiki_strings
+
+let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
+ let template = get_template dbh hostid "page_email_send.txt" in
+
+ let page = q#param "page" in
+ let email = trim (q#param "email") in
+
+ if email = "" then (
+ error ~title:"No email address" ~back_button:true
+ q "You must give an email address.";
+ return ()
+ );
+
+ (* Good a place as any to delete old, unconfirmed emails. *)
+ let sth = dbh#prepare_cached "delete from page_emails
+ where pending is not null
+ and entry_date < current_date - 7" in
+ sth#execute [];
+ dbh#commit ();
+
+ (* Is that email address already registered in the database? *)
+ let sth = dbh#prepare_cached "select 1 from page_emails where hostid = ?
+ and url = ? and email = ?" in
+ sth#execute [`Int hostid; `String page; `String email];
+
+ let registered = try sth#fetch1int () = 1 with Not_found -> false in
+
+ if registered then (
+ error ~title:"Email address already used" ~back_button:true
+ q
+ ("That email address is already used for notifications from this page. "^
+ "If you are not receiving updates for this page, then you will " ^
+ "need to confirm that address. If you continue to have problems " ^
+ "please contact the site administrator.");
+ return ()
+ );
+
+ (* Create the unique pending and opt_out fields. The pending field
+ * allows the user to register. The opt_out field allows the user
+ * to unsubscribe.
+ *)
+ let pending = random_sessionid () in
+ let opt_out = random_sessionid () in
+
+ (* Insert into the database. *)
+ let sth = dbh#prepare_cached "insert into page_emails (hostid, url, email,
+ pending, opt_out) values (?, ?, ?, ?, ?)" in
+ sth#execute [`Int hostid; `String page; `String email; `String pending;
+ `String opt_out];
+
+ dbh#commit ();
+
+ (* Send the initial email to the user. *)
+ template#set "hostname" hostname;
+ template#set "page" page;
+ template#set "pending" pending;
+ template#set "opt_out" opt_out;
+
+ let body = template#to_string in
+ let subject = "Site notice: " ^ hostname ^ ": Confirm your email address" in
+ Sendmail.send_mail ~subject ~body ~to_addr:[email] ();
+
+ (* Finish up. *)
+ let buttons = [ ok_button ("/" ^ page) ] in
+ ok ~buttons ~title:"Confirmation email sent"
+ q ("Please check your email now. You have been sent a confirmation " ^
+ "email so we can verify the email address is yours. Click on the " ^
+ "first link in that email to confirm.")
+
+let () =
+ register_script run