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

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

Tons of little updates and bugfixes. CSS updates to templates and core css files. File upload ability to module_maker. Remade Upload interface to use setParam/getParam.

File size: 26.3 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 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 extensions and corresponding mime-types.
40    var $mime_extension_map = array(
41        'Z'       => 'application/x-compress',
42        'ai'      => 'application/postscript',
43        'aif'     => 'audio/x-aiff',
44        'aifc'    => 'audio/x-aiff',
45        'aiff'    => 'audio/x-aiff',
46        'asc'     => 'text/plain',
47        'asf'     => 'video/x-ms-asf',
48        'asx'     => 'video/x-ms-asf',
49        'au'      => 'audio/basic',
50        'avi'     => 'video/x-msvideo',
51        'bcpio'   => 'application/x-bcpio',
52        'bin'     => 'application/octet-stream',
53        'bmp'     => 'image/bmp',
54        'cdf'     => 'application/x-netcdf',
55        'class'   => 'application/octet-stream',
56        'cpio'    => 'application/x-cpio',
57        'cpt'     => 'application/mac-compactpro',
58        'csh'     => 'application/x-csh',
59        'css'     => 'text/css',
60        'dcr'     => 'application/x-director',
61        'diff'    => 'text/diff',
62        'dir'     => 'application/x-director',
63        'dll'     => 'application/octet-stream',
64        'dms'     => 'application/octet-stream',
65        'doc'     => 'application/msword',
66        'dvi'     => 'application/x-dvi',
67        'dxr'     => 'application/x-director',
68        'eps'     => 'application/postscript',
69        'etx'     => 'text/x-setext',
70        'exe'     => 'application/octet-stream',
71        'ez'      => 'application/andrew-inset',
72        'gif'     => 'image/gif',
73        'gtar'    => 'application/x-gtar',
74        'gz'      => 'application/x-gzip',
75        'hdf'     => 'application/x-hdf',
76        'hqx'     => 'application/mac-binhex40',
77        'htm'     => 'text/html',
78        'html'    => 'text/html',
79        'ice'     => 'x-conference/x-cooltalk',
80        'ics'     => 'text/calendar',
81        'ief'     => 'image/ief',
82        'ifb'     => 'text/calendar',
83        'iges'    => 'model/iges',
84        'igs'     => 'model/iges',
85        'jpe'     => 'image/jpeg',
86        'jpeg'    => 'image/jpeg',
87        'jpg'     => 'image/jpeg',
88        'js'      => 'application/x-javascript',
89        'kar'     => 'audio/midi',
90        'latex'   => 'application/x-latex',
91        'lha'     => 'application/octet-stream',
92        'log'     => 'text/plain',
93        'lzh'     => 'application/octet-stream',
94        'm3u'     => 'audio/x-mpegurl',
95        'man'     => 'application/x-troff-man',
96        'me'      => 'application/x-troff-me',
97        'mesh'    => 'model/mesh',
98        'mid'     => 'audio/midi',
99        'midi'    => 'audio/midi',
100        'mif'     => 'application/vnd.mif',
101        'mov'     => 'video/quicktime',
102        'movie'   => 'video/x-sgi-movie',
103        'mp2'     => 'audio/mpeg',
104        'mp3'     => 'audio/mpeg',
105        'mpe'     => 'video/mpeg',
106        'mpeg'    => 'video/mpeg',
107        'mpg'     => 'video/mpeg',
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     * 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        if (!isset($_FILES[$form_name])) {
281            App::logMsg(sprintf(_("Form element %s not posted."), $form_name), LOG_ERR, __FILE__, __LINE__);
282            $this->raiseMsg(_("There was a problem with the file upload. Please try again."), MSG_ERR, __FILE__, __LINE__);
283            return false;
284        }
285       
286        if (is_array($_FILES[$form_name]['name'])) {
287            $files = $_FILES[$form_name];
288        } else {
289            // Convert variables to single-cell array so it will loop.
290            $files = array(
291                'name'      => array($_FILES[$form_name]['name']),
292                'type'      => array($_FILES[$form_name]['type']),
293                'tmp_name'  => array($_FILES[$form_name]['tmp_name']),
294                'error'     => array($_FILES[$form_name]['error']),
295                'size'      => array($_FILES[$form_name]['size']),
296            );
297        }
298
299        // To keep this script running even if user tries to stop browser.
300        ignore_user_abort(true); 
301        if (!ini_get('safe_mode')) { 
302            set_time_limit(300); 
303        }
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            // Check The php upload error messages.
317            if (UPLOAD_ERR_INI_SIZE === $files['error'][$i]) {
318                if ($this->getParam('display_messages')) {
319                $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__);
320                }
321                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__);
322                $this->errors[] = $files['name'][$i];
323                continue;
324            }
325            if (UPLOAD_ERR_FORM_SIZE === $files['error'][$i]) {
326                $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__);
327                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__);
328                $this->errors[] = $files['name'][$i];
329                continue;
330            }
331            if (UPLOAD_ERR_PARTIAL === $files['error'][$i]) {
332                $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it was only partially uploaded."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
333                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__);
334                $this->errors[] = $files['name'][$i];
335                continue;
336            }
337            if (UPLOAD_ERR_NO_FILE === $files['error'][$i]) {
338                $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: no file was uploaded."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
339                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__);
340                $this->errors[] = $files['name'][$i];
341                continue;
342            }
343            if (UPLOAD_ERR_NO_TMP_DIR === $files['error'][$i]) {
344                $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: temporary upload directory missing."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
345                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__);
346                $this->errors[] = $files['name'][$i];
347                continue;
348            }
349           
350            // Check to be sure it's an uploaded file.
351            if (!is_uploaded_file($files['tmp_name'][$i])) {
352                $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
353                App::logMsg(sprintf(_("The file %s failed is_uploaded_file."), $files['name'][$i]), LOG_ERR, __FILE__, __LINE__);
354                $this->errors[] = $files['name'][$i];
355                continue;
356            }
357           
358            // Check to be sure the file is not empty.
359            if ($files['size'][$i] < 1) {
360                $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it contains zero bytes."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
361                App::logMsg(sprintf(_("The uploaded file %s contains zero bytes."), $files['name'][$i]), LOG_ERR, __FILE__, __LINE__);
362                $this->errors[] = $files['name'][$i];
363                continue;
364            }
365           
366            // Check to be sure the file has a valid file extension.
367            if (!in_array(strtolower($this->getFilenameExtension($files['name'][$i])), $this->getParam('valid_file_extensions'))) {
368                $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->getParam('valid_file_extensions'))), MSG_ERR, __FILE__, __LINE__);
369                App::logMsg(sprintf(_("The uploaded file %s has an unrecognized file extension."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__);
370                $this->errors[] = $files['name'][$i];
371                continue;
372            }
373           
374            // Check to be sure the file has a unique file name.
375            if (!$this->getParam('allow_overwriting') && $this->exists($files['name'][$i])) {
376                $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: a file with that name already exists."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
377                App::logMsg(sprintf(_("The uploaded file %s doesn't have a unique filename."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__);
378                $this->errors[] = $files['name'][$i];
379                continue;
380            }
381           
382            // Determine file name.
383            if ($num == 1) {
384                // Single upload.
385                if (isset($custom_file_name) && '' != $custom_file_name) {
386                    // Valid custom file name.
387                    $file_name = $custom_file_name;
388                    $this->raiseMsg(sprintf(_("The file <strong>%s</strong> has been renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__);
389                    App::logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
390                } else {
391                    // Invalid custom file name provided. Use uploaded file name.
392                    $file_name = $files['name'][$i];
393                    App::logMsg(sprintf('Using uploaded file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
394                }
395            } else {
396                // Multiple upload. Final file names must be array.
397                if (isset($custom_file_name) && is_array($custom_file_name) && '' != $custom_file_name[$i]) {
398                    // Valid custom file name.
399                    $file_name = $custom_file_name[$i];
400                    $this->raiseMsg(sprintf(_("The file <strong>%s</strong> has been renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__);
401                    App::logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
402                } else {
403                    // Invalid custom file name provided. Use uploaded file name.
404                    $file_name = $files['name'][$i];
405                    App::logMsg(sprintf('Using uploaded file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__);
406                }
407            }
408
409            // Clean the file name of bad characters.
410            $file_name = $this->cleanFileName($file_name);
411           
412            // If the file name has no extension, use the mime-type extension.
413            if (!preg_match('/\.[^.]{1,5}$/', $file_name) && function_exists('mime_content_type')) {
414                if ($ext = array_search(mime_content_type($files['type'][$i]), $this->mime_extension_map)) {
415                    $file_name .= ".$ext";
416                }
417            }
418           
419            // Set the path and file name.
420            $file_path_name = $this->getParam('upload_path') . '/' . $file_name;
421           
422            // Move the file to the final place.
423            if (move_uploaded_file($files['tmp_name'][$i], $file_path_name)) {
424                chmod($file_path_name, $this->getParam('dest_file_perms'));
425                $this->raiseMsg(sprintf(_("The file <strong>%s</strong> uploaded successfully."), $files['name'][$i]), MSG_SUCCESS, __FILE__, __LINE__);
426                if (!isset($custom_file_name) && $files['name'][$i] != $file_name) {
427                    // Notify user if uploaded file name was modified (unless a custom file name will be used anyways).
428                    $this->raiseMsg(sprintf(_("The file <strong>%s</strong> was renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__);
429                }
430                $new_file_names[] = array(
431                    'name' => $file_name,
432                    'size' => filesize($file_path_name),
433                    'extension' => strtolower(substr($file_name, strrpos($file_name, '.') + 1)),
434                );
435                continue;
436            } else {
437                $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__);
438                App::logMsg(sprintf(_("Moving file failed: %s -> %s"), $files['tmp_name'][$i], $file_path_name), LOG_ALERT, __FILE__, __LINE__);
439                $this->errors[] = $files['name'][$i];
440                continue;
441            }
442        }
443        return (sizeof($new_file_names) > 0) ? $new_file_names : false;
444    }
445   
446    /**
447     *
448     */
449    function deleteFile($file_name)
450    {
451        // Ensure we have a upload directory.
452        if (!$this->getParam('upload_path')) {
453            App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
454            return false;
455        }
456       
457        $file_path_name = $this->getParam('upload_path') . '/' . $file_name;
458
459        if (!is_file($file_path_name)) {
460            App::logMsg(sprintf(_("Error deleting nonexistent file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__);
461            return false;
462        } else if (unlink($file_path_name)) {
463            App::logMsg(sprintf('Deleted file: %s', $file_path_name), LOG_DEBUG, __FILE__, __LINE__);
464        } else {
465            $this->raiseMsg(sprintf(_("The file <strong>%s</strong> could not be deleted."), $file_name), MSG_ERROR, __FILE__, __LINE__);
466            App::logMsg(sprintf(_("Failed deleting file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__);
467            return false;
468        }
469    }
470   
471    /**
472     *
473     */
474    function moveFile($old_name, $new_name)
475    {
476        // Ensure we have an upload directory.
477        if (!$this->getParam('upload_path')) {
478            App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
479            return false;
480        }
481       
482        $old_file_path_name = $this->getParam('upload_path') . '/' . $old_name;
483        $new_file_path_name = $this->getParam('upload_path') . '/' . $new_name;
484        if (file_exists($old_file_path_name)) {
485            if (rename($old_file_path_name, $new_file_path_name)) {
486                $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__);
487                App::logMsg(sprintf('File renamed from %s to %s', $old_file_path_name, $new_file_path_name), LOG_DEBUG, __FILE__, __LINE__);
488            } else {
489                $this->raiseMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), MSG_ERR, __FILE__, __LINE__);
490                App::logMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), LOG_ERR, __FILE__, __LINE__);
491                return false;
492            }
493        } else {
494            $this->raiseMsg(sprintf(_("Couldn't rename nonexistent file <strong>%s</strong>."), $old_name), MSG_ERR, __FILE__, __LINE__);
495            App::logMsg(sprintf(_("Error renaming nonexistent file: %s"), $old_file_path_name), LOG_ERR, __FILE__, __LINE__);
496            return false;
497        }
498    }
499   
500    /**
501     *
502     */
503    function exists($file_name)
504    {
505        // Ensure we have a upload directory.
506        if (!$this->getParam('upload_path')) {
507            App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__);
508            return false;
509        }
510       
511        return file_exists($this->getParam('upload_path') . '/' . $file_name);
512    }
513
514    /**
515     * Get filename by glob pattern. Searches a directory for an image that matches the
516     * specified glob pattern and returns the filename of the first file found.
517     *
518     * @access  public
519     * @param   string  $pattern   Pattern to match filename.
520     * @return  string filename on success, empty string on failure.
521     * @author  Quinn Comendant <quinn@strangecode.com>
522     * @since   15 Nov 2005 20:55:22
523     */
524    function getFilenameGlob($pattern)
525    {
526        $file_list = glob(sprintf('%s/%s', $this->getParam('upload_path'), $pattern));
527        if (isset($file_list[0])) {
528            return basename($file_list[0]);
529        } else {
530            return '';
531        }
532    }
533
534    /**
535     *
536     */
537    function getErrors()
538    {
539        return $this->errors;
540    }
541
542    /**
543     *
544     */
545    function cleanFileName($file_name)
546    {
547        $bad  = 'áéíóúàèìòùäëïöüÁÉÍÓÚÀÈÌÒÙÄËÏÖÜâêîôûÂÊÎÔÛñçÇ@';
548        $good = 'aeiouaeiouaeiouAEIOUAEIOUAEIOUaeiouAEIOUncCa';
549        $file_name = trim($file_name);
550        $file_name = strtr($file_name, $bad, $good);
551        $file_name = preg_replace('/[^-\w.,~_=+()]/i', '_', $file_name);
552        $file_name = substr($file_name, 0, 250);
553        return $file_name;
554    }
555
556    /**
557     *
558     */
559    function getFilenameExtension($file_name)
560    {
561        preg_match('/.*?\.(\w+)$/i', $file_name, $ext);
562        return $ext[1];
563    }
564   
565    /**
566     * An alias for App::raiseMsg that only sends messages if display_messages is true.
567     *
568     * @access public
569     *
570     * @param string $message The text description of the message.
571     * @param int    $type    The type of message: MSG_NOTICE,
572     *                        MSG_SUCCESS, MSG_WARNING, or MSG_ERR.
573     * @param string $file    __FILE__.
574     * @param string $line    __LINE__.
575     */
576    function raiseMsg($message, $type, $file, $line)
577    {
578        if ($this->getParam('display_messages')) {
579            App::raiseMsg($message, $type, $file, $line);
580        }
581    }
582}
583
584?>
Note: See TracBrowser for help on using the repository browser.