/[drupal]/contributions/modules/mimedetect/mimedetect.module
ViewVC logotype

Contents of /contributions/modules/mimedetect/mimedetect.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.13 - (show annotations) (download) (as text)
Sat Sep 12 17:11:07 2009 UTC (2 months, 1 week ago) by drewish
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +2 -2 lines
File MIME type: text/x-php
#306217 by guix and drewish: Add variable to specify magic file path.
1 <?php
2 // $Id: mimedetect.module,v 1.12 2009/05/09 17:42:01 drewish Exp $
3
4 /**
5 * @file
6 * Provide server side mime type detection.
7 *
8 * @author Darrel O'Pry, http://www.drupal.org/user/22202
9 * @copyright Copyright(c) 2007, Darrel O'Pry
10 */
11
12
13 function mimedetect_menu() {
14 $items = array();
15
16 // The admin settings form.
17 $items['admin/settings/mimedetect'] = array(
18 'title' => 'Mime type detection',
19 'description' => 'Control how the mime type of uploaded files will be determined.',
20 'page callback' => 'drupal_get_form',
21 'page arguments' => array('mimedetect_settings'),
22 'access arguments' => array('administer site configuration'),
23 'file' => 'mimedetect.admin.inc',
24 );
25
26 return $items;
27 }
28
29 /**
30 * Detect File Mime Type.
31 *
32 * @param $file A standard Drupal file object. The filepath property is used to
33 * locate the file and if the mime detection fails, the mimetype property is
34 * returned.
35 * @return String containing the file's MIME type.
36 */
37 function mimedetect_mime($file) {
38 $file = (object)$file;
39
40 // An additional array of mimetypes not included in file_get_mimetype().
41 static $additional_mimes = array(
42 // Audio types
43 'rmi' => 'audio/midi',
44 'aidff' => 'audio/x-aiff',
45 // Image types
46 'cod' => 'image/cis-cod',
47 'jfif' => 'image/pipeg',
48 'cmx' => 'image/x-cmx',
49 // Video types
50 'mpa' => 'video/mpeg',
51 'mpv2' => 'video/mpeg',
52 'asr' => 'video/x-ms-asf',
53 );
54
55 $mime = FALSE;
56 $magic_file = variable_get('mimedetect_magic', drupal_get_path('module', 'mimedetect') .'/magic');
57
58 // Try to use the fileinfo extension first.
59 if (extension_loaded('fileinfo')) {
60 static $finfo = FALSE;
61 if ($finfo || $finfo = @finfo_open(FILEINFO_MIME, $magic_file)) {
62 $mime = finfo_file($finfo, realpath($file->filepath));
63 }
64 }
65
66 // Try the 'file' binary.
67 if (!$mime && variable_get('mimedetect_enable_file_binary', FALSE)
68 && ($filebin = variable_get('mimedetect_file_binary', '/usr/bin/file'))
69 && is_executable($filebin))
70 {
71 // On OSX the -i switch is -I, so if we use the long flags everyone is
72 // happy. I checked back to version 3.41 and it still supports the long
73 // names but if you run into problems you can use " -bi ".
74 $command = $filebin . ' --brief --mime --magic-file=' . escapeshellarg($magic_file) . ' ' . escapeshellarg($file->filepath);
75 $mime = trim(exec($command));
76 // with text we often get charset like 'text/plain; charset=us-ascii'
77 $mime = split(';', $mime);
78 $mime = trim($mime[0]);
79 }
80
81 // ASF derived media formats are hard to detect with magic. They're typically
82 // all reported as video/x-ms-asf or application/octet-stream. These aren't
83 // really informative about the media type, so we attempt to figure it out by
84 // extension. I expect OGG to present similar difficulties in determining how
85 // it should be played.
86 if (!$mime || $mime == 'application/octet-stream') {
87 // Try core's mime mapping first...
88 $mime = file_get_mimetype($file->filename);
89 // ...and if that doesn't turn up anything try our additional mappings.
90 if ($mime == 'application/octet-stream') {
91 $mime = file_get_mimetype($file->filename, $additional_mimes);
92 }
93 }
94
95 return $mime;
96 }

  ViewVC Help
Powered by ViewVC 1.1.2