00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 class eZMimeType
00057 {
00058
00059
00060
00061 function eZMimeType()
00062 {
00063 $this->SuffixList = array();
00064 $this->PrefixList = array();
00065 $this->MIMEList = array();
00066
00067 foreach ( $this->QuickMIMETypes as $quickMIMEType )
00068 {
00069 $mimeEntry =& $this->MIMEList[$quickMIMEType[1]];
00070 if ( !isset( $mimeEntry ) )
00071 $mimeEntry = array( 'suffixes' => array(),
00072 'prefixes' => false );
00073 $mimeEntry['suffixes'][] = $quickMIMEType[0];
00074 }
00075
00076 eZMimeType::prepareSuffixList( $this->SuffixList, $this->MIMEList );
00077 eZMimeType::preparePrefixList( $this->PrefixList, $this->MIMEList );
00078 }
00079
00080
00081
00082
00083
00084 function defaultMimeType()
00085 {
00086 return array( 'name' => 'application/octet-stream',
00087 'url' => false,
00088 'filename' => false,
00089 'dirpath' => false,
00090 'basename' => false,
00091 'suffix' => false,
00092 'prefix' => false,
00093 'suffixes' => false,
00094 'prefixes' => false,
00095 'is_valid' => true );
00096 }
00097
00098
00099
00100
00101
00102 function defaultValue( $url, $returnDefault )
00103 {
00104 if ( $returnDefault )
00105 {
00106 $mime = eZMimeType::defaultMimeType();
00107 $mime['url'] = $url;
00108 $suffixPos = strpos( $url, '.' );
00109
00110 if ( $suffixPos !== false )
00111 {
00112 $mime['suffix'] = substr( $url, $suffixPos + 1 );
00113 $mime['dirpath'] = dirname( $url );
00114 $mime['basename'] = basename( $url, '.' . $mime['suffix'] );
00115 $mime['filename'] = $mime['basename'] . '.' . $mime['suffix'];
00116 }
00117 else
00118 {
00119 $mime['basename'] = $url;
00120 $mime['suffix'] = false;
00121 }
00122 $mime['is_valid'] = false;
00123 return $mime;
00124 }
00125 else
00126 return false;
00127 }
00128
00129
00130
00131
00132
00133 function changeMIMEType( &$mimeInfo, $mimetype )
00134 {
00135 $mimeInfo['name'] = $mimetype;
00136 $newMimeInfo = eZMimeType::findByName( $mimetype );
00137 $mimeInfo['suffixes'] = $newMimeInfo['suffixes'];
00138 $mimeInfo['prefixes'] = $newMimeInfo['prefixes'];
00139 $mimeInfo['suffix'] = $newMimeInfo['suffix'];
00140 $mimeInfo['prefix'] = $newMimeInfo['prefix'];
00141 $filename = $mimeInfo['filename'];
00142 $dotPosition = strrpos( $filename, '.' );
00143 $basename = $filename;
00144 if ( $dotPosition !== false )
00145 $basename = substr( $filename, 0, $dotPosition );
00146 $mimeInfo['filename'] = $basename . '.' . $mimeInfo['suffix'];
00147 if ( $mimeInfo['dirpath'] )
00148 $mimeInfo['url'] = $mimeInfo['dirpath'] . '/' . $mimeInfo['filename'];
00149 else
00150 $mimeInfo['url'] = $mimeInfo['filename'];
00151 }
00152
00153
00154
00155
00156
00157 function changeBasename( &$mimeInfo, $basename )
00158 {
00159 $mimeInfo['basename'] = $basename;
00160 $mimeInfo['filename'] = $basename . '.' . $mimeInfo['suffix'];
00161 if ( $mimeInfo['dirpath'] )
00162 $mimeInfo['url'] = $mimeInfo['dirpath'] . '/' . $mimeInfo['filename'];
00163 else
00164 $mimeInfo['url'] = $mimeInfo['filename'];
00165 }
00166
00167
00168
00169
00170
00171 function changeDirectoryPath( &$mimeInfo, $dirpath )
00172 {
00173 $mimeInfo['dirpath'] = $dirpath;
00174 if ( $mimeInfo['dirpath'] )
00175 $mimeInfo['url'] = $mimeInfo['dirpath'] . '/' . $mimeInfo['filename'];
00176 else
00177 $mimeInfo['url'] = $mimeInfo['filename'];
00178 }
00179
00180
00181
00182
00183
00184 function changeFileData( &$mimeInfo, $dirpath = false, $basename = false, $suffix = false, $filename = false )
00185 {
00186 if ( $basename !== false )
00187 $mimeInfo['basename'] = $basename;
00188 if ( $suffix !== false )
00189 $mimeInfo['suffix'] = $suffix;
00190 if ( $filename !== false )
00191 {
00192 $mimeInfo['filename'] = $filename;
00193 }
00194 else
00195 {
00196 $mimeInfo['filename'] = $mimeInfo['basename'];
00197 $mimeInfo['filename'] .= '.' . $mimeInfo['suffix'];
00198 }
00199 if ( $dirpath !== false )
00200 $mimeInfo['dirpath'] = $dirpath;
00201
00202 if ( $mimeInfo['dirpath'] )
00203 $mimeInfo['url'] = $mimeInfo['dirpath'] . '/' . $mimeInfo['filename'];
00204 else
00205 $mimeInfo['url'] = $mimeInfo['filename'];
00206 }
00207
00208
00209
00210
00211
00212
00213 function findByName( $mimeName, $returnDefault = true )
00214 {
00215 if ( isset( $this ) and
00216 get_class( $this ) == 'ezmimetype' )
00217 $instance =& $this;
00218 else
00219 $instance = eZMimeType::instance();
00220 $mimeList =& $instance->MIMEList;
00221 if ( isset( $mimeList[$mimeName] ) )
00222 {
00223 $mime = $mimeList[$mimeName];
00224 $mime['name'] = $mimeName;
00225 $mime['url'] = false;
00226 $mime['filename'] = false;
00227 $mime['dirpath'] = false;
00228 $mime['basename'] = false;
00229 $mime['suffix'] = false;
00230 if ( isset( $mime['suffixes'][0] ) )
00231 $mime['suffix'] = $mime['suffixes'][0];
00232 $mime['prefix'] = false;
00233 if ( isset( $mime['prefixes'][0] ) )
00234 $mime['prefix'] = $mime['prefixes'][0];
00235 $mime['is_valid'] = true;
00236 return $mime;
00237 }
00238 return eZMimeType::defaultValue( false, $returnDefault );
00239 }
00240
00241
00242
00243
00244
00245
00246
00247 function findByURL( $url, $returnDefault = true )
00248 {
00249 if ( isset( $this ) and
00250 get_class( $this ) == 'ezmimetype' )
00251 $instance =& $this;
00252 else
00253 $instance = eZMimeType::instance();
00254
00255 $file = $url;
00256 $dirPosition = strrpos( $url, '/' );
00257 if ( $dirPosition !== false )
00258 $file = substr( $url, $dirPosition + 1 );
00259 $suffixPosition = strrpos( $file, '.' );
00260 $suffix = false;
00261 $prefix = false;
00262 $mimeName = false;
00263 if ( $suffixPosition !== false )
00264 {
00265 $suffix = strtolower( substr( $file, $suffixPosition + 1 ) );
00266 if ( $suffix )
00267 {
00268 $subURL = substr( $file, 0, $suffixPosition );
00269 $suffixList =& $instance->SuffixList;
00270 if ( array_key_exists( $suffix, $suffixList ) )
00271 {
00272 $mimeName = $suffixList[$suffix];
00273 }
00274 }
00275 if ( !$mimeName )
00276 {
00277 $prefixPosition = strpos( $file, '.' );
00278 if ( $prefixPosition !== false )
00279 {
00280 $prefix = strtolower( substr( $file, 0, $prefixPosition ) );
00281 }
00282 if ( $prefix )
00283 {
00284 $subURL = substr( $file, $suffixPosition + 1 );
00285 $prefixList =& $instance->PrefixList;
00286 if ( array_key_exists( $prefix, $prefixList ) )
00287 {
00288 $mimeName = $prefixList[$prefix];
00289 }
00290 }
00291 }
00292 if ( $mimeName )
00293 {
00294 $mimeList =& $instance->MIMEList;
00295 if ( array_key_exists( $mimeName, $mimeList ) )
00296 {
00297 $lastDirPosition = strrpos( $url, '/' );
00298 $filename = $url;
00299 $dirpath = false;
00300 if ( $lastDirPosition !== false )
00301 {
00302 $filename = substr( $url, $lastDirPosition + 1 );
00303 $dirpath = substr( $url, 0, $lastDirPosition );
00304 }
00305 $lastDirPosition = strrpos( $subURL, '/' );
00306 $basename = $subURL;
00307 if ( $lastDirPosition !== false )
00308 $basename = substr( $subURL, $lastDirPosition + 1 );
00309 $mime = $mimeList[$mimeName];
00310 $mime['name'] = $mimeName;
00311 $mime['url'] = $url;
00312 $mime['filename'] = $filename;
00313 $mime['dirpath'] = $dirpath;
00314 $mime['basename'] = $basename;
00315 $mime['suffix'] = $suffix;
00316 $mime['prefix'] = $prefix;
00317 $mime['is_valid'] = true;
00318 return $mime;
00319 }
00320 }
00321 }
00322 return eZMimeType::defaultValue( $url, $returnDefault );
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332 function findByFileContents( $url, $returnDefault = true )
00333 {
00334 return eZMimeType::findByURL( $url, $returnDefault );
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 function findByBuffer( $buffer, $length = false, $offset = false, $url = false, $returnDefault = true )
00348 {
00349 return eZMimeType::findByURL( $url, $returnDefault );
00350 }
00351
00352
00353
00354
00355
00356 function instance()
00357 {
00358 $instance =& $GLOBALS['eZMIMETypeInstance'];
00359 if ( !isset( $instance ) )
00360 {
00361 $instance = new eZMimeType();
00362 }
00363 return $instance;
00364 }
00365
00366
00367
00368
00369
00370
00371 function mimeTypeFor( $path, $file )
00372 {
00373 eZDebug::writeWarning( 'eZMimeType::mimeTypeFor() is deprecated, use eZMimeType::findByURL() instead',
00374 'DEPRECATED FUNCTION eZMimeType::mimeTypeFor' );
00375 $url = $path;
00376 if ( $url )
00377 $url .= '/' . $file;
00378 else
00379 $url = $file;
00380 $match = eZMimeType::findByURL( $url, false );
00381 if ( $match )
00382 return $match['name'];
00383 else
00384 return false;
00385 }
00386
00387
00388
00389
00390
00391 function prepareSuffixList( &$suffixList, $mimeList )
00392 {
00393 foreach ( $mimeList as $mimeName => $mimeData )
00394 {
00395 if ( is_array( $mimeData['suffixes'] ) )
00396 {
00397 foreach ( $mimeData['suffixes'] as $suffix )
00398 {
00399 if ( !isset( $suffixList[$suffix] ) )
00400 $suffixList[$suffix] = $mimeName;
00401 }
00402 }
00403 }
00404 }
00405
00406
00407
00408
00409
00410 function preparePrefixList( &$prefixList, $mimeList )
00411 {
00412 foreach ( $mimeList as $mimeName => $mimeData )
00413 {
00414 if ( is_array( $mimeData['prefixes'] ) )
00415 {
00416 foreach ( $mimeData['prefixes'] as $prefix )
00417 {
00418 if ( !isset( $prefixList[$prefix] ) )
00419 $prefixList[$prefix] = $mimeName;
00420 }
00421 }
00422 }
00423 }
00424
00425
00426
00427
00428 var $SuffixList;
00429
00430 var $PrefixList;
00431
00432 var $MIMEList;
00433
00434 var $QuickContentMatch = array(
00435 array( array( 0, 'string', 'GIF87a', 'image/gif' ),
00436 array( 0, 'string', 'GIF89a', 'image/gif' ) )
00437 );
00438
00439
00440
00441 var $QuickMIMETypes = array(
00442 array( 'ezpkg', 'application/x-ezpublish-package' ),
00443 array( 'ez', 'application/andrew-inset' ),
00444 array( 'hqx', 'application/mac-binhex40' ),
00445 array( 'cpt', 'application/mac-compactpro' ),
00446 array( 'doc', 'application/msword' ),
00447 array( 'bin', 'application/octet-stream' ),
00448 array( 'dms', 'application/octet-stream' ),
00449 array( 'lha', 'application/octet-stream' ),
00450 array( 'lzh', 'application/octet-stream' ),
00451 array( 'exe', 'application/octet-stream' ),
00452 array( 'class', 'application/octet-stream' ),
00453 array( 'oda', 'application/oda' ),
00454 array( 'pdf', 'application/pdf' ),
00455 array( 'ps', 'application/postscript' ),
00456 array( 'eps', 'application/postscript' ),
00457 array( 'ai', 'application/postscript' ),
00458 array( 'rtf', 'application/rtf' ),
00459 array( 'sgml', 'application/sgml' ),
00460 array( 'ppt', 'application/vnd.ms-powerpoint' ),
00461 array( 'xls', 'application/vnd.ms-excel' ),
00462 array( 'slc', 'application/vnd.wap.slc' ),
00463 array( 'sic', 'application/vnd.wap.sic' ),
00464 array( 'wmlc', 'application/vnd.wap.wmlc' ),
00465 array( 'wmlsc', 'application/vnd.wap.wmlscriptc' ),
00466 array( 'bcpio', 'application/x-bcpio' ),
00467 array( 'bz2', 'application/x-bzip2' ),
00468 array( 'vcd', 'application/x-cdlink' ),
00469 array( 'pgn', 'application/x-chess-pgn' ),
00470 array( 'cpio', 'application/x-cpio' ),
00471 array( 'csh', 'application/x-csh' ),
00472 array( 'dcr', 'application/x-director' ),
00473 array( 'dir', 'application/x-director' ),
00474 array( 'dxr', 'application/x-director' ),
00475 array( 'dvi', 'application/x-dvi' ),
00476 array( 'spl', 'application/x-futuresplash' ),
00477 array( 'gtar', 'application/x-gtar' ),
00478 array( 'gz', 'application/x-gzip' ),
00479 array( 'tgz', 'application/x-gzip' ),
00480 array( 'hdf', 'application/x-hdf' ),
00481 array( 'js', 'application/x-javascript' ),
00482 array( 'kwd', 'application/x-kword' ),
00483 array( 'kwt', 'application/x-kword' ),
00484 array( 'ksp', 'application/x-kspread' ),
00485 array( 'kpr', 'application/x-kpresenter' ),
00486 array( 'kpt', 'application/x-kpresenter' ),
00487 array( 'chrt', 'application/x-kchart' ),
00488 array( 'kil', 'application/x-killustrator' ),
00489 array( 'skp', 'application/x-koan' ),
00490 array( 'skd', 'application/x-koan' ),
00491 array( 'skt', 'application/x-koan' ),
00492 array( 'skm', 'application/x-koan' ),
00493 array( 'latex', 'application/x-latex' ),
00494 array( 'nc', 'application/x-netcdf' ),
00495 array( 'cdf', 'application/x-netcdf' ),
00496 array( 'rpm', 'application/x-rpm' ),
00497 array( 'sh', 'application/x-sh' ),
00498 array( 'shar', 'application/x-shar' ),
00499 array( 'swf', 'application/x-shockwave-flash' ),
00500 array( 'sit', 'application/x-stuffit' ),
00501 array( 'sv4cpio', 'application/x-sv4cpio' ),
00502 array( 'sv4crc', 'application/x-sv4crc' ),
00503 array( 'tar', 'application/x-tar' ),
00504 array( 'tcl', 'application/x-tcl' ),
00505 array( 'tex', 'application/x-tex' ),
00506 array( 'texinfo', 'application/x-texinfo' ),
00507 array( 'texi', 'application/x-texinfo' ),
00508 array( 't', 'application/x-troff' ),
00509 array( 'tr', 'application/x-troff' ),
00510 array( 'roff', 'application/x-troff' ),
00511 array( 'man', 'application/x-troff-man' ),
00512 array( 'me', 'application/x-troff-me' ),
00513 array( 'ms', 'application/x-troff-ms' ),
00514 array( 'ustar', 'application/x-ustar' ),
00515 array( 'src', 'application/x-wais-source' ),
00516 array( 'zip', 'application/zip' ),
00517 array( 'mid', 'audio/midi' ),
00518 array( 'midi', 'audio/midi' ),
00519 array( 'kar', 'audio/midi' ),
00520 array( 'mpga', 'audio/mpeg' ),
00521 array( 'mp2', 'audio/mpeg' ),
00522 array( 'mp3', 'audio/mpeg' ),
00523 array( 'ra', 'audio/x-realaudio' ),
00524 array( 'pdb', 'chemical/x-pdb' ),
00525 array( 'xyz', 'chemical/x-pdb' ),
00526 array( 'gif', 'image/gif' ),
00527 array( 'ief', 'image/ief' ),
00528 array( 'jpg', 'image/jpeg' ),
00529 array( 'jpeg', 'image/jpeg' ),
00530 array( 'jpe', 'image/jpeg' ),
00531 array( 'png', 'image/png' ),
00532 array( 'tif', 'image/tiff' ),
00533 array( 'tiff', 'image/tiff' ),
00534 array( 'pcx', 'image/x-pcx' ),
00535
00536
00537 array( 'xml', 'text/xml' ),
00538 array( 'svg', 'image/svg+xml' ),
00539 array( 'svgz', 'image/svg+xml' ),
00540 array( 'ent', 'application/xml-external-parsed-entity' ),
00541 array( 'dtd', 'application/xml-dtd' ),
00542 array( 'mod', 'application/xml-dtd' ),
00543 array( 'xsl', 'application/xslt+xml' ),
00544 array( 'xslt', 'application/xslt+xml' ),
00545 array( 'rdf', 'application/rdf+xml' ),
00546 array( 'mml', 'application/mathml+xml' ),
00547
00548 array( 'wbmp', 'image/vnd.wap.wbmp' ),
00549 array( 'ras', 'image/x-cmu-raster' ),
00550 array( 'pnm', 'image/x-portable-anymap' ),
00551 array( 'pbm', 'image/x-portable-bitmap' ),
00552 array( 'pgm', 'image/x-portable-graymap' ),
00553 array( 'ppm', 'image/x-portable-pixmap' ),
00554 array( 'rgb', 'image/x-rgb' ),
00555 array( 'xbm', 'image/x-xbitmap' ),
00556 array( 'xpm', 'image/x-xpixmap' ),
00557 array( 'xwd', 'image/x-xwindowdump' ),
00558 array( 'bmp', 'image/bmp' ),
00559 array( 'pict', 'image/x-pict' ),
00560 array( 'pct', 'image/x-pict' ),
00561 array( 'tga', 'image/tga' ),
00562 array( 'xcf', 'image/x-xcf-gimp' ),
00563 array( 'psd', 'application/x-photoshop' ),
00564 array( 'igs', 'model/iges' ),
00565 array( 'iges', 'model/iges' ),
00566 array( 'msh', 'model/mesh' ),
00567 array( 'mesh', 'model/mesh' ),
00568 array( 'silo', 'model/mesh' ),
00569 array( 'wrl', 'model/vrml' ),
00570 array( 'vrml', 'model/vrml' ),
00571 array( 'css', 'text/css' ),
00572 array( 'txt', 'text/plain' ),
00573 array( 'asc', 'text/plain' ),
00574 array( 'rtx', 'text/richtext' ),
00575 array( 'rtf', 'text/rtf' ),
00576 array( 'sgml', 'text/sgml' ),
00577 array( 'sgm', 'text/sgml' ),
00578 array( 'tsv', 'text/tab-separated-values' ),
00579 array( 'sl', 'text/vnd.wap.sl' ),
00580 array( 'si', 'text/vnd.wap.si' ),
00581 array( 'wml', 'text/vnd.wap.wml' ),
00582 array( 'wmls', 'text/vnd.wap.wmlscript' ),
00583 array( 'etx', 'text/x-setext' ),
00584 array( 'mpeg', 'video/mpeg' ),
00585 array( 'mpg', 'video/mpeg' ),
00586 array( 'mpe', 'video/mpeg' ),
00587 array( 'mov', 'video/quicktime' ),
00588 array( 'qt', 'video/quicktime' ),
00589 array( 'avi', 'video/x-msvideo' ),
00590 array( 'movie', 'video/x-sgi-movie' ),
00591 array( 'ice', 'x-conference/x-cooltalk' ),
00592 array( 'rmm', 'audio/x-pn-realaudio' ),
00593 array( 'ram', 'audio/x-pn-realaudio' ),
00594 array( 'ra', 'audio/vnd.rn-realaudio' ),
00595 array( 'smi', 'application/smil' ),
00596 array( 'smil', 'application/smil' ),
00597 array( 'rt', 'text/vnd.rn-realtext' ),
00598 array( 'rv', 'video/vnd.rn-realvideo' ),
00599 array( 'rf', 'image/vnd.rn-realflash' ),
00600 array( 'swf', 'image/vnd.rn-realflash' ),
00601 array( 'rf', 'application/x-shockwave-flash2-preview' ),
00602 array( 'swf', 'application/x-shockwave-flash2-preview' ),
00603 array( 'flv', 'video/x-flv' ),
00604 array( 'sdp', 'application/sdp' ),
00605 array( 'sdp', 'application/x-sdp' ),
00606 array( 'rm', 'application/vnd.rn-realmedia' ),
00607 array( 'rp', 'image/vnd.rn-realpix' ),
00608 array( 'wav', 'audio/wav' ),
00609 array( 'wav', 'audio/x-wav' ),
00610 array( 'wav', 'audio/x-pn-wav' ),
00611 array( 'wav', 'audio/x-pn-windows-acm' ),
00612 array( 'au', 'audio/basic' ),
00613 array( 'au', 'audio/x-pn-au' ),
00614 array( 'aiff', 'audio/aiff' ),
00615 array( 'af', 'audio/aiff' ),
00616 array( 'aiff', 'audio/x-aiff' ),
00617 array( 'af', 'audio/x-aiff' ),
00618 array( 'aiff', 'audio/x-pn-aiff' ),
00619 array( 'af', 'audio/x-pn-aiff' ),
00620 array( 'html', 'text/html' ),
00621 array( 'htm', 'text/html' ),
00622 array( 'sxw', 'application/vnd.sun.xml.writer' ),
00623 array( 'sxc', 'application/vnd.sun.xml.calc' ),
00624 array( 'sxi', 'application/vnd.sun.xml.impress' ),
00625 array( 'odt', 'application/vnd.oasis.opendocument.text' ),
00626 array( 'ods', 'application/vnd.oasis.opendocument.spreadsheet' ),
00627 array( 'odp', 'application/vnd.oasis.opendocument.presentation' ),
00628 array( 'odg', 'application/vnd.oasis.opendocument.graphics' ),
00629 array( 'odc', 'application/vnd.oasis.opendocument.chart' ),
00630 array( 'odf', 'application/vnd.oasis.opendocument.formula' ),
00631 array( 'odb', 'application/vnd.oasis.opendocument.database' ),
00632 array( 'odi', 'application/vnd.oasis.opendocument.image' ),
00633 array( 'odm', 'application/vnd.oasis.opendocument.text-master' ),
00634 array( 'ott', 'application/vnd.oasis.opendocument.text-template' ),
00635 array( 'ots', 'application/vnd.oasis.opendocument.spreadsheet-template' ),
00636 array( 'otp', 'application/vnd.oasis.opendocument.presentation-template' ),
00637 array( 'otg', 'application/vnd.oasis.opendocument.graphics-template' ),
00638 array( 'asf', 'video/x-ms-asf' ),
00639 array( 'wmv', 'video/x-ms-wmv' )
00640 );
00641 }
00642 ?>