source: tags/1.0.0/lib/Upload.inc.php @ 1

Last change on this file since 1 was 1, checked in by scdev, 19 years ago

Initial import.

File size: 24.8 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    // 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 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     *
221     * @param  array    $params     Array of parameters (key => val pairs).
222     */
223    function setParam($params=null)
224    {
225        if (isset($params) && is_array($params)) {
226            // Set parameters for this object.
227            $this->_params = array_merge($this->_params, $params);
228        }
229    }
230
231    /**
232     * Return the value of a parameter.
233     *
234     * @access  public
235     *
236     * @param   string  $param      The key of the parameter to return.
237     *
238     * @return  mixed               Parameter value.
239     */
240    function getParam($param)
241    {
242        return $this->_params[$param];
243    }
244
245    /**
246     *
247     */
248    function setUploadPath($path)
249    {
250        $path = realpath($path);
251       
252        if (!is_dir($path)) {
253            logMsg(sprintf('Upload directory invalid: %s', $path), LOG_ERR, __FILE__, __LINE__);
254        }
255        if (!is_writable($path)) {
256            logMsg(sprintf('Upload directory not writable: %s', $path), LOG_ERR, __FILE__, __LINE__);
257        }
258       
259        // Set the default upload path, stripping any extra slashes if needed.
260        $this->upload_directory_path = preg_replace('!/+$!', '', $path);
261    }
262
263    /**
264     *
265     */
266    function process($form_name, $custom_file_name=null)
267    {
268        // Ensure we have a upload directory.
269        if (!isset($this->upload_directory_path)) {
270            logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
271            if ($this->getParam('display_messages')) {
272                raiseMsg(_("There was a problem with the file upload. Please try again later."), MSG_ERR, __FILE__, __LINE__);
273            }
274            return false;
275        }
276       
277        if (!isset($_FILES[$form_name])) {
278            logMsg(sprintf(_("Form element %s not posted."), $form_name), LOG_ERR, __FILE__, __LINE__);
279            if ($this->getParam('display_messages')) {
280                raiseMsg(_("There was a problem with the file upload. Please try again."), MSG_ERR, __FILE__, __LINE__);
281            }
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                    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__);
319                }
320                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                if ($this->getParam('display_messages')) {
326                    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__);
327                }
328                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__);
329                $this->errors[] = $files['name'][$i];
330                continue;
331            }
332            if (UPLOAD_ERR_PARTIAL === $files['error'][$i]) {
333                if ($this->getParam('display_messages')) {
334                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it was only partially uploaded."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
335                }
336                logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_PARTIAL."), $files['error'][$i], $files['name'][$i]), LOG_ERR, __FILE__, __LINE__);
337                $this->errors[] = $files['name'][$i];
338                continue;
339            }
340            if (UPLOAD_ERR_NO_FILE === $files['error'][$i]) {
341                if ($this->getParam('display_messages')) {
342                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: no file was uploaded."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
343                }
344                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__);
345                $this->errors[] = $files['name'][$i];
346                continue;
347            }
348            if (UPLOAD_ERR_NO_TMP_DIR === $files['error'][$i]) {
349                if ($this->getParam('display_messages')) {
350                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: temporary upload directory missing."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
351                }
352                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__);
353                $this->errors[] = $files['name'][$i];
354                continue;
355            }
356           
357            // Check to be sure it's an uploaded file.
358            if (!is_uploaded_file($files['tmp_name'][$i])) {
359                if ($this->getParam('display_messages')) {
360                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
361                }
362                logMsg(sprintf(_("The file %s failed is_uploaded_file."), $files['name'][$i]), LOG_ERR, __FILE__, __LINE__);
363                $this->errors[] = $files['name'][$i];
364                continue;
365            }
366           
367            // Check to be sure the file is not empty.
368            if ($files['size'][$i] < 1) {
369                if ($this->getParam('display_messages')) {
370                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it contains zero bytes."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
371                }
372                logMsg(sprintf(_("The uploaded file %s contains zero bytes."), $files['name'][$i]), LOG_ERR, __FILE__, __LINE__);
373                $this->errors[] = $files['name'][$i];
374                continue;
375            }
376           
377            // Check to be sure the file has a valid file extension.
378            if (!in_array(strtolower($this->getFilenameExtension($files['name'][$i])), $this->valid_file_extensions)) {
379                if ($this->getParam('display_messages')) {
380                    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__);
381                }
382                logMsg(sprintf(_("The uploaded file %s has an unrecognised file extension."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__);
383                $this->errors[] = $files['name'][$i];
384                continue;
385            }
386           
387            // Check to be sure the file has a unique file name.
388            if (!$this->getParam('allow_overwriting') && $this->exists($files['name'][$i])) {
389                if ($this->getParam('display_messages')) {
390                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: a file with that name already exists."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
391                }
392                logMsg(sprintf(_("The uploaded file %s doesn't have a unique filename."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__);
393                $this->errors[] = $files['name'][$i];
394                continue;
395            }
396           
397            // Determine file name.
398            if ($num == 1) {
399                // Single upload.
400                if (isset($custom_file_name) && '' != $custom_file_name) {
401                    // Valid custom file name.
402                    $file_name = $custom_file_name;
403                    if ($this->getParam('display_messages')) {
404                        raiseMsg(sprintf(_("The file <strong>%s</strong> has been renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__);
405                    }
406                    logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
407                } else {
408                    // Invalid custom file name provided. Use uploaded file name.
409                    $file_name = $files['name'][$i];
410                    logMsg(sprintf('Using uploaded file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
411                }
412            } else {
413                // Multiple upload. Final file names must be array.
414                if (isset($custom_file_name) && is_array($custom_file_name) && '' != $custom_file_name[$i]) {
415                    // Valid custom file name.
416                    $file_name = $custom_file_name[$i];
417                    if ($this->getParam('display_messages')) {
418                        raiseMsg(sprintf(_("The file <strong>%s</strong> has been renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__);
419                    }
420                    logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
421                } else {
422                    // Invalid custom file name provided. Use uploaded file name.
423                    $file_name = $files['name'][$i];
424                    logMsg(sprintf('Using uploaded file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
425                }
426            }
427
428            // Clean the file name of bad characters.
429            $file_name = $this->cleanFileName($file_name);
430           
431            // If the file name has no extension, use the mime-type extension.
432            if (!preg_match('/\.[^.]{1,5}$/', $file_name) && function_exists('mime_content_type')) {
433                if ($ext = array_search(mime_content_type($files['type'][$i]), $this->mime_extension_map)) {
434                    $file_name .= ".$ext";
435                }
436            }
437           
438            // Set the path and file name.
439            $file_path_name = $this->upload_directory_path . '/' . $file_name;
440           
441            // Move the file to the final place.
442            if (move_uploaded_file($files['tmp_name'][$i], $file_path_name)) {
443                chmod($file_path_name, $this->dest_file_perms);
444                if ($this->getParam('display_messages')) {
445                    raiseMsg(sprintf(_("The file <strong>%s</strong> uploaded successfully."), $files['name'][$i]), MSG_SUCCESS, __FILE__, __LINE__);
446                }
447                if (!isset($custom_file_name) && $files['name'][$i] != $file_name) {
448                    // Notify user if uploaded file name was modified (unless a custom file name will be used anyways).
449                    if ($this->getParam('display_messages')) {
450                        raiseMsg(sprintf(_("The file <strong>%s</strong> was renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__);
451                    }
452                }
453                $new_file_names[] = array(
454                    'name' => $file_name,
455                    'size' => filesize($file_path_name),
456                    'extension' => strtolower(substr($file_name, strrpos($file_name, '.') + 1)),
457                );
458                continue;
459            } else {
460                if ($this->getParam('display_messages')) {
461                    raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
462                }
463                logMsg(sprintf(_("Moving file failed: %s -> %s"), $files['tmp_name'][$i], $file_path_name), LOG_ALERT, __FILE__, __LINE__);
464                $this->errors[] = $files['name'][$i];
465                continue;
466            }
467        }
468        return (sizeof($new_file_names) > 0) ? $new_file_names : false;
469    }
470   
471    /**
472     *
473     */
474    function deleteFile($file_name)
475    {
476        // Ensure we have a upload directory.
477        if (!isset($this->upload_directory_path)) {
478            logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
479            return false;
480        }
481       
482        $file_path_name = $this->upload_directory_path . '/' . $file_name;
483
484        if (!is_file($file_path_name)) {
485            logMsg(sprintf(_("Error deleting nonexistant file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__);
486            return false;
487        } else if (unlink($file_path_name)) {
488            logMsg(sprintf('Deleted file: %s', $file_path_name), LOG_DEBUG, __FILE__, __LINE__);
489        } else {
490            if ($this->getParam('display_messages')) {
491                raiseMsg(sprintf(_("The file <strong>%s</strong> could not be deleted."), $file_name), MSG_ERROR, __FILE__, __LINE__);
492            }
493            logMsg(sprintf(_("Failed deleting file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__);
494            return false;
495        }
496    }
497   
498    /**
499     *
500     */
501    function moveFile($old_name, $new_name)
502    {
503        // Ensure we have an upload directory.
504        if (!isset($this->upload_directory_path)) {
505            logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
506            return false;
507        }
508       
509        $old_file_path_name = $this->upload_directory_path . '/' . $old_name;
510        $new_file_path_name = $this->upload_directory_path . '/' . $new_name;
511        if (file_exists($old_file_path_name)) {
512            if (!rename($old_file_path_name, $new_file_path_name)) {
513                if ($this->getParam('display_messages')) {
514                    raiseMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), MSG_ERR, __FILE__, __LINE__);
515                }
516                logMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), LOG_ERR, __FILE__, __LINE__);
517                return false;
518            }
519        } else {
520            if ($this->getParam('display_messages')) {
521                raiseMsg(sprintf(_("Couldn't rename nonexistant file <strong>%s</strong>."), $old_name), MSG_ERR, __FILE__, __LINE__);
522            }
523            logMsg(sprintf(_("Error renaming nonexistant file: %s"), $old_file_path_name), LOG_ERR, __FILE__, __LINE__);
524            return false;
525        }
526    }
527   
528    /**
529     *
530     */
531    function exists($file_name)
532    {
533        // Ensure we have a upload directory.
534        if (!isset($this->upload_directory_path)) {
535            logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
536            return false;
537        }
538       
539        return file_exists($this->upload_directory_path . '/' . $file_name);
540    }
541
542    /**
543     *
544     */
545    function getErrors()
546    {
547        return $this->errors;
548    }
549
550    /**
551     *
552     */
553    function cleanFileName($file_name)
554    {
555        $bad  = '‡Ž’—œˆ“˜Š‘•šŸçƒêîòËéíñô€è셆‰”™žåæëï󖍂@';
556        $good = 'aeiouaeiouaeiouAEIOUAEIOUAEIOUaeiouAEIOUncCa';
557        $file_name = trim($file_name);
558        $file_name = strtr($file_name, $bad, $good);
559        $file_name = preg_replace('/[^-\w.,~_=+\(\)]/', '_', $file_name);
560        $file_name = substr($file_name, 0, 250);
561        return $file_name;
562    }
563
564    /**
565     *
566     */
567    function getFilenameExtension($file_name)
568    {
569        preg_match('/.*?\.(\w+)$/i', $file_name, $ext);
570        return $ext[1];
571    }
572}
573
574?>
Note: See TracBrowser for help on using the repository browser.