+let login (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let env = cgi#environment in
+
+ assert (cgi#arguments <> []);
+ let uid = cgi#argument_value "userid" in
+ let userpw = cgi#argument_value "password" in
+ let pw,_ = MatitaAuthentication.lookup_user uid in
+
+ if pw = userpw then
+ begin
+ let ft = MatitaAuthentication.read_ft uid in
+ let _ = MatitaFilesystem.html_of_library uid ft in
+ let sid = MatitaAuthentication.create_session uid in
+ (* let cookie = Netcgi.Cookie.make "session" (Uuidm.to_string sid) in
+ cgi#set_header ~set_cookies:[cookie] (); *)
+ env#set_output_header_field
+ "Set-Cookie" ("session=" ^ (Uuidm.to_string sid));
+(* env#set_output_header_field "Location" "/index.html" *)
+ cgi#out_channel#output_string
+ ("<html><head><meta http-equiv=\"refresh\" content=\"2;url=/index.html\">"
+ ^ "</head><body>Redirecting to Matita page...</body></html>")
+ end
+ else
+ begin
+ cgi#set_header
+ ~cache:`No_cache
+ ~content_type:"text/html; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string
+ "<html><head></head><body>Authentication error</body></html>"
+ end;
+
+ cgi#out_channel#commit_work()
+
+;;
+
+let logout (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let env = cgi#environment in
+ (try
+ let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
+ let sid = HExtlib.unopt sid in
+ MatitaAuthentication.logout_user sid;
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/html; charset=\"utf-8\""
+ ();
+ let text = read_file (rt_path () ^ "/logout.html") in
+ cgi#out_channel#output_string text
+ with
+ | Not_found _ ->
+ cgi # set_header
+ ~status:`Internal_server_error
+ ~cache:`No_cache
+ ~content_type:"text/html; charset=\"utf-8\""
+ ());
+ cgi#out_channel#commit_work()
+;;
+
+exception File_already_exists;;
+
+let save (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let env = cgi#environment in
+ (try
+ let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
+ let sid = HExtlib.unopt sid in
+ let status = MatitaAuthentication.get_status sid in
+ let uid = MatitaAuthentication.user_of_session sid in
+ assert (cgi#arguments <> []);
+ let locked = cgi#argument_value "locked" in
+ let unlocked = cgi#argument_value "unlocked" in
+ let dir = cgi#argument_value "dir" in
+ let rel_filename = cgi # argument_value "file" in
+ let filename = libdir uid ^ "/" ^ rel_filename in
+ let force = bool_of_string (cgi#argument_value "force") in
+ let already_exists = Sys.file_exists filename in
+
+ if ((not force) && already_exists) then
+ raise File_already_exists;
+
+ if dir = "true" then
+ Unix.mkdir filename 0o744
+ else
+ begin
+ let oc = open_out filename in
+ output_string oc (locked ^ unlocked);
+ close_out oc;
+ if MatitaEngine.eos status unlocked then
+ begin
+ (* prerr_endline ("serializing proof objects..."); *)
+ GrafiteTypes.Serializer.serialize
+ ~baseuri:(NUri.uri_of_string status#baseuri) status;
+ (* prerr_endline ("done."); *)
+ end;
+ end;
+ let old_flag =
+ try
+ List.assoc rel_filename (MatitaAuthentication.read_ft uid)
+ with Not_found -> MatitaFilesystem.MUnversioned
+ in
+ (if old_flag <> MatitaFilesystem.MConflict &&
+ old_flag <> MatitaFilesystem.MAdd then
+ let newflag =
+ if already_exists then MatitaFilesystem.MModified
+ else MatitaFilesystem.MAdd
+ in
+ MatitaAuthentication.set_file_flag uid [rel_filename, Some newflag]);
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string "<response>ok</response>"
+ with
+ | File_already_exists ->
+ cgi#out_channel#output_string "<response>cancelled</response>"
+ | Sys_error _ ->
+ cgi # set_header
+ ~status:`Internal_server_error
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ()
+ | e ->
+ let estr = Printexc.to_string e in
+ cgi#out_channel#output_string ("<response>" ^ estr ^ "</response>"));
+ cgi#out_channel#commit_work()
+;;
+
+let initiate_commit (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let _env = cgi#environment in
+ (try
+ let out = do_global_commit () in
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string "<commit>";
+ cgi#out_channel#output_string "<response>ok</response>";
+ cgi#out_channel#output_string ("<details>" ^ out ^ "</details>");
+ cgi#out_channel#output_string "</commit>"
+ with
+ | Not_found _ ->
+ cgi # set_header
+ ~status:`Internal_server_error
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ());
+ cgi#out_channel#commit_work()
+;;