Index: trunk/phase3/includes/MimeMagic.php |
— | — | @@ -111,56 +111,67 @@ |
112 | 112 | * --- load mime.types --- |
113 | 113 | */ |
114 | 114 | |
115 | | - global $wgMimeTypeFile; |
| 115 | + global $wgMimeTypeFile, $IP; |
116 | 116 | |
117 | | - $types= MM_WELL_KNOWN_MIME_TYPES; |
| 117 | + $types = MM_WELL_KNOWN_MIME_TYPES; |
118 | 118 | |
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" ); |
125 | 130 | } |
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" ); |
127 | 133 | } |
128 | | - else wfDebug("MimeMagic::MimeMagic: no mime types file defined, using build-ins only.\n"); |
129 | 134 | |
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 ); |
132 | 137 | |
133 | | - $this->mMimeToExt= array(); |
134 | | - $this->mToMime= array(); |
| 138 | + $this->mMimeToExt = array(); |
| 139 | + $this->mToMime = array(); |
135 | 140 | |
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; |
141 | 146 | |
142 | | - $s= strtolower($s); |
143 | | - $i= strpos($s,' '); |
| 147 | + $s = strtolower( $s ); |
| 148 | + $i = strpos( $s, ' ' ); |
144 | 149 | |
145 | | - if ($i===false) continue; |
| 150 | + if ( $i === false ) continue; |
146 | 151 | |
147 | 152 | #print "processing MIME line $s<br>"; |
148 | 153 | |
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 ) ); |
151 | 156 | |
152 | | - if (empty($ext)) continue; |
| 157 | + if ( empty( $ext ) ) continue; |
153 | 158 | |
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 | + } |
156 | 164 | |
157 | | - $extensions= explode(' ',$ext); |
| 165 | + $extensions = explode( ' ', $ext ); |
158 | 166 | |
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; |
162 | 170 | |
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 | + } |
165 | 176 | } |
166 | 177 | } |
167 | 178 | |
— | — | @@ -169,62 +180,69 @@ |
170 | 181 | */ |
171 | 182 | |
172 | 183 | global $wgMimeInfoFile; |
| 184 | + if ( $wgMimeInfoFile == 'includes/mime.info' ) { |
| 185 | + $wgMimeInfoFile = "$IP/$wgMimeInfoFile"; |
| 186 | + } |
173 | 187 | |
174 | | - $info= MM_WELL_KNOWN_MIME_INFO; |
| 188 | + $info = MM_WELL_KNOWN_MIME_INFO; |
175 | 189 | |
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"); |
182 | 197 | } |
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"); |
184 | 200 | } |
185 | | - else wfDebug("MimeMagic::MimeMagic: no mime info file defined, using build-ins only.\n"); |
186 | 201 | |
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 ); |
189 | 204 | |
190 | | - $this->mMimeTypeAliases= array(); |
191 | | - $this->mMediaTypes= array(); |
| 205 | + $this->mMimeTypeAliases = array(); |
| 206 | + $this->mMediaTypes = array(); |
192 | 207 | |
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; |
198 | 213 | |
199 | | - $s= strtolower($s); |
200 | | - $i= strpos($s,' '); |
| 214 | + $s = strtolower( $s ); |
| 215 | + $i = strpos( $s, ' ' ); |
201 | 216 | |
202 | | - if ($i===false) continue; |
| 217 | + if ( $i === false ) continue; |
203 | 218 | |
204 | 219 | #print "processing MIME INFO line $s<br>"; |
205 | 220 | |
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; |
210 | 227 | } |
211 | | - else $mtype= MEDIATYPE_UNKNOWN; |
212 | 228 | |
213 | | - $m= explode(' ',$s); |
| 229 | + $m = explode( ' ', $s ); |
214 | 230 | |
215 | | - if (!isset($this->mMediaTypes[$mtype])) $this->mMediaTypes[$mtype]= array(); |
| 231 | + if ( !isset( $this->mMediaTypes[$mtype] ) ) { |
| 232 | + $this->mMediaTypes[$mtype] = array(); |
| 233 | + } |
216 | 234 | |
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; |
220 | 238 | |
221 | | - $this->mMediaTypes[$mtype][]= $mime; |
| 239 | + $this->mMediaTypes[$mtype][] = $mime; |
222 | 240 | } |
223 | 241 | |
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; |
229 | 247 | } |
230 | 248 | } |
231 | 249 | } |
— | — | @@ -244,14 +262,14 @@ |
245 | 263 | /** returns a list of file extensions for a given mime type |
246 | 264 | * as a space separated string. |
247 | 265 | */ |
248 | | - function getExtensionsForType($mime) { |
249 | | - $mime= strtolower($mime); |
| 266 | + function getExtensionsForType( $mime ) { |
| 267 | + $mime = strtolower( $mime ); |
250 | 268 | |
251 | | - $r= @$this->mMimeToExt[$mime]; |
| 269 | + $r = @$this->mMimeToExt[$mime]; |
252 | 270 | |
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]; |
256 | 274 | } |
257 | 275 | |
258 | 276 | return $r; |
— | — | @@ -260,22 +278,22 @@ |
261 | 279 | /** returns a list of mime types for a given file extension |
262 | 280 | * as a space separated string. |
263 | 281 | */ |
264 | | - function getTypesForExtension($ext) { |
265 | | - $ext= strtolower($ext); |
| 282 | + function getTypesForExtension( $ext ) { |
| 283 | + $ext = strtolower( $ext ); |
266 | 284 | |
267 | | - $r= isset( $this->mExtToMime[$ext] ) ? $this->mExtToMime[$ext] : null; |
| 285 | + $r = isset( $this->mExtToMime[$ext] ) ? $this->mExtToMime[$ext] : null; |
268 | 286 | return $r; |
269 | 287 | } |
270 | 288 | |
271 | 289 | /** returns a single mime type for a given file extension. |
272 | 290 | * This is always the first type from the list returned by getTypesForExtension($ext). |
273 | 291 | */ |
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; |
277 | 295 | |
278 | | - $m= trim( $m ); |
279 | | - $m= preg_replace('/\s.*$/','',$m); |
| 296 | + $m = trim( $m ); |
| 297 | + $m = preg_replace( '/\s.*$/', '', $m ); |
280 | 298 | |
281 | 299 | return $m; |
282 | 300 | } |
— | — | @@ -285,17 +303,17 @@ |
286 | 304 | * returns true if a match was found, NULL if the mime type is unknown, |
287 | 305 | * and false if the mime type is known but no matches where found. |
288 | 306 | */ |
289 | | - function isMatchingExtension($extension,$mime) { |
290 | | - $ext= $this->getExtensionsForType($mime); |
| 307 | + function isMatchingExtension( $extension, $mime ) { |
| 308 | + $ext = $this->getExtensionsForType( $mime ); |
291 | 309 | |
292 | | - if (!$ext) { |
| 310 | + if ( !$ext ) { |
293 | 311 | return NULL; //unknown |
294 | 312 | } |
295 | 313 | |
296 | | - $ext= explode(' ',$ext); |
| 314 | + $ext = explode( ' ', $ext ); |
297 | 315 | |
298 | | - $extension= strtolower($extension); |
299 | | - if (in_array($extension,$ext)) { |
| 316 | + $extension = strtolower( $extension ); |
| 317 | + if ( in_array( $extension, $ext ) ) { |
300 | 318 | return true; |
301 | 319 | } |
302 | 320 | |
— | — | @@ -347,13 +365,13 @@ |
348 | 366 | * or misinterpreter by the default mime detection (namely xml based formats like XHTML or SVG). |
349 | 367 | * |
350 | 368 | * @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. |
352 | 371 | * |
353 | 372 | * @return string the mime type of $file |
354 | 373 | */ |
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 ); |
358 | 376 | |
359 | 377 | // Read a chunk of the file |
360 | 378 | $f = fopen( $file, "rt" ); |
— | — | @@ -369,67 +387,88 @@ |
370 | 388 | $mime = "application/x-msmetafile"; |
371 | 389 | } |
372 | 390 | |
373 | | - if (strpos($mime,"text/")===0 || $mime==="application/xml") { |
| 391 | + if ( strpos( $mime, "text/" ) === 0 || $mime === "application/xml" ) { |
374 | 392 | |
375 | | - $xml_type= NULL; |
376 | | - $script_type= NULL; |
| 393 | + $xml_type = NULL; |
| 394 | + $script_type = NULL; |
377 | 395 | |
378 | 396 | /* |
379 | 397 | * look for XML formats (XHTML and SVG) |
380 | 398 | */ |
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") { |
386 | 404 | |
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 | + } |
391 | 414 | |
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 | + } |
394 | 419 | |
395 | | - $match= array(); |
396 | | - $doctype= ""; |
397 | | - $tag= ""; |
| 420 | + $match = array(); |
| 421 | + $doctype = ""; |
| 422 | + $tag = ""; |
398 | 423 | |
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 | + } |
401 | 431 | |
402 | 432 | #print "<br>ANALYSING $file ($mime): doctype= $doctype; tag= $tag<br>"; |
403 | 433 | |
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 | + } |
408 | 443 | } |
409 | 444 | } |
410 | 445 | |
411 | 446 | /* |
412 | 447 | * look for shell scripts |
413 | 448 | */ |
414 | | - if (!$xml_type) { |
415 | | - $script_type= NULL; |
| 449 | + if ( !$xml_type ) { |
| 450 | + $script_type = NULL; |
416 | 451 | |
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 | + } |
422 | 462 | |
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 | + } |
425 | 467 | |
426 | | - $match= array(); |
427 | | - $prog= ""; |
| 468 | + $match = array(); |
428 | 469 | |
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]}"; |
431 | 472 | } |
432 | | - |
433 | | - $mime= "application/x-$prog"; |
434 | 473 | } |
435 | 474 | } |
436 | 475 | |
— | — | @@ -450,42 +489,43 @@ |
451 | 490 | ( strpos( $head, "<\x00?\x00\t" ) !== false ) || |
452 | 491 | ( strpos( $head, "<\x00?\x00=" ) !== false ) ) { |
453 | 492 | |
454 | | - $mime= "application/x-php"; |
| 493 | + $mime = "application/x-php"; |
455 | 494 | } |
456 | 495 | } |
457 | 496 | |
458 | 497 | } |
459 | 498 | |
460 | | - if (isset($this->mMimeTypeAliases[$mime])) $mime= $this->mMimeTypeAliases[$mime]; |
| 499 | + if ( isset( $this->mMimeTypeAliases[$mime] ) ) { |
| 500 | + $mime = $this->mMimeTypeAliases[$mime]; |
| 501 | + } |
461 | 502 | |
462 | | - wfDebug("$fname: final mime type of $file: $mime\n"); |
| 503 | + wfDebug(__METHOD__.": final mime type of $file: $mime\n"); |
463 | 504 | return $mime; |
464 | 505 | } |
465 | 506 | |
466 | 507 | /** Internal mime type detection, please use guessMimeType() for application code instead. |
467 | 508 | * Detection is done using an external program, if $wgMimeDetectorCommand is set. |
468 | 509 | * 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. |
470 | 512 | * If the mime type is still unknown, getimagesize is used to detect the mime type if the file is an image. |
471 | 513 | * If no mime type can be determined, this function returns "unknown/unknown". |
472 | 514 | * |
473 | 515 | * @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. |
475 | 518 | * |
476 | 519 | * @return string the mime type of $file |
477 | 520 | * @access private |
478 | 521 | */ |
479 | | - function detectMimeType( $file, $useExt=true ) { |
480 | | - $fname = 'MimeMagic::detectMimeType'; |
481 | | - |
| 522 | + function detectMimeType( $file, $ext = true ) { |
482 | 523 | global $wgMimeDetectorCommand; |
483 | 524 | |
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" ) ) { |
490 | 530 | |
491 | 531 | # This required the fileinfo extension by PECL, |
492 | 532 | # see http://pecl.php.net/package/fileinfo |
— | — | @@ -500,13 +540,12 @@ |
501 | 541 | $mime_magic_resource = finfo_open(FILEINFO_MIME); /* return mime type ala mimetype extension */ |
502 | 542 | |
503 | 543 | 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" ); |
507 | 548 | } |
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" ) ) { |
511 | 550 | |
512 | 551 | # NOTE: this function is available since PHP 4.3.0, but only if |
513 | 552 | # PHP was compiled with --with-mime-magic or, before 4.3.2, with --enable-mime-magic. |
— | — | @@ -517,93 +556,99 @@ |
518 | 557 | # Also note that this has been DEPRECATED in favor of the fileinfo extension by PECL, see above. |
519 | 558 | # see http://www.php.net/manual/en/ref.mime-magic.php for details. |
520 | 559 | |
521 | | - $m= mime_content_type($file); |
| 560 | + $m = mime_content_type($file); |
522 | 561 | |
523 | 562 | if ( $m == 'text/plain' ) { |
524 | 563 | // mime_content_type sometimes considers DJVU files to be text/plain. |
525 | 564 | $deja = new DjVuImage( $file ); |
526 | 565 | 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" ); |
528 | 567 | $m = 'image/vnd.djvu'; |
529 | 568 | } |
530 | 569 | } |
| 570 | + } else { |
| 571 | + wfDebug( __METHOD__.": no magic mime detector found!\n" ); |
531 | 572 | } |
532 | | - else wfDebug("$fname: no magic mime detector found!\n"); |
533 | 573 | |
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 ); |
539 | 579 | |
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" ); |
543 | 584 | return $m; |
544 | 585 | } |
545 | 586 | } |
546 | 587 | |
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? |
549 | 590 | wfSuppressWarnings(); |
550 | 591 | $gis = getimagesize( $file ); |
551 | 592 | wfRestoreWarnings(); |
552 | 593 | |
553 | | - $notAnImage= false; |
| 594 | + $notAnImage = false; |
554 | 595 | |
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; |
573 | 615 | } |
574 | 616 | |
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" ); |
577 | 619 | return $m; |
578 | 620 | } |
579 | | - else $notAnImage= true; |
| 621 | + else { |
| 622 | + $notAnImage = true; |
| 623 | + } |
580 | 624 | } else { |
581 | 625 | // Also test DjVu |
582 | 626 | $deja = new DjVuImage( $file ); |
583 | 627 | if( $deja->isValid() ) { |
584 | | - wfDebug("$fname: detected $file as image/vnd.djvu\n"); |
| 628 | + wfDebug( __METHOD__.": detected $file as image/vnd.djvu\n" ); |
585 | 629 | return 'image/vnd.djvu'; |
586 | 630 | } |
587 | 631 | } |
588 | 632 | |
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 ) { |
591 | 635 | $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 ); |
593 | 640 | |
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 |
597 | 642 | # the results is one of the image types that should have been recognized |
598 | 643 | # by getimagesize |
599 | 644 | |
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" ); |
602 | 647 | return $m; |
603 | 648 | } |
604 | 649 | } |
605 | 650 | |
606 | 651 | #unknown type |
607 | | - wfDebug("$fname: failed to guess mime type for $file!\n"); |
| 652 | + wfDebug( __METHOD__.": failed to guess mime type for $file!\n" ); |
608 | 653 | return "unknown/unknown"; |
609 | 654 | } |
610 | 655 | |
— | — | @@ -623,61 +668,61 @@ |
624 | 669 | * |
625 | 670 | * @return (int?string?) a value to be used with the MEDIATYPE_xxx constants. |
626 | 671 | */ |
627 | | - function getMediaType($path=NULL,$mime=NULL) { |
| 672 | + function getMediaType( $path = NULL, $mime = NULL ) { |
628 | 673 | if( !$mime && !$path ) return MEDIATYPE_UNKNOWN; |
629 | 674 | |
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 ); |
632 | 677 | |
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 ) ) { |
636 | 681 | |
637 | 682 | // Read a chunk of the file |
638 | 683 | $f = fopen( $path, "rt" ); |
639 | | - if( !$f ) return MEDIATYPE_UNKNOWN; |
| 684 | + if ( !$f ) return MEDIATYPE_UNKNOWN; |
640 | 685 | $head = fread( $f, 256 ); |
641 | 686 | fclose( $f ); |
642 | 687 | |
643 | | - $head= strtolower( $head ); |
| 688 | + $head = strtolower( $head ); |
644 | 689 | |
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; |
650 | 695 | else return MEDIATYPE_MULTIMEDIA; |
651 | 696 | } |
652 | 697 | |
653 | | - #check for entry for full mime type |
| 698 | + # check for entry for full mime type |
654 | 699 | 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; |
657 | 702 | } |
658 | 703 | |
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 ) { |
662 | 707 | $i = strrpos( $path, '.' ); |
663 | | - $e= strtolower( $i ? substr( $path, $i + 1 ) : '' ); |
| 708 | + $e = strtolower( $i ? substr( $path, $i + 1 ) : '' ); |
664 | 709 | |
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 |
666 | 711 | |
667 | | - $type= $this->findMediaType('.'.$e); |
668 | | - if( $type!==MEDIATYPE_UNKNOWN ) return $type; |
| 712 | + $type = $this->findMediaType( '.' . $e ); |
| 713 | + if ( $type !== MEDIATYPE_UNKNOWN ) return $type; |
669 | 714 | } |
670 | 715 | |
671 | | - #check major mime type |
| 716 | + # Check major mime type |
672 | 717 | if( $mime ) { |
673 | | - $i= strpos($mime,'/'); |
| 718 | + $i = strpos( $mime, '/' ); |
674 | 719 | 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; |
678 | 723 | } |
679 | 724 | } |
680 | 725 | |
681 | | - if( !$type ) $type= MEDIATYPE_UNKNOWN; |
| 726 | + if( !$type ) $type = MEDIATYPE_UNKNOWN; |
682 | 727 | |
683 | 728 | return $type; |
684 | 729 | } |
— | — | @@ -689,25 +734,26 @@ |
690 | 735 | * This funktion relies on the mapping defined by $this->mMediaTypes |
691 | 736 | * @access private |
692 | 737 | */ |
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; |
694 | 742 | |
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]; |
704 | 748 | } |
705 | 749 | |
706 | | - $m= array($extMime); |
| 750 | + $m = array($extMime); |
707 | 751 | } |
708 | 752 | |
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 | + } |
712 | 758 | } |
713 | 759 | } |
714 | 760 | |