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.9 2006/07/31 09:49:43 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 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 "update users set email_notify = $email_notify
47 where hostid = $hostid and id = $userid";
49 (* Have we changed the email address? *)
51 if new_email = "" then (
52 (* Set the email field in the database to null. No need for
55 PGSQL(dbh) "update users set email = null
56 where hostid = $hostid and id = $userid";
60 (* Is the new email address different from the one currently recorded
63 let changed = Option.get (
65 PGSQL(dbh) "select $new_email <> coalesce (email, '')
66 from users where hostid = $hostid and id = $userid"
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
75 values ($key, $userid, $new_email)";
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
82 let subject = "Please verify your new email address at " ^ hostname in
85 "text/plain", ["charset", Mimestring.mk_param "UTF-8"] in
86 let to_addrs = [ "", new_email ] in
88 let msg = Netsendmail.compose ~subject ~to_addrs ~content_type body in
89 Netsendmail.sendmail msg
95 (* Good place to remove old rows in the pending_email_changes table. *)
96 PGSQL(dbh) "delete from pending_email_changes
97 where change_date - current_date > 7";
99 (* Commit and finish off. *)
102 let buttons = [ ok_button "/_userprefs" ] in
103 ok ~title:"Preferences updated" ~buttons
105 ("Your user preferences were updated. " ^
106 if confirm_needed then
107 ("Because you changed your email address, we have sent a " ^
108 "confirmation email to your new address. You will need to " ^
109 "click on the link in that email to verify your new address.")
114 register_script ~anonymous:false run