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