Using the freedesktop.org.xml file as a starting point, I wrote a simple PHP program to generate two "getMIMEtype()" functions, one for PHP and the other for Javascript.
MakeGetMIMEtype.php:
<?php // make getMIMEtype() functions for PHP and JS, starting from freedesktop.org.xml
// create document header echo ' <html>
<head>
<title>Make getMIMEtype</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
';
$xmlFilename = "freedesktop.org.xml"; // input file $phpFilename = "getMIMEtype.php"; // output file $jsFilename = "getMIMEtype.js"; // output file
$xml = simplexml_load_file($xmlFilename) or die ("XML error: Unable to load XML file ".$xmlFilename."!"); //print_r($xml); // php file $phpHandle = fopen($phpFilename, "wb"); fwrite($phpHandle, '<?PHP function getMIMEtype($filename) {
- $fn = strtolower(basename($filename));
' ); // javascript file $jsHandle = fopen($jsFilename, "wb"); fwrite($jsHandle, ' function getMIMEtype(filename) {
- var fn = new String(filename); var fn1 = new String(); fn1 = fn.match(/[\/\\\\][^\/\\\\]*$/);
if(fn1!=null && fn1.length>0) fn = fn1; fn = fn.toLowerCase();
' ); foreach ($xml->{'mime-type'} as $mimetype) {
foreach($mimetype->glob as $glob) {
//echo $mimetype["type"] . '--->' . $glob["pattern"] . '<BR/>'; $match = $glob["pattern"]; $match = preg_replace('/\./', '\\.', $match); // convert glob to regexp $match = preg_replace('/\+/', '[+]', $match); // convert glob to regexp if(substr($match,0,1)=="*") $match = "." . $match; // convert glob to regexp $match = "^" . $match; // force match of whole string // php file fwrite($phpHandle, ' if(preg_match("/'.$match.'$/",$fn)) {
');
- fwrite($phpHandle, ' return("'.$mimetype["type"].'"); }
');
- // javascript file
fwrite($jsHandle, ' if(fn.search(/'.$match.'$/)>=0) {
');
- fwrite($jsHandle, ' return("'.$mimetype["type"].'"); }
');
- }
} // php file fwrite($phpHandle, ' return("binary/octet-stream"); } ?>'); fclose($phpHandle); chmod($phpFilename, 0666); // javascript file fwrite($jsHandle, ' return("binary/octet-stream"); }'); fclose($jsHandle); chmod($jsFilename, 0666);
echo $phpFilename . ' CREATED<BR/>'; echo $jsFilename . ' CREATED<BR/>';
// create document footer echo '
</body>
</html> '; ?>
END - MakeGetMIMEtype.php


