Scripts updated to use new PG interface.
[cocanwiki.git] / scripts / admin / edit_hostnames.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: edit_hostnames.ml,v 1.10 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 Cocanwiki
28 open Cocanwiki_ok
29 open Cocanwiki_strings
30
31 let split_re = Pcre.regexp "[\\s,;]+"
32
33 let run r (q : cgi) dbh _ host' _ =
34   let hostid = Int32.of_string (q#param "hostid") in
35
36   if q#param_true "cancel" then (
37     let { hostname = hostname } = host' in
38     q#redirect ("http://" ^ hostname ^ "/_bin/admin/host.cmo?hostid=" ^
39                   Int32.to_string hostid);
40     return ()
41   );
42
43   let canonical_hostname = q#param "canonical_hostname" in
44   let hostnames = try q#param "hostnames" with Not_found -> "" in
45
46   (* In theory we could verify characters in hostnames, and call
47    * error / return () if the format is incorrect.  However
48    * it's probably best to assume the sysadmin knows what they're up to
49    * here.  If this script is allowed to be accessed by untrusted users,
50    * then this has security implications.
51    *)
52   let check_hostname h =
53     let h = trim h in                   (* Trim whitespace. *)
54     let h = String.lowercase h in       (* Lowercase. *)
55     h
56   in
57
58   let canonical_hostname = check_hostname canonical_hostname in
59   let hostnames = Pcre.split ~rex:split_re hostnames in
60   let hostnames = List.map check_hostname hostnames in
61   let hostnames = List.filter ((<>) "") hostnames in
62
63   (* Update the database. *)
64   PGSQL(dbh) "set constraints \"hosts_hostname_cn\" deferred";
65   PGSQL(dbh) "update hosts set canonical_hostname = $canonical_hostname
66                where id = $hostid";
67   PGSQL(dbh) "delete from hostnames where hostid = $hostid";
68   List.iter (
69     fun name ->
70       PGSQL(dbh) "insert into hostnames (hostid, name)
71                   values ($hostid, $name)";
72   ) hostnames;
73
74   (* Commit to the database. *)
75   PGOCaml.commit dbh;
76
77   (* Print confirmation page. *)
78   let buttons = [
79     { Template.StdPages.label = "OK";
80       Template.StdPages.link = "/_bin/admin/host.cmo";
81       Template.StdPages.method_ = None;
82       Template.StdPages.params = [ "hostid", Int32.to_string hostid ] }
83   ] in
84
85   ok ~title:"Saved" ~buttons
86     dbh (-1l) q "Hostnames updated."
87
88 let () =
89   register_script run