Scripts updated to use new PG interface.
[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.8 2006/03/28 16:24:08 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     return ()
40   );
41
42   (* Get the feedback email for this host. *)
43   let to_addr = List.hd (
44     PGSQL(dbh)
45       "select feedback_email from hosts where id = $hostid"
46   ) in
47   let to_addr = Option.get to_addr in
48
49   (* Get the fields. *)
50   let page = q#param "page" in
51   let name = q#param "name" in
52   let email = q#param "email" in
53   let feedback = q#param "feedback" in
54
55   template#set "page" page;
56   template#set "name" name;
57   template#set "email" email;
58   template#set "feedback" feedback;
59
60   (* Get the IP address for logging purposes. *)
61   let ip =
62     try Connection.remote_ip (Request.connection r) with Not_found -> "" in
63
64   (* Get the User-Agent string.  Consider in future rejecting spammers
65    * who don't set User-Agent.
66    *)
67   let ua =
68     try Table.get (Request.headers_in r) "User-Agent" with Not_found -> "" in
69
70   (* Get the user details, if any. *)
71   let username =
72     match user with
73         Anonymous -> "anonymous"
74       | User (userid, username, _, _) ->
75           sprintf "%s (%ld)" username userid in
76
77   template#set "ip" ip;
78   template#set "ua" ua;
79   template#set "username" username;
80   template#set "hostname" hostname;
81
82   (* Send the feedback email. *)
83   let subject = "Wiki feedback: Feedback about " ^ page ^ " page" in
84   let body = template#to_string in
85   Sendmail.send_mail ~subject ~to_addr:[to_addr] body;
86
87   (* Confirm. *)
88   ok ~title:"Thank you for your feedback" ~buttons:[ok_button "/"]
89     dbh hostid q "An email has been sent to the site administrators."
90
91 let () =
92   register_script ~restrict:[CanView] run