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.14 2006/12/06 09:46:57 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.
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.
31 open Cocanwiki_strings
32 open Cocanwiki_create_host
34 let split_re = Pcre.regexp "[\\s,;]+"
38 let dbh = PGOCaml.connect ~database:"cocanwiki" () in
39 PGOCaml.begin_work dbh;
41 let canonical_hostname = q#param "canonical_hostname" in
42 let hostnames = try q#param "hostnames" with Not_found -> "" in
43 let title = q#param "title" in
45 (* Check the title is reasonable. *)
46 let title = trim title in
48 Cocanwiki_ok.error ~back_button:true ~title:"Bad title"
49 r dbh (-1l) q "You must give a title for this Wiki.";
51 (* In theory we could verify characters in hostnames. However
52 * it's probably best to assume the sysadmin knows what they're up to
53 * here. If this script is allowed to be accessed by untrusted
54 * users, then this has security implications.
56 let check_hostname h =
57 let h = trim h in (* Trim whitespace. *)
58 let h = lowercase h in (* Lowercase. *)
62 let canonical_hostname = check_hostname canonical_hostname in
63 let hostnames = Pcre.split ~rex:split_re hostnames in
64 let hostnames = List.map check_hostname hostnames in
65 let hostnames = List.filter ((<>) "") hostnames in
68 if q#param_true "template" then Int32.of_string (q#param "template")
71 let hostid = create_host dbh canonical_hostname hostnames template title
72 "Administrator" "123456" true None in
74 (* Commit to the database. *)
77 (* Print confirmation page. *)
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 ] }
85 Cocanwiki_ok.ok ~title:"Wiki created" ~buttons
86 r dbh (-1l) q "A new Wiki was created."