Scripts updated to use new PG interface.
[cocanwiki.git] / scripts / page_email_send.ml
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.6 2006/03/28 16:24:07 rich Exp $
5  *
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.
10  *
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.
15  *
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.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_ok
29 open Cocanwiki_template
30 open Cocanwiki_strings
31
32 let run r (q : cgi) dbh hostid { hostname = hostname } _ =
33   let template = get_template dbh hostid "page_email_send.txt" in
34
35   let page = q#param "page" in
36   let email = trim (q#param "email") in
37
38   if email = "" then (
39     error ~title:"No email address" ~back_button:true
40       dbh hostid q "You must give an email address.";
41     return ()
42   );
43
44   (* Good a place as any to delete old, unconfirmed emails. *)
45   PGSQL(dbh)
46     "delete from page_emails
47       where pending is not null
48         and entry_date < current_date - 7";
49
50   PGOCaml.commit dbh;
51   PGOCaml.begin_work dbh;
52
53   (* Is that email address already registered in the database? *)
54   let rows = PGSQL(dbh)
55     "select 1 from page_emails where hostid = $hostid
56         and url = $page and email = $email" in
57
58   let registered = rows = [Some 1l] in
59
60   if registered then (
61     error ~title:"Email address already used" ~back_button:true
62       dbh hostid q
63       ("That email address is already used for notifications from this page. "^
64        "If you are not receiving updates for this page, then you will " ^
65        "need to confirm that address. If you continue to have problems " ^
66        "please contact the site administrator.");
67     return ()
68   );
69
70   (* Create the unique pending and opt_out fields.  The pending field
71    * allows the user to register.  The opt_out field allows the user
72    * to unsubscribe.
73    *)
74   let pending = random_sessionid () in
75   let opt_out = random_sessionid () in
76
77   (* Insert into the database. *)
78   PGSQL(dbh)
79     "insert into page_emails (hostid, url, email, pending, opt_out)
80      values ($hostid, $page, $email, $pending, $opt_out)";
81
82   PGOCaml.commit dbh;
83
84   (* Send the initial email to the user. *)
85   template#set "hostname" hostname;
86   template#set "page" page;
87   template#set "pending" pending;
88   template#set "opt_out" opt_out;
89
90   let body = template#to_string in
91   let subject = "Site notice: " ^ hostname ^ ": Confirm your email address" in
92   Sendmail.send_mail ~subject ~to_addr:[email] body;
93
94   (* Finish up. *)
95   let buttons = [ ok_button ("/" ^ page) ] in
96   ok ~buttons ~title:"Confirmation email sent"
97     dbh hostid q
98     ("Please check your email now.  You have been sent a confirmation " ^
99      "email so we can verify the email address is yours.  Click on the " ^
100      "first link in that email to confirm.")
101
102 let () =
103   register_script ~restrict:[CanView] run