JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
wrote upload handler (untested
[wfpl.git] / upload.php
1 <?php
2
3 #  Copyright (C) 2007 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it
8 #  under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but
13 #  WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with wfpl; see the file COPYING.  If not, write to the
19 #  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 #  MA 02111-1307, USA.
21
22 $GLOBALS['mime_to_ext'] = array(
23         'text/plain' => 'txt',
24         'text/html'  => 'html',
25         'image/jpeg' => 'jpg',
26         'image/jpg'  => 'jpg',
27         'image/gif'  => 'gif',
28         'image/png'  => 'png',
29         'application/pdf' => 'pdf'
30 );
31
32 $GLOBALS['ext_to_ext'] = array(
33         'text' => 'txt',
34         'jpe'  => 'jpg',
35         'jpeg' => 'jpg',
36         'htm'  => 'html'
37 );
38
39
40 # pass in the client's path that came from an html <input type="file"/> tag
41 #
42 # mime time used to generate extension ONLY IF it doesn't have one already.
43 function generate_filename($path, $mime = 'text/plain') {
44         # lower case
45         $filename = strtolower($path)
46
47         # remove directories (unix, windows and mac paths)
48         $last = strrpos($filename, '/');
49         if($last === false) {
50                 $last = strrpos($filename, '\\');
51         }
52         if($last === false) {
53                 $last = strrpos($filename, ':');
54         }
55         if($last) {
56                 $filename = substr($filename, $last + 1);
57         }
58
59         # remove dots from the begning (no invisible files)
60         $filename = ereg_replace('^\.*', '', $filename);
61
62         # fix extension
63         $last_dot = strrpos($filename, '.');
64         if($last_dot === false) {
65                 #no extension
66                 if(isset($GLOBALS['mime_to_ext'][$mime])) {
67                         $filename .= '.' . $GLOBALS['mime_to_ext'][$mime];
68                 }
69         } else {
70                 $basename = substr($filename, 0, $last_dot);
71                 $ext = substr($filename, $last_dot + 1);
72                 if(isset($GLOBALS['ext_to_ext'][$ext])) {
73                         $ext .= $GLOBALS['ext_to_ext'][$ext];
74                 }
75                 $filename = $basename . '.' . $ext;
76         }
77 }
78
79
80
81 # Move to save folder, and return new filename.
82 #
83 # Pass in the index into the $_FILES array (the name of the html input tag) and
84 # the path to the folder you'd like it saved to. If path ends with a slash this
85 # function will generate a filename based on the client's name, otherwise it'll
86 # name the file that.
87 #
88 # <input type="image" name="photo">
89 # example: save_uploaded_image('photo', '/www/example.com/images/');
90 # example: save_uploaded_image('photo', '/www/example.com/images/example.jpg');
91
92 function save_uploaded_file($key, $path) {
93         if(substr($path, -1) == '/') {
94                 $filename = $path . generate_filename($_FILES[$key]['name'], $_FILES[$key]['type']);
95         } else {
96                 $filename = $path;
97         }
98
99         if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $filename)) {
100                 die('file upload failed');
101         }
102 }
103
104
105 # returns new filename with .png extension
106 function gif_to_png($filename, $new_filename = 'just change extension') {
107         if($new_filename == 'just change extension') {
108                 $last_dot = strrpos($filename, '.');
109                 if($last_dot !== false) {
110                         $new_filename = substr($filename, 0, $last_dot);
111                 }
112                 $new_filename = $filename . '.png';
113
114                 $newfilename = $filename;
115                 $filename = substr($filename, 0 -4) . '.png';
116         }
117
118         $convert = '/usr/local/bin/convert';
119         if(!file_exists($convert)) {
120                 $convert = '/usr/bin/convert';
121         }
122         if(!file_exists($convert)) {
123                 $convert = `which convert`;
124         }
125         if(!file_exists($convert)) {
126                 die("can't find imagemagick's 'convert' program");
127         }
128                 
129         $command = "$convert " . escapeshellarg($filename) . ' ' . escapeshellarg($new_filename);
130
131         exec($command, null, $ret);
132         if($ret != 0) {
133                 die("image conversion failed. convert did exit($ret)");
134         }
135         unlink($filename);
136         return $new_filename;
137 }
138
139 # like save_uploaded_file except it converts gifs to pngs.
140 #
141 # FIXME: if destination has an extension, it should convert to that type.
142 function save_uploaded_image($key, $path) {
143         if(substr($path, -1) != '/') {
144                 $filename = save_uploaded_file($key, $path);
145                 if(substr($filename, -4) == '.gif') {
146                         $filename = gif_to_png($filename);
147                 }
148                 return $filename;
149         } else {
150                 return save_file_upload($key, $path);
151         }
152 }
153
154 ?>