Serial columns now 64 bits.
[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.8 2005/03/31 14:24:04 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     return ()
40   in
41
42   (* Get the name field. *)
43   let name =
44     try q#param "id"
45     with
46         Not_found ->
47           fail "The 'name' field is missing in that form." in
48
49   (* Get the contacts / emails from the database. *)
50   let sth = dbh#prepare_cached "select id, subject from contacts
51                                  where hostid = ? and name = ?" in
52   sth#execute [`Int hostid; `String name];
53
54   let id, subject =
55     try
56       (match sth#fetch1 () with
57            [ `Int id; `String subject ] -> id, subject
58          | _ -> assert false
59       )
60     with Not_found -> fail "There is no such contact form in the database." in
61
62   let sth = dbh#prepare_cached "select email from contact_emails
63                                  where contactid = ?" in
64   sth#execute [`Int id];
65
66   let emails = sth#map (function [`String email] -> email
67                                | _ -> assert false) in
68
69   if emails = [] then
70     fail "There are no email addresses associated with that contact id.";
71
72   (* Now process the strings passed as parameters to the script.  Any
73    * parameter which starts with 'file' (eg. 'file0') is treated as a
74    * file upload automatically.
75    *)
76   let names = List.map fst q#params in
77   let names, uploads =
78     if q#is_multipart then (
79       let uploads =
80         List.filter (fun str -> String.starts_with str "file") names in
81       let names =
82         List.filter (fun str -> str <> "id" &&
83                        not (String.starts_with str "file")) names in
84       names, uploads
85     ) else
86       names, [] in
87
88   (* Sort them.
89    * Ignore repeat parameters. - Don't use these in forms.
90    *)
91   let rec uniq = function
92       [] -> []
93     | [x] -> [x]
94     | x :: y :: xs when compare x y = 0 -> uniq (x :: xs)
95     | x :: y :: xs -> x :: uniq (y :: xs)
96   in
97
98   let names = uniq (List.sort compare names) in
99   let uploads = uniq (List.sort compare uploads) in
100
101   (* Some browsers send an empty file for empty uploads.  Remove those. *)
102   let uploads =
103     let not_empty name = (q#upload name).upload_value <> "" in
104     List.filter not_empty uploads in
105
106   (* Get the IP address for logging purposes. *)
107   let ip =
108     try Connection.remote_ip (Request.connection r) with Not_found -> "" in
109
110   (* Get the User-Agent string.  Consider in future rejecting spammers
111    * who don't set User-Agent.
112    *)
113   let ua =
114     try Table.get (Request.headers_in r) "User-Agent" with Not_found -> "" in
115
116   (* Get the user details, if any. *)
117   let username =
118     match user with
119         Anonymous -> "anonymous"
120       | User (userid, username, _, _) ->
121           sprintf "%s (%d)" username userid in
122
123   template#set "ip" ip;
124   template#set "ua" ua;
125   template#set "username" username;
126   template#set "hostname" hostname;
127
128   (* Construct the table of names, values for the initial email. *)
129   let table = List.map (fun name ->
130                           let value = q#param name in
131                           [ "name", Template.VarString name;
132                             "value", Template.VarString value ]) names in
133   template#table "names" table;
134
135   (* Any uploads to follow? *)
136   template#conditional "uploads" (uploads <> []);
137   template#set "nr_uploads" (string_of_int (List.length uploads));
138
139   (* Send the initial email. *)
140   let body = template#to_string in
141   Sendmail.send_mail ~subject ~to_addr:emails body;
142
143   (* Send the following uploads by email. *)
144   List.iter (fun name ->
145                let upload = q#upload name in
146                let subject = upload.upload_filename in
147                (* XXX This is insecure. *)
148                let content_type = upload.upload_content_type in
149                let body = upload.upload_value in
150
151                Sendmail.send_mail ~subject ~to_addr:emails ~content_type body)
152     uploads;
153
154   (* Confirm. *)
155   ok ~title:"Thank you for your contact" ~buttons:[ok_button "/"]
156     q "An email was sent and you should receive a reply shortly."
157
158 let () =
159   register_script ~restrict:[CanView] run