Serial columns now 64 bits.
[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.3 2005/03/31 14:24:04 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 : Dbi.connection) 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 sth = dbh#prepare_cached "select email, id from users
40                                  where hostid = ? and
41                                        name = ? and invite = ?" in
42   sth#execute [`Int hostid; `String username; `String invite];
43
44   let email, userid =
45     try
46       match sth#fetch1 () with
47           [ `String email; `Int userid ] -> Some email, userid
48         | [ `Null; `Int userid ] -> None, userid
49         | _ -> assert false
50     with Not_found ->
51       error ~title:"Already signed up"
52         q "It looks like you have already used your invitation.";
53       return () in
54
55   let password1 = q#param "password1" in
56   let password2 = q#param "password2" in
57
58   if password1 = "" || password2 = "" then (
59     error ~back_button:true ~title:"Bad password"
60       q "The password you gave is empty.";
61     return ()
62   );
63
64   if password1 <> password2 then (
65     error ~back_button:true ~title:"Passwords don't match"
66       q "The two passwords you gave aren't identical.";
67     return ()
68   );
69
70   let password = password1 in
71
72   (* Change the password. *)
73   let sth =
74     dbh#prepare_cached
75       "update users set password = ?, invite = null,
76                         force_password_change = false
77         where hostid = ? and id = ?" in
78   sth#execute [`String password; `Int hostid; `Int userid];
79
80   (* Send email to this user. *)
81   (match email with
82        None -> ()
83      | Some email ->
84          template#set "username" username;
85          template#set "hostname" hostname;
86
87          let body = template#to_string in
88
89          let subject = "Your new account details" in
90          Sendmail.send_mail ~to_addr:[email] ~subject body);
91
92   dbh#commit ();
93
94   (* Redirect to the login page. *)
95   let redirect =
96     "http://" ^ hostname ^ "/_bin/login.cmo?" ^
97     "username=" ^ Cgi_escape.escape_url username ^ "&" ^
98     "password=" ^ Cgi_escape.escape_url password ^ "&" ^
99     "permanent=1" in
100   q#redirect redirect
101
102 let () =
103   register_script run