source: trunk/lib/Upload.inc.php @ 35

Last change on this file since 35 was 35, checked in by scdev, 18 years ago

Fixed mime_content_type bug in Upload.inc.php

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