e603c1972c29826c2b1d44429ef02209985b3deb
[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.3 2004/10/30 10:16:10 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 : Dbi.connection) 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   let diff_sidebyside = q#param_true "diff_sidebyside" in
39
40   let userid =
41     match user with
42         Anonymous -> assert false
43       | User (userid, _, _, _) -> userid in
44
45   (* Update the preferences fields. *)
46   let sth =
47     dbh#prepare_cached "update users set email_notify = ?, diff_sidebyside = ?
48                          where hostid = ? and id = ?" in
49   sth#execute [`Bool email_notify; `Bool diff_sidebyside;
50                `Int hostid; `Int userid];
51
52   (* Have we changed the email address? *)
53   let confirm_needed =
54     if new_email = "" then (
55       (* Set the email field in the database to null.  No need for
56        * any confirmation.
57        *)
58       let sth = dbh#prepare_cached "update users set email = null
59                                      where hostid = ? and id = ?" in
60       sth#execute [`Int hostid; `Int userid];
61
62       false
63     ) else (
64       (* Is the new email address different from the one currently recorded
65        * in the database?
66        *)
67       let sth = dbh#prepare_cached "select ? <> coalesce (email, '')
68                                      from users where hostid = ? and id = ?" in
69       sth#execute [`String new_email; `Int hostid; `Int userid];
70
71       let changed =
72         match sth#fetch1 () with [ `Bool b ] -> b | _ -> assert false in
73
74       if changed then (
75         let key = random_sessionid () in
76         (* Changed, so we add to the pending_email_changes table. *)
77         let sth = dbh#prepare_cached "insert into pending_email_changes
78                                       (key, userid, email) values (?, ?, ?)" in
79         sth#execute [`String key; `Int userid; `String new_email];
80
81         (* Send the confirm email. *)
82         email_change_template#set "hostname" hostname;
83         email_change_template#set "key" key;
84         let body = email_change_template#to_string in
85
86         let subject = "Please verify your new email address at " ^ hostname in
87         Sendmail.send_mail ~subject ~to_addr:[new_email] ~body ()
88       );
89
90       changed
91     ) in
92
93   (* Good place to remove old rows in the pending_email_changes table. *)
94   let sth = dbh#prepare_cached "delete from pending_email_changes
95                                  where change_date - current_date > 7" in
96   sth#execute [];
97
98   (* Commit and finish off. *)
99   dbh#commit ();
100
101   let buttons = [ ok_button "/_userprefs" ] in
102   ok ~title:"Preferences updated" ~buttons
103     q ("Your user preferences were updated.  " ^
104        if confirm_needed then
105          ("Because you changed your email address, we have sent a " ^
106           "confirmation email to your new address.  You will need to " ^
107           "click on the link in that email to verify your new address.")
108        else
109          "")
110
111 let () =
112   register_script ~anonymous:false run