All new templates. Site menu displays basically everywhere.
[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.4 2004/09/09 09:35:34 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
57                                  where hostid = ? and name = ?" in
58   sth#execute [`Int hostid; `String username];
59
60   (try
61      sth#fetch1 ();
62      error ~back_button:true ~title:"Username already taken"
63        q ("Someone, possibly you, has already taken that username. " ^
64           "If you think you have forgotten your password, try going back " ^
65           "and clicking on the 'Forgotten your password?' link.");
66      raise CgiExit
67    with
68        Not_found -> ());
69
70   (* Create the user account. *)
71   let sth = dbh#prepare_cached "insert into users (name, password, email,
72                                                    hostid)
73                                 values (?, ?, ?, ?)" in
74   sth#execute [`String username; `String password; email; `Int hostid];
75
76   let userid = sth#serial "users_id_seq" in
77
78   (* Create a cookie. *)
79   let cookie = random_sessionid () in
80   let sth = dbh#prepare_cached "insert into usercookies (userid, cookie)
81                                 values (?, ?)" in
82   sth#execute [`Int userid; `String cookie];
83
84   dbh#commit ();
85
86   let buttons = [ ok_button "/" ] in
87   let cookie = Cookie.cookie ~name:"auth" ~value:cookie ~path:"/" () in
88
89   ok ~title:"Account created"
90     ~buttons
91     ~cookie
92     q ("An account was created for you, " ^ username ^ ". " ^
93        "We hope you enjoy using this service.")
94
95 let () =
96   register_script run