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_file.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.
30 open Cocanwiki_template
32 open Cocanwiki_emailnotify
35 let is_ws_re = Pcre.regexp "^\\s*$"
36 let is_whitespace str = Pcre.pmatch ~rex:is_ws_re str
38 (* Valid file names. *)
39 let file_ok_re = Pcre.regexp "^[a-z0-9][-._a-z0-9]*$"
41 let run r (q : cgi) (dbh : Dbi.connection) hostid { hostname = hostname } _ =
42 let name = q#param "name" in
43 let title = q#param "title" in
45 (* See if there was an upload. *)
48 let upload = q#upload "file" in
52 error ~title:"No file" ~back_button:true
53 q "No file was uploaded.";
56 (* Check the name is valid. *)
57 if not (Pcre.pmatch ~rex:file_ok_re name) then (
58 error ~title:"Bad File Name" ~back_button:true
59 q ("The File Name must contain only lowercase English letters, " ^
60 "numbers, dots, dashes and underscore.");
64 (* Identify the MIME type from the extension. *)
65 let mime_type = mime_type_of_filename name in
67 let title = if is_whitespace title then `Null else `String title in
69 (* Put the file into the database. *)
72 "insert into files (hostid, name, content, title, mime_type)
73 values (?, ?, ?, ?, ?)" in
74 sth#execute [`Int hostid; `String name; `Binary file; title;
80 let subject = "File " ^ name ^ " has been uploaded." in
82 "Page: http://" ^ hostname ^ "/_files" in
84 email_notify ~body ~subject dbh hostid;
86 let buttons = [ ok_button "/_files" ] in
87 ok ~title:"File uploaded" ~buttons
88 q "File was uploaded successfully."
91 register_script ~restrict:[CanEdit] run