/_sitemap.rss for COCANWIKI.
[cocanwiki.git] / scripts / send_feedback.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: send_feedback.ml,v 1.10 2006/07/31 09:49:43 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 hostid {hostname = hostname} user =
34   let template = get_template dbh hostid "send_feedback.txt" in
35
36   if q#param_true "cancel" then
37     (* Request cancelled. *)
38     q#redirect ("http://" ^ hostname);
39
40   (* Get the feedback email for this host. *)
41   let to_addr = List.hd (
42     PGSQL(dbh)
43       "select feedback_email from hosts where id = $hostid"
44   ) in
45   let to_addr = Option.get to_addr in
46
47   (* Get the fields. *)
48   let page = q#param "page" in
49   let name = q#param "name" in
50   let email = q#param "email" in
51   let feedback = q#param "feedback" in
52
53   template#set "page" page;
54   template#set "name" name;
55   template#set "email" email;
56   template#set "feedback" feedback;
57
58   (* Get the IP address for logging purposes. *)
59   let ip =
60     try Connection.remote_ip (Request.connection r) with Not_found -> "" in
61
62   (* Get the User-Agent string.  Consider in future rejecting spammers
63    * who don't set User-Agent.
64    *)
65   let ua =
66     try Table.get (Request.headers_in r) "User-Agent" with Not_found -> "" in
67
68   (* Get the user details, if any. *)
69   let username =
70     match user with
71         Anonymous -> "anonymous"
72       | User (userid, username, _, _) ->
73           sprintf "%s (%ld)" username userid in
74
75   template#set "ip" ip;
76   template#set "ua" ua;
77   template#set "username" username;
78   template#set "hostname" hostname;
79
80   (* Send the feedback email. *)
81   let subject = "Wiki feedback: Feedback about " ^ page ^ " page" in
82   let body = template#to_string in
83
84   let to_addrs = [ "", to_addr ] in
85   let content_type =
86     "text/plain", ["charset", Mimestring.mk_param "UTF-8"] in
87
88   let msg = Netsendmail.compose ~subject ~to_addrs ~content_type body in
89   Netsendmail.sendmail msg;
90
91   (* Confirm. *)
92   ok ~title:"Thank you for your feedback" ~buttons:[ok_button "/"]
93     dbh hostid q "An email has been sent to the site administrators."
94
95 let () =
96   register_script ~restrict:[CanView] run