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 $
14 open Cocanwiki_strings
16 let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) _ =
17 (* Verify that we're allowed to create accounts anonymously
20 let sth = dbh#prepare_cached "select create_account_anon from hosts
22 sth#execute [`Int hostid];
24 let create_account_anon =
25 match sth#fetch1 () with
27 | _ -> assert false in
29 let username = trim (q#param "username") in
30 let password1 = trim (q#param "password1") in
31 let password2 = trim (q#param "password2") in
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.";
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.";
45 let password = password1 in
48 Uh oh ... Not making UNICODE assumptions ... XXX
49 if String.length username > 32 || String.length password > 32 then
52 let email = trim (q#param "email") in
53 let email = if string_is_whitespace email then `Null else `String email in
55 (* Not a duplicate? *)
56 let sth = dbh#prepare_cached "select id from users where name = ?" in
57 sth#execute [`String username];
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.");
69 (* Create the user account. *)
70 let sth = dbh#prepare_cached "insert into users (name, password, email,
72 values (?, ?, ?, ?)" in
73 sth#execute [`String username; `String password; email; `Int hostid];
75 let userid = sth#serial "users_id_seq" in
77 (* Create a cookie. *)
78 let cookie = random_sessionid () in
79 let sth = dbh#prepare_cached "insert into usercookies (userid, cookie)
81 sth#execute [`Int userid; `String cookie];
85 let buttons = [ ok_button "/" ] in
86 let cookie = Cookie.cookie ~name:"auth" ~value:cookie ~path:"/" () in
88 ok ~title:"Account created"
91 q ("An account was created for you, " ^ username ^ ". " ^
92 "We hope you enjoy using this service.")