Added a "side-by-side" mode using ''diff -y''. Disabled by default,
[cocanwiki.git] / scripts / forgot_password.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: forgot_password.ml,v 1.6 2004/10/09 12:20: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 : Dbi.connection) hostid { hostname = hostname } _ =
32   let name = trim (q#param "name") in
33
34   if name = "" then (
35     error ~back_button:true ~title:"No username or email address"
36       q "You didn't give a username or email address";
37     return ()
38   );
39
40   (* Look it up in the database. *)
41   let sth = dbh#prepare_cached "select email, name, password from users
42                                  where hostid = ?
43                                    and email is not null
44                                    and (lower (name) = lower (?)
45                                         or lower (email) = lower (?))" in
46   sth#execute [`Int hostid; `String name; `String name];
47
48   try
49     let email, name, password = match sth#fetch1 () with
50         [ `String email; `String name; `String password ] ->
51           email, name, password
52       | _ -> assert false in
53
54     (* Get the IP address of the user, if available. *)
55     let ip =
56       try Connection.remote_ip (Request.connection r) with Not_found -> "" in
57
58     let subject = "Password for " ^ hostname in
59
60     let body =
61       "Someone, possibly you, requested your password for " ^ hostname ^
62       ".\n\n" ^
63       "Username: " ^ name ^ "\n" ^
64       "Password: " ^ password ^ "\n" ^
65       "\n" ^
66       "IP address of request: " ^ ip ^ "\n" in
67
68     Sendmail.send_mail ~subject ~to_addr:[ email ] ~body ();
69
70     let buttons = [ ok_button "/_login" ] in
71     ok ~buttons ~title:"Password sent by email"
72       q
73       ("Your password was sent by email.  If you don't receive the password " ^
74        "within an hour, please notify the site's administrator.")
75   with
76       Not_found ->
77         (* Artificially limit the rate at which people can search the database
78          * for usernames.
79          *)
80         Unix.sleep 10;
81
82         error ~back_button:true ~title:"Nothing known"
83           q "Sorry, don't know anyone with that name or email address."
84
85 let () =
86   register_script run