2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: upload_image.ml,v 1.1 2004/09/07 10:14:09 rich Exp $
18 open Cocanwiki_emailnotify
20 let is_ws_re = Pcre.regexp "^\\s*$"
21 let is_whitespace str = Pcre.pmatch ~rex:is_ws_re str
23 (* Valid image names. *)
24 let image_ok_re = Pcre.regexp "^[a-z0-9][_a-z0-9]*\\.(jpg|jpeg|gif|ico|png)$"
26 let run r (q : cgi) (dbh : Dbi.connection) (hostid, hostname) _ =
27 let name = q#param "name" in
28 let alt = q#param "alt" in
29 let title = q#param "title" in
30 let longdesc = q#param "longdesc" in
31 let clazz = q#param "class" in
33 (* See if there was an upload. *)
36 let upload = q#upload "image" in
40 error ~title:"No image" ~back_button:true
41 q "No image was uploaded.";
44 (* Check the name is valid. *)
45 if not (Pcre.pmatch ~rex:image_ok_re name) then (
46 error ~title:"Bad Image Name" ~back_button:true
47 q ("The Image Name must contain only lowercase English letters, " ^
48 "numbers and underscore. It must end with .jpg, .gif or .png " ^
49 "depending on the image format.");
53 (* Check the image is an image, and get the size. *)
54 let mime_type, width, height =
55 try image_identify image
58 error ~title:"Bad image" ~back_button:true
59 q ("Unknown image type. Is the file you uploaded really an " ^
63 (* Check the image filename extension matches the MIME type. *)
67 String.ends_with name ".jpg" ||
68 String.ends_with name ".jpeg"
70 String.ends_with name ".gif"
72 String.ends_with name ".png"
73 | _ -> assert false in
75 error ~title:"Bad Image Name" ~back_button:true
76 q ("The Image Name extension has to match the image format. " ^
77 "For example if the image is in JPEG format, the name must " ^
78 "be 'something.jpg'. I detected the following image type " ^
79 "in the file you uploaded: " ^ mime_type);
83 (* Check some ALT text was supplied. *)
84 if is_whitespace alt then (
85 error ~title:"Missing Alt text" ~back_button:true
86 q ("You must supply Alt text describing the image. This is required " ^
87 "by accessibility laws and to allow search engines to discover the " ^
88 "content of images.");
92 let title = if is_whitespace title then `Null else `String title in
93 let longdesc = if is_whitespace longdesc then `Null else `String longdesc in
94 let clazz = if is_whitespace clazz then `Null else `String clazz in
96 (* Make a thumbnail of this image. *)
97 let thumbnail, tn_mime_type, tn_width, tn_height =
98 image_thumbnail image 120 120 in
100 (* Put the image into the database. *)
103 "insert into images (hostid, name, image, width, height, alt,
104 title, longdesc, class, thumbnail, tn_width,
105 tn_height, mime_type, tn_mime_type)
106 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" in
107 sth#execute [`Int hostid; `String name; `Binary image; `Int width;
108 `Int height; `String alt; title; longdesc; clazz;
109 `Binary thumbnail; `Int tn_width; `Int tn_height;
110 `String mime_type; `String tn_mime_type];
115 let subject = "Image " ^ name ^ " has been uploaded." in
117 "Page: http://" ^ hostname ^ "/_images" in
119 email_notify ~body ~subject dbh hostid;
121 let buttons = [ ok_button "/_images" ] in
122 ok ~title:"Image uploaded" ~buttons
123 q "Image was uploaded successfully."