+ let user =
+ try
+ let cookie =
+ (* Allow the user to deliberately specify an extra "cookie"
+ * parameter, which we will send back as a cookie. This is
+ * useful for "mail my password"-type scripts.
+ *)
+ if q#param_exists "cookie" then (
+ let value = q#param "cookie" in
+ let cookie = Cookie.cookie ~name:"auth" ~value ~path:"/" () in
+ Table.set (Request.headers_out r) "Set-Cookie" cookie#as_string;
+ value
+ ) else (
+ (* Normal cookie, from the headers. *)
+ let header = Table.get (Request.headers_in r) "Cookie" in
+ let cookies = Cookie.parse header in
+ let cookie =
+ List.find (fun cookie -> cookie#name = "auth") cookies in
+ cookie#value
+ ) in
+
+ let sth =
+ dbh#prepare_cached
+ "select u.id, u.name, u.can_edit, u.can_manage_users,
+ u.can_manage_contacts, u.can_manage_site,
+ u.can_edit_global_css
+ from usercookies uc, users u
+ where uc.cookie = ? and uc.userid = u.id and u.hostid = ?" in
+ sth#execute [`String cookie; `Int hostid];
+ (match sth#fetch1 () with
+ [ `Int userid; `String name;
+ `Bool can_edit; `Bool can_manage_users;
+ `Bool can_manage_contacts; `Bool can_manage_site;
+ `Bool can_edit_global_css ] ->
+ let perms = if can_edit then [ CanEdit ] else [] in
+ let perms =
+ if can_manage_users then CanManageUsers :: perms
+ else perms in
+ let perms =
+ if can_manage_contacts then CanManageContacts :: perms
+ else perms in
+ let perms =
+ if can_manage_site then CanManageSite :: perms
+ else perms in
+ let perms =
+ if can_edit_global_css then CanEditGlobalCSS :: perms
+ else perms in
+ User (userid, name, perms)
+ | _ -> assert false)
+ with
+ Not_found -> Anonymous
+ in
+
+ (* If the ~restrict parameter is given, then we want to check that
+ * the user has sufficient permission to run this script.
+ *)
+ let permitted =
+ if not anonymous && user = Anonymous then false
+ else
+ match restrict with
+ [] -> true (* empty list = no restrictions *)
+ | rs ->
+ List.fold_left (||) false
+ (List.map (fun r -> test_permission edit_anon r user) rs) in
+
+ if permitted then (
+ (* Call the actual CGI script. *)
+ try
+ run r q dbh hostid host user
+ with
+ CgiExit -> ()
+ ) else
+ error ~back_button:true
+ ~title:"Access denied"
+ q "You do not have permission to access this part of the site."
+ )
+
+(* Convert a section name into something valid for use in <a name="...">
+ * XXX This breaks horribly for non-7-bit strings.
+ * XXX This is stuck here because we don't have a good place for it, and
+ * because it needs to be fixed for i18n compliance.
+ *)
+let linkname_of_sectionname str =
+ let str = String.copy str in
+ for i = 0 to String.length str - 1 do
+ if not (isalnum str.[i]) then str.[i] <- '_'
+ done;
+ str