+csv dep for PG'OCaml.
[cocanwiki.git] / scripts / signup.ml
1 (* COCANWIKI - a wiki written in Objective CAML.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: signup.ml,v 1.12 2006/12/06 09:46:57 rich Exp $
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_ok
29 open Cocanwiki_strings
30
31 let run r (q : cgi) dbh hostid _ _ =
32   (* Verify that we're allowed to create accounts anonymously
33    * on this host.
34    *)
35   let create_account_anon = List.hd (
36     PGSQL(dbh) "select create_account_anon from hosts
37                  where id = $hostid"
38   ) in
39
40   if not create_account_anon then (
41     error ~title:"Not allowed to create accounts"
42       r dbh hostid q
43       ("To get an account on this service, please contact the " ^
44          "administrator.");
45     return ()
46   );
47
48   let username = trim (q#param "username") in
49   let password1 = trim (q#param "password1") in
50   let password2 = trim (q#param "password2") in
51
52   if username = "" || password1 = "" || password2 = "" then (
53     error ~back_button:true ~title:"Bad username or password"
54       r dbh hostid q "The username or password you gave is empty.";
55     return ()
56   );
57
58   if password1 <> password2 then (
59     error ~back_button:true ~title:"Passwords don't match"
60       r dbh hostid q "The two passwords you gave aren't identical.";
61     return ()
62   );
63
64   let password = password1 in
65
66   if UTF8.length username > 32 || UTF8.length password > 128 then (
67     error ~back_button:true ~title:"Username or password too long"
68       r dbh hostid q "Usernames should be less than 32 characters long.  For passwords we let you have a generous 128 characters.";
69     return ()
70   );
71
72   let email = trim (q#param "email") in
73   let email = if string_is_whitespace email then None else Some email in
74
75   (* Not a duplicate? *)
76   let rows = PGSQL(dbh)
77     "select id from users where hostid = $hostid and name = $username" in
78
79   (match rows with
80    | [_] ->
81        error ~back_button:true ~title:"Username already taken"
82          r dbh hostid q
83          ("Someone, possibly you, has already taken that username. " ^
84             "If you think you have forgotten your password, try going back " ^
85             "and clicking on the 'Forgotten your password?' link.");
86        return ()
87    | [] -> ()
88    | _ -> assert false
89   );
90
91   (* Create the user account. *)
92   PGSQL(dbh) "insert into users (name, password, email, hostid)
93               values ($username, $password, $?email, $hostid)";
94
95   let userid = PGOCaml.serial4 dbh "users_id_seq" in
96
97   (* Create a cookie. *)
98   let cookie = random_sessionid () in
99   PGSQL(dbh) "insert into usercookies (userid, cookie)
100               values ($userid, $cookie)";
101
102   PGOCaml.commit dbh;
103
104   let buttons = [ ok_button "/" ] in
105   let cookie = Cookie.cookie "auth" cookie ~path:"/" in
106
107   ok ~title:"Account created"
108     ~buttons
109     ~cookie
110     r dbh hostid q
111     ("An account was created for you, " ^ username ^ ". " ^
112      "We hope you enjoy using this service.")
113
114 let () =
115   register_script run