Split the 'page' template into two halves to enable pipelining. The
[cocanwiki.git] / scripts / change_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: change_password.ml,v 1.3 2004/10/30 10:16:09 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
30 let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
31   let old_password = q#param "old_password" in
32
33   (* Check the old password was supplied correctly. *)
34   let userid =
35     match user with
36         Anonymous -> assert false (* cannot happen *)
37       | User (userid, _, _, _) -> userid in
38
39   let sth = dbh#prepare_cached "select 1 from users
40                                  where hostid = ? and id = ?
41                                    and password = ?" in
42   sth#execute [`Int hostid; `Int userid; `String old_password];
43
44   let old_password_ok =
45     try 1 = sth#fetch1int ()
46     with
47         Not_found -> false in
48
49   if not old_password_ok then (
50     error ~title:"Bad password"
51       ~back_button:true
52       q "The password you gave is wrong.";
53     return ()
54   );
55
56   let password1 = q#param "password1" in
57   let password2 = q#param "password2" in
58
59   if password1 = "" || password2 = "" then (
60     error ~back_button:true ~title:"Bad password"
61       q "The password you gave is empty.";
62     return ()
63   );
64
65   if password1 <> password2 then (
66     error ~back_button:true ~title:"Passwords don't match"
67       q "The two passwords you gave aren't identical.";
68     return ()
69   );
70
71   let password = password1 in
72
73   (* Change the password. *)
74   let sth =
75     dbh#prepare_cached
76       "update users set password = ?, force_password_change = false
77         where hostid = ? and id = ?" in
78   sth#execute [`String password; `Int hostid; `Int userid];
79
80   dbh#commit ();
81
82   let buttons = [ ok_button "/" ] in
83   ok ~buttons ~title:"Password changed"
84     q "The password was changed."
85
86 let () =
87   register_script ~anonymous:false run