Global replace 'raise CgiExit' -> 'return ()' to match a future version of mod_caml.
[cocanwiki.git] / scripts / signup.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: signup.ml,v 1.6 2004/09/23 11:56:47 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
31 let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
32   (* Verify that we're allowed to create accounts anonymously
33    * on this host.
34    *)
35   let sth = dbh#prepare_cached "select create_account_anon from hosts
36                                  where id = ?" in
37   sth#execute [`Int hostid];
38
39   let create_account_anon =
40     match sth#fetch1 () with
41         [ `Bool true ] -> ()
42       | _ -> assert false in
43
44   let username = trim (q#param "username") in
45   let password1 = trim (q#param "password1") in
46   let password2 = trim (q#param "password2") in
47
48   if username = "" || password1 = "" || password2 = "" then (
49     error ~back_button:true ~title:"Bad username or password"
50       q "The username or password you gave is empty.";
51     return ()
52   );
53
54   if password1 <> password2 then (
55     error ~back_button:true ~title:"Passwords don't match"
56       q "The two passwords you gave aren't identical.";
57     return ()
58   );
59
60   let password = password1 in
61
62   (*
63     Uh oh ... Not making UNICODE assumptions ... XXX
64   if String.length username > 32 || String.length password > 32 then
65   *)
66
67   let email = trim (q#param "email") in
68   let email = if string_is_whitespace email then `Null else `String email in
69
70   (* Not a duplicate? *)
71   let sth = dbh#prepare_cached "select id from users
72                                  where hostid = ? and name = ?" in
73   sth#execute [`Int hostid; `String username];
74
75   (try
76      sth#fetch1 ();
77      error ~back_button:true ~title:"Username already taken"
78        q ("Someone, possibly you, has already taken that username. " ^
79           "If you think you have forgotten your password, try going back " ^
80           "and clicking on the 'Forgotten your password?' link.");
81      return ()
82    with
83        Not_found -> ());
84
85   (* Create the user account. *)
86   let sth = dbh#prepare_cached "insert into users (name, password, email,
87                                                    hostid)
88                                 values (?, ?, ?, ?)" in
89   sth#execute [`String username; `String password; email; `Int hostid];
90
91   let userid = sth#serial "users_id_seq" in
92
93   (* Create a cookie. *)
94   let cookie = random_sessionid () in
95   let sth = dbh#prepare_cached "insert into usercookies (userid, cookie)
96                                 values (?, ?)" in
97   sth#execute [`Int userid; `String cookie];
98
99   dbh#commit ();
100
101   let buttons = [ ok_button "/" ] in
102   let cookie = Cookie.cookie ~name:"auth" ~value:cookie ~path:"/" () in
103
104   ok ~title:"Account created"
105     ~buttons
106     ~cookie
107     q ("An account was created for you, " ^ username ^ ". " ^
108        "We hope you enjoy using this service.")
109
110 let () =
111   register_script run