r23055 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23054‎ | r23055 | r23056 >
Date:06:33, 18 June 2007
Author:tstarling
Status:old
Tags:
Comment:
* Allow the extension used for mime type detection to be different from the temporary file extension
* Fixed detection of script type from shell-style #! line
* When loading type info files, don't rely on $IP being the working directory
* Formatting
Modified paths:
  • /trunk/phase3/includes/MimeMagic.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/MimeMagic.php
@@ -111,56 +111,67 @@
112112 * --- load mime.types ---
113113 */
114114
115 - global $wgMimeTypeFile;
 115+ global $wgMimeTypeFile, $IP;
116116
117 - $types= MM_WELL_KNOWN_MIME_TYPES;
 117+ $types = MM_WELL_KNOWN_MIME_TYPES;
118118
119 - if ($wgMimeTypeFile) {
120 - if (is_file($wgMimeTypeFile) and is_readable($wgMimeTypeFile)) {
121 - wfDebug("MimeMagic::MimeMagic: loading mime types from $wgMimeTypeFile\n");
122 -
123 - $types.= "\n";
124 - $types.= file_get_contents($wgMimeTypeFile);
 119+ if ( $wgMimeTypeFile == 'includes/mime.types' ) {
 120+ $wgMimeTypeFile = "$IP/$wgMimeTypeFile";
 121+ }
 122+
 123+ if ( $wgMimeTypeFile ) {
 124+ if ( is_file( $wgMimeTypeFile ) and is_readable( $wgMimeTypeFile ) ) {
 125+ wfDebug( __METHOD__.": loading mime types from $wgMimeTypeFile\n" );
 126+ $types .= "\n";
 127+ $types .= file_get_contents( $wgMimeTypeFile );
 128+ } else {
 129+ wfDebug( __METHOD__.": can't load mime types from $wgMimeTypeFile\n" );
125130 }
126 - else wfDebug("MimeMagic::MimeMagic: can't load mime types from $wgMimeTypeFile\n");
 131+ } else {
 132+ wfDebug( __METHOD__.": no mime types file defined, using build-ins only.\n" );
127133 }
128 - else wfDebug("MimeMagic::MimeMagic: no mime types file defined, using build-ins only.\n");
129134
130 - $types= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$types);
131 - $types= str_replace("\t"," ",$types);
 135+ $types = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $types );
 136+ $types = str_replace( "\t", " ", $types );
132137
133 - $this->mMimeToExt= array();
134 - $this->mToMime= array();
 138+ $this->mMimeToExt = array();
 139+ $this->mToMime = array();
