+ (* Create the host object. *)
+ let host = { hostname = hostname;
+ edit_anon = edit_anon;
+ view_anon = view_anon } in
+
+ (* Look for the user's cookie, and determine from this the user
+ * object.
+ *)
+ 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, u.can_import_mail
+ 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; `Bool can_import_mail ] ->
+ (* Every logged in user can view. *)
+ let perms = [CanView] in
+ let perms =
+ if can_edit then CanEdit :: perms
+ else perms 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
+ let perms =
+ if can_import_mail then CanImportMail :: 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.