comparison includes/file.inc @ 9:acef7ccb09b5 6.4

Drupal 6.4
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:32:08 +0100
parents fff6d4c8c043
children 589fb7c02327
comparison
equal deleted inserted replaced
8:85cbd6048071 9:acef7ccb09b5
1 <?php 1 <?php
2 // $Id: file.inc,v 1.121.2.2 2008/04/23 18:18:09 dries Exp $ 2 // $Id: file.inc,v 1.121.2.3 2008/08/13 23:59:12 drumm Exp $
3 3
4 /** 4 /**
5 * @file 5 * @file
6 * API for handling file uploads and server file management. 6 * API for handling file uploads and server file management.
7 */ 7 */
525 525
526 // Begin building file object. 526 // Begin building file object.
527 $file = new stdClass(); 527 $file = new stdClass();
528 $file->filename = file_munge_filename(trim(basename($_FILES['files']['name'][$source]), '.'), $extensions); 528 $file->filename = file_munge_filename(trim(basename($_FILES['files']['name'][$source]), '.'), $extensions);
529 $file->filepath = $_FILES['files']['tmp_name'][$source]; 529 $file->filepath = $_FILES['files']['tmp_name'][$source];
530 $file->filemime = $_FILES['files']['type'][$source]; 530 $file->filemime = file_get_mimetype($file->filename);
531 531
532 // Rename potentially executable files, to help prevent exploits. 532 // Rename potentially executable files, to help prevent exploits.
533 if (preg_match('/\.(php|pl|py|cgi|asp|js)$/i', $file->filename) && (substr($file->filename, -4) != '.txt')) { 533 if (preg_match('/\.(php|pl|py|cgi|asp|js)$/i', $file->filename) && (substr($file->filename, -4) != '.txt')) {
534 $file->filemime = 'text/plain'; 534 $file->filemime = 'text/plain';
535 $file->filepath .= '.txt'; 535 $file->filepath .= '.txt';
978 } 978 }
979 return $max_size; 979 return $max_size;
980 } 980 }
981 981
982 /** 982 /**
983 * Determine an Internet Media Type, or MIME type from a filename.
984 *
985 * @param $filename
986 * Name of the file, including extension.
987 * @param $mapping
988 * An optional array of extension to media type mappings in the form
989 * 'extension1|extension2|...' => 'type'.
990 *
991 * @return
992 * The internet media type registered for the extension or application/octet-stream for unknown extensions.
993 */
994 function file_get_mimetype($filename, $mapping = NULL) {
995 if (!is_array($mapping)) {
996 $mapping = variable_get('mime_extension_mapping', array(
997 'ez' => 'application/andrew-inset',
998 'atom' => 'application/atom',
999 'atomcat' => 'application/atomcat+xml',
1000 'atomsrv' => 'application/atomserv+xml',
1001 'cap|pcap' => 'application/cap',
1002 'cu' => 'application/cu-seeme',
1003 'tsp' => 'application/dsptype',
1004 'spl' => 'application/x-futuresplash',
1005 'hta' => 'application/hta',
1006 'jar' => 'application/java-archive',
1007 'ser' => 'application/java-serialized-object',
1008 'class' => 'application/java-vm',
1009 'hqx' => 'application/mac-binhex40',
1010 'cpt' => 'image/x-corelphotopaint',
1011 'nb' => 'application/mathematica',
1012 'mdb' => 'application/msaccess',
1013 'doc|dot' => 'application/msword',
1014 'bin' => 'application/octet-stream',
1015 'oda' => 'application/oda',
1016 'ogg|ogx' => 'application/ogg',
1017 'pdf' => 'application/pdf',
1018 'key' => 'application/pgp-keys',
1019 'pgp' => 'application/pgp-signature',
1020 'prf' => 'application/pics-rules',
1021 'ps|ai|eps' => 'application/postscript',
1022 'rar' => 'application/rar',
1023 'rdf' => 'application/rdf+xml',
1024 'rss' => 'application/rss+xml',
1025 'rtf' => 'application/rtf',
1026 'smi|smil' => 'application/smil',
1027 'wpd' => 'application/wordperfect',
1028 'wp5' => 'application/wordperfect5.1',
1029 'xhtml|xht' => 'application/xhtml+xml',
1030 'xml|xsl' => 'application/xml',
1031 'zip' => 'application/zip',
1032 'cdy' => 'application/vnd.cinderella',
1033 'kml' => 'application/vnd.google-earth.kml+xml',
1034 'kmz' => 'application/vnd.google-earth.kmz',
1035 'xul' => 'application/vnd.mozilla.xul+xml',
1036 'xls|xlb|xlt' => 'application/vnd.ms-excel',
1037 'cat' => 'application/vnd.ms-pki.seccat',
1038 'stl' => 'application/vnd.ms-pki.stl',
1039 'ppt|pps' => 'application/vnd.ms-powerpoint',
1040 'odc' => 'application/vnd.oasis.opendocument.chart',
1041 'odb' => 'application/vnd.oasis.opendocument.database',
1042 'odf' => 'application/vnd.oasis.opendocument.formula',
1043 'odg' => 'application/vnd.oasis.opendocument.graphics',
1044 'otg' => 'application/vnd.oasis.opendocument.graphics-template',
1045 'odi' => 'application/vnd.oasis.opendocument.image',
1046 'odp' => 'application/vnd.oasis.opendocument.presentation',
1047 'otp' => 'application/vnd.oasis.opendocument.presentation-template',
1048 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
1049 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
1050 'odt' => 'application/vnd.oasis.opendocument.text',
1051 'odm' => 'application/vnd.oasis.opendocument.text-master',
1052 'ott' => 'application/vnd.oasis.opendocument.text-template',
1053 'oth' => 'application/vnd.oasis.opendocument.text-web',
1054 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
1055 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
1056 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
1057 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
1058 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
1059 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
1060 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
1061 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
1062 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
1063 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
1064 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
1065 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
1066 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
1067 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
1068 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
1069 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
1070 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
1071 'cod' => 'application/vnd.rim.cod',
1072 'mmf' => 'application/vnd.smaf',
1073 'sdc' => 'application/vnd.stardivision.calc',
1074 'sds' => 'application/vnd.stardivision.chart',
1075 'sda' => 'application/vnd.stardivision.draw',
1076 'sdd' => 'application/vnd.stardivision.impress',
1077 'sdf' => 'application/vnd.stardivision.math',
1078 'sdw' => 'application/vnd.stardivision.writer',
1079 'sgl' => 'application/vnd.stardivision.writer-global',
1080 'sxc' => 'application/vnd.sun.xml.calc',
1081 'stc' => 'application/vnd.sun.xml.calc.template',
1082 'sxd' => 'application/vnd.sun.xml.draw',
1083 'std' => 'application/vnd.sun.xml.draw.template',
1084 'sxi' => 'application/vnd.sun.xml.impress',
1085 'sti' => 'application/vnd.sun.xml.impress.template',
1086 'sxm' => 'application/vnd.sun.xml.math',
1087 'sxw' => 'application/vnd.sun.xml.writer',
1088 'sxg' => 'application/vnd.sun.xml.writer.global',
1089 'stw' => 'application/vnd.sun.xml.writer.template',
1090 'sis' => 'application/vnd.symbian.install',
1091 'vsd' => 'application/vnd.visio',
1092 'wbxml' => 'application/vnd.wap.wbxml',
1093 'wmlc' => 'application/vnd.wap.wmlc',
1094 'wmlsc' => 'application/vnd.wap.wmlscriptc',
1095 'wk' => 'application/x-123',
1096 '7z' => 'application/x-7z-compressed',
1097 'abw' => 'application/x-abiword',
1098 'dmg' => 'application/x-apple-diskimage',
1099 'bcpio' => 'application/x-bcpio',
1100 'torrent' => 'application/x-bittorrent',
1101 'cab' => 'application/x-cab',
1102 'cbr' => 'application/x-cbr',
1103 'cbz' => 'application/x-cbz',
1104 'cdf' => 'application/x-cdf',
1105 'vcd' => 'application/x-cdlink',
1106 'pgn' => 'application/x-chess-pgn',
1107 'cpio' => 'application/x-cpio',
1108 'csh' => 'text/x-csh',
1109 'deb|udeb' => 'application/x-debian-package',
1110 'dcr|dir|dxr' => 'application/x-director',
1111 'dms' => 'application/x-dms',
1112 'wad' => 'application/x-doom',
1113 'dvi' => 'application/x-dvi',
1114 'rhtml' => 'application/x-httpd-eruby',
1115 'flac' => 'application/x-flac',
1116 'pfa|pfb|gsf|pcf|pcf.Z' => 'application/x-font',
1117 'mm' => 'application/x-freemind',
1118 'gnumeric' => 'application/x-gnumeric',
1119 'sgf' => 'application/x-go-sgf',
1120 'gcf' => 'application/x-graphing-calculator',
1121 'gtar|tgz|taz' => 'application/x-gtar',
1122 'hdf' => 'application/x-hdf',
1123 'phtml|pht|php' => 'application/x-httpd-php',
1124 'phps' => 'application/x-httpd-php-source',
1125 'php3' => 'application/x-httpd-php3',
1126 'php3p' => 'application/x-httpd-php3-preprocessed',
1127 'php4' => 'application/x-httpd-php4',
1128 'ica' => 'application/x-ica',
1129 'ins|isp' => 'application/x-internet-signup',
1130 'iii' => 'application/x-iphone',
1131 'iso' => 'application/x-iso9660-image',
1132 'jnlp' => 'application/x-java-jnlp-file',
1133 'js' => 'application/x-javascript',
1134 'jmz' => 'application/x-jmol',
1135 'chrt' => 'application/x-kchart',
1136 'kil' => 'application/x-killustrator',
1137 'skp|skd|skt|skm' => 'application/x-koan',
1138 'kpr|kpt' => 'application/x-kpresenter',
1139 'ksp' => 'application/x-kspread',
1140 'kwd|kwt' => 'application/x-kword',
1141 'latex' => 'application/x-latex',
1142 'lha' => 'application/x-lha',
1143 'lyx' => 'application/x-lyx',
1144 'lzh' => 'application/x-lzh',
1145 'lzx' => 'application/x-lzx',
1146 'frm|maker|frame|fm|fb|book|fbdoc' => 'application/x-maker',
1147 'mif' => 'application/x-mif',
1148 'wmd' => 'application/x-ms-wmd',
1149 'wmz' => 'application/x-ms-wmz',
1150 'com|exe|bat|dll' => 'application/x-msdos-program',
1151 'msi' => 'application/x-msi',
1152 'nc' => 'application/x-netcdf',
1153 'pac' => 'application/x-ns-proxy-autoconfig',
1154 'nwc' => 'application/x-nwc',
1155 'o' => 'application/x-object',
1156 'oza' => 'application/x-oz-application',
1157 'p7r' => 'application/x-pkcs7-certreqresp',
1158 'crl' => 'application/x-pkcs7-crl',
1159 'pyc|pyo' => 'application/x-python-code',
1160 'qtl' => 'application/x-quicktimeplayer',
1161 'rpm' => 'application/x-redhat-package-manager',
1162 'sh' => 'text/x-sh',
1163 'shar' => 'application/x-shar',
1164 'swf|swfl' => 'application/x-shockwave-flash',
1165 'sit|sitx' => 'application/x-stuffit',
1166 'sv4cpio' => 'application/x-sv4cpio',
1167 'sv4crc' => 'application/x-sv4crc',
1168 'tar' => 'application/x-tar',
1169 'tcl' => 'application/x-tcl',
1170 'gf' => 'application/x-tex-gf',
1171 'pk' => 'application/x-tex-pk',
1172 'texinfo|texi' => 'application/x-texinfo',
1173 '~|%|bak|old|sik' => 'application/x-trash',
1174 't|tr|roff' => 'application/x-troff',
1175 'man' => 'application/x-troff-man',
1176 'me' => 'application/x-troff-me',
1177 'ms' => 'application/x-troff-ms',
1178 'ustar' => 'application/x-ustar',
1179 'src' => 'application/x-wais-source',
1180 'wz' => 'application/x-wingz',
1181 'crt' => 'application/x-x509-ca-cert',
1182 'xcf' => 'application/x-xcf',
1183 'fig' => 'application/x-xfig',
1184 'xpi' => 'application/x-xpinstall',
1185 'au|snd' => 'audio/basic',
1186 'mid|midi|kar' => 'audio/midi',
1187 'mpga|mpega|mp2|mp3|m4a' => 'audio/mpeg',
1188 'm3u' => 'audio/x-mpegurl',
1189 'oga|spx' => 'audio/ogg',
1190 'sid' => 'audio/prs.sid',
1191 'aif|aiff|aifc' => 'audio/x-aiff',
1192 'gsm' => 'audio/x-gsm',
1193 'wma' => 'audio/x-ms-wma',
1194 'wax' => 'audio/x-ms-wax',
1195 'ra|rm|ram' => 'audio/x-pn-realaudio',
1196 'ra' => 'audio/x-realaudio',
1197 'pls' => 'audio/x-scpls',
1198 'sd2' => 'audio/x-sd2',
1199 'wav' => 'audio/x-wav',
1200 'alc' => 'chemical/x-alchemy',
1201 'cac|cache' => 'chemical/x-cache',
1202 'csf' => 'chemical/x-cache-csf',
1203 'cbin|cascii|ctab' => 'chemical/x-cactvs-binary',
1204 'cdx' => 'chemical/x-cdx',
1205 'cer' => 'chemical/x-cerius',
1206 'c3d' => 'chemical/x-chem3d',
1207 'chm' => 'chemical/x-chemdraw',
1208 'cif' => 'chemical/x-cif',
1209 'cmdf' => 'chemical/x-cmdf',
1210 'cml' => 'chemical/x-cml',
1211 'cpa' => 'chemical/x-compass',
1212 'bsd' => 'chemical/x-crossfire',
1213 'csml|csm' => 'chemical/x-csml',
1214 'ctx' => 'chemical/x-ctx',
1215 'cxf|cef' => 'chemical/x-cxf',
1216 'emb|embl' => 'chemical/x-embl-dl-nucleotide',
1217 'spc' => 'chemical/x-galactic-spc',
1218 'inp|gam|gamin' => 'chemical/x-gamess-input',
1219 'fch|fchk' => 'chemical/x-gaussian-checkpoint',
1220 'cub' => 'chemical/x-gaussian-cube',
1221 'gau|gjc|gjf' => 'chemical/x-gaussian-input',
1222 'gal' => 'chemical/x-gaussian-log',
1223 'gcg' => 'chemical/x-gcg8-sequence',
1224 'gen' => 'chemical/x-genbank',
1225 'hin' => 'chemical/x-hin',
1226 'istr|ist' => 'chemical/x-isostar',
1227 'jdx|dx' => 'chemical/x-jcamp-dx',
1228 'kin' => 'chemical/x-kinemage',
1229 'mcm' => 'chemical/x-macmolecule',
1230 'mmd|mmod' => 'chemical/x-macromodel-input',
1231 'mol' => 'chemical/x-mdl-molfile',
1232 'rd' => 'chemical/x-mdl-rdfile',
1233 'rxn' => 'chemical/x-mdl-rxnfile',
1234 'sd|sdf' => 'chemical/x-mdl-sdfile',
1235 'tgf' => 'chemical/x-mdl-tgf',
1236 'mcif' => 'chemical/x-mmcif',
1237 'mol2' => 'chemical/x-mol2',
1238 'b' => 'chemical/x-molconn-Z',
1239 'gpt' => 'chemical/x-mopac-graph',
1240 'mop|mopcrt|mpc|dat|zmt' => 'chemical/x-mopac-input',
1241 'moo' => 'chemical/x-mopac-out',
1242 'mvb' => 'chemical/x-mopac-vib',
1243 'asn' => 'chemical/x-ncbi-asn1-spec',
1244 'prt|ent' => 'chemical/x-ncbi-asn1-ascii',
1245 'val|aso' => 'chemical/x-ncbi-asn1-binary',
1246 'pdb|ent' => 'chemical/x-pdb',
1247 'ros' => 'chemical/x-rosdal',
1248 'sw' => 'chemical/x-swissprot',
1249 'vms' => 'chemical/x-vamas-iso14976',
1250 'vmd' => 'chemical/x-vmd',
1251 'xtel' => 'chemical/x-xtel',
1252 'xyz' => 'chemical/x-xyz',
1253 'gif' => 'image/gif',
1254 'ief' => 'image/ief',
1255 'jpeg|jpg|jpe' => 'image/jpeg',
1256 'pcx' => 'image/pcx',
1257 'png' => 'image/png',
1258 'svg|svgz' => 'image/svg+xml',
1259 'tiff|tif' => 'image/tiff',
1260 'djvu|djv' => 'image/vnd.djvu',
1261 'wbmp' => 'image/vnd.wap.wbmp',
1262 'ras' => 'image/x-cmu-raster',
1263 'cdr' => 'image/x-coreldraw',
1264 'pat' => 'image/x-coreldrawpattern',
1265 'cdt' => 'image/x-coreldrawtemplate',
1266 'ico' => 'image/x-icon',
1267 'art' => 'image/x-jg',
1268 'jng' => 'image/x-jng',
1269 'bmp' => 'image/x-ms-bmp',
1270 'psd' => 'image/x-photoshop',
1271 'pnm' => 'image/x-portable-anymap',
1272 'pbm' => 'image/x-portable-bitmap',
1273 'pgm' => 'image/x-portable-graymap',
1274 'ppm' => 'image/x-portable-pixmap',
1275 'rgb' => 'image/x-rgb',
1276 'xbm' => 'image/x-xbitmap',
1277 'xpm' => 'image/x-xpixmap',
1278 'xwd' => 'image/x-xwindowdump',
1279 'eml' => 'message/rfc822',
1280 'igs|iges' => 'model/iges',
1281 'msh|mesh|silo' => 'model/mesh',
1282 'wrl|vrml' => 'model/vrml',
1283 'ics|icz' => 'text/calendar',
1284 'css' => 'text/css',
1285 'csv' => 'text/csv',
1286 '323' => 'text/h323',
1287 'html|htm|shtml' => 'text/html',
1288 'uls' => 'text/iuls',
1289 'mml' => 'text/mathml',
1290 'asc|txt|text|pot' => 'text/plain',
1291 'rtx' => 'text/richtext',
1292 'sct|wsc' => 'text/scriptlet',
1293 'tm|ts' => 'text/texmacs',
1294 'tsv' => 'text/tab-separated-values',
1295 'jad' => 'text/vnd.sun.j2me.app-descriptor',
1296 'wml' => 'text/vnd.wap.wml',
1297 'wmls' => 'text/vnd.wap.wmlscript',
1298 'bib' => 'text/x-bibtex',
1299 'boo' => 'text/x-boo',
1300 'h++|hpp|hxx|hh' => 'text/x-c++hdr',
1301 'c++|cpp|cxx|cc' => 'text/x-c++src',
1302 'h' => 'text/x-chdr',
1303 'htc' => 'text/x-component',
1304 'c' => 'text/x-csrc',
1305 'd' => 'text/x-dsrc',
1306 'diff|patch' => 'text/x-diff',
1307 'hs' => 'text/x-haskell',
1308 'java' => 'text/x-java',
1309 'lhs' => 'text/x-literate-haskell',
1310 'moc' => 'text/x-moc',
1311 'p|pas' => 'text/x-pascal',
1312 'gcd' => 'text/x-pcs-gcd',
1313 'pl|pm' => 'text/x-perl',
1314 'py' => 'text/x-python',
1315 'etx' => 'text/x-setext',
1316 'tcl|tk' => 'text/x-tcl',
1317 'tex|ltx|sty|cls' => 'text/x-tex',
1318 'vcs' => 'text/x-vcalendar',
1319 'vcf' => 'text/x-vcard',
1320 '3gp' => 'video/3gpp',
1321 'dl' => 'video/dl',
1322 'dif|dv' => 'video/dv',
1323 'fli' => 'video/fli',
1324 'gl' => 'video/gl',
1325 'mpeg|mpg|mpe' => 'video/mpeg',
1326 'mp4' => 'video/mp4',
1327 'ogv' => 'video/ogg',
1328 'qt|mov' => 'video/quicktime',
1329 'mxu' => 'video/vnd.mpegurl',
1330 'lsf|lsx' => 'video/x-la-asf',
1331 'mng' => 'video/x-mng',
1332 'asf|asx' => 'video/x-ms-asf',
1333 'wm' => 'video/x-ms-wm',
1334 'wmv' => 'video/x-ms-wmv',
1335 'wmx' => 'video/x-ms-wmx',
1336 'wvx' => 'video/x-ms-wvx',
1337 'avi' => 'video/x-msvideo',
1338 'movie' => 'video/x-sgi-movie',
1339 'ice' => 'x-conference/x-cooltalk',
1340 'sisx' => 'x-epoc/x-sisx-app',
1341 'vrm|vrml|wrl' => 'x-world/x-vrml',
1342 'xps' => 'application/vnd.ms-xpsdocument',
1343 ));
1344 }
1345 foreach ($mapping as $ext_preg => $mime_match) {
1346 if (preg_match('!\.('. $ext_preg .')$!i', $filename)) {
1347 return $mime_match;
1348 }
1349 }
1350
1351 return 'application/octet-stream';
1352 }
1353
1354 /**
983 * @} End of "defgroup file". 1355 * @} End of "defgroup file".
984 */ 1356 */