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