1 (* COCANWIKI - a wiki written in Objective CAML.
2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: page_email_send.ml,v 1.3 2005/03/31 14:24:04 rich Exp $
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
29 open Cocanwiki_template
30 open Cocanwiki_strings
32 let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
33 let template = get_template dbh hostid "page_email_send.txt" in
35 let page = q#param "page" in
36 let email = trim (q#param "email") in
39 error ~title:"No email address" ~back_button:true
40 q "You must give an email address.";
44 (* Good a place as any to delete old, unconfirmed emails. *)
45 let sth = dbh#prepare_cached "delete from page_emails
46 where pending is not null
47 and entry_date < current_date - 7" in
51 (* Is that email address already registered in the database? *)
52 let sth = dbh#prepare_cached "select 1 from page_emails where hostid = ?
53 and url = ? and email = ?" in
54 sth#execute [`Int hostid; `String page; `String email];
56 let registered = try sth#fetch1int () = 1 with Not_found -> false in
59 error ~title:"Email address already used" ~back_button:true
61 ("That email address is already used for notifications from this page. "^
62 "If you are not receiving updates for this page, then you will " ^
63 "need to confirm that address. If you continue to have problems " ^
64 "please contact the site administrator.");
68 (* Create the unique pending and opt_out fields. The pending field
69 * allows the user to register. The opt_out field allows the user
72 let pending = random_sessionid () in
73 let opt_out = random_sessionid () in
75 (* Insert into the database. *)
76 let sth = dbh#prepare_cached "insert into page_emails (hostid, url, email,
77 pending, opt_out) values (?, ?, ?, ?, ?)" in
78 sth#execute [`Int hostid; `String page; `String email; `String pending;
83 (* Send the initial email to the user. *)
84 template#set "hostname" hostname;
85 template#set "page" page;
86 template#set "pending" pending;
87 template#set "opt_out" opt_out;
89 let body = template#to_string in
90 let subject = "Site notice: " ^ hostname ^ ": Confirm your email address" in
91 Sendmail.send_mail ~subject ~to_addr:[email] body;
94 let buttons = [ ok_button ("/" ^ page) ] in
95 ok ~buttons ~title:"Confirmation email sent"
96 q ("Please check your email now. You have been sent a confirmation " ^
97 "email so we can verify the email address is yours. Click on the " ^
98 "first link in that email to confirm.")
101 register_script ~restrict:[CanView] run