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.4 2004/11/01 12:57:53 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} user =
33 let email_change_template = _get_template "user_prefs_email_change.txt" in
36 let new_email = trim (q#param "email") in
37 let email_notify = q#param_true "email_notify" in
41 Anonymous -> assert false
42 | User (userid, _, _, _) -> userid in
44 (* Update the preferences fields. *)
46 dbh#prepare_cached "update users set email_notify = ?
47 where hostid = ? and id = ?" in
48 sth#execute [`Bool email_notify;
49 `Int hostid; `Int userid];
51 (* Have we changed the email address? *)
53 if new_email = "" then (
54 (* Set the email field in the database to null. No need for
57 let sth = dbh#prepare_cached "update users set email = null
58 where hostid = ? and id = ?" in
59 sth#execute [`Int hostid; `Int userid];
63 (* Is the new email address different from the one currently recorded
66 let sth = dbh#prepare_cached "select ? <> coalesce (email, '')
67 from users where hostid = ? and id = ?" in
68 sth#execute [`String new_email; `Int hostid; `Int userid];
71 match sth#fetch1 () with [ `Bool b ] -> b | _ -> assert false in
74 let key = random_sessionid () in
75 (* Changed, so we add to the pending_email_changes table. *)
76 let sth = dbh#prepare_cached "insert into pending_email_changes
77 (key, userid, email) values (?, ?, ?)" in
78 sth#execute [`String key; `Int userid; `String new_email];
80 (* Send the confirm email. *)
81 email_change_template#set "hostname" hostname;
82 email_change_template#set "key" key;
83 let body = email_change_template#to_string in
85 let subject = "Please verify your new email address at " ^ hostname in
86 Sendmail.send_mail ~subject ~to_addr:[new_email] ~body ()
92 (* Good place to remove old rows in the pending_email_changes table. *)
93 let sth = dbh#prepare_cached "delete from pending_email_changes
94 where change_date - current_date > 7" in
97 (* Commit and finish off. *)
100 let buttons = [ ok_button "/_userprefs" ] in
101 ok ~title:"Preferences updated" ~buttons
102 q ("Your user preferences were updated. " ^
103 if confirm_needed then
104 ("Because you changed your email address, we have sent a " ^
105 "confirmation email to your new address. You will need to " ^
106 "click on the link in that email to verify your new address.")
111 register_script ~anonymous:false run