Modified all scripts with appropriate restrictions based on
[cocanwiki.git] / scripts / delete_image.ml
1 (* COCANWIKI scripts.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: delete_image.ml,v 1.4 2004/09/08 09:54:28 rich Exp $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open Cocanwiki
13 open Cocanwiki_ok
14 open Cocanwiki_emailnotify
15
16 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname, _) _ =
17   let id = int_of_string (q#param "id") in
18
19   if q#param_true "yes" then (
20     (* Delete the image. *)
21     let sth = dbh#prepare_cached "update images
22                                      set name_deleted = name, name = null
23                                    where hostid = ? and id = ?
24                                      and name is not null" in
25     sth#execute [`Int hostid; `Int id];
26
27     dbh#commit ();
28
29     (* Email notify. *)
30     let subject = "Image #" ^ string_of_int id ^ " has been deleted." in
31     let body = fun () ->
32       "Page: http://" ^ hostname ^ "/_images?deleted=1" in
33
34     email_notify ~body ~subject dbh hostid;
35     (* Done. *)
36     let buttons = [ ok_button "/_images" ] in
37     ok ~title:"Image deleted" ~buttons
38       q "Image was deleted successfully."
39   ) else
40     q#redirect ("http://" ^ hostname ^ "/_images")
41
42 let () =
43   register_script ~restrict:[CanEdit] run