ea90b34e2f4a2bee010c41e9d6f9c0edbdd48045
[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.1 2004/09/07 16:58:03 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   let email = trim (q#param "email") in
48   let email = if string_is_whitespace email then `Null else `String email in
49
50   (* Not a duplicate? *)
51   let sth = dbh#prepare_cached "select id from users where name = ?" in
52   sth#execute [`String username];
53
54   (try
55      sth#fetch1 ();
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.");
60      raise CgiExit
61    with
62        Not_found -> ());
63
64   (* Create the user account. *)
65   let sth = dbh#prepare_cached "insert into users (name, password, email,
66                                                    hostid)
67                                 values (?, ?, ?, ?)" in
68   sth#execute [`String username; `String password; email; `Int hostid];
69
70   let userid = sth#serial "users_id_seq" in
71
72   (* Create a cookie. *)
73   let cookie = random_sessionid () in
74   let sth = dbh#prepare_cached "insert into usercookies (userid, cookie)
75                                 values (?, ?)" in
76   sth#execute [`Int userid; `String cookie];
77
78   dbh#commit ();
79
80   let buttons = [ ok_button "/" ] in
81   let cookie = Cookie.cookie ~name:"auth" ~value:cookie ~path:"/" () in
82
83   ok ~title:"Account created"
84     ~buttons
85     ~cookie
86     q ("An account was created for you, " ^ username ^ ". " ^
87        "We hope you enjoy using this service.")
88
89 let () =
90   register_script run