370b501bff03ac0938fb7828bd255a20fda46b89
[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.11 2006/03/27 18:09:47 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 open Cocanwiki_create_host
33
34 let split_re = Pcre.regexp "[\\s,;]+"
35
36 let run r =
37   let q = new cgi r in
38   let dbh = Cocanwiki._get_dbh r in
39
40   let canonical_hostname = q#param "canonical_hostname" in
41   let hostnames = try q#param "hostnames" with Not_found -> "" in
42   let title = q#param "title" in
43
44   (* Check the title is reasonable. *)
45   let title = trim title in
46   if title = "" then (
47     Cocanwiki_ok.error ~back_button:true ~title:"Bad title"
48       dbh (-1) q "You must give a title for this Wiki.";
49   ) else (
50     (* In theory we could verify characters in hostnames.  However
51      * it's probably best to assume the sysadmin knows what they're up to
52      * here.  If this script is allowed to be accessed by untrusted
53      * users, then this has security implications.
54      *)
55     let check_hostname h =
56       let h = trim h in                 (* Trim whitespace. *)
57       let h = String.lowercase h in     (* Lowercase. *)
58       h
59     in
60
61     let canonical_hostname = check_hostname canonical_hostname in
62     let hostnames = Pcre.split ~rex:split_re hostnames in
63     let hostnames = List.map check_hostname hostnames in
64     let hostnames = List.filter ((<>) "") hostnames in
65
66     let template =
67       if q#param_true "template" then int_of_string (q#param "template")
68       else 0 in
69
70     let hostid = create_host dbh canonical_hostname hostnames template title
71                    "Administrator" "123456" true None in
72
73     (* Commit to the database. *)
74     PGOCaml.commit dbh;
75
76     (* Print confirmation page. *)
77     let buttons = [
78       { Template.StdPages.label = "OK";
79         Template.StdPages.link = "/_bin/admin/host.cmo";
80         Template.StdPages.method_ = None;
81         Template.StdPages.params = [ "hostid", string_of_int hostid ] }
82     ] in
83
84     Cocanwiki_ok.ok ~title:"Wiki created" ~buttons
85       dbh (-1) q "A new Wiki was created."
86   )
87
88 let () =
89   register_script run