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