Allow images and files to be replaced.
[cocanwiki.git] / scripts / upload_image.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_image.ml,v 1.10 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_ok
31 open Cocanwiki_emailnotify
32 open Cocanwiki_images
33
34 let is_ws_re = Pcre.regexp "^\\s*$"
35 let is_whitespace str = Pcre.pmatch ~rex:is_ws_re str
36
37 (* Valid image names. *)
38 let image_ok_re = Pcre.regexp "^[a-z0-9][-._a-z0-9]*\\.(jpg|jpeg|gif|ico|png)$"
39
40 let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } user=
41   let name = q#param "name" in
42   let alt = q#param "alt" in
43   let title = q#param "title" in
44   let longdesc = q#param "longdesc" in
45   let clazz = q#param "class" in
46
47   (* See if there was an upload. *)
48   let image =
49     try
50       let upload = q#upload "file" in
51       upload.upload_value
52     with
53         Not_found ->
54           error ~title:"No image" ~back_button:true
55             q "No image was uploaded.";
56           return () in
57
58   (* Check the name is valid. *)
59   if not (Pcre.pmatch ~rex:image_ok_re name) then (
60     error ~title:"Bad Image Name" ~back_button:true
61       q ("The Image Name must contain only lowercase English letters, " ^
62          "numbers, dots, dashes and underscore.  " ^
63          "It must end with .jpg, .gif or .png " ^
64          "depending on the image format.");
65     return ()
66   );
67
68   (* Check the image is an image, and get the size. *)
69   let mime_type, width, height =
70     try image_identify image
71     with
72         Invalid_argument _ ->
73           error ~title:"Bad image" ~back_button:true
74             q ("Unknown image type.  Is the file you uploaded really an " ^
75                "image?");
76           return () in
77
78   (* Check the image filename extension matches the MIME type. *)
79   let ext_ok =
80     match mime_type with
81         "image/jpeg" ->
82           String.ends_with name ".jpg" ||
83           String.ends_with name ".jpeg"
84       | "image/gif" ->
85           String.ends_with name ".gif"
86       | "image/png" ->
87           String.ends_with name ".png"
88       | _ -> assert false in
89   if not ext_ok then (
90     error ~title:"Bad Image Name" ~back_button:true
91       q ("The Image Name extension has to match the image format.  " ^
92          "For example if the image is in JPEG format, the name must " ^
93          "be 'something.jpg'.  I detected the following image type " ^
94          "in the file you uploaded: " ^ mime_type);
95     return ()
96   );
97
98   (* Check some ALT text was supplied. *)
99   if is_whitespace alt then (
100     error ~title:"Missing Alt text" ~back_button:true
101       q ("You must supply Alt text describing the image.  This is required " ^
102          "by accessibility laws and to allow search engines to discover the " ^
103          "content of images.");
104     return ()
105   );
106
107   let title = if is_whitespace title then `Null else `String title in
108   let longdesc = if is_whitespace longdesc then `Null else `String longdesc in
109   let clazz = if is_whitespace clazz then `Null else `String clazz in
110
111   (* Make a thumbnail of this image. *)
112   let thumbnail, tn_mime_type, tn_width, tn_height =
113     image_thumbnail image 120 120 in
114
115   (* Check if something with the same name already exists.  If replace=1
116    * then we can replace it, otherwise we must present an error message.
117    *)
118   let replace = q#param_true "replace" in
119   let sth = dbh#prepare_cached "select 1 from images
120                                  where hostid = ? and name = ?" in
121   sth#execute [`Int hostid; `String name];
122
123   let exists = try sth#fetch1int () = 1 with Not_found -> false in
124
125   if exists then (
126     if not replace then (
127       error ~title:"Image already exists" ~back_button:true
128         q ("An image with the same name already exists.");
129     return ()
130     ) else (
131       let sth = dbh#prepare_cached "update images
132                                        set name_deleted = name, name = null
133                                      where hostid = ? and name = ?" in
134       sth#execute [`Int hostid; `String name];
135     )
136   );
137
138   (* Put the image into the database. *)
139   let sth =
140     dbh#prepare_cached
141       "insert into images (hostid, name, image, width, height, alt,
142                            title, longdesc, class, thumbnail, tn_width,
143                            tn_height, mime_type, tn_mime_type)
144        values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" in
145   sth#execute [`Int hostid; `String name; `Binary image; `Int width;
146                `Int height; `String alt; title; longdesc; clazz;
147                `Binary thumbnail; `Int tn_width; `Int tn_height;
148                `String mime_type; `String tn_mime_type];
149
150   dbh#commit ();
151
152   (* Email notify. *)
153   let subject = "Image " ^ name ^ " has been uploaded." in
154   let body = fun () ->
155     "Page: http://" ^ hostname ^ "/_images" in
156
157   email_notify ~body ~subject ~user dbh hostid;
158
159   let buttons = [ ok_button "/_images" ] in
160   ok ~title:"Image uploaded" ~buttons
161     q "Image was uploaded successfully."
162
163 let () =
164   register_script ~restrict:[CanEdit] run