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