/_sitemap.rss for COCANWIKI.
[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.14 2006/03/28 16:24:08 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 open Cocanwiki_strings
34
35 (* Valid image names. *)
36 let image_ok_re = Pcre.regexp "^[a-z0-9][-._a-z0-9]*\\.(jpg|jpeg|gif|ico|png)$"
37
38 let run r (q : cgi) dbh hostid { hostname = hostname } user=
39   let name = q#param "name" in
40   let alt = q#param "alt" in
41   let title = q#param "title" in
42   let longdesc = q#param "longdesc" in
43   let clazz = q#param "class" in
44
45   (* See if there was an upload. *)
46   let image =
47     try
48       let upload = q#upload "file" in
49       upload.upload_value
50     with
51         Not_found ->
52           error ~title:"No image" ~back_button:true
53             dbh hostid q "No image was uploaded.";
54           return () in
55
56   (* Check the name is valid. *)
57   if not (Pcre.pmatch ~rex:image_ok_re name) then (
58     error ~title:"Bad Image Name" ~back_button:true
59       dbh hostid q
60       ("The Image Name must contain only lowercase English letters, " ^
61        "numbers, dots, dashes and underscore.  " ^
62        "It must end with .jpg, .gif or .png " ^
63        "depending on the image format.");
64     return ()
65   );
66
67   (* Check the image is an image, and get the size. *)
68   let mime_type, width, height =
69     try image_identify image
70     with
71         Invalid_argument _ ->
72           error ~title:"Bad image" ~back_button:true
73             dbh hostid q
74             ("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       dbh hostid q
92       ("The Image Name extension has to match the image format.  " ^
93        "For example if the image is in JPEG format, the name must " ^
94        "be 'something.jpg'.  I detected the following image type " ^
95        "in the file you uploaded: " ^ mime_type);
96     return ()
97   );
98
99   (* Check some ALT text was supplied. *)
100   if string_is_whitespace alt then (
101     error ~title:"Missing Alt text" ~back_button:true
102       dbh hostid q
103       ("You must supply Alt text describing the image.  This is required " ^
104        "by accessibility laws and to allow search engines to discover the " ^
105        "content of images.");
106     return ()
107   );
108
109   let title = if string_is_whitespace title then None else Some title in
110   let longdesc =
111     if string_is_whitespace longdesc then None else Some longdesc in
112   let clazz = if string_is_whitespace clazz then None else Some clazz in
113
114   (* Make a thumbnail of this image. *)
115   let thumbnail, tn_mime_type, tn_width, tn_height =
116     image_thumbnail image 120 120 in
117
118   (* Check if something with the same name already exists.  If replace=1
119    * then we can replace it, otherwise we must present an error message.
120    *)
121   let replace = q#param_true "replace" in
122   let rows = PGSQL(dbh) "select 1 from images
123                           where hostid = $hostid and name = $name" in
124   let exists = rows = [Some 1l] in
125
126   if exists then (
127     if not replace then (
128       error ~title:"Image already exists" ~back_button:true
129         dbh hostid q "An image with the same name already exists.";
130     return ()
131     ) else (
132       PGSQL(dbh) "update images
133                      set name_deleted = name, name = null
134                    where hostid = $hostid and name = $name"
135     )
136   );
137
138   (* Put the image into the database. *)
139   let width = Int32.of_int width in
140   let height = Int32.of_int height in
141   let tn_width = Int32.of_int tn_width in
142   let tn_height = Int32.of_int tn_height in
143   PGSQL(dbh)
144     "insert into images (hostid, name, image, width, height, alt,
145                          title, longdesc, class, thumbnail, tn_width,
146                          tn_height, mime_type, tn_mime_type)
147      values ($hostid, $name, $image, $width, $height, $alt, $?title,
148              $?longdesc, $?clazz, $thumbnail, $tn_width, $tn_height,
149              $mime_type, $tn_mime_type)";
150
151   PGOCaml.commit dbh;
152
153   (* Email notify. *)
154   let subject = "Image " ^ name ^ " has been uploaded." in
155   let body = fun () ->
156     "Page: http://" ^ hostname ^ "/_images" in
157
158   email_notify ~body ~subject ~user dbh hostid;
159
160   let buttons = [ ok_button "/_images" ] in
161   ok ~title:"Image uploaded" ~buttons
162     dbh hostid q "Image was uploaded successfully."
163
164 let () =
165   register_script ~restrict:[CanEdit] run