All new templates. Site menu displays basically everywhere.
[cocanwiki.git] / scripts / forgot_password.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: forgot_password.ml,v 1.3 2004/09/09 09:35:33 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 { hostname = hostname } _ =
17   let name = trim (q#param "name") in
18
19   if name = "" then (
20     error ~back_button:true ~title:"No username or email address"
21       q "You didn't give a username or email address";
22     raise CgiExit
23   );
24
25   (* Look it up in the database. *)
26   let sth = dbh#prepare_cached "select email, name, password from users
27                                  where hostid = ?
28                                    and (lower (name) = lower (?)
29                                         or lower (email) = lower (?))" in
30   sth#execute [`Int hostid; `String name; `String name];
31
32   try
33     let email, name, password = match sth#fetch1 () with
34         [ `String email; `String name; `String password ] ->
35           email, name, password
36       | _ -> assert false in
37
38     (* Get the IP address of the user, if available. *)
39     let ip =
40       try Connection.remote_ip (Request.connection r) with Not_found -> "" in
41
42     let subject = "Password for " ^ hostname in
43
44     let body =
45       "Someone, possibly you, requested your password for " ^ hostname ^
46       ".\n\n" ^
47       "Username: " ^ name ^ "\n" ^
48       "Password: " ^ password ^ "\n" ^
49       "\n" ^
50       "IP address of request: " ^ ip ^ "\n" in
51
52     Sendmail.send_mail ~subject ~to_addr:[ email ] ~body ();
53
54     let buttons = [ ok_button "/_login" ] in
55     ok ~buttons ~title:"Password sent by email"
56       q
57       ("Your password was sent by email.  If you don't receive the password " ^
58        "within an hour, please notify the site's administrator.")
59   with
60       Not_found ->
61         (* Artificially limit the rate at which people can search the database
62          * for usernames.
63          *)
64         Unix.sleep 10;
65
66         error ~back_button:true ~title:"Nothing known"
67           q "Sorry, don't know anyone with that name or email address."
68
69 let () =
70   register_script run