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.7 2004/09/23 11:56:47 rich Exp $
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.
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.
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.
31 open Cocanwiki_emailnotify
34 let is_ws_re = Pcre.regexp "^\\s*$"
35 let is_whitespace str = Pcre.pmatch ~rex:is_ws_re str
37 (* Valid image names. *)
38 let image_ok_re = Pcre.regexp "^[a-z0-9][_a-z0-9]*\\.(jpg|jpeg|gif|ico|png)$"
40 let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
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
47 (* See if there was an upload. *)
50 let upload = q#upload "image" in
54 error ~title:"No image" ~back_button:true
55 q "No image was uploaded.";
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 and underscore. It must end with .jpg, .gif or .png " ^
63 "depending on the image format.");
67 (* Check the image is an image, and get the size. *)
68 let mime_type, width, height =
69 try image_identify image
72 error ~title:"Bad image" ~back_button:true
73 q ("Unknown image type. Is the file you uploaded really an " ^
77 (* Check the image filename extension matches the MIME type. *)
81 String.ends_with name ".jpg" ||
82 String.ends_with name ".jpeg"
84 String.ends_with name ".gif"
86 String.ends_with name ".png"
87 | _ -> assert false in
89 error ~title:"Bad Image Name" ~back_button:true
90 q ("The Image Name extension has to match the image format. " ^
91 "For example if the image is in JPEG format, the name must " ^
92 "be 'something.jpg'. I detected the following image type " ^
93 "in the file you uploaded: " ^ mime_type);
97 (* Check some ALT text was supplied. *)
98 if is_whitespace alt then (
99 error ~title:"Missing Alt text" ~back_button:true
100 q ("You must supply Alt text describing the image. This is required " ^
101 "by accessibility laws and to allow search engines to discover the " ^
102 "content of images.");
106 let title = if is_whitespace title then `Null else `String title in
107 let longdesc = if is_whitespace longdesc then `Null else `String longdesc in
108 let clazz = if is_whitespace clazz then `Null else `String clazz in
110 (* Make a thumbnail of this image. *)
111 let thumbnail, tn_mime_type, tn_width, tn_height =
112 image_thumbnail image 120 120 in
114 (* Put the image into the database. *)
117 "insert into images (hostid, name, image, width, height, alt,
118 title, longdesc, class, thumbnail, tn_width,
119 tn_height, mime_type, tn_mime_type)
120 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" in
121 sth#execute [`Int hostid; `String name; `Binary image; `Int width;
122 `Int height; `String alt; title; longdesc; clazz;
123 `Binary thumbnail; `Int tn_width; `Int tn_height;
124 `String mime_type; `String tn_mime_type];
129 let subject = "Image " ^ name ^ " has been uploaded." in
131 "Page: http://" ^ hostname ^ "/_images" in
133 email_notify ~body ~subject dbh hostid;
135 let buttons = [ ok_button "/_images" ] in
136 ok ~title:"Image uploaded" ~buttons
137 q "Image was uploaded successfully."
140 register_script ~restrict:[CanEdit] run