Allow images and files to be replaced.
[cocanwiki.git] / scripts / upload_file.ml
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: upload_file.ml,v 1.9 2004/11/01 16:24:50 rich Exp $
5  *
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.
10  *
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.
15  *
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.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open ExtString
28
29 open Cocanwiki
30 open Cocanwiki_template
31 open Cocanwiki_ok
32 open Cocanwiki_emailnotify
33 open Cocanwiki_images
34
35 let is_ws_re = Pcre.regexp "^\\s*$"
36 let is_whitespace str = Pcre.pmatch ~rex:is_ws_re str
37
38 (* Valid file names. *)
39 let file_ok_re = Pcre.regexp "^[a-z0-9][-._a-z0-9]*$"
40
41 let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } user=
42   let name = q#param "name" in
43   let title = q#param "title" in
44
45   (* See if there was an upload. *)
46   let file =
47     try
48       let upload = q#upload "file" in
49       upload.upload_value
50     with
51         Not_found ->
52           error ~title:"No file" ~back_button:true
53             q "No file was uploaded.";
54           return () in
55
56   (* Check the name is valid. *)
57   if not (Pcre.pmatch ~rex:file_ok_re name) then (
58     error ~title:"Bad File Name" ~back_button:true
59       q ("The File Name must contain only lowercase English letters, " ^
60          "numbers, dots, dashes and underscore.");
61     return ()
62   );
63
64   (* Identify the MIME type from the extension. *)
65   let mime_type = mime_type_of_filename name in
66
67   let title = if is_whitespace title then `Null else `String title in
68
69   (* Check if something with the same name already exists.  If replace=1
70    * then we can replace it, otherwise we must present an error message.
71    *)
72   let replace = q#param_true "replace" in
73   let sth = dbh#prepare_cached "select 1 from files
74                                  where hostid = ? and name = ?" in
75   sth#execute [`Int hostid; `String name];
76
77   let exists = try sth#fetch1int () = 1 with Not_found -> false in
78
79   if exists then (
80     if not replace then (
81       error ~title:"File already exists" ~back_button:true
82         q ("An file with the same name already exists.");
83     return ()
84     ) else (
85       let sth = dbh#prepare_cached "update files
86                                        set name_deleted = name, name = null
87                                      where hostid = ? and name = ?" in
88       sth#execute [`Int hostid; `String name];
89     )
90   );
91
92   (* Put the file into the database. *)
93   let sth =
94     dbh#prepare_cached
95       "insert into files (hostid, name, content, title, mime_type)
96        values (?, ?, ?, ?, ?)" in
97   sth#execute [`Int hostid; `String name; `Binary file; title;
98                `String mime_type];
99
100   dbh#commit ();
101
102   (* Email notify. *)
103   let subject = "File " ^ name ^ " has been uploaded." in
104   let body = fun () ->
105     "Page: http://" ^ hostname ^ "/_files" in
106
107   email_notify ~body ~subject ~user dbh hostid;
108
109   let buttons = [ ok_button "/_files" ] in
110   ok ~title:"File uploaded" ~buttons
111     q "File was uploaded successfully."
112
113 let () =
114   register_script ~restrict:[CanEdit] run