Add IE6- and IE7-specific stylesheets, only served to broken browsers.
[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.14 2006/12/06 09:46:56 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 subj_rex = Pcre.regexp "\\$\\w+"
34
35 let run r (q : cgi) dbh hostid {hostname = hostname} user =
36   let template = get_template r dbh hostid "contact.txt" in
37
38   let fail msg =
39     error ~back_button:true ~title:"Bad form"
40       r dbh hostid q
41       (msg ^ "  Please contact the owner of the site by email.");
42     return ()
43   in
44
45   (* Get the name field. *)
46   let name =
47     try q#param "id"
48     with
49         Not_found ->
50           fail "The 'name' field is missing in that form." in
51
52   (* Get the contacts / emails from the database. *)
53   let rows = PGSQL(dbh)
54     "select id, subject from contacts
55       where hostid = $hostid and name = $name" in
56
57   let id, subject =
58     match rows with
59     | [row] -> row
60     | [] -> fail "There is no such contact form in the database."
61     | _ -> assert false in
62
63   let to_addrs =
64     PGSQL(dbh) "select email from contact_emails where contactid = $id" in
65   let to_addrs = List.map (fun email -> "", email) to_addrs in
66
67   if to_addrs = [] then
68     fail "There are no email addresses associated with that contact id.";
69
70   (* Now process the strings passed as parameters to the script.  Any
71    * parameter which starts with 'file' (eg. 'file0') is treated as a
72    * file upload automatically.
73    *)
74   let names = List.map fst q#params in
75   let names, uploads =
76     if q#is_multipart then (
77       let uploads =
78         List.filter (fun str -> String.starts_with str "file") names in
79       let names =
80         List.filter (fun str -> str <> "id" &&
81                        not (String.starts_with str "file")) names in
82       names, uploads
83     ) else
84       names, [] in
85
86   (* Sort them.
87    * Ignore repeat parameters. - Don't use these in forms.
88    *)
89   let rec uniq = function
90       [] -> []
91     | [x] -> [x]
92     | x :: y :: xs when compare x y = 0 -> uniq (x :: xs)
93     | x :: y :: xs -> x :: uniq (y :: xs)
94   in
95
96   let names = uniq (List.sort compare names) in
97   let uploads = uniq (List.sort compare uploads) in
98
99   (* Some browsers send an empty file for empty uploads.  Remove those. *)
100   let uploads =
101     let not_empty name = (q#upload name).upload_value <> "" in
102     List.filter not_empty uploads in
103
104   (* Substitute any $Field fields in the subject line.  The substitution
105    * is very simple-minded.
106    *)
107   let subst pat =
108     let n = String.length pat in
109     assert (n > 0 && pat.[0] = '$');
110     let fieldname = String.sub pat 1 (n-1) in
111     if List.mem fieldname names then
112       q#param fieldname
113     else
114       pat
115   in
116   let subject = Pcre.substitute ~rex:subj_rex ~subst subject in
117
118   (* Get the IP address for logging purposes. *)
119   let ip =
120     try Connection.remote_ip (Request.connection r) with Not_found -> "" in
121
122   (* Get the User-Agent string.  Consider in future rejecting spammers
123    * who don't set User-Agent.
124    *)
125   let ua =
126     try Table.get (Request.headers_in r) "User-Agent" with Not_found -> "" in
127
128   (* Get the user details, if any. *)
129   let username =
130     match user with
131         Anonymous -> "anonymous"
132       | User (userid, username, _, _) ->
133           sprintf "%s (%ld)" username userid in
134
135   template#set "ip" ip;
136   template#set "ua" ua;
137   template#set "username" username;
138   template#set "hostname" hostname;
139
140   (* Construct the table of names, values for the initial email. *)
141   let table = List.map (fun name ->
142                           let value = q#param name in
143                           [ "name", Template.VarString name;
144                             "value", Template.VarString value ]) names in
145   template#table "names" table;
146
147   (* Any uploads to follow? *)
148   template#conditional "uploads" (uploads <> []);
149   template#set "nr_uploads" (string_of_int (List.length uploads));
150
151   (* Send the initial email. *)
152   let content_type =
153     "text/plain", ["charset", Mimestring.mk_param "UTF-8"] in
154   let body = template#to_string in
155   let msg = Netsendmail.compose ~to_addrs ~subject ~content_type body in
156   Netsendmail.sendmail msg;
157
158   (* Send the following uploads by email. *)
159   List.iter (fun name ->
160                let upload = q#upload name in
161                let subject = upload.upload_filename in
162                let content_type = upload.upload_content_type, [] in
163                let body = upload.upload_value in
164
165                let msg =
166                  Netsendmail.compose ~to_addrs ~subject ~content_type body in
167                Netsendmail.sendmail msg)
168     uploads;
169
170   (* Confirm. *)
171   ok ~title:"Thank you for your contact" ~buttons:[ok_button "/"]
172     r dbh hostid q "An email was sent and you should receive a reply shortly."
173
174 let () =
175   register_script ~restrict:[CanView] run