Global replace 'raise CgiExit' -> 'return ()' to match a future version of mod_caml.
[cocanwiki.git] / scripts / edit_contact.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: edit_contact.ml,v 1.2 2004/09/23 11:56:47 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 split_re = Pcre.regexp "[\\r\\n,;]+"
32
33 let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
34   let id = int_of_string (q#param "id") in
35
36   let name = trim (q#param "name") in
37   let subject = trim (q#param "subject") in
38
39   let emails = try q#param "emails" with Not_found -> "" in
40
41   let emails = Pcre.split ~rex:split_re emails in
42   let emails = List.map trim emails in
43   let emails = List.filter ((<>) "") emails in
44
45   if name = "" then (
46     error ~back_button:true ~title:"Name field missing"
47       q "You must name your contact form.";
48     return ()
49   );
50
51   if subject = "" then (
52     error ~back_button:true ~title:"Subject line missing"
53       q "You must give a subject line, which appears on contact emails.";
54     return ()
55   );
56
57   if emails = [] then (
58     error ~back_button:true ~title:"No email addresses"
59       q ("There are no email addresses listed for this contact form.  You " ^
60          "must list at least one valid email address.");
61     return ()
62   );
63
64   (* Need to verify that the contact belongs to the host. *)
65   let sth = dbh#prepare_cached "select 1 from contacts
66                                  where hostid = ? and id = ?" in
67   sth#execute [`Int hostid; `Int id];
68
69   assert (sth#fetch1int () = 1);
70
71   (* Update the database. *)
72   let sth = dbh#prepare_cached "update contacts set name = ?, subject = ?
73                                  where hostid = ? and id = ?" in
74   sth#execute [`String name; `String subject; `Int hostid; `Int id];
75
76   let sth =
77     dbh#prepare_cached "delete from contact_emails where contactid = ?" in
78   sth#execute [`Int id];
79
80   let sth = dbh#prepare_cached "insert into contact_emails (contactid, email)
81                                 values (?, ?)" in
82   List.iter (fun email ->
83                sth#execute [`Int id; `String email]) emails;
84
85   (* Finish off. *)
86   dbh#commit ();
87
88   let buttons = [
89     ok_button "/_bin/contacts.cmo";
90     { StdPages.label = "   View contact form   ";
91       StdPages.link = "/_bin/contact_show.cmo";
92       StdPages.method_ = None;
93       StdPages.params = [ "id", string_of_int id ] } ] in
94   ok ~title:"Contact form edited" ~buttons
95     q "The contact form was edited."
96
97 let () =
98   register_script ~restrict:[CanManageContacts] run