9db853a1be9bc5d7eb35db98db39e69f7d5deb1f
[cocanwiki.git] / scripts / invite_user_confirm.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: invite_user_confirm.ml,v 1.6 2006/03/28 13:20:00 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_strings
30 open Cocanwiki_template
31
32 let run r (q : cgi) dbh hostid { hostname = hostname } _ =
33   let template = _get_template "invite_user_confirm.txt" in
34
35   let username = q#param "username" in
36   let invite = q#param "invite" in
37
38   (* Verify the username, invite combination. *)
39   let rows = PGSQL(dbh) "select email, id from users
40                           where hostid = $hostid and
41                                 name = $username and invite = $invite" in
42
43   let email, userid =
44     match rows with
45     | [ Some email; userid ] -> Some email, userid
46     | [ None; userid ] -> None, userid
47     | [] ->
48         error ~title:"Already signed up"
49           dbh hostid q "It looks like you have already used your invitation.";
50         return ()
51     | _ -> assert false in
52
53   let password1 = q#param "password1" in
54   let password2 = q#param "password2" in
55
56   if password1 = "" || password2 = "" then (
57     error ~back_button:true ~title:"Bad password"
58       dbh hostid q "The password you gave is empty.";
59     return ()
60   );
61
62   if password1 <> password2 then (
63     error ~back_button:true ~title:"Passwords don't match"
64       dbh hostid q "The two passwords you gave aren't identical.";
65     return ()
66   );
67
68   let password = password1 in
69
70   (* Change the password. *)
71   PGSQL(dbh)
72     "update users set password = $password, invite = null,
73                       force_password_change = false
74       where hostid = $hostid and id = $userid";
75
76   (* Send email to this user. *)
77   (match email with
78        None -> ()
79      | Some email ->
80          template#set "username" username;
81          template#set "hostname" hostname;
82
83          let body = template#to_string in
84
85          let subject = "Your new account details" in
86          Sendmail.send_mail ~to_addr:[email] ~subject body);
87
88   PGOCaml.commit dbh;
89
90   (* Redirect to the login page. *)
91   let redirect =
92     "http://" ^ hostname ^ "/_bin/login.cmo?" ^
93     "username=" ^ Cgi_escape.escape_url username ^ "&" ^
94     "password=" ^ Cgi_escape.escape_url password ^ "&" ^
95     "permanent=1" in
96   q#redirect redirect
97
98 let () =
99   register_script run