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.14 2004/10/04 15:19:56 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;
42 (* Permissions and restrictions.
44 * Use the optional ~restrict parameter to register_script to restrict
45 * who can use the script. For example:
46 * register_script ~restrict:[CanEdit ; CanManageUsers] run
48 type permissions_t = CanView | CanEdit | CanManageUsers | CanManageContacts
49 | CanManageSite | CanEditGlobalCSS
51 (* The "user object". *)
52 type user_t = Anonymous (* Not logged in. *)
53 | User of int * string * permissions_t list
54 (* Userid, name, permissions. *)
56 let test_permission {edit_anon = edit_anon; view_anon = view_anon} perm user =
57 if perm = CanEdit && edit_anon then true
58 else if perm = CanView && view_anon then true
61 | User (_, _, perms) -> List.mem perm perms
63 let can_edit host = test_permission host CanEdit
64 let can_manage_users host = test_permission host CanManageUsers
65 let can_manage_contacts host = test_permission host CanManageContacts
66 let can_manage_site host = test_permission host CanManageSite
67 let can_edit_global_css host = test_permission host CanEditGlobalCSS
69 (* Our wrapper around the standard [register_script] function.
71 * The optional ~restrict and ~anonymous parameters work as follows:
73 * By default (neither parameter given), anonymous or logged-in users
74 * at any level are permitted to run the script.
76 * If ~anonymous:false then a user must be logged in to use the script.
78 * If ~restrict contains a list of permissions (eg. CanEdit, etc.) then
79 * the user must have the ability to do AT LEAST ONE of those actions.
80 * (Note that this does not necessarily imply that the user must be
81 * logged in, because in some circumstances even anonymous users have
82 * the CanEdit permission - very typical for a wiki).
84 * If ~anonymous:false and ~restrict is given then the user must be
85 * logged in AND have the ability to do AT LEAST ONE of those actions.
87 let register_script ?(restrict = []) ?(anonymous = true) run =
88 (* Actually register the script with the real [Registry] module. *)
92 let dbh = _get_dbh r in
94 (* Get the host ID, by comparing the Host: header with the hostnames
95 * table in the database.
97 let hostid, hostname, edit_anon, view_anon =
98 let hostname = try Request.hostname r
99 with Not_found -> failwith "No ``Host:'' header in request" in
100 let hostname = String.lowercase hostname in
104 "select h.id, h.canonical_hostname, h.edit_anon, h.view_anon
105 from hostnames hn, hosts h
106 where hn.name = ? and hn.hostid = h.id" in
107 sth#execute [`String hostname];
110 (match sth#fetch1 () with
111 [ `Int id; `String hostname;
112 `Bool edit_anon; `Bool view_anon ] ->
113 id, hostname, edit_anon, view_anon
117 failwith ("Hostname ``" ^ hostname ^ "'' not found in " ^
118 "the hosts/hostnames tables in the database.") in
120 (* Create the host object. *)
121 let host = { hostname = hostname;
122 edit_anon = edit_anon;
123 view_anon = view_anon } in
125 (* Look for the user's cookie, and determine from this the user
131 (* Allow the user to deliberately specify an extra "cookie"
132 * parameter, which we will send back as a cookie. This is
133 * useful for "mail my password"-type scripts.
135 if q#param_exists "cookie" then (
136 let value = q#param "cookie" in
137 let cookie = Cookie.cookie ~name:"auth" ~value ~path:"/" () in
138 Table.set (Request.headers_out r) "Set-Cookie" cookie#as_string;
141 (* Normal cookie, from the headers. *)
142 let header = Table.get (Request.headers_in r) "Cookie" in
143 let cookies = Cookie.parse header in
145 List.find (fun cookie -> cookie#name = "auth") cookies in
151 "select u.id, u.name, u.can_edit, u.can_manage_users,
152 u.can_manage_contacts, u.can_manage_site,
153 u.can_edit_global_css
154 from usercookies uc, users u
155 where uc.cookie = ? and uc.userid = u.id and u.hostid = ?" in
156 sth#execute [`String cookie; `Int hostid];
157 (match sth#fetch1 () with
158 [ `Int userid; `String name;
159 `Bool can_edit; `Bool can_manage_users;
160 `Bool can_manage_contacts; `Bool can_manage_site;
161 `Bool can_edit_global_css ] ->
162 (* Every logged in user can view. *)
163 let perms = [CanView] in
165 if can_edit then CanEdit :: perms
168 if can_manage_users then CanManageUsers :: perms
171 if can_manage_contacts then CanManageContacts :: perms
174 if can_manage_site then CanManageSite :: perms
177 if can_edit_global_css then CanEditGlobalCSS :: perms
179 User (userid, name, perms)
182 Not_found -> Anonymous
185 (* If the ~restrict parameter is given, then we want to check that
186 * the user has sufficient permission to run this script.
189 if not anonymous && user = Anonymous then false
192 [] -> true (* empty list = no restrictions *)
194 List.fold_left (||) false
195 (List.map (fun r -> test_permission host r user) rs) in
198 (* Call the actual CGI script. *)
199 run r q dbh hostid host user
201 if user = Anonymous then
202 q#redirect ("http://" ^ hostname ^ "/_login")
204 error ~back_button:true
205 ~title:"Access denied"
206 q "You do not have permission to access this part of the site."
210 (* Convert a section name into something valid for use in <a name="...">
211 * XXX This breaks horribly for non-7-bit strings.
212 * XXX This is stuck here because we don't have a good place for it, and
213 * because it needs to be fixed for i18n compliance.
215 let linkname_of_sectionname str =
216 let str = String.copy str in
217 for i = 0 to String.length str - 1 do
218 if not (isalnum str.[i]) then str.[i] <- '_'