[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 |
---|
[37] | 10 | * @version 1.3 |
---|
[1] | 11 | */ |
---|
| 12 | |
---|
[37] | 13 | // Message Types. |
---|
| 14 | define('UPLOAD_MSG_ERR', MSG_ERR); |
---|
| 15 | define('UPLOAD_MSG_ERROR', MSG_ERROR); |
---|
| 16 | define('UPLOAD_MSG_WARNING', MSG_WARNING); |
---|
| 17 | define('UPLOAD_MSG_NOTICE', MSG_NOTICE); |
---|
| 18 | define('UPLOAD_MSG_SUCCESS', MSG_SUCCESS); |
---|
| 19 | define('UPLOAD_MSG_ALL', MSG_SUCCESS + MSG_NOTICE + MSG_WARNING + MSG_ERROR); |
---|
| 20 | |
---|
[1] | 21 | require_once dirname(__FILE__) . '/App.inc.php'; |
---|
| 22 | |
---|
| 23 | class Upload { |
---|
[42] | 24 | |
---|
[1] | 25 | // General object parameters. |
---|
| 26 | var $_params = array( |
---|
[42] | 27 | |
---|
[37] | 28 | // Which messages do we pass to raiseMsg? |
---|
| 29 | 'display_messages' => UPLOAD_MSG_ALL, |
---|
[42] | 30 | |
---|
[20] | 31 | // Existing files will be overwritten when there is a name conflict? |
---|
[1] | 32 | 'allow_overwriting' => false, |
---|
[20] | 33 | |
---|
| 34 | // The filesystem path to the final upload directory. |
---|
| 35 | 'upload_path' => null, |
---|
| 36 | |
---|
| 37 | // The file permissions of the uploaded files. Remember, files will be owned by the web server user. |
---|
| 38 | 'dest_file_perms' => 0600, |
---|
| 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. |
---|
[106] | 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', '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] | 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 | { |
---|
| 58 | if (isset($params) && is_array($params)) { |
---|
[42] | 59 | |
---|
[20] | 60 | // Enforce valid upload_path parameter. |
---|
| 61 | if (isset($params['upload_path'])) { |
---|
[220] | 62 | // $params['upload_path'] = realpath($params['upload_path']); |
---|
| 63 | $params['upload_path'] = $params['upload_path']; |
---|
[20] | 64 | // Must be directory. |
---|
| 65 | if (!is_dir($params['upload_path'])) { |
---|
| 66 | App::logMsg(sprintf('Upload directory invalid: %s', $params['upload_path']), LOG_ERR, __FILE__, __LINE__); |
---|
| 67 | trigger_error(sprintf('Upload directory invalid: %s', $params['upload_path']), E_USER_ERROR); |
---|
| 68 | } |
---|
| 69 | // Must be writable. |
---|
| 70 | if (!is_writable($params['upload_path'])) { |
---|
| 71 | App::logMsg(sprintf('Upload directory not writable: %s', $params['upload_path']), LOG_ERR, __FILE__, __LINE__); |
---|
| 72 | trigger_error(sprintf('Upload directory not writable: %s', $params['upload_path']), E_USER_ERROR); |
---|
| 73 | } |
---|
| 74 | } |
---|
[42] | 75 | |
---|
[1] | 76 | // Merge new parameters with old overriding only those passed. |
---|
| 77 | $this->_params = array_merge($this->_params, $params); |
---|
| 78 | } else { |
---|
| 79 | App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Return the value of a parameter, if it exists. |
---|
| 85 | * |
---|
| 86 | * @access public |
---|
| 87 | * @param string $param Which parameter to return. |
---|
| 88 | * @return mixed Configured parameter value. |
---|
| 89 | */ |
---|
| 90 | function getParam($param) |
---|
| 91 | { |
---|
| 92 | if (isset($this->_params[$param])) { |
---|
| 93 | return $this->_params[$param]; |
---|
| 94 | } else { |
---|
| 95 | App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 96 | return null; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /** |
---|
[37] | 101 | * Process uploaded files. Processes files existing within the specified $_FILES['form_name'] array. |
---|
[42] | 102 | * It tests for errors, cleans the filename, optionally sets custom file names. It will process |
---|
[37] | 103 | * multiple files automatically if the file form element is an array (<input type="file" name="myfiles[]" />). |
---|
[1] | 104 | * |
---|
[37] | 105 | * @access public |
---|
| 106 | * @param string $form_name The name of the form to process. |
---|
[118] | 107 | * @param string $custom_file_name The new name of the file. An array of filename in the case of multiple files. |
---|
[37] | 108 | * @return mixed Returns FALSE if a major error occured preventing any file uploads. |
---|
| 109 | * Returns an empty array if any minor errors occured or no files were found. |
---|
| 110 | * Returns a multidimentional array of filenames, sizes and extentions, if one-or-more files succeeded uploading. |
---|
| 111 | * Note: this last option presents a problem in the case of when some files uploaded successfully, and some failed. |
---|
| 112 | * In this case it is necessary to check the Upload::anyErrors method to discover if any did fail. |
---|
[1] | 113 | */ |
---|
| 114 | function process($form_name, $custom_file_name=null) |
---|
| 115 | { |
---|
| 116 | // Ensure we have a upload directory. |
---|
[20] | 117 | if (!$this->getParam('upload_path')) { |
---|
[1] | 118 | App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 119 | $this->raiseMsg(_("There was a problem with the file upload. Please try again later."), MSG_ERR, __FILE__, __LINE__); |
---|
| 120 | return false; |
---|
| 121 | } |
---|
[42] | 122 | |
---|
[22] | 123 | // Ensure the file form element specified actually exists. |
---|
[1] | 124 | if (!isset($_FILES[$form_name])) { |
---|
[22] | 125 | App::logMsg(sprintf(_("Form element %s does not exist."), $form_name), LOG_ERR, __FILE__, __LINE__); |
---|
| 126 | $this->raiseMsg(_("There was a problem with the file upload. Please try again later."), MSG_ERR, __FILE__, __LINE__); |
---|
[1] | 127 | return false; |
---|
| 128 | } |
---|
[42] | 129 | |
---|
[1] | 130 | if (is_array($_FILES[$form_name]['name'])) { |
---|
| 131 | $files = $_FILES[$form_name]; |
---|
| 132 | } else { |
---|
| 133 | // Convert variables to single-cell array so it will loop. |
---|
| 134 | $files = array( |
---|
| 135 | 'name' => array($_FILES[$form_name]['name']), |
---|
| 136 | 'type' => array($_FILES[$form_name]['type']), |
---|
| 137 | 'tmp_name' => array($_FILES[$form_name]['tmp_name']), |
---|
| 138 | 'error' => array($_FILES[$form_name]['error']), |
---|
| 139 | 'size' => array($_FILES[$form_name]['size']), |
---|
| 140 | ); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | // To keep this script running even if user tries to stop browser. |
---|
[42] | 144 | ignore_user_abort(true); |
---|
[35] | 145 | ini_set('max_execution_time', 300); |
---|
| 146 | ini_set('max_input_time', 300); |
---|
[1] | 147 | |
---|
| 148 | $new_file_names = array(); |
---|
| 149 | |
---|
| 150 | $num = sizeof($files['name']); |
---|
| 151 | for ($i=0; $i<$num; $i++) { |
---|
| 152 | $file_path_name = ''; |
---|
| 153 | |
---|
[49] | 154 | if ('' == trim($files['name'][$i])) { |
---|
[1] | 155 | // User may not have attached a file. |
---|
| 156 | continue; |
---|
| 157 | } |
---|
[42] | 158 | |
---|
[22] | 159 | // Determine final file name. |
---|
[1] | 160 | if ($num == 1) { |
---|
| 161 | // Single upload. |
---|
| 162 | if (isset($custom_file_name) && '' != $custom_file_name) { |
---|
| 163 | // Valid custom file name. |
---|
| 164 | $file_name = $custom_file_name; |
---|
| 165 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> has been renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__); |
---|
| 166 | App::logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 167 | } else { |
---|
| 168 | // Invalid custom file name provided. Use uploaded file name. |
---|
| 169 | $file_name = $files['name'][$i]; |
---|
| 170 | App::logMsg(sprintf('Using uploaded file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 171 | } |
---|
| 172 | } else { |
---|
| 173 | // Multiple upload. Final file names must be array. |
---|
[118] | 174 | if (isset($custom_file_name) && is_array($custom_file_name) && is_array($custom_file_name[$i]) && '' != $custom_file_name[$i]) { |
---|
[1] | 175 | // Valid custom file name. |
---|
| 176 | $file_name = $custom_file_name[$i]; |
---|
| 177 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> has been renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__); |
---|
| 178 | App::logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 179 | } else { |
---|
| 180 | // Invalid custom file name provided. Use uploaded file name. |
---|
| 181 | $file_name = $files['name'][$i]; |
---|
| 182 | App::logMsg(sprintf('Using uploaded file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | // Clean the file name of bad characters. |
---|
| 187 | $file_name = $this->cleanFileName($file_name); |
---|
[42] | 188 | |
---|
[1] | 189 | // If the file name has no extension, use the mime-type extension. |
---|
| 190 | if (!preg_match('/\.[^.]{1,5}$/', $file_name) && function_exists('mime_content_type')) { |
---|
[35] | 191 | if ($ext = array_search(mime_content_type($files['tmp_name'][$i]), $this->mime_extension_map)) { |
---|
[1] | 192 | $file_name .= ".$ext"; |
---|
| 193 | } |
---|
| 194 | } |
---|
[42] | 195 | |
---|
[1] | 196 | // Set the path and file name. |
---|
[20] | 197 | $file_path_name = $this->getParam('upload_path') . '/' . $file_name; |
---|
[42] | 198 | |
---|
| 199 | |
---|
[22] | 200 | // Check The php upload error messages. |
---|
| 201 | if (UPLOAD_ERR_INI_SIZE === $files['error'][$i]) { |
---|
| 202 | if ($this->getParam('display_messages')) { |
---|
| 203 | $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__); |
---|
| 204 | } |
---|
| 205 | 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__); |
---|
| 206 | $this->errors[] = $file_name; |
---|
| 207 | continue; |
---|
| 208 | } |
---|
| 209 | if (UPLOAD_ERR_FORM_SIZE === $files['error'][$i]) { |
---|
| 210 | $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__); |
---|
| 211 | 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__); |
---|
| 212 | $this->errors[] = $file_name; |
---|
| 213 | continue; |
---|
| 214 | } |
---|
| 215 | if (UPLOAD_ERR_PARTIAL === $files['error'][$i]) { |
---|
| 216 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it was only partially uploaded."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
| 217 | App::logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_PARTIAL."), $files['error'][$i], $file_name), LOG_ERR, __FILE__, __LINE__); |
---|
| 218 | $this->errors[] = $file_name; |
---|
| 219 | continue; |
---|
| 220 | } |
---|
| 221 | if (UPLOAD_ERR_NO_FILE === $files['error'][$i]) { |
---|
| 222 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: no file was uploaded."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
| 223 | 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__); |
---|
| 224 | $this->errors[] = $file_name; |
---|
| 225 | continue; |
---|
| 226 | } |
---|
| 227 | if (UPLOAD_ERR_NO_TMP_DIR === $files['error'][$i]) { |
---|
| 228 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: temporary upload directory missing."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
| 229 | 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__); |
---|
| 230 | $this->errors[] = $file_name; |
---|
| 231 | continue; |
---|
| 232 | } |
---|
[42] | 233 | |
---|
[22] | 234 | // Check to be sure it's an uploaded file. |
---|
| 235 | if (!is_uploaded_file($files['tmp_name'][$i])) { |
---|
| 236 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
| 237 | App::logMsg(sprintf(_("The file %s failed is_uploaded_file."), $file_name), LOG_ERR, __FILE__, __LINE__); |
---|
| 238 | $this->errors[] = $file_name; |
---|
| 239 | continue; |
---|
| 240 | } |
---|
[42] | 241 | |
---|
[22] | 242 | // Check to be sure the file is not empty. |
---|
| 243 | if ($files['size'][$i] < 1) { |
---|
| 244 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it contains zero bytes."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
| 245 | App::logMsg(sprintf(_("The uploaded file %s contains zero bytes."), $file_name), LOG_ERR, __FILE__, __LINE__); |
---|
| 246 | $this->errors[] = $file_name; |
---|
| 247 | continue; |
---|
| 248 | } |
---|
[42] | 249 | |
---|
[22] | 250 | // Check to be sure the file has a valid file name extension. |
---|
| 251 | if (!in_array(strtolower($this->getFilenameExtension($file_name)), $this->getParam('valid_file_extensions'))) { |
---|
| 252 | $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__); |
---|
| 253 | App::logMsg(sprintf(_("The uploaded file %s has an unrecognized file name extension."), $file_name), LOG_WARNING, __FILE__, __LINE__); |
---|
| 254 | $this->errors[] = $file_name; |
---|
| 255 | continue; |
---|
| 256 | } |
---|
[42] | 257 | |
---|
[22] | 258 | // Check to be sure the file has a unique file name. |
---|
| 259 | if (!$this->getParam('allow_overwriting') && $this->exists($file_name)) { |
---|
| 260 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: a file with that name already exists."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
| 261 | App::logMsg(sprintf(_("The uploaded file %s doesn't have a unique filename."), $file_name), LOG_WARNING, __FILE__, __LINE__); |
---|
| 262 | $this->errors[] = $file_name; |
---|
| 263 | continue; |
---|
| 264 | } |
---|
[42] | 265 | |
---|
[1] | 266 | // Move the file to the final place. |
---|
| 267 | if (move_uploaded_file($files['tmp_name'][$i], $file_path_name)) { |
---|
[20] | 268 | chmod($file_path_name, $this->getParam('dest_file_perms')); |
---|
[40] | 269 | App::logMsg(sprintf('File uploaded: %s', $file_path_name), LOG_INFO, __FILE__, __LINE__); |
---|
[22] | 270 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> uploaded successfully."), $file_name), MSG_SUCCESS, __FILE__, __LINE__); |
---|
[1] | 271 | if (!isset($custom_file_name) && $files['name'][$i] != $file_name) { |
---|
| 272 | // Notify user if uploaded file name was modified (unless a custom file name will be used anyways). |
---|
| 273 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> was renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__); |
---|
| 274 | } |
---|
| 275 | $new_file_names[] = array( |
---|
| 276 | 'name' => $file_name, |
---|
[298] | 277 | 'mime' => $files['type'][$i], |
---|
[1] | 278 | 'size' => filesize($file_path_name), |
---|
[298] | 279 | 'extension' => $this->getFilenameExtension($file_name), |
---|
| 280 | 'original_index' => $i, |
---|
[301] | 281 | 'original_name' => $files['name'][$i], |
---|
[1] | 282 | ); |
---|
| 283 | continue; |
---|
| 284 | } else { |
---|
[22] | 285 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
[1] | 286 | App::logMsg(sprintf(_("Moving file failed: %s -> %s"), $files['tmp_name'][$i], $file_path_name), LOG_ALERT, __FILE__, __LINE__); |
---|
[22] | 287 | $this->errors[] = $file_name; |
---|
[1] | 288 | continue; |
---|
| 289 | } |
---|
| 290 | } |
---|
[42] | 291 | |
---|
[22] | 292 | // Return names of files uploaded (or empty array when none processed). |
---|
| 293 | return $new_file_names; |
---|
[1] | 294 | } |
---|
[42] | 295 | |
---|
[1] | 296 | /** |
---|
[37] | 297 | * Remove file within upload path. |
---|
[1] | 298 | * |
---|
[37] | 299 | * @access public |
---|
| 300 | * @param string $file_name A name of a file. |
---|
[301] | 301 | * @param bool $use_glob Set true to use glob to find the filename (using $file_name as a pattern) |
---|
[37] | 302 | * @return bool Success of operation. |
---|
[1] | 303 | */ |
---|
[301] | 304 | function deleteFile($file_name, $use_glob=false) |
---|
[1] | 305 | { |
---|
| 306 | // Ensure we have a upload directory. |
---|
[20] | 307 | if (!$this->getParam('upload_path')) { |
---|
[1] | 308 | App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 309 | return false; |
---|
| 310 | } |
---|
[42] | 311 | |
---|
[301] | 312 | $file_path_name = $this->getParam('upload_path') . '/' . ($use_glob ? $this->getFilenameGlob($file_name) : $file_name); |
---|
[1] | 313 | |
---|
| 314 | if (!is_file($file_path_name)) { |
---|
| 315 | App::logMsg(sprintf(_("Error deleting nonexistent file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__); |
---|
| 316 | return false; |
---|
| 317 | } else if (unlink($file_path_name)) { |
---|
[40] | 318 | App::logMsg(sprintf('Deleted file: %s', $file_path_name), LOG_INFO, __FILE__, __LINE__); |
---|
[1] | 319 | } else { |
---|
[37] | 320 | $this->raiseMsg(sprintf(_("The file <strong>%s</strong> could not be deleted."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
[1] | 321 | App::logMsg(sprintf(_("Failed deleting file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__); |
---|
| 322 | return false; |
---|
| 323 | } |
---|
| 324 | } |
---|
[42] | 325 | |
---|
[1] | 326 | /** |
---|
[37] | 327 | * Renames a file within the upload path. |
---|
[1] | 328 | * |
---|
[37] | 329 | * @access public |
---|
| 330 | * @param string $old_name The currently existing file name. |
---|
| 331 | * @param string $new_name The new name for this file. |
---|
| 332 | * @return bool Success of operation. |
---|
[1] | 333 | */ |
---|
| 334 | function moveFile($old_name, $new_name) |
---|
| 335 | { |
---|
| 336 | // Ensure we have an upload directory. |
---|
[20] | 337 | if (!$this->getParam('upload_path')) { |
---|
[1] | 338 | App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 339 | return false; |
---|
| 340 | } |
---|
[42] | 341 | |
---|
[20] | 342 | $old_file_path_name = $this->getParam('upload_path') . '/' . $old_name; |
---|
| 343 | $new_file_path_name = $this->getParam('upload_path') . '/' . $new_name; |
---|
[1] | 344 | if (file_exists($old_file_path_name)) { |
---|
[20] | 345 | if (rename($old_file_path_name, $new_file_path_name)) { |
---|
| 346 | $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__); |
---|
| 347 | App::logMsg(sprintf('File renamed from %s to %s', $old_file_path_name, $new_file_path_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 348 | } else { |
---|
[37] | 349 | $this->raiseMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), MSG_WARNING, __FILE__, __LINE__); |
---|
| 350 | App::logMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 351 | return false; |
---|
| 352 | } |
---|
| 353 | } else { |
---|
[37] | 354 | $this->raiseMsg(sprintf(_("Couldn't rename nonexistent file <strong>%s</strong>."), $old_name), MSG_WARNING, __FILE__, __LINE__); |
---|
| 355 | App::logMsg(sprintf(_("Error renaming nonexistent file: %s"), $old_file_path_name), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 356 | return false; |
---|
| 357 | } |
---|
| 358 | } |
---|
[42] | 359 | |
---|
[1] | 360 | /** |
---|
[37] | 361 | * Tests if a file exists within the current upload_path. |
---|
[1] | 362 | * |
---|
[37] | 363 | * @access public |
---|
| 364 | * @param string $file_name A name of a file. |
---|
| 365 | * @return bool Existence of file. |
---|
[1] | 366 | */ |
---|
| 367 | function exists($file_name) |
---|
| 368 | { |
---|
| 369 | // Ensure we have a upload directory. |
---|
[20] | 370 | if (!$this->getParam('upload_path')) { |
---|
[1] | 371 | App::logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 372 | return false; |
---|
| 373 | } |
---|
[42] | 374 | |
---|
[20] | 375 | return file_exists($this->getParam('upload_path') . '/' . $file_name); |
---|
[1] | 376 | } |
---|
| 377 | |
---|
| 378 | /** |
---|
[42] | 379 | * Get filename by glob pattern. Searches a directory for an image that matches the |
---|
[20] | 380 | * specified glob pattern and returns the filename of the first file found. |
---|
[1] | 381 | * |
---|
[20] | 382 | * @access public |
---|
| 383 | * @param string $pattern Pattern to match filename. |
---|
| 384 | * @return string filename on success, empty string on failure. |
---|
| 385 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 386 | * @since 15 Nov 2005 20:55:22 |
---|
[1] | 387 | */ |
---|
[20] | 388 | function getFilenameGlob($pattern) |
---|
| 389 | { |
---|
| 390 | $file_list = glob(sprintf('%s/%s', $this->getParam('upload_path'), $pattern)); |
---|
| 391 | if (isset($file_list[0])) { |
---|
| 392 | return basename($file_list[0]); |
---|
| 393 | } else { |
---|
| 394 | return ''; |
---|
| 395 | } |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | /** |
---|
[37] | 399 | * Returns an array of file names that failed uploading. |
---|
[20] | 400 | * |
---|
[37] | 401 | * @access public |
---|
| 402 | * @return array List of file names. |
---|
[20] | 403 | */ |
---|
[1] | 404 | function getErrors() |
---|
| 405 | { |
---|
| 406 | return $this->errors; |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | /** |
---|
[37] | 410 | * Determintes if any errors occured while calling the Upload::process method. |
---|
[1] | 411 | * |
---|
[37] | 412 | * @access public |
---|
[1] | 413 | */ |
---|
[22] | 414 | function anyErrors() |
---|
| 415 | { |
---|
| 416 | return sizeof($this->errors) > 0; |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | /** |
---|
[37] | 420 | * Removes unsafe characters from file name. |
---|
[22] | 421 | * |
---|
[37] | 422 | * @access public |
---|
| 423 | * @param string $file_name A name of a file. |
---|
| 424 | * @return string The same name, but cleaned. |
---|
[22] | 425 | */ |
---|
[1] | 426 | function cleanFileName($file_name) |
---|
| 427 | { |
---|
[106] | 428 | $bad = 'áéÃóúà ÚìòùÀëïöÌÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃâêîÎûÃÃÃÃÃñçÃ@'; |
---|
[1] | 429 | $good = 'aeiouaeiouaeiouAEIOUAEIOUAEIOUaeiouAEIOUncCa'; |
---|
| 430 | $file_name = trim($file_name); |
---|
| 431 | $file_name = strtr($file_name, $bad, $good); |
---|
| 432 | $file_name = preg_replace('/[^-\w.,~_=+()]/i', '_', $file_name); |
---|
| 433 | $file_name = substr($file_name, 0, 250); |
---|
| 434 | return $file_name; |
---|
| 435 | } |
---|
| 436 | |
---|
[37] | 437 | |
---|
[1] | 438 | /** |
---|
[37] | 439 | * Returns the extention of a file name, or an empty string if non exists. |
---|
[1] | 440 | * |
---|
[37] | 441 | * @access public |
---|
| 442 | * @param string $file_name A name of a file, with extention after a dot. |
---|
| 443 | * @return string The value found after the dot |
---|
[1] | 444 | */ |
---|
| 445 | function getFilenameExtension($file_name) |
---|
| 446 | { |
---|
[37] | 447 | preg_match('/.*?\.(\w+)$/i', trim($file_name), $ext); |
---|
[22] | 448 | return isset($ext[1]) ? $ext[1] : ''; |
---|
[1] | 449 | } |
---|
[42] | 450 | |
---|
[1] | 451 | /** |
---|
[37] | 452 | * An alias for App::raiseMsg that only sends messages configured by display_messages. |
---|
[1] | 453 | * |
---|
| 454 | * @access public |
---|
| 455 | * |
---|
| 456 | * @param string $message The text description of the message. |
---|
| 457 | * @param int $type The type of message: MSG_NOTICE, |
---|
| 458 | * MSG_SUCCESS, MSG_WARNING, or MSG_ERR. |
---|
| 459 | * @param string $file __FILE__. |
---|
| 460 | * @param string $line __LINE__. |
---|
| 461 | */ |
---|
| 462 | function raiseMsg($message, $type, $file, $line) |
---|
| 463 | { |
---|
[37] | 464 | if ($this->getParam('display_messages') === true || (is_int($this->getParam('display_messages')) && $this->getParam('display_messages') >= $type)) { |
---|
[1] | 465 | App::raiseMsg($message, $type, $file, $line); |
---|
| 466 | } |
---|
| 467 | } |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | ?> |
---|