135140
136 - $lines= explode("\n",$types);
137 - foreach ($lines as $s) {
138 - $s= trim($s);
139 - if (empty($s)) continue;
140 - if (strpos($s,'#')===0) continue;
 141+ $lines = explode( "\n",$types );
 142+ foreach ( $lines as $s ) {
 143+ $s = trim( $s );
 144+ if ( empty( $s ) ) continue;
 145+ if ( strpos( $s, '#' ) === 0 ) continue;
141146
142 - $s= strtolower($s);
143 - $i= strpos($s,' ');
 147+ $s = strtolower( $s );
 148+ $i = strpos( $s, ' ' );
144149
145 - if ($i===false) continue;
 150+ if ( $i === false ) continue;
146151
147152 #print "processing MIME line $s<br>";
148153
149 - $mime= substr($s,0,$i);
150 - $ext= trim(substr($s,$i+1));
 154+ $mime = substr( $s, 0, $i );
 155+ $ext = trim( substr($s, $i+1 ) );
151156
152 - if (empty($ext)) continue;
 157+ if ( empty( $ext ) ) continue;
153158
154 - if ( !empty($this->mMimeToExt[$mime])) $this->mMimeToExt[$mime] .= ' '.$ext;
155 - else $this->mMimeToExt[$mime]= $ext;
 159+ if ( !empty( $this->mMimeToExt[$mime] ) ) {
 160+ $this->mMimeToExt[$mime] .= ' ' . $ext;
 161+ } else {
 162+ $this->mMimeToExt[$mime] = $ext;
 163+ }
156164
157 - $extensions= explode(' ',$ext);
 165+ $extensions = explode( ' ', $ext );
158166
159 - foreach ($extensions as $e) {
160 - $e= trim($e);
161 - if (empty($e)) continue;
 167+ foreach ( $extensions as $e ) {
 168+ $e = trim( $e );
 169+ if ( empty( $e ) ) continue;
162170
163 - if ( !empty($this->mExtToMime[$e])) $this->mExtToMime[$e] .= ' '.$mime;
164 - else $this->mExtToMime[$e]= $mime;
 171+ if ( !empty( $this->mExtToMime[$e] ) ) {
 172+ $this->mExtToMime[$e] .= ' ' . $mime;
 173+ } else {
 174+ $this->mExtToMime[$e] = $mime;
 175+ }
165176 }
166177 }
167178
@@ -169,62 +180,69 @@
170181 */
171182
172183 global $wgMimeInfoFile;
 184+ if ( $wgMimeInfoFile == 'includes/mime.info' ) {
 185+ $wgMimeInfoFile = "$IP/$wgMimeInfoFile";
 186+ }
173187
174 - $info= MM_WELL_KNOWN_MIME_INFO;
 188+ $info = MM_WELL_KNOWN_MIME_INFO;
175189
176 - if ($wgMimeInfoFile) {
177 - if (is_file($wgMimeInfoFile) and is_readable($wgMimeInfoFile)) {
178 - wfDebug("MimeMagic::MimeMagic: loading mime info from $wgMimeInfoFile\n");
179 -
180 - $info.= "\n";
181 - $info.= file_get_contents($wgMimeInfoFile);
 190+ if ( $wgMimeInfoFile ) {
 191+ if ( is_file( $wgMimeInfoFile ) and is_readable( $wgMimeInfoFile ) ) {
 192+ wfDebug( __METHOD__.": loading mime info from $wgMimeInfoFile\n" );
 193+ $info .= "\n";
 194+ $info .= file_get_contents( $wgMimeInfoFile );
 195+ } else {
 196+ wfDebug(__METHOD__.": can't load mime info from $wgMimeInfoFile\n");
182197 }
183 - else wfDebug("MimeMagic::MimeMagic: can't load mime info from $wgMimeInfoFile\n");
 198+ } else {
 199+ wfDebug(__METHOD__.": no mime info file defined, using build-ins only.\n");
184200 }
185 - else wfDebug("MimeMagic::MimeMagic: no mime info file defined, using build-ins only.\n");
186201
187 - $info= str_replace(array("\r\n","\n\r","\n\n","\r\r","\r"),"\n",$info);
188 - $info= str_replace("\t"," ",$info);
 202+ $info = str_replace( array( "\r\n", "\n\r", "\n\n", "\r\r", "\r" ), "\n", $info);
 203+ $info = str_replace( "\t", " ", $info );
189204
190 - $this->mMimeTypeAliases= array();
191 - $this->mMediaTypes= array();
 205+ $this->mMimeTypeAliases = array();
 206+ $this->mMediaTypes = array();
192207
193 - $lines= explode("\n",$info);
194 - foreach ($lines as $s) {
195 - $s= trim($s);
196 - if (empty($s)) continue;
197 - if (strpos($s,'#')===0) continue;
 208+ $lines = explode( "\n", $info );
 209+ foreach ( $lines as $s ) {
 210+ $s = trim( $s );
 211+ if ( empty( $s ) ) continue;
 212+ if ( strpos( $s, '#' ) === 0 ) continue;
198213
199 - $s= strtolower($s);
200 - $i= strpos($s,' ');
 214+ $s = strtolower( $s );
 215+ $i = strpos( $s, ' ' );
201216
202 - if ($i===false) continue;
 217+ if ( $i === false ) continue;
203218
204219 #print "processing MIME INFO line $s<br>";
205220
206 - $match= array();
207 - if (preg_match('!\[\s*(\w+)\s*\]!',$s,$match)) {
208 - $s= preg_replace('!\[\s*(\w+)\s*\]!','',$s);
209 - $mtype= trim(strtoupper($match[1]));
 221+ $match = array();
 222+ if ( preg_match( '!\[\s*(\w+)\s*\]!', $s, $match ) ) {
 223+ $s = preg_replace( '!\[\s*(\w+)\s*\]!', '', $s );
 224+ $mtype = trim( strtoupper( $match[1] ) );
 225+ } else {
 226+ $mtype = MEDIATYPE_UNKNOWN;
210227 }
211 - else $mtype= MEDIATYPE_UNKNOWN;
212228
213 - $m= explode(' ',$s);
 229+ $m = explode( ' ', $s );
214230
215 - if (!isset($this->mMediaTypes[$mtype])) $this->mMediaTypes[$mtype]= array();
 231+ if ( !isset( $this->mMediaTypes[$mtype] ) ) {
 232+ $this->mMediaTypes[$mtype] = array();
 233+ }
216234
217 - foreach ($m as $mime) {
218 - $mime= trim($mime);
219 - if (empty($mime)) continue;
 235+ foreach ( $m as $mime ) {
 236+ $mime = trim( $mime );
 237+ if ( empty( $mime ) ) continue;
220238
221 - $this->mMediaTypes[$mtype][]= $mime;
 239+ $this->mMediaTypes[$mtype][] = $mime;
222240 }
223241
224 - if (sizeof($m)>1) {
225 - $main= $m[0];
226 - for ($i=1; $i<sizeof($m); $i+= 1) {
227 - $mime= $m[$i];
228 - $this->mMimeTypeAliases[$mime]= $main;
 242+ if ( sizeof( $m ) > 1 ) {
 243+ $main = $m[0];
 244+ for ( $i=1; $i<sizeof($m); $i += 1 ) {
 245+ $mime = $m[$i];
 246+ $this->mMimeTypeAliases[$mime] = $main;
229247 }
230248 }
231249 }
@@ -244,14 +262,14 @@
245263 /** returns a list of file extensions for a given mime type
246264 * as a space separated string.
247265 */
248 - function getExtensionsForType($mime) {
249 - $mime= strtolower($mime);
 266+ function getExtensionsForType( $mime ) {
 267+ $mime = strtolower( $mime );
250268
251 - $r= @$this->mMimeToExt[$mime];
 269+ $r = @$this->mMimeToExt[$mime];
252270
253 - if (@!$r and isset($this->mMimeTypeAliases[$mime])) {
254 - $mime= $this->mMimeTypeAliases[$mime];
255 - $r= @$this->mMimeToExt[$mime];
 271+ if ( @!$r and isset( $this->mMimeTypeAliases[$mime] ) ) {
 272+ $mime = $this->mMimeTypeAliases[$mime];
 273+ $r = @$this->mMimeToExt[$mime];
256274 }
257275
258276 return $r;
@@ -260,22 +278,22 @@
261279 /** returns a list of mime types for a given file extension
262280 * as a space separated string.
263281 */
264 - function getTypesForExtension($ext) {
265 - $ext= strtolower($ext);
 282+ function getTypesForExtension( $ext ) {
 283+ $ext = strtolower( $ext );
266284
267 - $r= isset( $this->mExtToMime[$ext] ) ? $this->mExtToMime[$ext] : null;
 285+ $r = isset( $this->mExtToMime[$ext] ) ? $this->mExtToMime[$ext] : null;
268286 return $r;
269287 }
270288
271289 /** returns a single mime type for a given file extension.
272290 * This is always the first type from the list returned by getTypesForExtension($ext).
273291 */
274 - function guessTypesForExtension($ext) {
275 - $m= $this->getTypesForExtension( $ext );
276 - if( is_null($m) ) return NULL;
 292+ function guessTypesForExtension( $ext ) {
 293+ $m = $this->getTypesForExtension( $ext );
 294+ if ( is_null( $m ) ) return NULL;
277295
278 - $m= trim( $m );
279 - $m= preg_replace('/\s.*$/','',$m);
 296+ $m = trim( $m );
 297+ $m = preg_replace( '/\s.*$/', '', $m );
280298
281299 return $m;
282300 }
@@ -285,17 +303,17 @@
286304 * returns true if a match was found, NULL if the mime type is unknown,
287305 * and false if the mime type is known but no matches where found.
288306 */
289 - function isMatchingExtension($extension,$mime) {
290 - $ext= $this->getExtensionsForType($mime);
 307+ function isMatchingExtension( $extension, $mime ) {
 308+ $ext = $this->getExtensionsForType( $mime );
291309
292 - if (!$ext) {
 310+ if ( !$ext ) {
293311 return NULL; //unknown
294312 }
295313
296 - $ext= explode(' ',$ext);
 314+ $ext = explode( ' ', $ext );
297315
298 - $extension= strtolower($extension);
299 - if (in_array($extension,$ext)) {
 316+ $extension = strtolower( $extension );
 317+ if ( in_array( $extension, $ext ) ) {
300318 return true;
301319 }
302320
@@ -347,13 +365,13 @@
348366 * or misinterpreter by the default mime detection (namely xml based formats like XHTML or SVG).
349367 *
350368 * @param string $file The file to check
351 - * @param bool $useExt switch for allowing to use the file extension to guess the mime type. true by default.
 369+ * @param mixed $ext The file extension, or true to extract it from the filename.
 370+ * Set it to false to ignore the extension.
352371 *
353372 * @return string the mime type of $file
354373 */
355 - function guessMimeType( $file, $useExt=true ) {
356 - $fname = 'MimeMagic::guessMimeType';
357 - $mime= $this->detectMimeType($file,$useExt);
 374+ function guessMimeType( $file, $ext = true ) {
 375+ $mime = $this->detectMimeType( $file, $ext );
358376
359377 // Read a chunk of the file
360378 $f = fopen( $file, "rt" );
@@ -369,67 +387,88 @@
370388 $mime = "application/x-msmetafile";
371389 }
372390
373 - if (strpos($mime,"text/")===0 || $mime==="application/xml") {
 391+ if ( strpos( $mime, "text/" ) === 0 || $mime === "application/xml" ) {
374392
375 - $xml_type= NULL;
376 - $script_type= NULL;
 393+ $xml_type = NULL;
 394+ $script_type = NULL;
377395
378396 /*
379397 * look for XML formats (XHTML and SVG)
380398 */
381 - if ($mime==="text/sgml" ||
382 - $mime==="text/plain" ||
383 - $mime==="text/html" ||
384 - $mime==="text/xml" ||
385 - $mime==="application/xml") {
 399+ if ($mime === "text/sgml" ||
 400+ $mime === "text/plain" ||
 401+ $mime === "text/html" ||
 402+ $mime === "text/xml" ||
 403+ $mime === "application/xml") {
386404
387 - if (substr($head,0,5)=="<?xml") $xml_type= "ASCII";
388 - elseif (substr($head,0,8)=="\xef\xbb\xbf<?xml") $xml_type= "UTF-8";
389 - elseif (substr($head,0,10)=="\xfe\xff\x00<\x00?\x00x\x00m\x00l") $xml_type= "UTF-16BE";
390 - elseif (substr($head,0,10)=="\xff\xfe<\x00?\x00x\x00m\x00l\x00") $xml_type= "UTF-16LE";
 405+ if ( substr( $head, 0, 5 ) == "<?xml" ) {
 406+ $xml_type = "ASCII";
 407+ } elseif ( substr( $head, 0, 8 ) == "\xef\xbb\xbf<?xml") {
 408+ $xml_type = "UTF-8";
 409+ } elseif ( substr( $head, 0, 10 ) == "\xfe\xff\x00<\x00?\x00x\x00m\x00l" ) {
 410+ $xml_type = "UTF-16BE";
 411+ } elseif ( substr( $head, 0, 10 ) == "\xff\xfe<\x00?\x00x\x00m\x00l\x00") {
 412+ $xml_type = "UTF-16LE";
 413+ }
391414
392 - if ($xml_type) {
393 - if ($xml_type!=="UTF-8" && $xml_type!=="ASCII") $head= iconv($xml_type,"ASCII//IGNORE",$head);
 415+ if ( $xml_type ) {
 416+ if ( $xml_type !== "UTF-8" && $xml_type !== "ASCII" ) {
 417+ $head = iconv( $xml_type, "ASCII//IGNORE", $head );
 418+ }
394419
395 - $match= array();
396 - $doctype= "";
397 - $tag= "";
 420+ $match = array();
 421+ $doctype = "";
 422+ $tag = "";
398423
399 - if (preg_match('%<!DOCTYPE\s+[\w-]+\s+PUBLIC\s+["'."'".'"](.*?)["'."'".'"].*>%sim',$head,$match)) $doctype= $match[1];
400 - if (preg_match('%<(\w+).*>%sim',$head,$match)) $tag= $match[1];
 424+ if ( preg_match( '%<!DOCTYPE\s+[\w-]+\s+PUBLIC\s+["'."'".'"](.*?)["'."'".'"].*>%sim',
 425+ $head, $match ) ) {
 426+ $doctype = $match[1];
 427+ }
 428+ if ( preg_match( '%<(\w+).*>%sim', $head, $match ) ) {
 429+ $tag = $match[1];
 430+ }
401431
402432 #print "<br>ANALYSING $file ($mime): doctype= $doctype; tag= $tag<br>";
403433
404 - if (strpos($doctype,"-//W3C//DTD SVG")===0) $mime= "image/svg+xml";
405 - elseif ($tag==="svg") $mime= "image/svg+xml";
406 - elseif (strpos($doctype,"-//W3C//DTD XHTML")===0) $mime= "text/html";
407 - elseif ($tag==="html") $mime= "text/html";
 434+ if ( strpos( $doctype, "-//W3C//DTD SVG" ) === 0 ) {
 435+ $mime = "image/svg+xml";
 436+ } elseif ( $tag === "svg" ) {
 437+ $mime = "image/svg+xml";
 438+ } elseif ( strpos( $doctype, "-//W3C//DTD XHTML" ) === 0 ) {
 439+ $mime = "text/html";
 440+ } elseif ( $tag === "html" ) {
 441+ $mime = "text/html";
 442+ }
408443 }
409444 }
410445
411446 /*
412447 * look for shell scripts
413448 */
414 - if (!$xml_type) {
415 - $script_type= NULL;
 449+ if ( !$xml_type ) {
 450+ $script_type = NULL;
416451
417 - #detect by shebang
418 - if (substr($head,0,2)=="#!") $script_type= "ASCII";
419 - elseif (substr($head,0,5)=="\xef\xbb\xbf#!") $script_type= "UTF-8";
420 - elseif (substr($head,0,7)=="\xfe\xff\x00#\x00!") $script_type= "UTF-16BE";
421 - elseif (substr($head,0,7)=="\xff\xfe#\x00!") $script_type= "UTF-16LE";
 452+ # detect by shebang
 453+ if ( substr( $head, 0, 2) == "#!" ) {
 454+ $script_type = "ASCII";
 455+ } elseif ( substr( $head, 0, 5) == "\xef\xbb\xbf#!" ) {
 456+ $script_type = "UTF-8";
 457+ } elseif ( substr( $head, 0, 7) == "\xfe\xff\x00#\x00!" ) {
 458+ $script_type = "UTF-16BE";
 459+ } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) {
 460+ $script_type= "UTF-16LE";
 461+ }
422462
423 - if ($script_type) {
424 - if ($script_type!=="UTF-8" && $script_type!=="ASCII") $head= iconv($script_type,"ASCII//IGNORE",$head);
 463+ if ( $script_type ) {
 464+ if ( $script_type !== "UTF-8" && $script_type !== "ASCII") {
 465+ $head = iconv( $script_type, "ASCII//IGNORE", $head);
 466+ }
425467
426 - $match= array();
427 - $prog= "";
 468+ $match = array();
428469
429 - if (preg_match('%/?([^\s]+/)(w+)%sim',$head,$match)) {
430 - $script= $match[2]; // FIXME: $script variable not used; should this be "$prog = $match[2];" instead?
 470+ if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
 471+ $mime = "application/x-{$match[2]}";
431472 }
432 -
433 - $mime= "application/x-$prog";
434473 }
435474 }
436475
@@ -450,42 +489,43 @@
451490 ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
452491 ( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
453492
454 - $mime= "application/x-php";
 493+ $mime = "application/x-php";
455494 }
456495 }
457496
458497 }
459498
460 - if (isset($this->mMimeTypeAliases[$mime])) $mime= $this->mMimeTypeAliases[$mime];
 499+ if ( isset( $this->mMimeTypeAliases[$mime] ) ) {
 500+ $mime = $this->mMimeTypeAliases[$mime];
 501+ }
461502
462 - wfDebug("$fname: final mime type of $file: $mime\n");
 503+ wfDebug(__METHOD__.": final mime type of $file: $mime\n");
463504 return $mime;
464505 }
465506
466507 /** Internal mime type detection, please use guessMimeType() for application code instead.
467508 * Detection is done using an external program, if $wgMimeDetectorCommand is set.
468509 * Otherwise, the fileinfo extension and mime_content_type are tried (in this order), if they are available.
469 - * If the dections fails and $useExt is true, the mime type is guessed from the file extension, using guessTypesForExtension.
 510+ * If the dections fails and $ext is not false, the mime type is guessed from the file extension, using
 511+ * guessTypesForExtension.
470512 * If the mime type is still unknown, getimagesize is used to detect the mime type if the file is an image.
471513 * If no mime type can be determined, this function returns "unknown/unknown".
472514 *
473515 * @param string $file The file to check
474 - * @param bool $useExt switch for allowing to use the file extension to guess the mime type. true by default.
 516+ * @param mixed $ext The file extension, or true to extract it from the filename.
 517+ * Set it to false to ignore the extension.
475518 *
476519 * @return string the mime type of $file
477520 * @access private
478521 */
479 - function detectMimeType( $file, $useExt=true ) {
480 - $fname = 'MimeMagic::detectMimeType';
481 -
 522+ function detectMimeType( $file, $ext = true ) {
482523 global $wgMimeDetectorCommand;
483524
484 - $m= NULL;
485 - if ($wgMimeDetectorCommand) {
486 - $fn= wfEscapeShellArg($file);
487 - $m= `$wgMimeDetectorCommand $fn`;
488 - }
489 - else if (function_exists("finfo_open") && function_exists("finfo_file")) {
 525+ $m = NULL;
 526+ if ( $wgMimeDetectorCommand ) {
 527+ $fn = wfEscapeShellArg( $file );
 528+ $m = `$wgMimeDetectorCommand $fn`;
 529+ } elseif ( function_exists( "finfo_open" ) && function_exists( "finfo_file" ) ) {
490530
491531 # This required the fileinfo extension by PECL,
492532 # see http://pecl.php.net/package/fileinfo
@@ -500,13 +540,12 @@
501541 $mime_magic_resource = finfo_open(FILEINFO_MIME); /* return mime type ala mimetype extension */
502542
503543 if ($mime_magic_resource) {
504 - $m= finfo_file($mime_magic_resource, $file);
505 -
506 - finfo_close($mime_magic_resource);
 544+ $m = finfo_file( $mime_magic_resource, $file );
 545+ finfo_close( $mime_magic_resource );
 546+ } else {
 547+ wfDebug( __METHOD__.": finfo_open failed on ".FILEINFO_MIME."!\n" );
507548 }
508 - else wfDebug("$fname: finfo_open failed on ".FILEINFO_MIME."!\n");
509 - }
510 - else if (function_exists("mime_content_type")) {
 549+ } elseif ( function_exists( "mime_content_type" ) ) {
511550
512551 # NOTE: this function is available since PHP 4.3.0, but only if
513552 # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic.
@@ -517,93 +556,99 @@
518557 # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above.
519558 # see http://www.php.net/manual/en/ref.mime-magic.php for details.
520559
521 - $m= mime_content_type($file);
 560+ $m = mime_content_type($file);
522561
523562 if ( $m == 'text/plain' ) {
524563 // mime_content_type sometimes considers DJVU files to be text/plain.
525564 $deja = new DjVuImage( $file );
526565 if( $deja->isValid() ) {
527 - wfDebug("$fname: (re)detected $file as image/vnd.djvu\n");
 566+ wfDebug( __METHOD__.": (re)detected $file as image/vnd.djvu\n" );
528567 $m = 'image/vnd.djvu';
529568 }
530569 }
 570+ } else {
 571+ wfDebug( __METHOD__.": no magic mime detector found!\n" );
531572 }
532 - else wfDebug("$fname: no magic mime detector found!\n");
533573
534 - if ($m) {
535 - #normalize
536 - $m= preg_replace('![;, ].*$!','',$m); #strip charset, etc
537 - $m= trim($m);
538 - $m= strtolower($m);
 574+ if ( $m ) {
 575+ # normalize
 576+ $m = preg_replace( '![;, ].*$!', '', $m ); #strip charset, etc
 577+ $m = trim( $m );
 578+ $m = strtolower( $m );
539579
540 - if (strpos($m,'unknown')!==false) $m= NULL;
541 - else {
542 - wfDebug("$fname: magic mime type of $file: $m\n");
 580+ if ( strpos( $m, 'unknown' ) !== false ) {
 581+ $m = NULL;
 582+ } else {
 583+ wfDebug( __METHOD__.": magic mime type of $file: $m\n" );
543584 return $m;
544585 }
545586 }
546587
547 - #if still not known, use getimagesize to find out the type of image
548 - #TODO: skip things that do not have a well-known image extension? Would that be safe?
 588+ # if still not known, use getimagesize to find out the type of image
 589+ # TODO: skip things that do not have a well-known image extension? Would that be safe?
549590 wfSuppressWarnings();
550591 $gis = getimagesize( $file );
551592 wfRestoreWarnings();
552593
553 - $notAnImage= false;
 594+ $notAnImage = false;
554595
555 - if ($gis && is_array($gis) && $gis[2]) {
556 - switch ($gis[2]) {
557 - case IMAGETYPE_GIF: $m= "image/gif"; break;
558 - case IMAGETYPE_JPEG: $m= "image/jpeg"; break;
559 - case IMAGETYPE_PNG: $m= "image/png"; break;
560 - case IMAGETYPE_SWF: $m= "application/x-shockwave-flash"; break;
561 - case IMAGETYPE_PSD: $m= "application/photoshop"; break;
562 - case IMAGETYPE_BMP: $m= "image/bmp"; break;
563 - case IMAGETYPE_TIFF_II: $m= "image/tiff"; break;
564 - case IMAGETYPE_TIFF_MM: $m= "image/tiff"; break;
565 - case IMAGETYPE_JPC: $m= "image"; break;
566 - case IMAGETYPE_JP2: $m= "image/jpeg2000"; break;
567 - case IMAGETYPE_JPX: $m= "image/jpeg2000"; break;
568 - case IMAGETYPE_JB2: $m= "image"; break;
569 - case IMAGETYPE_SWC: $m= "application/x-shockwave-flash"; break;
570 - case IMAGETYPE_IFF: $m= "image/vnd.xiff"; break;
571 - case IMAGETYPE_WBMP: $m= "image/vnd.wap.wbmp"; break;
572 - case IMAGETYPE_XBM: $m= "image/x-xbitmap"; break;
 596+ if ( $gis && is_array($gis) && $gis[2] ) {
 597+
 598+ switch ( $gis[2] ) {
 599+ case IMAGETYPE_GIF: $m = "image/gif"; break;
 600+ case IMAGETYPE_JPEG: $m = "image/jpeg"; break;
 601+ case IMAGETYPE_PNG: $m = "image/png"; break;
 602+ case IMAGETYPE_SWF: $m = "application/x-shockwave-flash"; break;
 603+ case IMAGETYPE_PSD: $m = "application/photoshop"; break;
 604+ case IMAGETYPE_BMP: $m = "image/bmp"; break;
 605+ case IMAGETYPE_TIFF_II: $m = "image/tiff"; break;
 606+ case IMAGETYPE_TIFF_MM: $m = "image/tiff"; break;
 607+ case IMAGETYPE_JPC: $m = "image"; break;
 608+ case IMAGETYPE_JP2: $m = "image/jpeg2000"; break;
 609+ case IMAGETYPE_JPX: $m = "image/jpeg2000"; break;
 610+ case IMAGETYPE_JB2: $m = "image"; break;
 611+ case IMAGETYPE_SWC: $m = "application/x-shockwave-flash"; break;
 612+ case IMAGETYPE_IFF: $m = "image/vnd.xiff"; break;
 613+ case IMAGETYPE_WBMP: $m = "image/vnd.wap.wbmp"; break;
 614+ case IMAGETYPE_XBM: $m = "image/x-xbitmap"; break;
573615 }
574616
575 - if ($m) {
576 - wfDebug("$fname: image mime type of $file: $m\n");
 617+ if ( $m ) {
 618+ wfDebug( __METHOD__.": image mime type of $file: $m\n" );
577619 return $m;
578620 }
579 - else $notAnImage= true;
 621+ else {
 622+ $notAnImage = true;
 623+ }
580624 } else {
581625 // Also test DjVu
582626 $deja = new DjVuImage( $file );
583627 if( $deja->isValid() ) {
584 - wfDebug("$fname: detected $file as image/vnd.djvu\n");
 628+ wfDebug( __METHOD__.": detected $file as image/vnd.djvu\n" );
585629 return 'image/vnd.djvu';
586630 }
587631 }
588632
589 - #if desired, look at extension as a fallback.
590 - if ($useExt) {
 633+ # if desired, look at extension as a fallback.
 634+ if ( $ext === true ) {
591635 $i = strrpos( $file, '.' );
592 - $e= strtolower( $i ? substr( $file, $i + 1 ) : '' );
 636+ $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' );
 637+ }
 638+ if ( $ext ) {
 639+ $m = $this->guessTypesForExtension( $ext );
593640
594 - $m= $this->guessTypesForExtension($e);
595 -
596 - #TODO: if $notAnImage is set, do not trust the file extension if
 641+ # TODO: if $notAnImage is set, do not trust the file extension if
597642 # the results is one of the image types that should have been recognized
598643 # by getimagesize
599644
600 - if ($m) {
601 - wfDebug("$fname: extension mime type of $file: $m\n");
 645+ if ( $m ) {
 646+ wfDebug( __METHOD__.": extension mime type of $file: $m\n" );
602647 return $m;
603648 }
604649 }
605650
606651 #unknown type
607 - wfDebug("$fname: failed to guess mime type for $file!\n");
 652+ wfDebug( __METHOD__.": failed to guess mime type for $file!\n" );
608653 return "unknown/unknown";
609654 }
610655
@@ -623,61 +668,61 @@
624669 *
625670 * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants.
626671 */
627 - function getMediaType($path=NULL,$mime=NULL) {
 672+ function getMediaType( $path = NULL, $mime = NULL ) {
628673 if( !$mime && !$path ) return MEDIATYPE_UNKNOWN;
629674
630 - #if mime type is unknown, guess it
631 - if( !$mime ) $mime= $this->guessMimeType($path,false);
 675+ # If mime type is unknown, guess it
 676+ if( !$mime ) $mime = $this->guessMimeType( $path, false );
632677
633 - #special code for ogg - detect if it's video (theora),
634 - #else label it as sound.
635 - if( $mime=="application/ogg" && file_exists($path) ) {
 678+ # Special code for ogg - detect if it's video (theora),
 679+ # else label it as sound.
 680+ if( $mime == "application/ogg" && file_exists( $path ) ) {
636681
637682 // Read a chunk of the file
638683 $f = fopen( $path, "rt" );
639 - if( !$f ) return MEDIATYPE_UNKNOWN;
 684+ if ( !$f ) return MEDIATYPE_UNKNOWN;
640685 $head = fread( $f, 256 );
641686 fclose( $f );
642687
643 - $head= strtolower( $head );
 688+ $head = strtolower( $head );
644689
645 - #This is an UGLY HACK, file should be parsed correctly
646 - if( strpos($head,'theora')!==false ) return MEDIATYPE_VIDEO;
647 - elseif( strpos($head,'vorbis')!==false ) return MEDIATYPE_AUDIO;
648 - elseif( strpos($head,'flac')!==false ) return MEDIATYPE_AUDIO;
649 - elseif( strpos($head,'speex')!==false ) return MEDIATYPE_AUDIO;
 690+ # This is an UGLY HACK, file should be parsed correctly
 691+ if ( strpos( $head, 'theora' ) !== false ) return MEDIATYPE_VIDEO;
 692+ elseif ( strpos( $head, 'vorbis' ) !== false ) return MEDIATYPE_AUDIO;
 693+ elseif ( strpos( $head, 'flac' ) !== false ) return MEDIATYPE_AUDIO;
 694+ elseif ( strpos( $head, 'speex' ) !== false ) return MEDIATYPE_AUDIO;
650695 else return MEDIATYPE_MULTIMEDIA;
651696 }
652697
653 - #check for entry for full mime type
 698+ # check for entry for full mime type
654699 if( $mime ) {
655 - $type= $this->findMediaType($mime);
656 - if( $type!==MEDIATYPE_UNKNOWN ) return $type;
 700+ $type = $this->findMediaType( $mime );
 701+ if( $type !== MEDIATYPE_UNKNOWN ) return $type;
657702 }
658703
659 - #check for entry for file extension
660 - $e= NULL;
661 - if( $path ) {
 704+ # Check for entry for file extension
 705+ $e = NULL;
 706+ if ( $path ) {
662707 $i = strrpos( $path, '.' );
663 - $e= strtolower( $i ? substr( $path, $i + 1 ) : '' );
 708+ $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
664709
665 - #TODO: look at multi-extension if this fails, parse from full path
 710+ # TODO: look at multi-extension if this fails, parse from full path
666711
667 - $type= $this->findMediaType('.'.$e);
668 - if( $type!==MEDIATYPE_UNKNOWN ) return $type;
 712+ $type = $this->findMediaType( '.' . $e );
 713+ if ( $type !== MEDIATYPE_UNKNOWN ) return $type;
669714 }
670715
671 - #check major mime type
 716+ # Check major mime type
672717 if( $mime ) {
673 - $i= strpos($mime,'/');
 718+ $i = strpos( $mime, '/' );
674719 if( $i !== false ) {
675 - $major= substr($mime,0,$i);
676 - $type= $this->findMediaType($major);
677 - if( $type!==MEDIATYPE_UNKNOWN ) return $type;
 720+ $major = substr( $mime, 0, $i );
 721+ $type = $this->findMediaType( $major );
 722+ if( $type !== MEDIATYPE_UNKNOWN ) return $type;
678723 }
679724 }
680725
681 - if( !$type ) $type= MEDIATYPE_UNKNOWN;
 726+ if( !$type ) $type = MEDIATYPE_UNKNOWN;
682727
683728 return $type;
684729 }
@@ -689,25 +734,26 @@
690735 * This funktion relies on the mapping defined by $this->mMediaTypes
691736 * @access private
692737 */
693 - function findMediaType($extMime) {
 738+ function findMediaType( $extMime ) {
 739+ if ( strpos( $extMime, '.' ) === 0 ) { #if it's an extension, look up the mime types
 740+ $m = $this->getTypesForExtension( substr( $extMime, 1 ) );
 741+ if ( !$m ) return MEDIATYPE_UNKNOWN;
694742
695 - if (strpos($extMime,'.')===0) { #if it's an extension, look up the mime types
696 - $m= $this->getTypesForExtension(substr($extMime,1));
697 - if (!$m) return MEDIATYPE_UNKNOWN;
698 -
699 - $m= explode(' ',$m);
700 - }
701 - else { #normalize mime type
702 - if (isset($this->mMimeTypeAliases[$extMime])) {
703 - $extMime= $this->mMimeTypeAliases[$extMime];
 743+ $m = explode( ' ', $m );
 744+ } else {
 745+ # Normalize mime type
 746+ if ( isset( $this->mMimeTypeAliases[$extMime] ) ) {
 747+ $extMime = $this->mMimeTypeAliases[$extMime];
704748 }
705749
706 - $m= array($extMime);
 750+ $m = array($extMime);
707751 }
708752
709 - foreach ($m as $mime) {
710 - foreach ($this->mMediaTypes as $type => $codes) {
711 - if (in_array($mime,$codes,true)) return $type;
 753+ foreach ( $m as $mime ) {
 754+ foreach ( $this->mMediaTypes as $type => $codes ) {
 755+ if ( in_array($mime, $codes, true ) ) {
 756+ return $type;
 757+ }
712758 }
713759 }
714760

Follow-up revisions

RevisionCommit summaryAuthorDate
r23087Merged revisions 23050-23086 via svnmerge from...david03:14, 19 June 2007

Status & tagging log