Fixed some problems found in testing. Now appears to be working fully.
[cocanwiki.git] / scripts / user_prefs.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: user_prefs.ml,v 1.8 2006/03/28 16:24:08 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} user =
33   let email_change_template = _get_template "user_prefs_email_change.txt" in
34
35   (* Get the fields. *)
36   let new_email = trim (q#param "email") in
37   let email_notify = q#param_true "email_notify" in
38
39   let userid =
40     match user with
41         Anonymous -> assert false
42       | User (userid, _, _, _) -> userid in
43
44   (* Update the preferences fields. *)
45   PGSQL(dbh)
46     "update users set email_notify = $email_notify
47       where hostid = $hostid and id = $userid";
48
49   (* Have we changed the email address? *)
50   let confirm_needed =
51     if new_email = "" then (
52       (* Set the email field in the database to null.  No need for
53        * any confirmation.
54        *)
55       PGSQL(dbh) "update users set email = null
56                    where hostid = $hostid and id = $userid";
57
58       false
59     ) else (
60       (* Is the new email address different from the one currently recorded
61        * in the database?
62        *)
63       let changed = Option.get (
64         List.hd (
65           PGSQL(dbh) "select $new_email <> coalesce (email, '')
66                         from users where hostid = $hostid and id = $userid"
67         )
68       ) in
69
70       if changed then (
71         let key = random_sessionid () in
72         (* Changed, so we add to the pending_email_changes table. *)
73         PGSQL(dbh) "insert into pending_email_changes
74                       (key, userid, email)
75                     values ($key, $userid, $new_email)";
76
77         (* Send the confirm email. *)
78         email_change_template#set "hostname" hostname;
79         email_change_template#set "key" key;
80         let body = email_change_template#to_string in
81
82         let subject = "Please verify your new email address at " ^ hostname in
83         Sendmail.send_mail ~subject ~to_addr:[new_email] body
84       );
85
86       changed
87     ) in
88
89   (* Good place to remove old rows in the pending_email_changes table. *)
90   PGSQL(dbh) "delete from pending_email_changes
91                where change_date - current_date > 7";
92
93   (* Commit and finish off. *)
94   PGOCaml.commit dbh;
95
96   let buttons = [ ok_button "/_userprefs" ] in
97   ok ~title:"Preferences updated" ~buttons
98     dbh hostid q
99     ("Your user preferences were updated.  " ^
100        if confirm_needed then
101          ("Because you changed your email address, we have sent a " ^
102           "confirmation email to your new address.  You will need to " ^
103           "click on the link in that email to verify your new address.")
104        else
105          "")
106
107 let () =
108   register_script ~anonymous:false run