Added a "name" field to the mailing list entries.
[cocanwiki.git] / scripts / admin / create_host.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: create_host.ml,v 1.3 2004/09/09 12:21:22 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  * NB. Because there might not be any hosts existing when this Wiki
22  * is created, this is not a normal Cocanwiki.register_script script.
23  * Instead, we're using the standard mod_caml Registry.
24  *)
25
26 open Apache
27 open Registry
28 open Cgi
29 open Printf
30
31 open Cocanwiki_strings
32
33 let split_re = Pcre.regexp "[\\s,;]+"
34
35 let run r =
36   let q = new cgi r in
37   let dbh = Cocanwiki._get_dbh r in
38
39   let canonical_hostname = q#param "canonical_hostname" in
40   let hostnames = try q#param "hostnames" with Not_found -> "" in
41   let title = q#param "title" in
42
43   (* Check the title is reasonable. *)
44   let title = trim title in
45   if title = "" then (
46     Cocanwiki_ok.error ~back_button:true ~title:"Bad title"
47       q "You must give a title for this Wiki.";
48   ) else (
49     (* In theory we could verify characters in hostnames.  However
50      * it's probably best to assume the sysadmin knows what they're up to
51      * here.  If this script is allowed to be accessed by untrusted
52      * users, then this has security implications.
53      *)
54     let check_hostname h =
55       let h = trim h in                 (* Trim whitespace. *)
56       let h = String.lowercase h in     (* Lowercase. *)
57       h
58     in
59
60     let canonical_hostname = check_hostname canonical_hostname in
61     let hostnames = Pcre.split ~rex:split_re hostnames in
62     let hostnames = List.map check_hostname hostnames in
63     let hostnames = List.filter ((<>) "") hostnames in
64
65     (* Update the database. *)
66     let sth = dbh#prepare_cached
67                 "set constraints \"hosts_hostname_cn\" deferred" in
68     sth#execute [];
69     let sth = dbh#prepare_cached "insert into hosts (canonical_hostname)
70                                   values (?)" in
71     sth#execute [`String canonical_hostname];
72
73     let hostid = sth#serial "hosts_id_seq" in
74
75     let sth = dbh#prepare_cached "insert into hostnames (hostid, name)
76                                   values (?, ?)" in
77     sth#execute [`Int hostid; `String canonical_hostname];
78     List.iter (fun name ->
79                  sth#execute [`Int hostid; `String name]) hostnames;
80
81     let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
82                                     description) values (?, 'index', ?, ?)" in
83     sth#execute [`Int hostid; `String title; `String title];
84
85     (* Commit to the database. *)
86     dbh#commit ();
87
88     (* Print confirmation page. *)
89     let buttons = [
90       { StdPages.label = "OK";
91         StdPages.link = "/_bin/admin/host.cmo";
92         StdPages.method_ = None;
93         StdPages.params = [ "hostid", string_of_int hostid ] }
94     ] in
95
96     Cocanwiki_ok.ok ~title:"Wiki created" ~buttons
97       q "A new Wiki was created."
98   )
99
100 let () =
101   register_script run