84c560e7f4767dde7697527969401325dde6c588
[cocanwiki.git] / scripts / signup.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: signup.ml,v 1.2 2004/09/07 17:16:46 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Cocanwiki
13 open Cocanwiki_ok
14 open Cocanwiki_strings
15
16 let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) _ =
17   (* Verify that we're allowed to create accounts anonymously
18    * on this host.
19    *)
20   let sth = dbh#prepare_cached "select create_account_anon from hosts
21                                  where id = ?" in
22   sth#execute [`Int hostid];
23
24   let create_account_anon =
25     match sth#fetch1 () with
26         [ `Bool true ] -> ()
27       | _ -> assert false in
28
29   let username = trim (q#param "username") in
30   let password1 = trim (q#param "password1") in
31   let password2 = trim (q#param "password2") in
32
33   if username = "" || password1 = "" || password2 = "" then (
34     error ~back_button:true ~title:"Bad username or password"
35       q "The username or password you gave is empty.";
36     raise CgiExit
37   );
38
39   if password1 <> password2 then (
40     error ~back_button:true ~title:"Passwords don't match"
41       q "The two passwords you gave aren't identical.";
42     raise CgiExit
43   );
44
45   let password = password1 in
46
47   (*
48     Uh oh ... Not making UNICODE assumptions ... XXX
49   if String.length username > 32 || String.length password > 32 then
50   *)
51
52   let email = trim (q#param "email") in
53   let email = if string_is_whitespace email then `Null else `String email in
54
55   (* Not a duplicate? *)
56   let sth = dbh#prepare_cached "select id from users where name = ?" in
57   sth#execute [`String username];
58
59   (try
60      sth#fetch1 ();
61      error ~back_button:true ~title:"Username already taken"
62        q ("Someone, possibly you, has already taken that username. " ^
63           "If you think you have forgotten your password, try going back " ^
64           "and clicking on the 'Forgotten your password?' link.");
65      raise CgiExit
66    with
67        Not_found -> ());
68
69   (* Create the user account. *)
70   let sth = dbh#prepare_cached "insert into users (name, password, email,
71                                                    hostid)
72                                 values (?, ?, ?, ?)" in
73   sth#execute [`String username; `String password; email; `Int hostid];
74
75   let userid = sth#serial "users_id_seq" in
76
77   (* Create a cookie. *)
78   let cookie = random_sessionid () in
79   let sth = dbh#prepare_cached "insert into usercookies (userid, cookie)
80                                 values (?, ?)" in
81   sth#execute [`Int userid; `String cookie];
82
83   dbh#commit ();
84
85   let buttons = [ ok_button "/" ] in
86   let cookie = Cookie.cookie ~name:"auth" ~value:cookie ~path:"/" () in
87
88   ok ~title:"Account created"
89     ~buttons
90     ~cookie
91     q ("An account was created for you, " ^ username ^ ". " ^
92        "We hope you enjoy using this service.")
93
94 let () =
95   register_script run