source: branches/1.1dev/lib/Upload.inc.php @ 648

Last change on this file since 648 was 648, checked in by anonymous, 6 years ago

Update hash-bang references. Minor.

File size: 25.7 KB
Line 
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
15require_once dirname(__FILE__) . '/App.inc.php';
16
17class Upload {
18
19    // Display error messages to the user?
20    var $display_messages = true;
21
22    // Existing files with the same name will be overwritten?
23    var $allow_overwriting = false;
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 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        'mp4'     => 'video/mp4',
108        'mpga'    => 'audio/mpeg',
109        'ms'      => 'application/x-troff-ms',
110        'msh'     => 'model/mesh',
111        'mxu'     => 'video/vnd.mpegurl',
112        'nc'      => 'application/x-netcdf',
113        'oda'     => 'application/oda',
114        'patch'   => 'text/diff',
115        'pbm'     => 'image/x-portable-bitmap',
116        'pdb'     => 'chemical/x-pdb',
117        'pdf'     => 'application/pdf',
118        'pgm'     => 'image/x-portable-graymap',
119        'pgn'     => 'application/x-chess-pgn',
120        'php'     => 'application/x-httpd-php',
121        'php3'    => 'application/x-httpd-php3',
122        'pl'      => 'application/x-perl',
123        'pm'      => 'application/x-perl',
124        'png'     => 'image/png',
125        'pnm'     => 'image/x-portable-anymap',
126        'po'      => 'text/plain',
127        'ppm'     => 'image/x-portable-pixmap',
128        'ppt'     => 'application/vnd.ms-powerpoint',
129        'ps'      => 'application/postscript',
130        'qt'      => 'video/quicktime',
131        'ra'      => 'audio/x-realaudio',
132        'ram'     => 'audio/x-pn-realaudio',
133        'ras'     => 'image/x-cmu-raster',
134        'rgb'     => 'image/x-rgb',
135        'rm'      => 'audio/x-pn-realaudio',
136        'roff'    => 'application/x-troff',
137        'rpm'     => 'audio/x-pn-realaudio-plugin',
138        'rtf'     => 'text/rtf',
139        'rtx'     => 'text/richtext',
140        'sgm'     => 'text/sgml',
141        'sgml'    => 'text/sgml',
142        'sh'      => 'application/x-sh',
143        'shar'    => 'application/x-shar',
144        'shtml'   => 'text/html',
145        'silo'    => 'model/mesh',
146        'sit'     => 'application/x-stuffit',
147        'skd'     => 'application/x-koan',
148        'skm'     => 'application/x-koan',
149        'skp'     => 'application/x-koan',
150        'skt'     => 'application/x-koan',
151        'smi'     => 'application/smil',
152        'smil'    => 'application/smil',
153        'snd'     => 'audio/basic',
154        'so'      => 'application/octet-stream',
155        'spl'     => 'application/x-futuresplash',
156        'src'     => 'application/x-wais-source',
157        'stc'     => 'application/vnd.sun.xml.calc.template',
158        'std'     => 'application/vnd.sun.xml.draw.template',
159        'sti'     => 'application/vnd.sun.xml.impress.template',
160        'stw'     => 'application/vnd.sun.xml.writer.template',
161        'sv4cpio' => 'application/x-sv4cpio',
162        'sv4crc'  => 'application/x-sv4crc',
163        'swf'     => 'application/x-shockwave-flash',
164        'sxc'     => 'application/vnd.sun.xml.calc',
165        'sxd'     => 'application/vnd.sun.xml.draw',
166        'sxg'     => 'application/vnd.sun.xml.writer.global',
167        'sxi'     => 'application/vnd.sun.xml.impress',
168        'sxm'     => 'application/vnd.sun.xml.math',
169        'sxw'     => 'application/vnd.sun.xml.writer',
170        't'       => 'application/x-troff',
171        'tar'     => 'application/x-tar',
172        'tcl'     => 'application/x-tcl',
173        'tex'     => 'application/x-tex',
174        'texi'    => 'application/x-texinfo',
175        'texinfo' => 'application/x-texinfo',
176        'tgz'     => 'application/x-gtar',
177        'tif'     => 'image/tiff',
178        'tiff'    => 'image/tiff',
179        'tr'      => 'application/x-troff',
180        'tsv'     => 'text/tab-separated-values',
181        'txt'     => 'text/plain',
182        'ustar'   => 'application/x-ustar',
183        'vcd'     => 'application/x-cdlink',
184        'vcf'     => 'text/x-vcard',
185        'vcs'     => 'text/calendar',
186        'vfb'     => 'text/calendar',
187        'vrml'    => 'model/vrml',
188        'vsd'     => 'application/vnd.visio',
189        'wav'     => 'audio/x-wav',
190        'wax'     => 'audio/x-ms-wax',
191        'wbmp'    => 'image/vnd.wap.wbmp',
192        'wbxml'   => 'application/vnd.wap.wbxml',
193        'wm'      => 'video/x-ms-wm',
194        'wma'     => 'audio/x-ms-wma',
195        'wmd'     => 'application/x-ms-wmd',
196        'wml'     => 'text/vnd.wap.wml',
197        'wmlc'    => 'application/vnd.wap.wmlc',
198        'wmls'    => 'text/vnd.wap.wmlscript',
199        'wmlsc'   => 'application/vnd.wap.wmlscriptc',
200        'wmv'     => 'video/x-ms-wmv',
201        'wmx'     => 'video/x-ms-wmx',
202        'wmz'     => 'application/x-ms-wmz',
203        'wrl'     => 'model/vrml',
204        'wvx'     => 'video/x-ms-wvx',
205        'xbm'     => 'image/x-xbitmap',
206        'xht'     => 'application/xhtml+xml',
207        'xhtml'   => 'application/xhtml+xml',
208        'xls'     => 'application/vnd.ms-excel',
209        'xml'     => 'application/xml',
210        'xpm'     => 'image/x-xpixmap',
211        'xsl'     => 'text/xml',
212        'xwd'     => 'image/x-xwindowdump',
213        'xyz'     => 'chemical/x-xyz',
214        'zip'     => 'application/zip'
215    );
216
217    /**
218     *
219     */
220    function setUploadPath($path)
221    {
222        $path = realpath($path);
223
224        if (!is_dir($path)) {
225            logMsg(sprintf('Upload directory invalid: %s', $path), LOG_ERR, __FILE__, __LINE__);
226        }
227        if (!is_writable($path)) {
228            logMsg(sprintf('Upload directory not writable: %s', $path), LOG_ERR, __FILE__, __LINE__);
229        }
230
231        // Set the default upload path, stripping any extra slashes if needed.
232        $this->upload_directory_path = preg_replace('!/+$!', '', $path);
233    }
234
235    /**
236     *
237     */
238    function process($form_name, $custom_file_name=null)
239    {
240        // Ensure we have a upload directory.
241        if (!isset($this->upload_directory_path)) {
242            logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
243            if ($this->display_messages) {
244                raiseMsg(_("There was a problem with the file upload. Please try again later."), MSG_ERR, __FILE__, __LINE__);
245            }
246            return false;
247        }
248
249        if (!isset($_FILES[$form_name])) {
250            logMsg(sprintf(_("Form element %s not posted."), $form_name), LOG_ERR, __FILE__, __LINE__);
251            if ($this->display_messages) {
252                raiseMsg(_("There was a problem with the file upload. Please try again."), MSG_ERR, __FILE__, __LINE__);
253            }
254            return false;
255        }
256
257        if (is_array($_FILES[$form_name]['name'])) {
258            $files = $_FILES[$form_name];
259        } else {
260            // Convert variables to single-cell array so it will loop.
261            $files = array(
262                'name'      => array($_FILES[$form_name]['name']),
263                'type'      => array($_FILES[$form_name]['type']),
264                'tmp_name'  => array($_FILES[$form_name]['tmp_name']),
265                'error'     => array($_FILES[$form_name]['error']),
266                'size'      => array($_FILES[$form_name]['size']),
267            );
268        }
269
270        // To keep this script running even if user tries to stop browser.
271        ignore_user_abort(true);
272        if (!ini_get('safe_mode')) {
273            set_time_limit(300);
274        }
275
276        $new_file_names = array();
277
278        $num = sizeof($files['name']);
279        for ($i=0; $i<$num; $i++) {
280            $file_path_name = '';
281
282            if ('' == trim($files['name'][$i])) {
283                // User may not have attached a file.
284                continue;
285            }
286
287            // Check The php upload error messages.
288            if (UPLOAD_ERR_INI_SIZE === $files['error'][$i]) {
289                if ($this->display_messages) {
290                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it exceeds the maximum allowed upload filesize of %s."), $files['name'][$i], ini_get('upload_max_filesize')), MSG_ERR, __FILE__, __LINE__);
291                }
292                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__);
293                $this->errors[] = $files['name'][$i];
294                continue;
295            }
296            if (UPLOAD_ERR_FORM_SIZE === $files['error'][$i]) {
297                if ($this->display_messages) {
298                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it exceeds the maximum allowed upload filesize of %s."), $files['name'][$i], $_POST['MAX_FILE_SIZE']), MSG_ERR, __FILE__, __LINE__);
299                }
300                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__);
301                $this->errors[] = $files['name'][$i];
302                continue;
303            }
304            if (UPLOAD_ERR_PARTIAL === $files['error'][$i]) {
305                if ($this->display_messages) {
306                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it was only partially uploaded."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
307                }
308                logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_PARTIAL."), $files['error'][$i], $files['name'][$i]), LOG_ERR, __FILE__, __LINE__);
309                $this->errors[] = $files['name'][$i];
310                continue;
311            }
312            if (UPLOAD_ERR_NO_FILE === $files['error'][$i]) {
313                if ($this->display_messages) {
314                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: no file was uploaded."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
315                }
316                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__);
317                $this->errors[] = $files['name'][$i];
318                continue;
319            }
320            if (UPLOAD_ERR_NO_TMP_DIR === $files['error'][$i]) {
321                if ($this->display_messages) {
322                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: temporary upload directory missing."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
323                }
324                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__);
325                $this->errors[] = $files['name'][$i];
326                continue;
327            }
328
329            // Check to be sure it's an uploaded file.
330            if (!is_uploaded_file($files['tmp_name'][$i])) {
331                if ($this->display_messages) {
332                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
333                }
334                logMsg(sprintf(_("The file %s failed is_uploaded_file."), $files['name'][$i]), LOG_ERR, __FILE__, __LINE__);
335                $this->errors[] = $files['name'][$i];
336                continue;
337            }
338
339            // Check to be sure the file is not empty.
340            if ($files['size'][$i] < 1) {
341                if ($this->display_messages) {
342                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it contains zero bytes."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
343                }
344                logMsg(sprintf(_("The uploaded file %s contains zero bytes."), $files['name'][$i]), LOG_ERR, __FILE__, __LINE__);
345                $this->errors[] = $files['name'][$i];
346                continue;
347            }
348
349            // Check to be sure the file has a valid file extension.
350            if (!in_array(strtolower($this->getFilenameExtension($files['name'][$i])), $this->valid_file_extensions)) {
351                if ($this->display_messages) {
352                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it is an unrecognised type. Files must have one of the following file extensions: %s."), $files['name'][$i], join(', ', $this->valid_file_extensions)), MSG_ERR, __FILE__, __LINE__);
353                }
354                logMsg(sprintf(_("The uploaded file %s has an unrecognised file extension."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__);
355                $this->errors[] = $files['name'][$i];
356                continue;
357            }
358
359            // Check to be sure the file has a unique file name.
360            if (!$this->allow_overwriting && $this->exists($files['name'][$i])) {
361                if ($this->display_messages) {
362                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: a file with that name already exists."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
363                }
364                logMsg(sprintf(_("The uploaded file %s doesn't have a unique filename."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__);
365                $this->errors[] = $files['name'][$i];
366                continue;
367            }
368
369            // Determine file name.
370            if ($num == 1) {
371                // Single upload.
372                if (isset($custom_file_name)) {
373                    if (is_array($custom_file_name) && sizeof($custom_file_name) == 1 && '' != trim($custom_file_name[0])) {
374                        // Is an array, but just one value. Pull it out.
375                        $file_name = trim($custom_file_name[0]);
376                        if ($this->display_messages) {
377                            raiseMsg(sprintf(_("The file %s has been renamed to %s."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__);
378                        }
379                        logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
380                    } else if (!is_array($custom_file_name) && '' != trim($custom_file_name)) {
381                        // Valid custom file name.
382                        $file_name = $custom_file_name;
383                        if ($this->display_messages) {
384                            raiseMsg(sprintf(_("The file %s has been renamed to %s."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__);
385                        }
386                        logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
387                    } else {
388                        // Invalid custom file name provided. Use uploaded file name.
389                        $file_name = $files['name'][$i];
390                        logMsg(sprintf('Custom filename invalid! Using uploaded file name: %s', $file_name), LOG_WARNING, __FILE__, __LINE__);
391                    }
392                } else {
393                    // Normal case. Use uploaded file name.
394                    $file_name = $files['name'][$i];
395                    logMsg(sprintf('Using uploaded file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
396                }
397            } else {
398                // Multiple upload. Final file names must be array.
399                if (isset($custom_file_name) && is_array($custom_file_name) && '' != $custom_file_name[$i]) {
400                    // Valid custom file name.
401                    $file_name = $custom_file_name[$i];
402                    if ($this->display_messages) {
403                        raiseMsg(sprintf(_("The file <strong>%s</strong> has been renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__);
404                    }
405                    logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
406                } else {
407                    // Invalid custom file name provided. Use uploaded file name.
408                    $file_name = $files['name'][$i];
409                    logMsg(sprintf('Using uploaded file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
410                }
411            }
412
413            // Clean the file name of bad characters.
414            $file_name = $this->cleanFileName($file_name);
415
416            // If the file name has no extension, use the mime-type extension.
417            if (!preg_match('/\.[^.]{1,5}$/', $file_name) && function_exists('mime_content_type')) {
418                if ($ext = array_search(mime_content_type($files['type'][$i]), $this->mime_extension_map)) {
419                    $file_name .= ".$ext";
420                }
421            }
422
423            // Set the path and file name.
424            $file_path_name = $this->upload_directory_path . '/' . $file_name;
425
426            // Move the file to the final place.
427            if (move_uploaded_file($files['tmp_name'][$i], $file_path_name)) {
428                chmod($file_path_name, $this->dest_file_perms);
429                if ($this->display_messages) {
430                    raiseMsg(sprintf(_("The file <strong>%s</strong> uploaded successfully."), $files['name'][$i]), MSG_SUCCESS, __FILE__, __LINE__);
431                }
432                if (!isset($custom_file_name) && $files['name'][$i] != $file_name) {
433                    // Notify user if uploaded file name was modified (unless a custom file name will be used anyways).
434                    if ($this->display_messages) {
435                        raiseMsg(sprintf(_("The file <strong>%s</strong> was renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__);
436                        logMsg(sprintf('File successfully renamed: %s -> %s', $files['tmp_name'][$i], $file_path_name), LOG_DEBUG, __FILE__, __LINE__);
437                    }
438                }
439                $new_file_names[] = array(
440                    'name' => $file_name,
441                    'mime' => $files['type'][$i],
442                    'size' => filesize($file_path_name),
443                    'extension' => $this->getFilenameExtension($file_name),
444                    'original_index' => $i,
445                );
446                continue;
447            } else {
448                if ($this->display_messages) {
449                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
450                }
451                logMsg(sprintf(_("Moving file failed: %s -> %s"), $files['tmp_name'][$i], $file_path_name), LOG_ALERT, __FILE__, __LINE__);
452                $this->errors[] = $files['name'][$i];
453                continue;
454            }
455        }
456        return (sizeof($new_file_names) > 0) ? $new_file_names : false;
457    }
458
459    /**
460     *
461     */
462    function deleteFile($file_name)
463    {
464        // Ensure we have a upload directory.
465        if (!isset($this->upload_directory_path)) {
466            logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
467            return false;
468        }
469
470        $file_path_name = $this->upload_directory_path . '/' . $file_name;
471
472        if (!is_file($file_path_name)) {
473            logMsg(sprintf(_("Error deleting nonexistant file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__);
474            return false;
475        } else if (unlink($file_path_name)) {
476            logMsg(sprintf('Deleted file: %s', $file_path_name), LOG_DEBUG, __FILE__, __LINE__);
477        } else {
478            if ($this->display_messages) {
479                raiseMsg(sprintf(_("The file <strong>%s</strong> could not be deleted."), $file_name), MSG_ERROR, __FILE__, __LINE__);
480            }
481            logMsg(sprintf(_("Failed deleting file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__);
482            return false;
483        }
484    }
485
486    /**
487     *
488     */
489    function moveFile($old_name, $new_name)
490    {
491        // Ensure we have an upload directory.
492        if (!isset($this->upload_directory_path)) {
493            logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
494            return false;
495        }
496
497        $old_file_path_name = $this->upload_directory_path . '/' . $old_name;
498        $new_file_path_name = $this->upload_directory_path . '/' . $new_name;
499        if (file_exists($old_file_path_name)) {
500            if (!rename($old_file_path_name, $new_file_path_name)) {
501                if ($this->display_messages) {
502                    raiseMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), MSG_ERR, __FILE__, __LINE__);
503                }
504                logMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), LOG_ERR, __FILE__, __LINE__);
505                return false;
506            }
507        } else {
508            if ($this->display_messages) {
509                raiseMsg(sprintf(_("Couldn't rename nonexistant file <strong>%s</strong>."), $old_name), MSG_ERR, __FILE__, __LINE__);
510            }
511            logMsg(sprintf(_("Error renaming nonexistant file: %s"), $old_file_path_name), LOG_ERR, __FILE__, __LINE__);
512            return false;
513        }
514    }
515
516    /**
517     *
518     */
519    function exists($file_name)
520    {
521        // Ensure we have a upload directory.
522        if (!isset($this->upload_directory_path)) {
523            logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
524            return false;
525        }
526
527        return file_exists($this->upload_directory_path . '/' . $file_name);
528    }
529
530    /**
531     * Get filename by glob pattern. Searches a directory for an image that matches the
532     * specified glob pattern and returns the filename of the first file found.
533     *
534     * @access  public
535     * @param   string  $pattern   Pattern to match filename.
536     * @return  string filename on success, empty string on failure.
537     * @author  Quinn Comendant <quinn@strangecode.com>
538     * @since   15 Nov 2005 20:55:22
539     */
540    function getFilenameGlob($pattern)
541    {
542        $file_list = glob(sprintf('%s/%s', $this->upload_directory_path, $pattern));
543        if (isset($file_list[0])) {
544            return basename($file_list[0]);
545        } else {
546            return '';
547        }
548    }
549
550    /**
551     *
552     */
553    function getErrors()
554    {
555        return $this->errors;
556    }
557
558    /**
559     *
560     */
561    function cleanFileName($file_name)
562    {
563        $bad  = 'áéíóúàÚìòùÀëïöÌÁÉÍÓÚÀÈÌÒÙÄËÏÖÜâêîÎûÂÊÎÔÛñçÇ@';
564        $good = 'aeiouaeiouaeiouAEIOUAEIOUAEIOUaeiouAEIOUncCa';
565        $file_name = trim($file_name);
566        $file_name = strtr($file_name, $bad, $good);
567        $file_name = preg_replace('/[^-\w.,~_=+]+/', '_', $file_name);
568        $file_name = substr($file_name, 0, 250);
569        return $file_name;
570    }
571
572    /**
573     *
574     */
575    function getFilenameExtension($file_name)
576    {
577        preg_match('/.*?\.(\w+)$/i', $file_name, $ext);
578        return $ext[1];
579    }
580}
581
582?>
Note: See TracBrowser for help on using the repository browser.