[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', |
---|
[203] | 107 | 'mp4' => 'video/mp4', |
---|
[1] | 108 | 'mpga' => 'audio/mpeg', |
---|
| 109 | 'ms' => 'application/x-troff-ms', |
---|
| 110 | 'msh' => 'model/mesh', |
---|
| 111 | 'mxu' => 'video/vnd.mpegurl', |
---|
| 112 | 'nc' => 'application/x-netcdf', |
---|
| 113 | 'oda' => 'application/oda', |
---|
| 114 | 'patch' => 'text/diff', |
---|
| 115 | 'pbm' => 'image/x-portable-bitmap', |
---|
| 116 | 'pdb' => 'chemical/x-pdb', |
---|
| 117 | 'pdf' => 'application/pdf', |
---|
| 118 | 'pgm' => 'image/x-portable-graymap', |
---|
| 119 | 'pgn' => 'application/x-chess-pgn', |
---|
| 120 | 'php' => 'application/x-httpd-php', |
---|
| 121 | 'php3' => 'application/x-httpd-php3', |
---|
| 122 | 'pl' => 'application/x-perl', |
---|
| 123 | 'pm' => 'application/x-perl', |
---|
| 124 | 'png' => 'image/png', |
---|
| 125 | 'pnm' => 'image/x-portable-anymap', |
---|
| 126 | 'po' => 'text/plain', |
---|
| 127 | 'ppm' => 'image/x-portable-pixmap', |
---|
| 128 | 'ppt' => 'application/vnd.ms-powerpoint', |
---|
| 129 | 'ps' => 'application/postscript', |
---|
| 130 | 'qt' => 'video/quicktime', |
---|
| 131 | 'ra' => 'audio/x-realaudio', |
---|
| 132 | 'ram' => 'audio/x-pn-realaudio', |
---|
| 133 | 'ras' => 'image/x-cmu-raster', |
---|
| 134 | 'rgb' => 'image/x-rgb', |
---|
| 135 | 'rm' => 'audio/x-pn-realaudio', |
---|
| 136 | 'roff' => 'application/x-troff', |
---|
| 137 | 'rpm' => 'audio/x-pn-realaudio-plugin', |
---|
| 138 | 'rtf' => 'text/rtf', |
---|
| 139 | 'rtx' => 'text/richtext', |
---|
| 140 | 'sgm' => 'text/sgml', |
---|
| 141 | 'sgml' => 'text/sgml', |
---|
| 142 | 'sh' => 'application/x-sh', |
---|
| 143 | 'shar' => 'application/x-shar', |
---|
| 144 | 'shtml' => 'text/html', |
---|
| 145 | 'silo' => 'model/mesh', |
---|
| 146 | 'sit' => 'application/x-stuffit', |
---|
| 147 | 'skd' => 'application/x-koan', |
---|
| 148 | 'skm' => 'application/x-koan', |
---|
| 149 | 'skp' => 'application/x-koan', |
---|
| 150 | 'skt' => 'application/x-koan', |
---|
| 151 | 'smi' => 'application/smil', |
---|
| 152 | 'smil' => 'application/smil', |
---|
| 153 | 'snd' => 'audio/basic', |
---|
| 154 | 'so' => 'application/octet-stream', |
---|
| 155 | 'spl' => 'application/x-futuresplash', |
---|
| 156 | 'src' => 'application/x-wais-source', |
---|
| 157 | 'stc' => 'application/vnd.sun.xml.calc.template', |
---|
| 158 | 'std' => 'application/vnd.sun.xml.draw.template', |
---|
| 159 | 'sti' => 'application/vnd.sun.xml.impress.template', |
---|
| 160 | 'stw' => 'application/vnd.sun.xml.writer.template', |
---|
| 161 | 'sv4cpio' => 'application/x-sv4cpio', |
---|
| 162 | 'sv4crc' => 'application/x-sv4crc', |
---|
| 163 | 'swf' => 'application/x-shockwave-flash', |
---|
| 164 | 'sxc' => 'application/vnd.sun.xml.calc', |
---|
| 165 | 'sxd' => 'application/vnd.sun.xml.draw', |
---|
| 166 | 'sxg' => 'application/vnd.sun.xml.writer.global', |
---|
| 167 | 'sxi' => 'application/vnd.sun.xml.impress', |
---|
| 168 | 'sxm' => 'application/vnd.sun.xml.math', |
---|
| 169 | 'sxw' => 'application/vnd.sun.xml.writer', |
---|
| 170 | 't' => 'application/x-troff', |
---|
| 171 | 'tar' => 'application/x-tar', |
---|
| 172 | 'tcl' => 'application/x-tcl', |
---|
| 173 | 'tex' => 'application/x-tex', |
---|
| 174 | 'texi' => 'application/x-texinfo', |
---|
| 175 | 'texinfo' => 'application/x-texinfo', |
---|
| 176 | 'tgz' => 'application/x-gtar', |
---|
| 177 | 'tif' => 'image/tiff', |
---|
| 178 | 'tiff' => 'image/tiff', |
---|
| 179 | 'tr' => 'application/x-troff', |
---|
| 180 | 'tsv' => 'text/tab-separated-values', |
---|
| 181 | 'txt' => 'text/plain', |
---|
| 182 | 'ustar' => 'application/x-ustar', |
---|
| 183 | 'vcd' => 'application/x-cdlink', |
---|
| 184 | 'vcf' => 'text/x-vcard', |
---|
| 185 | 'vcs' => 'text/calendar', |
---|
| 186 | 'vfb' => 'text/calendar', |
---|
| 187 | 'vrml' => 'model/vrml', |
---|
| 188 | 'vsd' => 'application/vnd.visio', |
---|
| 189 | 'wav' => 'audio/x-wav', |
---|
| 190 | 'wax' => 'audio/x-ms-wax', |
---|
| 191 | 'wbmp' => 'image/vnd.wap.wbmp', |
---|
| 192 | 'wbxml' => 'application/vnd.wap.wbxml', |
---|
| 193 | 'wm' => 'video/x-ms-wm', |
---|
| 194 | 'wma' => 'audio/x-ms-wma', |
---|
| 195 | 'wmd' => 'application/x-ms-wmd', |
---|
| 196 | 'wml' => 'text/vnd.wap.wml', |
---|
| 197 | 'wmlc' => 'application/vnd.wap.wmlc', |
---|
| 198 | 'wmls' => 'text/vnd.wap.wmlscript', |
---|
| 199 | 'wmlsc' => 'application/vnd.wap.wmlscriptc', |
---|
| 200 | 'wmv' => 'video/x-ms-wmv', |
---|
| 201 | 'wmx' => 'video/x-ms-wmx', |
---|
| 202 | 'wmz' => 'application/x-ms-wmz', |
---|
| 203 | 'wrl' => 'model/vrml', |
---|
| 204 | 'wvx' => 'video/x-ms-wvx', |
---|
| 205 | 'xbm' => 'image/x-xbitmap', |
---|
| 206 | 'xht' => 'application/xhtml+xml', |
---|
| 207 | 'xhtml' => 'application/xhtml+xml', |
---|
| 208 | 'xls' => 'application/vnd.ms-excel', |
---|
| 209 | 'xml' => 'application/xml', |
---|
| 210 | 'xpm' => 'image/x-xpixmap', |
---|
| 211 | 'xsl' => 'text/xml', |
---|
| 212 | 'xwd' => 'image/x-xwindowdump', |
---|
| 213 | 'xyz' => 'chemical/x-xyz', |
---|
| 214 | 'zip' => 'application/zip' |
---|
| 215 | ); |
---|
| 216 | |
---|
| 217 | /** |
---|
| 218 | * |
---|
| 219 | */ |
---|
| 220 | function setUploadPath($path) |
---|
| 221 | { |
---|
| 222 | $path = realpath($path); |
---|
| 223 | |
---|
| 224 | if (!is_dir($path)) { |
---|
| 225 | logMsg(sprintf('Upload directory invalid: %s', $path), LOG_ERR, __FILE__, __LINE__); |
---|
| 226 | } |
---|
| 227 | if (!is_writable($path)) { |
---|
| 228 | logMsg(sprintf('Upload directory not writable: %s', $path), LOG_ERR, __FILE__, __LINE__); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | // Set the default upload path, stripping any extra slashes if needed. |
---|
| 232 | $this->upload_directory_path = preg_replace('!/+$!', '', $path); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | /** |
---|
| 236 | * |
---|
| 237 | */ |
---|
| 238 | function process($form_name, $custom_file_name=null) |
---|
| 239 | { |
---|
| 240 | // Ensure we have a upload directory. |
---|
| 241 | if (!isset($this->upload_directory_path)) { |
---|
| 242 | logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
[105] | 243 | if ($this->display_messages) { |
---|
[1] | 244 | raiseMsg(_("There was a problem with the file upload. Please try again later."), MSG_ERR, __FILE__, __LINE__); |
---|
| 245 | } |
---|
| 246 | return false; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | if (!isset($_FILES[$form_name])) { |
---|
| 250 | logMsg(sprintf(_("Form element %s not posted."), $form_name), LOG_ERR, __FILE__, __LINE__); |
---|
[105] | 251 | if ($this->display_messages) { |
---|
[1] | 252 | raiseMsg(_("There was a problem with the file upload. Please try again."), MSG_ERR, __FILE__, __LINE__); |
---|
| 253 | } |
---|
| 254 | return false; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | if (is_array($_FILES[$form_name]['name'])) { |
---|
| 258 | $files = $_FILES[$form_name]; |
---|
| 259 | } else { |
---|
| 260 | // Convert variables to single-cell array so it will loop. |
---|
| 261 | $files = array( |
---|
| 262 | 'name' => array($_FILES[$form_name]['name']), |
---|
| 263 | 'type' => array($_FILES[$form_name]['type']), |
---|
| 264 | 'tmp_name' => array($_FILES[$form_name]['tmp_name']), |
---|
| 265 | 'error' => array($_FILES[$form_name]['error']), |
---|
| 266 | 'size' => array($_FILES[$form_name]['size']), |
---|
| 267 | ); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | // To keep this script running even if user tries to stop browser. |
---|
| 271 | ignore_user_abort(true); |
---|
| 272 | if (!ini_get('safe_mode')) { |
---|
| 273 | set_time_limit(300); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | $new_file_names = array(); |
---|
| 277 | |
---|
| 278 | $num = sizeof($files['name']); |
---|
| 279 | for ($i=0; $i<$num; $i++) { |
---|
| 280 | $file_path_name = ''; |
---|
| 281 | |
---|
[105] | 282 | if ('' == trim($files['name'][$i])) { |
---|
[1] | 283 | // User may not have attached a file. |
---|
| 284 | continue; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | // Check The php upload error messages. |
---|
| 288 | if (UPLOAD_ERR_INI_SIZE === $files['error'][$i]) { |
---|
[105] | 289 | if ($this->display_messages) { |
---|
[1] | 290 | 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__); |
---|
| 291 | } |
---|
| 292 | 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__); |
---|
| 293 | $this->errors[] = $files['name'][$i]; |
---|
| 294 | continue; |
---|
| 295 | } |
---|
| 296 | if (UPLOAD_ERR_FORM_SIZE === $files['error'][$i]) { |
---|
[105] | 297 | if ($this->display_messages) { |
---|
[1] | 298 | 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__); |
---|
| 299 | } |
---|
| 300 | 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__); |
---|
| 301 | $this->errors[] = $files['name'][$i]; |
---|
| 302 | continue; |
---|
| 303 | } |
---|
| 304 | if (UPLOAD_ERR_PARTIAL === $files['error'][$i]) { |
---|
[105] | 305 | if ($this->display_messages) { |
---|
[1] | 306 | raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it was only partially uploaded."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
| 307 | } |
---|
| 308 | logMsg(sprintf(_("The file %s failed uploading with PHP error %s UPLOAD_ERR_PARTIAL."), $files['error'][$i], $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); |
---|
| 309 | $this->errors[] = $files['name'][$i]; |
---|
| 310 | continue; |
---|
| 311 | } |
---|
| 312 | if (UPLOAD_ERR_NO_FILE === $files['error'][$i]) { |
---|
[105] | 313 | if ($this->display_messages) { |
---|
[1] | 314 | raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: no file was uploaded."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
| 315 | } |
---|
| 316 | 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__); |
---|
| 317 | $this->errors[] = $files['name'][$i]; |
---|
| 318 | continue; |
---|
| 319 | } |
---|
| 320 | if (UPLOAD_ERR_NO_TMP_DIR === $files['error'][$i]) { |
---|
[105] | 321 | if ($this->display_messages) { |
---|
[1] | 322 | raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: temporary upload directory missing."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
| 323 | } |
---|
| 324 | 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__); |
---|
| 325 | $this->errors[] = $files['name'][$i]; |
---|
| 326 | continue; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | // Check to be sure it's an uploaded file. |
---|
| 330 | if (!is_uploaded_file($files['tmp_name'][$i])) { |
---|
[105] | 331 | if ($this->display_messages) { |
---|
[1] | 332 | raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
| 333 | } |
---|
| 334 | logMsg(sprintf(_("The file %s failed is_uploaded_file."), $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); |
---|
| 335 | $this->errors[] = $files['name'][$i]; |
---|
| 336 | continue; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | // Check to be sure the file is not empty. |
---|
| 340 | if ($files['size'][$i] < 1) { |
---|
[105] | 341 | if ($this->display_messages) { |
---|
[1] | 342 | raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: it contains zero bytes."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
| 343 | } |
---|
| 344 | logMsg(sprintf(_("The uploaded file %s contains zero bytes."), $files['name'][$i]), LOG_ERR, __FILE__, __LINE__); |
---|
| 345 | $this->errors[] = $files['name'][$i]; |
---|
| 346 | continue; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | // Check to be sure the file has a valid file extension. |
---|
| 350 | if (!in_array(strtolower($this->getFilenameExtension($files['name'][$i])), $this->valid_file_extensions)) { |
---|
[105] | 351 | if ($this->display_messages) { |
---|
[1] | 352 | 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__); |
---|
| 353 | } |
---|
| 354 | logMsg(sprintf(_("The uploaded file %s has an unrecognised file extension."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__); |
---|
| 355 | $this->errors[] = $files['name'][$i]; |
---|
| 356 | continue; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | // Check to be sure the file has a unique file name. |
---|
[105] | 360 | if (!$this->allow_overwriting && $this->exists($files['name'][$i])) { |
---|
| 361 | if ($this->display_messages) { |
---|
[1] | 362 | raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading: a file with that name already exists."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
| 363 | } |
---|
| 364 | logMsg(sprintf(_("The uploaded file %s doesn't have a unique filename."), $files['name'][$i]), LOG_WARNING, __FILE__, __LINE__); |
---|
| 365 | $this->errors[] = $files['name'][$i]; |
---|
| 366 | continue; |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | // Determine file name. |
---|
| 370 | if ($num == 1) { |
---|
| 371 | // Single upload. |
---|
[286] | 372 | if (isset($custom_file_name)) { |
---|
[287] | 373 | if (is_array($custom_file_name) && sizeof($custom_file_name) == 1 && '' != trim($custom_file_name[0])) { |
---|
[286] | 374 | // Is an array, but just one value. Pull it out. |
---|
[287] | 375 | $file_name = trim($custom_file_name[0]); |
---|
[286] | 376 | raiseMsg(sprintf(_("The file %s has been renamed to %s."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__); |
---|
| 377 | logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
[287] | 378 | } else if (!is_array($custom_file_name) && '' != trim($custom_file_name)) { |
---|
[286] | 379 | // Valid custom file name. |
---|
| 380 | $file_name = $custom_file_name; |
---|
| 381 | raiseMsg(sprintf(_("The file %s has been renamed to %s."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__); |
---|
| 382 | logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 383 | } else { |
---|
| 384 | // Invalid custom file name provided. Use uploaded file name. |
---|
| 385 | $file_name = $files['name'][$i]; |
---|
| 386 | logMsg(sprintf('Custom filename invalid! Using uploaded file name: %s', $file_name), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 387 | } |
---|
[286] | 388 | } else { |
---|
| 389 | // Normal case. Use uploaded file name. |
---|
| 390 | $file_name = $files['name'][$i]; |
---|
| 391 | logMsg(sprintf('Using uploaded file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 392 | } |
---|
[1] | 393 | } else { |
---|
| 394 | // Multiple upload. Final file names must be array. |
---|
| 395 | if (isset($custom_file_name) && is_array($custom_file_name) && '' != $custom_file_name[$i]) { |
---|
| 396 | // Valid custom file name. |
---|
| 397 | $file_name = $custom_file_name[$i]; |
---|
[105] | 398 | if ($this->display_messages) { |
---|
[1] | 399 | raiseMsg(sprintf(_("The file <strong>%s</strong> has been renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__); |
---|
| 400 | } |
---|
| 401 | logMsg(sprintf('Using custom file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 402 | } else { |
---|
| 403 | // Invalid custom file name provided. Use uploaded file name. |
---|
| 404 | $file_name = $files['name'][$i]; |
---|
| 405 | logMsg(sprintf('Using uploaded file name: %s', $file_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | // Clean the file name of bad characters. |
---|
| 410 | $file_name = $this->cleanFileName($file_name); |
---|
| 411 | |
---|
| 412 | // If the file name has no extension, use the mime-type extension. |
---|
| 413 | if (!preg_match('/\.[^.]{1,5}$/', $file_name) && function_exists('mime_content_type')) { |
---|
| 414 | if ($ext = array_search(mime_content_type($files['type'][$i]), $this->mime_extension_map)) { |
---|
| 415 | $file_name .= ".$ext"; |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | // Set the path and file name. |
---|
| 420 | $file_path_name = $this->upload_directory_path . '/' . $file_name; |
---|
| 421 | |
---|
| 422 | // Move the file to the final place. |
---|
| 423 | if (move_uploaded_file($files['tmp_name'][$i], $file_path_name)) { |
---|
| 424 | chmod($file_path_name, $this->dest_file_perms); |
---|
[105] | 425 | if ($this->display_messages) { |
---|
[1] | 426 | raiseMsg(sprintf(_("The file <strong>%s</strong> uploaded successfully."), $files['name'][$i]), MSG_SUCCESS, __FILE__, __LINE__); |
---|
| 427 | } |
---|
| 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). |
---|
[105] | 430 | if ($this->display_messages) { |
---|
[1] | 431 | raiseMsg(sprintf(_("The file <strong>%s</strong> was renamed to <strong>%s</strong>."), $files['name'][$i], $file_name), MSG_NOTICE, __FILE__, __LINE__); |
---|
[105] | 432 | logMsg(sprintf('File successfully renamed: %s -> %s', $files['tmp_name'][$i], $file_path_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 433 | } |
---|
| 434 | } |
---|
| 435 | $new_file_names[] = array( |
---|
| 436 | 'name' => $file_name, |
---|
[285] | 437 | 'mime' => $files['type'][$i], |
---|
[1] | 438 | 'size' => filesize($file_path_name), |
---|
[285] | 439 | 'extension' => $this->getFilenameExtension($file_name), |
---|
| 440 | 'original_index' => $i, |
---|
[1] | 441 | ); |
---|
| 442 | continue; |
---|
| 443 | } else { |
---|
[105] | 444 | if ($this->display_messages) { |
---|
[1] | 445 | raiseMsg(sprintf(_("The file <strong>%s</strong> failed uploading."), $files['name'][$i]), MSG_ERR, __FILE__, __LINE__); |
---|
| 446 | } |
---|
| 447 | logMsg(sprintf(_("Moving file failed: %s -> %s"), $files['tmp_name'][$i], $file_path_name), LOG_ALERT, __FILE__, __LINE__); |
---|
| 448 | $this->errors[] = $files['name'][$i]; |
---|
| 449 | continue; |
---|
| 450 | } |
---|
| 451 | } |
---|
| 452 | return (sizeof($new_file_names) > 0) ? $new_file_names : false; |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | /** |
---|
| 456 | * |
---|
| 457 | */ |
---|
| 458 | function deleteFile($file_name) |
---|
| 459 | { |
---|
| 460 | // Ensure we have a upload directory. |
---|
| 461 | if (!isset($this->upload_directory_path)) { |
---|
| 462 | logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 463 | return false; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | $file_path_name = $this->upload_directory_path . '/' . $file_name; |
---|
| 467 | |
---|
| 468 | if (!is_file($file_path_name)) { |
---|
| 469 | logMsg(sprintf(_("Error deleting nonexistant file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__); |
---|
| 470 | return false; |
---|
| 471 | } else if (unlink($file_path_name)) { |
---|
| 472 | logMsg(sprintf('Deleted file: %s', $file_path_name), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 473 | } else { |
---|
[105] | 474 | if ($this->display_messages) { |
---|
[1] | 475 | raiseMsg(sprintf(_("The file <strong>%s</strong> could not be deleted."), $file_name), MSG_ERROR, __FILE__, __LINE__); |
---|
| 476 | } |
---|
| 477 | logMsg(sprintf(_("Failed deleting file: %s"), $file_path_name), LOG_ERR, __FILE__, __LINE__); |
---|
| 478 | return false; |
---|
| 479 | } |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | /** |
---|
| 483 | * |
---|
| 484 | */ |
---|
| 485 | function moveFile($old_name, $new_name) |
---|
| 486 | { |
---|
| 487 | // Ensure we have an upload directory. |
---|
| 488 | if (!isset($this->upload_directory_path)) { |
---|
| 489 | logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 490 | return false; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | $old_file_path_name = $this->upload_directory_path . '/' . $old_name; |
---|
| 494 | $new_file_path_name = $this->upload_directory_path . '/' . $new_name; |
---|
| 495 | if (file_exists($old_file_path_name)) { |
---|
| 496 | if (!rename($old_file_path_name, $new_file_path_name)) { |
---|
[105] | 497 | if ($this->display_messages) { |
---|
[1] | 498 | raiseMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), MSG_ERR, __FILE__, __LINE__); |
---|
| 499 | } |
---|
| 500 | logMsg(sprintf(_("Error renaming file to %s"), $new_file_path_name), LOG_ERR, __FILE__, __LINE__); |
---|
| 501 | return false; |
---|
| 502 | } |
---|
| 503 | } else { |
---|
[105] | 504 | if ($this->display_messages) { |
---|
[1] | 505 | raiseMsg(sprintf(_("Couldn't rename nonexistant file <strong>%s</strong>."), $old_name), MSG_ERR, __FILE__, __LINE__); |
---|
| 506 | } |
---|
| 507 | logMsg(sprintf(_("Error renaming nonexistant file: %s"), $old_file_path_name), LOG_ERR, __FILE__, __LINE__); |
---|
| 508 | return false; |
---|
| 509 | } |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | /** |
---|
| 513 | * |
---|
| 514 | */ |
---|
| 515 | function exists($file_name) |
---|
| 516 | { |
---|
| 517 | // Ensure we have a upload directory. |
---|
| 518 | if (!isset($this->upload_directory_path)) { |
---|
| 519 | logMsg(sprintf('Upload directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
| 520 | return false; |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | return file_exists($this->upload_directory_path . '/' . $file_name); |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | /** |
---|
[285] | 527 | * Get filename by glob pattern. Searches a directory for an image that matches the |
---|
| 528 | * specified glob pattern and returns the filename of the first file found. |
---|
[1] | 529 | * |
---|
[285] | 530 | * @access public |
---|
| 531 | * @param string $pattern Pattern to match filename. |
---|
| 532 | * @return string filename on success, empty string on failure. |
---|
| 533 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 534 | * @since 15 Nov 2005 20:55:22 |
---|
[1] | 535 | */ |
---|
[285] | 536 | function getFilenameGlob($pattern) |
---|
| 537 | { |
---|
| 538 | $file_list = glob(sprintf('%s/%s', $this->upload_directory_path, $pattern)); |
---|
| 539 | if (isset($file_list[0])) { |
---|
| 540 | return basename($file_list[0]); |
---|
| 541 | } else { |
---|
| 542 | return ''; |
---|
| 543 | } |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | /** |
---|
| 547 | * |
---|
| 548 | */ |
---|
[1] | 549 | function getErrors() |
---|
| 550 | { |
---|
| 551 | return $this->errors; |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | /** |
---|
| 555 | * |
---|
| 556 | */ |
---|
| 557 | function cleanFileName($file_name) |
---|
| 558 | { |
---|
[105] | 559 | $bad = 'áéÃóúà ÚìòùÀëïöÌÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃâêîÎûÃÃÃÃÃñçÃ@'; |
---|
[1] | 560 | $good = 'aeiouaeiouaeiouAEIOUAEIOUAEIOUaeiouAEIOUncCa'; |
---|
| 561 | $file_name = trim($file_name); |
---|
| 562 | $file_name = strtr($file_name, $bad, $good); |
---|
[310] | 563 | $file_name = preg_replace('/[^-\w.,~_=+]+/', '_', $file_name); |
---|
[1] | 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); |
---|
| 574 | return $ext[1]; |
---|
| 575 | } |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | ?> |
---|