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: cocanwiki.ml,v 1.3 2004/11/01 12:57:53 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.
28 open Cocanwiki_strings
30 module Pool = DbiPool (Dbi_postgres)
32 (* This function is used to grab a database handle. It's used in a couple
33 * of very special places, and is not for general consumption.
35 let _get_dbh r = Pool.get r "cocanwiki"
37 (* The "host object". *)
38 type host_t = { hostname : string;
39 canonical_hostname : string;
43 (* Permissions and restrictions.
45 * Use the optional ~restrict parameter to register_script to restrict
46 * who can use the script. For example:
47 * register_script ~restrict:[CanEdit ; CanManageUsers] run
49 type permissions_t = CanView | CanEdit | CanManageUsers | CanManageContacts
50 | CanManageSite | CanEditGlobalCSS | CanImportMail
52 (* User preferences and other settings (some cannot be changed by the user). *)
54 email : string option; (* Email address. *)
55 email_notify : bool; (* Email notification. *)
58 (* The "user object". *)
59 type user_t = Anonymous (* Not logged in. *)
60 | User of int * string * permissions_t list * prefs_t
61 (* Userid, name, perms, prefs. *)
63 let test_permission {edit_anon = edit_anon; view_anon = view_anon} perm user =
64 if perm = CanEdit && edit_anon then true
65 else if perm = CanView && view_anon then true
68 | User (_, _, perms, _) -> List.mem perm perms
70 let can_edit host = test_permission host CanEdit
71 let can_manage_users host = test_permission host CanManageUsers
72 let can_manage_contacts host = test_permission host CanManageContacts
73 let can_manage_site host = test_permission host CanManageSite
74 let can_edit_global_css host = test_permission host CanEditGlobalCSS
75 let can_import_mail host = test_permission host CanImportMail
77 (* Our wrapper around the standard [register_script] function.
79 * The optional ~restrict and ~anonymous parameters work as follows:
81 * By default (neither parameter given), anonymous or logged-in users
82 * at any level are permitted to run the script.
84 * If ~anonymous:false then a user must be logged in to use the script.
86 * If ~restrict contains a list of permissions (eg. CanEdit, etc.) then
87 * the user must have the ability to do AT LEAST ONE of those actions.
88 * (Note that this does not necessarily imply that the user must be
89 * logged in, because in some circumstances even anonymous users have
90 * the CanEdit permission - very typical for a wiki).
92 * If ~anonymous:false and ~restrict is given then the user must be
93 * logged in AND have the ability to do AT LEAST ONE of those actions.
95 let register_script ?(restrict = []) ?(anonymous = true) run =
96 (* Actually register the script with the real [Registry] module. *)
100 let dbh = _get_dbh r in
102 (* Get the host ID, by comparing the Host: header with the hostnames
103 * table in the database.
105 let hostid, hostname, canonical_hostname, edit_anon, view_anon =
106 let hostname = try Request.hostname r
107 with Not_found -> failwith "No ``Host:'' header in request" in
108 let hostname = String.lowercase hostname in
112 "select h.id, h.canonical_hostname, h.edit_anon, h.view_anon
113 from hostnames hn, hosts h
114 where hn.name = ? and hn.hostid = h.id" in
115 sth#execute [`String hostname];
118 (match sth#fetch1 () with
119 [ `Int id; `String canonical_hostname;
120 `Bool edit_anon; `Bool view_anon ] ->
121 id, hostname, canonical_hostname, edit_anon, view_anon
125 failwith ("Hostname ``" ^ hostname ^ "'' not found in " ^
126 "the hosts/hostnames tables in the database.") in
128 (* Create the host object. *)
129 let host = { hostname = hostname;
130 canonical_hostname = canonical_hostname;
131 edit_anon = edit_anon;
132 view_anon = view_anon } in
134 (* Look for the user's cookie, and determine from this the user
140 (* Allow the user to deliberately specify an extra "cookie"
141 * parameter, which we will send back as a cookie. This is
142 * useful for "mail my password"-type scripts.
144 if q#param_exists "cookie" then (
145 let value = q#param "cookie" in
146 let cookie = Cookie.cookie ~name:"auth" ~value ~path:"/" () in
147 Table.set (Request.headers_out r) "Set-Cookie" cookie#as_string;
150 (* Normal cookie, from the headers. *)
151 let header = Table.get (Request.headers_in r) "Cookie" in
152 let cookies = Cookie.parse header in
154 List.find (fun cookie -> cookie#name = "auth") cookies in
160 "select u.id, u.name, u.can_edit, u.can_manage_users,
161 u.can_manage_contacts, u.can_manage_site,
162 u.can_edit_global_css, u.can_import_mail,
163 u.email, u.email_notify
164 from usercookies uc, users u
165 where uc.cookie = ? and uc.userid = u.id and u.hostid = ?" in
166 sth#execute [`String cookie; `Int hostid];
167 (match sth#fetch1 () with
168 [ `Int userid; `String name;
169 `Bool can_edit; `Bool can_manage_users;
170 `Bool can_manage_contacts; `Bool can_manage_site;
171 `Bool can_edit_global_css; `Bool can_import_mail;
172 (`Null | `String _) as email; `Bool email_notify ] ->
173 (* Every logged in user can view. *)
174 let perms = [CanView] in
176 if can_edit then CanEdit :: perms
179 if can_manage_users then CanManageUsers :: perms
182 if can_manage_contacts then CanManageContacts :: perms
185 if can_manage_site then CanManageSite :: perms
188 if can_edit_global_css then CanEditGlobalCSS :: perms
191 if can_import_mail then CanImportMail :: perms
197 | `String email -> Some email in
198 let prefs = { email = email;
199 email_notify = email_notify; } in
200 User (userid, name, perms, prefs)
203 Not_found -> Anonymous
206 (* If the ~restrict parameter is given, then we want to check that
207 * the user has sufficient permission to run this script.
210 if not anonymous && user = Anonymous then false
213 [] -> true (* empty list = no restrictions *)
215 List.fold_left (||) false
216 (List.map (fun r -> test_permission host r user) rs) in
219 (* Call the actual CGI script. *)
220 run r q dbh hostid host user
222 if user = Anonymous then
223 q#redirect ("http://" ^ hostname ^ "/_login")
225 error ~back_button:true
226 ~title:"Access denied"
227 q "You do not have permission to access this part of the site."
231 (* Convert a section name into something valid for use in <a name="...">
232 * XXX This breaks horribly for non-7-bit strings.
233 * XXX This is stuck here because we don't have a good place for it, and
234 * because it needs to be fixed for i18n compliance.
236 let linkname_of_sectionname str =
237 let str = String.copy str in
238 for i = 0 to String.length str - 1 do
239 if not (isalnum str.[i]) then str.[i] <- '_'
243 (* List of extensions currently registered. *)
244 type extension_t = Dbi.connection -> int -> string -> string
245 let extensions = ref ([] : (string * extension_t) list)
247 (* Maximum degree of redirection. *)