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.6 2004/09/23 11:56:47 rich Exp $
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.
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.
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.
29 open Cocanwiki_strings
31 let split_re = Pcre.regexp "[\\s,;]+"
33 let run r (q : cgi) (dbh : Dbi.connection) _ host' _ =
34 let hostid = int_of_string (q#param "hostid") in
36 if q#param_true "cancel" then (
37 let { hostname = hostname } = host' in
38 q#redirect ("http://" ^ hostname ^ "/_bin/admin/host.cmo?hostid=" ^
39 string_of_int hostid);
43 let canonical_hostname = q#param "canonical_hostname" in
44 let hostnames = try q#param "hostnames" with Not_found -> "" in
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.
52 let check_hostname h =
53 let h = trim h in (* Trim whitespace. *)
54 let h = String.lowercase h in (* Lowercase. *)
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
63 (* Update the database. *)
64 let sth = dbh#prepare_cached
65 "set constraints \"hosts_hostname_cn\" deferred" in
67 let sth = dbh#prepare_cached "update hosts set canonical_hostname = ?
69 sth#execute [`String canonical_hostname; `Int hostid];
70 let sth = dbh#prepare_cached "delete from hostnames where hostid = ?" in
71 sth#execute [`Int hostid];
72 let sth = dbh#prepare_cached "insert into hostnames (hostid, name)
74 sth#execute [`Int hostid; `String canonical_hostname];
75 List.iter (fun name ->
76 sth#execute [`Int hostid; `String name]) hostnames;
78 (* Commit to the database. *)
81 (* Print confirmation page. *)
83 { StdPages.label = "OK";
84 StdPages.link = "/_bin/admin/host.cmo";
85 StdPages.method_ = None;
86 StdPages.params = [ "hostid", string_of_int hostid ] }
89 ok ~title:"Saved" ~buttons
90 q "Hostnames updated."