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