All new templates. Site menu displays basically everywhere.
[cocanwiki.git] / scripts / admin / edit_emails.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: edit_emails.ml,v 1.4 2004/09/09 09:35:35 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Cocanwiki
13 open Cocanwiki_ok
14 open Cocanwiki_strings
15
16 let split_re = Pcre.regexp "[\\r\\n,;]+"
17 let email_re = Pcre.regexp "(.*)<(.*)>"
18
19 let run r (q : cgi) (dbh : Dbi.connection) _ host' _ =
20   let hostid = int_of_string (q#param "hostid") in
21
22   if q#param_true "cancel" then (
23     let { hostname = hostname } = host' in
24     q#redirect ("http://" ^ hostname ^ "/_bin/admin/host.cmo?hostid=" ^
25                 string_of_int hostid);
26     raise CgiExit
27   );
28
29   let emails = try q#param "emails" with Not_found -> "" in
30
31   (* It's very hard to verify email addresses.  Thus this script
32    * should not be exposed to untrusted users.
33    *)
34   let check_email str =
35     let name, email =
36       try
37         let subs = Pcre.exec ~rex:email_re str in
38         Pcre.get_substring subs 1, Pcre.get_substring subs 2
39       with
40           Not_found ->
41             "", str in
42
43     (* Trim whitespace. *)
44     trim name, trim email
45   in
46
47   let emails = Pcre.split ~rex:split_re emails in
48   let emails = List.map check_email emails in
49   let emails = List.filter ((<>) ("","")) emails in
50
51   (* Update the database. *)
52   let sth = dbh#prepare_cached
53               "delete from email_notify where hostid = ?" in
54   sth#execute [`Int hostid];
55   let sth = dbh#prepare_cached "insert into email_notify (hostid, email, name)
56                                 values (?, ?, ?)" in
57   List.iter (fun (name, email) ->
58                if name = "" then
59                  sth#execute [`Int hostid; `String email; `Null]
60                else
61                  sth#execute [`Int hostid; `String email; `String name])
62     emails;
63
64   (* Commit to the database. *)
65   dbh#commit ();
66
67   (* Print confirmation page. *)
68   let buttons = [
69     { StdPages.label = "OK";
70       StdPages.link = "/_bin/admin/host.cmo";
71       StdPages.method_ = None;
72       StdPages.params = [ "hostid", string_of_int hostid ] }
73   ] in
74
75   ok ~title:"Saved" ~buttons
76     q "Email notifications updated."
77
78 let () =
79   register_script run