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.11 2006/07/26 13:12:11 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 _ host' _ =
34 let hostid = Int32.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 Int32.to_string hostid)
42 let canonical_hostname = q#param "canonical_hostname" in
43 let hostnames = try q#param "hostnames" with Not_found -> "" in
45 (* In theory we could verify characters in hostnames, and call
46 * error / return () if the format is incorrect. However
47 * it's probably best to assume the sysadmin knows what they're up to
48 * here. If this script is allowed to be accessed by untrusted users,
49 * then this has security implications.
51 let check_hostname h =
52 let h = trim h in (* Trim whitespace. *)
53 let h = String.lowercase h in (* Lowercase. *)
57 let canonical_hostname = check_hostname canonical_hostname in
58 let hostnames = Pcre.split ~rex:split_re hostnames in
59 let hostnames = List.map check_hostname hostnames in
60 let hostnames = List.filter ((<>) "") hostnames in
62 (* Update the database. *)
63 PGSQL(dbh) "set constraints \"hosts_hostname_cn\" deferred";
64 PGSQL(dbh) "update hosts set canonical_hostname = $canonical_hostname
66 PGSQL(dbh) "delete from hostnames where hostid = $hostid";
69 PGSQL(dbh) "insert into hostnames (hostid, name)
70 values ($hostid, $name)";
73 (* Commit to the database. *)
76 (* Print confirmation page. *)
78 { Template.StdPages.label = "OK";
79 Template.StdPages.link = "/_bin/admin/host.cmo";
80 Template.StdPages.method_ = None;
81 Template.StdPages.params = [ "hostid", Int32.to_string hostid ] }
84 ok ~title:"Saved" ~buttons
85 dbh (-1l) q "Hostnames updated."