Basic contact form implemented (no user interface for it yet, however).
[cocanwiki.git] / scripts / 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: contact.ml,v 1.1 2004/09/17 12:35:38 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 ExtString
28
29 open Cocanwiki
30 open Cocanwiki_template
31 open Cocanwiki_ok
32
33 let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} user =
34   let template = get_template dbh hostid "contact.txt" in
35
36   let fail msg =
37     error ~back_button:true ~title:"Bad form"
38       q (msg ^ "  Please contact the owner of the site by email.");
39     raise CgiExit
40   in
41
42   (* Get the id field. *)
43   let id =
44     try int_of_string (q#param "id")
45     with
46         Not_found -> fail "The 'id' field is missing in that form."
47       | Failure "int_of_string" ->
48           fail "The 'id' field in that form is not a number." in
49
50   (* Get the contacts / emails from the database. *)
51   let sth = dbh#prepare_cached "select subject from contacts
52                                  where hostid = ? and id = ?" in
53   sth#execute [`Int hostid; `Int id];
54
55   let subject =
56     try sth#fetch1string ()
57     with Not_found -> fail "There is no such contact id in the database." in
58
59   let sth = dbh#prepare_cached "select email from contact_emails
60                                  where contactid = ?" in
61   sth#execute [`Int id];
62
63   let emails = sth#map (function [`String email] -> email
64                                | _ -> assert false) in
65
66   if emails = [] then
67     fail "There are no email addresses associated with that contact id.";
68
69   (* Now process the strings passed as parameters to the script.  Any
70    * parameter which starts with 'file' (eg. 'file0') is treated as a
71    * file upload automatically.
72    *)
73   let names = List.map fst q#params in
74   let names, uploads =
75     if q#is_multipart then (
76       let uploads =
77         List.filter (fun str -> String.starts_with str "file") names in
78       let names =
79         List.filter (fun str -> str <> "id" &&
80                        not (String.starts_with str "file")) names in
81       names, uploads
82     ) else
83       names, [] in
84
85   (* Sort them.
86    * Ignore repeat parameters. - Don't use these in forms.
87    *)
88   let rec uniq = function
89       [] -> []
90     | [x] -> [x]
91     | x :: y :: xs when compare x y = 0 -> uniq (x :: xs)
92     | x :: y :: xs -> x :: uniq (y :: xs)
93   in
94
95   let names = uniq (List.sort compare names) in
96   let uploads = uniq (List.sort compare uploads) in
97
98   (* Get the IP address for logging purposes. *)
99   let ip =
100     try Connection.remote_ip (Request.connection r) with Not_found -> "" in
101
102   (* Get the User-Agent string.  Consider in future rejecting spammers
103    * who don't set User-Agent.
104    *)
105   let ua =
106     try Table.get (Request.headers_in r) "User-Agent" with Not_found -> "" in
107
108   (* Get the user details, if any. *)
109   let username =
110     match user with
111         Anonymous -> "anonymous"
112       | User (userid, username, _) ->
113           sprintf "%s (%d)" username userid in
114
115   template#set "ip" ip;
116   template#set "ua" ua;
117   template#set "username" username;
118   template#set "hostname" hostname;
119
120   (* Construct the table of names, values for the initial email. *)
121   let table = List.map (fun name ->
122                           let value = q#param name in
123                           [ "name", Template.VarString name;
124                             "value", Template.VarString value ]) names in
125   template#table "names" table;
126
127   (* Any uploads to follow? *)
128   template#conditional "uploads" (uploads <> []);
129   template#set "nr_uploads" (string_of_int (List.length uploads));
130
131   (* Send the initial email. *)
132   let body = template#to_string in
133   Sendmail.send_mail ~subject ~to_addr:emails ~body ();
134
135   (* Send the following uploads by email. *)
136   List.iter (fun name ->
137                let upload = q#upload name in
138                let subject = upload.upload_filename in
139                (* XXX This is insecure. *)
140                let content_type = upload.upload_content_type in
141                let body = upload.upload_value in
142
143                Sendmail.send_mail ~subject ~to_addr:emails ~content_type
144                  ~body ())
145     uploads;
146
147   (* Confirm. *)
148   ok ~title:"Thank you for your contact" ~buttons:[ok_button "/"]
149     q "An email was sent and you should receive a reply shortly."
150
151 let () =
152   register_script run