Moved to merjis/tools/wiki.
[cocanwiki.git] / scripts / upload_image.ml
1 (* COCANWIKI scripts.
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 $
5  *)
6
7 open Apache
8 open Registry
9 open Cgi
10 open Printf
11
12 open ExtString
13
14 open Merjisforwiki
15
16 open Cocanwiki
17 open Cocanwiki_ok
18 open Cocanwiki_emailnotify
19
20 let is_ws_re = Pcre.regexp "^\\s*$"
21 let is_whitespace str = Pcre.pmatch ~rex:is_ws_re str
22
23 (* Valid image names. *)
24 let image_ok_re = Pcre.regexp "^[a-z0-9][_a-z0-9]*\\.(jpg|jpeg|gif|ico|png)$"
25
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
32
33   (* See if there was an upload. *)
34   let image =
35     try
36       let upload = q#upload "image" in
37       upload.upload_value
38     with
39         Not_found ->
40           error ~title:"No image" ~back_button:true
41             q "No image was uploaded.";
42           raise CgiExit in
43
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.");
50     raise CgiExit
51   );
52
53   (* Check the image is an image, and get the size. *)
54   let mime_type, width, height =
55     try image_identify image
56     with
57         Invalid_argument _ ->
58           error ~title:"Bad image" ~back_button:true
59             q ("Unknown image type.  Is the file you uploaded really an " ^
60                "image?");
61           raise CgiExit in
62
63   (* Check the image filename extension matches the MIME type. *)
64   let ext_ok =
65     match mime_type with
66         "image/jpeg" ->
67           String.ends_with name ".jpg" ||
68           String.ends_with name ".jpeg"
69       | "image/gif" ->
70           String.ends_with name ".gif"
71       | "image/png" ->
72           String.ends_with name ".png"
73       | _ -> assert false in
74   if not ext_ok then (
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);
80     raise CgiExit
81   );
82
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.");
89     raise CgiExit
90   );
91
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
95
96   (* Make a thumbnail of this image. *)
97   let thumbnail, tn_mime_type, tn_width, tn_height =
98     image_thumbnail image 120 120 in
99
100   (* Put the image into the database. *)
101   let sth =
102     dbh#prepare_cached
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];
111
112   dbh#commit ();
113
114   (* Email notify. *)
115   let subject = "Image " ^ name ^ " has been uploaded." in
116   let body = fun () ->
117     "Page: http://" ^ hostname ^ "/_images" in
118
119   email_notify ~body ~subject dbh hostid;
120
121   let buttons = [ ok_button "/_images" ] in
122   ok ~title:"Image uploaded" ~buttons
123     q "Image was uploaded successfully."
124
125 let () =
126   register_script run