1 | <?php |
---|
2 | /** |
---|
3 | * Upload.inc.php |
---|
4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
5 | */ |
---|
6 | |
---|
7 | /** |
---|
8 | * The Upload class provides an interface to deal with http uploaded files. |
---|
9 | * |
---|
10 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
11 | * @requires App.inc.php |
---|
12 | * @version 1.0 |
---|
13 | */ |
---|
14 | |
---|
15 | require_once dirname(__FILE__) . '/App.inc.php'; |
---|
16 | |
---|
17 | class Upload { |
---|
18 | |
---|
19 | // General object parameters. |
---|
20 | var $_params = array( |
---|
21 | 'display_messages' => true, |
---|
22 | 'allow_overwriting' => false, |
---|
23 | ); |
---|
24 | |
---|
25 | // Disk path where new image(s) will be uploaded. |
---|
26 | var $upload_directory_path = null; |
---|
27 | |
---|
28 | // Array of files with errors. |
---|
29 | var $errors = array(); |
---|
30 | |
---|
31 | // Array of acceptable file extensions (lowercase). |
---|
32 | var $valid_file_extensions = array('jpg', 'jpeg', 'gif', 'png', 'pdf', 'txt', 'text', 'html', 'htm'); |
---|
33 | |
---|
34 | // The uploaded files will normally be owned by user 'apache'. Set world-read/write |
---|
35 | // if the website admin needs to read/delete these files. |
---|
36 | var $dest_file_perms = 0600; |
---|
37 | |
---|
38 | // Array of file extensions and corresponding mime-types. |
---|
39 | var $mime_extension_map = array( |
---|
40 | 'Z' => 'application/x-compress', |
---|
41 | 'ai' => 'application/postscript', |
---|
42 | 'aif' => 'audio/x-aiff', |
---|
43 | 'aifc' => 'audio/x-aiff', |
---|
44 | 'aiff' => 'audio/x-aiff', |
---|
45 | 'asc' => 'text/plain', |
---|
46 | 'asf' => 'video/x-ms-asf', |
---|
47 | 'asx' => 'video/x-ms-asf', |
---|
48 | 'au' => 'audio/basic', |
---|
49 | 'avi' => 'video/x-msvideo', |
---|
50 | 'bcpio' => 'application/x-bcpio', |
---|
51 | 'bin' => 'application/octet-stream', |
---|
52 | 'bmp' => 'image/bmp', |
---|
53 | 'cdf' => 'application/x-netcdf', |
---|
54 | 'class' => 'application/octet-stream', |
---|
55 | 'cpio' => 'application/x-cpio', |
---|
56 | 'cpt' => 'application/mac-compactpro', |
---|
57 | 'csh' => 'application/x-csh', |
---|
58 | 'css' => 'text/css', |
---|
59 | 'dcr' => 'application/x-director', |
---|
60 | 'diff' => 'text/diff', |
---|
61 | 'dir' => 'application/x-director', |
---|
62 | 'dll' => 'application/octet-stream', |
---|
63 | 'dms' => 'application/octet-stream', |
---|
64 | 'doc' => 'application/msword', |
---|
65 | 'dvi' => 'application/x-dvi', |
---|
66 | 'dxr' => 'application/x-director', |
---|
67 | 'eps' => 'application/postscript', |
---|
68 | 'etx' => 'text/x-setext', |
---|
69 | 'exe' => 'application/octet-stream', |
---|
70 | 'ez' => 'application/andrew-inset', |
---|
71 | 'gif' => 'image/gif', |
---|
72 | 'gtar' => 'application/x-gtar', |
---|
73 | 'gz' => 'application/x-gzip', |
---|
74 | 'hdf' => 'application/x-hdf', |
---|
75 | 'hqx' => 'application/mac-binhex40', |
---|
76 | 'htm' => 'text/html', |
---|
77 | 'html' => 'text/html', |
---|
78 | 'ice' => 'x-conference/x-cooltalk', |
---|
79 | 'ics' => 'text/calendar', |
---|
80 | 'ief' => 'image/ief', |
---|
81 | 'ifb' => 'text/calendar', |
---|
82 | 'iges' => 'model/iges', |
---|
83 | 'igs' => 'model/iges', |
---|
84 | 'jpe' => 'image/jpeg', |
---|
85 | 'jpeg' => 'image/jpeg', |
---|
86 | 'jpg' => 'image/jpeg', |
---|
87 | 'js' => 'application/x-javascript', |
---|
88 | 'kar' => 'audio/midi', |
---|
89 | 'latex' => 'application/x-latex', |
---|
90 | 'lha' => 'application/octet-stream', |
---|
91 | 'log' => 'text/plain', |
---|
92 | 'lzh' => 'application/octet-stream', |
---|
93 | 'm3u' => 'audio/x-mpegurl', |
---|
94 | 'man' => 'application/x-troff-man', |
---|
95 | 'me' => 'application/x-troff-me', |
---|
96 | 'mesh' => 'model/mesh', |
---|
97 | 'mid' => 'audio/midi', |
---|
98 | 'midi' => 'audio/midi', |
---|
99 | 'mif' => 'application/vnd.mif', |
---|
100 | 'mov' => 'video/quicktime', |
---|
101 | 'movie' => 'video/x-sgi-movie', |
---|
102 | 'mp2' => 'audio/mpeg', |
---|
103 | 'mp3' => 'audio/mpeg', |
---|
104 | 'mpe' => 'video/mpeg', |
---|
105 | 'mpeg' => 'video/mpeg', |
---|
106 | 'mpg' => 'video/mpeg', |
---|
107 | 'mpga' => 'audio/mpeg', |
---|
108 | 'ms' => 'application/x-troff-ms', |
---|
109 | 'msh' => 'model/mesh', |
---|
110 | 'mxu' => 'video/vnd.mpegurl', |
---|
111 | 'nc' => 'application/x-netcdf', |
---|
112 | 'oda' => 'application/oda', |
---|
113 | 'patch' => 'text/diff', |
---|
114 | 'pbm' => 'image/x-portable-bitmap', |
---|
115 | 'pdb' => 'chemical/x-pdb', |
---|
116 | 'pdf' => 'application/pdf', |
---|
117 | 'pgm' => 'image/x-portable-graymap', |
---|
118 | 'pgn' => 'application/x-chess-pgn', |
---|
119 | 'php' => 'application/x-httpd-php', |
---|
120 | 'php3' => 'application/x-httpd-php3', |
---|
121 | 'pl' => 'application/x-perl', |
---|
122 | 'pm' => 'application/x-perl', |
---|
123 | 'png' => 'image/png', |
---|
124 | 'pnm' => 'image/x-portable-anymap', |
---|
125 | 'po' => 'text/plain', |
---|
126 | 'ppm' => 'image/x-portable-pixmap', |
---|
127 | 'ppt' => 'application/vnd.ms-powerpoint', |
---|
128 | 'ps' => 'application/postscript', |
---|
129 | 'qt' => 'video/quicktime', |
---|
130 | 'ra' => 'audio/x-realaudio', |
---|
131 | 'ram' => 'audio/x-pn-realaudio', |
---|
132 | 'ras' => 'image/x-cmu-raster', |
---|
133 | 'rgb' => 'image/x-rgb', |
---|
134 | 'rm' => 'audio/x-pn-realaudio', |
---|
135 | 'roff' => 'application/x-troff', |
---|
136 | 'rpm' => 'audio/x-pn-realaudio-plugin', |
---|
137 | 'rtf' => 'text/rtf', |
---|
138 | 'rtx' => 'text/richtext', |
---|
139 | 'sgm' => 'text/sgml', |
---|
140 | 'sgml' => 'text/sgml', |
---|
141 | 'sh' => 'application/x-sh', |
---|
142 | 'shar' => 'application/x-shar', |
---|
143 | 'shtml' => 'text/html', |
---|
144 | 'silo' => 'model/mesh', |
---|
145 | 'sit' => 'application/x-stuffit', |
---|
146 | 'skd' => 'application/x-koan', |
---|
147 | 'skm' => 'application/x-koan', |
---|
148 | 'skp' => 'application/x-koan', |
---|
149 | 'skt' => 'application/x-koan', |
---|
150 | 'smi' => 'application/smil', |
---|
151 | 'smil' => 'application/smil', |
---|
152 | 'snd' => 'audio/basic', |
---|
153 | 'so' => 'application/octet-stream', |
---|
154 | 'spl' => 'application/x-futuresplash', |
---|
155 | 'src' => 'application/x-wais-source', |
---|
156 | 'stc' => 'application/vnd.sun.xml.calc.template', |
---|
157 | 'std' => 'application/vnd.sun.xml.draw.template', |
---|
158 | 'sti' => 'application/vnd.sun.xml.impress.template', |
---|
159 | 'stw' => 'application/vnd.sun.xml.writer.template', |
---|
160 | 'sv4cpio' => 'application/x-sv4cpio', |
---|
161 | 'sv4crc' => 'application/x-sv4crc', |
---|
162 | 'swf' => 'application/x-shockwave-flash', |
---|
163 | 'sxc' => 'application/vnd.sun.xml.calc', |
---|
164 | 'sxd' => 'application/vnd.sun.xml.draw', |
---|
165 | 'sxg' => 'application/vnd.sun.xml.writer.global', |
---|
166 | 'sxi' => 'application/vnd.sun.xml.impress', |
---|
167 | 'sxm' => 'application/vnd.sun.xml.math', |
---|
168 | 'sxw' => 'application/vnd.sun.xml.writer', |
---|
169 | 't' => 'application/x-troff', |
---|
170 | 'tar' => 'application/x-tar', |
---|
171 | 'tcl' => 'application/x-tcl', |
---|
172 | 'tex' => 'application/x-tex', |
---|
173 | 'texi' => 'application/x-texinfo', |
---|
174 | 'texinfo' => 'application/x-texinfo', |
---|
175 | 'tgz' => 'application/x-gtar', |
---|
176 | 'tif' => 'image/tiff', |
---|
177 | 'tiff' => 'image/tiff', |
---|
178 | 'tr' => 'application/x-troff', |
---|
179 | 'tsv' => 'text/tab-separated-values', |
---|
180 | 'txt' => 'text/plain', |
---|
181 | 'ustar' => 'application/x-ustar', |
---|
182 | 'vcd' => 'application/x-cdlink', |
---|
183 | 'vcf' => 'text/x-vcard', |
---|
184 | 'vcs' => 'text/calendar', |
---|
185 | 'vfb' => 'text/calendar', |
---|
186 | 'vrml' => 'model/vrml', |
---|
187 | 'vsd' => 'application/vnd.visio', |
---|
188 | 'wav' => 'audio/x-wav', |
---|
189 | 'wax' => 'audio/x-ms-wax', |
---|
190 | 'wbmp' => 'image/vnd.wap.wbmp', |
---|
191 | 'wbxml' => 'application/vnd.wap.wbxml', |
---|
192 | 'wm' => 'video/x-ms-wm', |
---|
193 | 'wma' => 'audio/x-ms-wma', |
---|
194 | 'wmd' => 'application/x-ms-wmd', |
---|
195 | 'wml' => 'text/vnd.wap.wml', |
---|
196 | 'wmlc' => 'application/vnd.wap.wmlc', |
---|
197 | 'wmls' => 'text/vnd.wap.wmlscript', |
---|
198 | 'wmlsc' => 'application/vnd.wap.wmlscriptc', |
---|
199 | 'wmv' => 'video/x-ms-wmv', |
---|
200 | 'wmx' => 'video/x-ms-wmx', |
---|
201 | 'wmz' => 'application/x-ms-wmz', |
---|
202 | 'wrl' => 'model/vrml', |
---|
203 | 'wvx' => 'video/x-ms-wvx', |
---|
204 | 'xbm' => 'image/x-xbitmap', |
---|
205 | 'xht' => 'application/xhtml+xml', |
---|
206 | 'xhtml' => 'application/xhtml+xml', |
---|
207 | 'xls' => 'application/vnd.ms-excel', |
---|
208 | 'xml' => 'application/xml', |
---|
209 | 'xpm' => 'image/x-xpixmap', |
---|
210 | 'xsl' => 'text/xml', |
---|
211 | 'xwd' => 'image/x-xwindowdump', |
---|
212 | 'xyz' => 'chemical/x-xyz', |
---|
213 | 'zip' => 'application/zip' |
---|
214 | ); |
---|
215 | |
---|
216 | /** |
---|
217 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
218 | * |
---|
219 | * @access public |
---|
220 | * @param array $params Array of parameters (key => val pairs). |
---|
221 | */ |
---|
222 | function setParam($params) |
---|
223 | { |
---|
224 | if (isset($params) && is_array($params)) { |
---|
225 | // Merge new parameters with old overriding only those passed. |
---|
226 | $this->_params = array_merge($this->_params, $params); |
---|
227 | } else { |
---|
228 | App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * Return the value of a parameter, if it exists. |
---|
234 | * |
---|
235 | * @access public |
---|
236 | * @param string $param Which parameter to return. |
---|
237 | * @return mixed Configured parameter value. |
---|
238 | */ |
---|
239 | function getParam($param) |
---|
240 | { |
---|
241 | if (isset($this->_params[$param])) { |
---|
242 | return $this->_params[$param]; |
---|
243 | } else { |
---|
244 | App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
245 | return null; |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | /** |
---|
250 | * |
---|
251 | */ |
---|
252 | function setUploadPath($path) |
---|
253 | { |
---|
254 | $path = realpath($path); |
---|
255 | |
---|
256 | if (!is_dir($path)) { |
---|
257 | App::logMsg(sprintf('Upload directory invalid: %s', $path), LOG_ERR, __FILE__, __LINE__); |
---|
258 | } |
---|
259 | if (!is_writable($path)) { |
---|
260 | App::logMsg(sprintf('Upload directory not writable: %s', $path), LOG_ERR, __FILE__, __LINE__); |
---|
261 | } |
---|
262 | |
---|
263 | // Set the default upload path, stripping any extra slashes if needed. |
---|
264 | $this->upload_directory_path = preg_replace('!/+$!', '', $path); |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * |
---|
269 | */ |
---|
270 | function process($form_name, $custom_file_name=null) |
---|
271 | { |
---|
272 | // Ensure we have a upload directory. |
---|
273 | if (!isset($this->upload_directory_path)) { |
---|
274 | App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
275 | $this->raiseMsg(_("There was a problem with the file upload. Please try again later."), MSG_ERR, __FILE__, __LINE__); |
---|
276 | return false; |
---|
277 | } |
---|
278 | |
---|
279 | if (!isset($_FILES[$form_name])) { |
---|
280 | App::logMsg(sprintf(_("Form element %s not posted."), $form_name), LOG_ERR, __FILE__, __LINE__); |
---|
281 | $this->raiseMsg(_("There was a problem with the file upload. Please try again."), MSG_ERR, __FILE__, __LINE__); |
---|
282 | return false; |
---|
283 | } |
---|
284 | |
---|
285 | if (is_array($_FILES[$form_name]['name'])) { |
---|
286 | $files = $_FILES[$form_name]; |
---|
287 | } else { |
---|
288 | // Convert variables to single-cell array so it will loop. |
---|
289 | $files = array( |
---|
290 | 'name' => array($_FILES[$form_name]['name']), |
---|
291 | 'type' => array($_FILES[$form_name]['type']), |
---|
292 | 'tmp_name' => array($_FILES[$form_name]['tmp_name']), |
---|
293 | 'error' => array($_FILES[$form_name]['error']), |
---|
294 | 'size' => array($_FILES[$form_name]['size']), |
---|
295 | ); |
---|
296 | } |
---|
297 | |
---|
298 | // To keep this script running even if user tries to stop browser. |
---|
299 | ignore_user_abort(true); |
---|
300 | if (!ini_get('safe_mode')) { |
---|
301 | set_time_limit(300); |
---|
302 | } |
---|
303 | |
---|
304 | $new_file_names = array(); |
---|
305 | |
---|
306 | $num = sizeof($files['name']); |
---|
307 | for ($i=0; $i<$num; $i++) { |
---|
308 | $file_path_name = ''; |
---|
309 | |
---|
310 | if ('' == trim($files['tmp_name'][$i])) { |
---|
311 | // User may not have attached a file. |
---|
312 | continue; |
---|
313 | } |
---|
314 | |
---|
315 | // Check The php upload error messages. |
---|
316 | if (UPLOAD_ERR_INI_SIZE === $files['error'][$i]) { |
---|
317 | if ($this->getParam('display_messages')) { |
---|
318 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it exceeds the maximum allowed upload file size of %s."), $files['name'][$i], ini_get('upload_max_filesize')), MSG_ERR, __FILE__, __LINE__); |
---|
319 | } |
---|
320 | App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_INI_SIZE (currently %s)."), $files['error'][$i], $files['name'][$i], ini_get('upload_max_filesize')), LOG_ERR, __FILE__, __LINE__); |
---|
321 | $this->errors[] = $files['name'][$i]; |
---|
322 | continue; |
---|
323 | } |
---|
324 | if (UPLOAD_ERR_FORM_SIZE === $files['error'][$i]) { |
---|
325 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it exceeds the maximum allowed upload file size of %s."), $files['name'][$i], $_POST['MAX_FILE_SIZE']), MSG_ERR, __FILE__, __LINE__); |
---|
326 | App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_FORM_SIZE (currently %s)."), $files['error'][$i], $files['name'][$i], $_POST['MAX_FILE_SIZE']), LOG_ERR, __FILE__, __LINE__); |
---|
327 | $this->errors[] = $files['name'][$i]; |
---|
328 | continue; |
---|
329 | } |
---|
330 | if (UPLOAD_ERR_PARTIAL === $files['error'][$i]) { |
---|
331 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it was only partially uploaded."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
332 | App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_PARTIAL."), $files['error'][$i], $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); |
---|
333 | $this->errors[] = $files['name'][$i]; |
---|
334 | continue; |
---|
335 | } |
---|
336 | if (UPLOAD_ERR_NO_FILE === $files['error'][$i]) { |
---|
337 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: no file was uploaded."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
338 | App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_NO_FILE."), $files['error'][$i], $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); |
---|
339 | $this->errors[] = $files['name'][$i]; |
---|
340 | continue; |
---|
341 | } |
---|
342 | if (UPLOAD_ERR_NO_TMP_DIR === $files['error'][$i]) { |
---|
343 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: temporary upload directory missing."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
344 | App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_NO_TMP_DIR."), $files['error'][$i], $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); |
---|
345 | $this->errors[] = $files['name'][$i]; |
---|
346 | continue; |
---|
347 | } |
---|
348 | |
---|
349 | // Check to be sure it's an uploaded file. |
---|
350 | if (!is_uploaded_file($files['tmp_name'][$i])) { |
---|
351 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
352 | App::logMsg(sprintf(_("The file %s failed is_uploaded_file."), $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); |
---|
353 | $this->errors[] = $files['name'][$i]; |
---|
354 | continue; |
---|
355 | } |
---|
356 | |
---|
357 | // Check to be sure the file is not empty. |
---|
358 | if ($files['size'][$i] < 1) { |
---|
359 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it contains zero bytes."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
360 | App::logMsg(sprintf(_("The uploaded file %s contains zero bytes."), $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); |
---|
361 | $this->errors[] = $files['name'][$i]; |
---|
362 | continue; |
---|
363 | } |
---|
364 | |
---|
365 | // Check to be sure the file has a valid file extension. |
---|
366 | if (!in_array(strtolower($this->getFilenameExtension($files['name'][$i])), $this->valid_file_extensions)) { |
---|
367 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it is an unrecognized type. Files must have one of the following file extensions: %s."), $files['name'][$i], join(', ', $this->valid_file_extensions)), MSG_ERR, __FILE__, __LINE__); |
---|
368 | App::logMsg(sprintf(_("The uploaded file %s has an unrecognized file extension."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__); |
---|
369 | $this->errors[] = $files['name'][$i]; |
---|
370 | continue; |
---|
371 | } |
---|
372 | |
---|
373 | // Check to be sure the file has a unique file name. |
---|
374 | if (!$this->getParam('allow_overwriting') && $this->exists($files['name'][$i])) { |
---|
375 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: a file with that name already exists."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
376 | App::logMsg(sprintf(_("The uploaded file %s doesn't have a unique filename."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__); |
---|
377 | $this->errors[] = $files['name'][$i]; |
---|
378 | continue; |
---|
379 | } |
---|
380 | |
---|
381 | // Determine file name. |
---|
382 | if ($num == 1) { |
---|
383 | // Single upload. |
---|
384 | if (isset($custom_file_name) && '' != $custom_file_name) { |
---|
385 | // Valid custom file name. |
---|
386 | $file_name = $custom_file_name; |
---|
387 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> has been renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__); |
---|
388 | App::logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
389 | } else { |
---|
390 | // Invalid custom file name provided. Use uploaded file name. |
---|
391 | $file_name = $files['name'][$i]; |
---|
392 | App::logMsg(sprintf('Using uploaded file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
393 | } |
---|
394 | } else { |
---|
395 | // Multiple upload. Final file names must be array. |
---|
396 | if (isset($custom_file_name) && is_array($custom_file_name) && '' != $custom_file_name[$i]) { |
---|
397 | // Valid custom file name. |
---|
398 | $file_name = $custom_file_name[$i]; |
---|
399 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> has been renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__); |
---|
400 | App::logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
401 | } else { |
---|
402 | // Invalid custom file name provided. Use uploaded file name. |
---|
403 | $file_name = $files['name'][$i]; |
---|
404 | App::logMsg(sprintf('Using uploaded file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | // Clean the file name of bad characters. |
---|
409 | $file_name = $this->cleanFileName($file_name); |
---|
410 | |
---|
411 | // If the file name has no extension, use the mime-type extension. |
---|
412 | if (!preg_match('/\.[^.]{1,5}$/', $file_name) && function_exists('mime_content_type')) { |
---|
413 | if ($ext = array_search(mime_content_type($files['type'][$i]), $this->mime_extension_map)) { |
---|
414 | $file_name .= ".$ext"; |
---|
415 | } |
---|
416 | } |
---|
417 | |
---|
418 | // Set the path and file name. |
---|
419 | $file_path_name = $this->upload_directory_path . '/' . $file_name; |
---|
420 | |
---|
421 | // Move the file to the final place. |
---|
422 | if (move_uploaded_file($files['tmp_name'][$i], $file_path_name)) { |
---|
423 | chmod($file_path_name, $this->dest_file_perms); |
---|
424 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> uploaded successfully."), $files['name'][$i]), MSG_SUCCESS, __FILE__, __LINE__); |
---|
425 | if (!isset($custom_file_name) && $files['name'][$i] != $file_name) { |
---|
426 | // Notify user if uploaded file name was modified (unless a custom file name will be used anyways). |
---|
427 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> was renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__); |
---|
428 | } |
---|
429 | $new_file_names[] = array( |
---|
430 | 'name' => $file_name, |
---|
431 | 'size' => filesize($file_path_name), |
---|
432 | 'extension' => strtolower(substr($file_name, strrpos($file_name, '.') + 1)), |
---|
433 | ); |
---|
434 | continue; |
---|
435 | } else { |
---|
436 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
437 | App::logMsg(sprintf(_("Moving file failed: %s -> %s"), $files['tmp_name'][$i], $file_path_name), LOG_ALERT, __FILE__, __LINE__); |
---|
438 | $this->errors[] = $files['name'][$i]; |
---|
439 | continue; |
---|
440 | } |
---|
441 | } |
---|
442 | return (sizeof($new_file_names) > 0) ? $new_file_names : false; |
---|
443 | } |
---|
444 | |
---|
445 | /** |
---|
446 | * |
---|
447 | */ |
---|
448 | function deleteFile($file_name) |
---|
449 | { |
---|
450 | // Ensure we have a upload directory. |
---|
451 | if (!isset($this->upload_directory_path)) { |
---|
452 | App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
453 | return false; |
---|
454 | } |
---|
455 | |
---|
456 | $file_path_name = $this->upload_directory_path . '/' . $file_name; |
---|
457 | |
---|
458 | if (!is_file($file_path_name)) { |
---|
459 | App::logMsg(sprintf(_("Error deleting nonexistent file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__); |
---|
460 | return false; |
---|
461 | } else if (unlink($file_path_name)) { |
---|
462 | App::logMsg(sprintf('Deleted file: %s', $file_path_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
463 | } else { |
---|
464 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> could not be deleted."), $file_name), MSG_ERROR, __FILE__, __LINE__); |
---|
465 | App::logMsg(sprintf(_("Failed deleting file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__); |
---|
466 | return false; |
---|
467 | } |
---|
468 | } |
---|
469 | |
---|
470 | /** |
---|
471 | * |
---|
472 | */ |
---|
473 | function moveFile($old_name, $new_name) |
---|
474 | { |
---|
475 | // Ensure we have an upload directory. |
---|
476 | if (!isset($this->upload_directory_path)) { |
---|
477 | App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
478 | return false; |
---|
479 | } |
---|
480 | |
---|
481 | $old_file_path_name = $this->upload_directory_path . '/' . $old_name; |
---|
482 | $new_file_path_name = $this->upload_directory_path . '/' . $new_name; |
---|
483 | if (file_exists($old_file_path_name)) { |
---|
484 | if (!rename($old_file_path_name, $new_file_path_name)) { |
---|
485 | $this->raiseMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), MSG_ERR, __FILE__, __LINE__); |
---|
486 | App::logMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), LOG_ERR, __FILE__, __LINE__); |
---|
487 | return false; |
---|
488 | } |
---|
489 | } else { |
---|
490 | $this->raiseMsg(sprintf(_("Couldn't rename nonexistent file <strong>%s</strong>."), $old_name), MSG_ERR, __FILE__, __LINE__); |
---|
491 | App::logMsg(sprintf(_("Error renaming nonexistent file: %s"), $old_file_path_name), LOG_ERR, __FILE__, __LINE__); |
---|
492 | return false; |
---|
493 | } |
---|
494 | } |
---|
495 | |
---|
496 | /** |
---|
497 | * |
---|
498 | */ |
---|
499 | function exists($file_name) |
---|
500 | { |
---|
501 | // Ensure we have a upload directory. |
---|
502 | if (!isset($this->upload_directory_path)) { |
---|
503 | App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
504 | return false; |
---|
505 | } |
---|
506 | |
---|
507 | return file_exists($this->upload_directory_path . '/' . $file_name); |
---|
508 | } |
---|
509 | |
---|
510 | /** |
---|
511 | * |
---|
512 | */ |
---|
513 | function getErrors() |
---|
514 | { |
---|
515 | return $this->errors; |
---|
516 | } |
---|
517 | |
---|
518 | /** |
---|
519 | * |
---|
520 | */ |
---|
521 | function cleanFileName($file_name) |
---|
522 | { |
---|
523 | $bad = 'áéíóúàèìòùäëïöüÁÉÍÓÚÀÈÌÒÙÄËÏÖÜâêîôûÂÊÎÔÛñçÇ@'; |
---|
524 | $good = 'aeiouaeiouaeiouAEIOUAEIOUAEIOUaeiouAEIOUncCa'; |
---|
525 | $file_name = trim($file_name); |
---|
526 | $file_name = strtr($file_name, $bad, $good); |
---|
527 | $file_name = preg_replace('/[^-\w.,~_=+()]/i', '_', $file_name); |
---|
528 | $file_name = substr($file_name, 0, 250); |
---|
529 | return $file_name; |
---|
530 | } |
---|
531 | |
---|
532 | /** |
---|
533 | * |
---|
534 | */ |
---|
535 | function getFilenameExtension($file_name) |
---|
536 | { |
---|
537 | preg_match('/.*?\.(\w+)$/i', $file_name, $ext); |
---|
538 | return $ext[1]; |
---|
539 | } |
---|
540 | |
---|
541 | /** |
---|
542 | * An alias for App::raiseMsg that only sends messages if display_messages is true. |
---|
543 | * |
---|
544 | * @access public |
---|
545 | * |
---|
546 | * @param string $message The text description of the message. |
---|
547 | * @param int $type The type of message: MSG_NOTICE, |
---|
548 | * MSG_SUCCESS, MSG_WARNING, or MSG_ERR. |
---|
549 | * @param string $file __FILE__. |
---|
550 | * @param string $line __LINE__. |
---|
551 | */ |
---|
552 | function raiseMsg($message, $type, $file, $line) |
---|
553 | { |
---|
554 | if ($this->getParam('display_messages')) { |
---|
555 | App::raiseMsg($message, $type, $file, $line); |
---|
556 | } |
---|
557 | } |
---|
558 | } |
---|
559 | |
---|
560 | ?> |
---|