2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: signup.ml,v 1.1 2004/09/07 16:58:03 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
47 let email = trim (q#param "email") in
48 let email = if string_is_whitespace email then `Null else `String email in
50 (* Not a duplicate? *)
51 let sth = dbh#prepare_cached "select id from users where name = ?" in
52 sth#execute [`String username];
56 error ~back_button:true ~title:"Username already taken"
57 q ("Someone, possibly you, has already taken that username. " ^
58 "If you think you have forgotten your password, try going back " ^
59 "and clicking on the 'Forgotten your password?' link.");
64 (* Create the user account. *)
65 let sth = dbh#prepare_cached "insert into users (name, password, email,
67 values (?, ?, ?, ?)" in
68 sth#execute [`String username; `String password; email; `Int hostid];
70 let userid = sth#serial "users_id_seq" in
72 (* Create a cookie. *)
73 let cookie = random_sessionid () in
74 let sth = dbh#prepare_cached "insert into usercookies (userid, cookie)
76 sth#execute [`Int userid; `String cookie];
80 let buttons = [ ok_button "/" ] in
81 let cookie = Cookie.cookie ~name:"auth" ~value:cookie ~path:"/" () in
83 ok ~title:"Account created"
86 q ("An account was created for you, " ^ username ^ ". " ^
87 "We hope you enjoy using this service.")