When copying a template during site creation, force the hosts.is_template
[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.6 2004/10/04 14:46:51 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     let sth =
66       dbh#prepare_cached "set constraints hosts_hostname_cn deferred" in
67     sth#execute [];
68     let sth = dbh#prepare_cached "insert into hosts (canonical_hostname)
69                                     values (?)" in
70     sth#execute [`String canonical_hostname];
71
72     let hostid = sth#serial "hosts_id_seq" in
73
74     let sth = dbh#prepare_cached "insert into hostnames (hostid, name)
75                                     values (?, ?)" in
76     sth#execute [`Int hostid; `String canonical_hostname];
77     List.iter (fun name ->
78                  sth#execute [`Int hostid; `String name]) hostnames;
79
80     (* Are we creating a blank site or copying a template? *)
81     if not (q#param_true "template") then (
82       (* Blank site. *)
83
84       let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
85                                     description) values (?, 'index', ?, ?)" in
86       sth#execute [`Int hostid; `String title; `String title];
87
88     ) else (
89       (* Copy from template. *)
90       let template = int_of_string (q#param "template") in
91
92       (*dbh#set_debug true;*)
93
94       let sth = dbh#prepare_cached "select * from hosts where id = ?" in
95       sth#execute [`Int template];
96
97       let names = sth#names in
98       let row = sth#fetch1 () in
99       sth#finish ();
100
101       List.iter
102         (fun (name, field) ->
103            if name <> "id" && name <> "canonical_hostname" then (
104              let sql = "update hosts set " ^ name ^ " = ? where id = ?" in
105              let sth = dbh#prepare_cached sql in
106              sth#execute [field; `Int hostid]
107            )
108         ) (List.combine names row);
109
110       (* Force new host.is_template field to false. *)
111       let sth =
112         dbh#prepare_cached
113           "update hosts set is_template = false where id = ?" in
114       sth#execute [`Int hostid];
115
116       (* Copy pages. *)
117       let sth =
118         dbh#prepare_cached
119           "insert into pages (hostid, url, title, description, redirect, css)
120            select ?, url, title, description, redirect, css
121              from pages
122             where hostid = ? and url is not null" in
123       sth#execute [`Int hostid; `Int template];
124
125       (* Copy page contents. *)
126       let sth =
127         dbh#prepare_cached
128           "insert into contents (pageid, ordering, sectionname, content,
129                                  divname)
130            select (select id from pages where hostid = ? and url = p.url),
131                   c.ordering, c.sectionname, c.content, c.divname
132              from contents c, pages p
133             where c.pageid = p.id and p.hostid = ? and p.url is not null" in
134       sth#execute [`Int hostid; `Int template];
135
136       (* Copy files and images. *)
137       let sth =
138         dbh#prepare_cached
139           "insert into files (hostid, name, content, title, mime_type)
140            select ?, name, content, title, mime_type
141              from files
142             where hostid = ? and name is not null" in
143       sth#execute [`Int hostid; `Int template];
144
145       let sth =
146         dbh#prepare_cached
147           "insert into images (hostid, name, image, width, height, alt, title,
148                                longdesc, class, mime_type, thumbnail,
149                                tn_width, tn_height, tn_mime_type)
150            select ?, name, image, width, height, alt, title, longdesc, class,
151                   mime_type, thumbnail, tn_width, tn_height, tn_mime_type
152              from images
153             where hostid = ? and name is not null" in
154       sth#execute [`Int hostid; `Int template];
155
156       (* Copy sitemenu. *)
157       let sth =
158         dbh#prepare_cached
159           "insert into sitemenu (hostid, url, label, ordering)
160            select ?, url, label, ordering from sitemenu where hostid = ?" in
161       sth#execute [`Int hostid; `Int template];
162
163       (* Copy contacts. *)
164       let sth =
165         dbh#prepare_cached
166           "insert into contacts (hostid, name, subject)
167            select ?, name, subject from contacts where hostid = ?" in
168       sth#execute [`Int hostid; `Int template];
169
170       let sth =
171         dbh#prepare_cached
172           "insert into contact_emails (contactid, email)
173            select (select id from contacts
174                     where hostid = ? and name = c.name), ce.email
175              from contact_emails ce, contacts c
176             where ce.contactid = c.id and c.hostid = ?" in
177       sth#execute [`Int hostid; `Int template];
178
179       (* Set the title of the index page. *)
180       let sth = dbh#prepare_cached "update pages set title = ?
181                                      where hostid = ? and url = 'index'" in
182       sth#execute [`String title; `Int hostid]
183     );
184
185     (* Create the administrator user. *)
186     let sth = dbh#prepare_cached "insert into users (hostid, name, password,
187                                   force_password_change, can_manage_users)
188                                   values (?, 'Administrator', '123456', true,
189                                   true)" in
190     sth#execute [`Int hostid];
191
192     (* Commit to the database. *)
193     dbh#commit ();
194
195     (* Print confirmation page. *)
196     let buttons = [
197       { StdPages.label = "OK";
198         StdPages.link = "/_bin/admin/host.cmo";
199         StdPages.method_ = None;
200         StdPages.params = [ "hostid", string_of_int hostid ] }
201     ] in
202
203     Cocanwiki_ok.ok ~title:"Wiki created" ~buttons
204       q "A new Wiki was created."
205   )
206
207 let () =
208   register_script run