'admin/settings/mimedetect', 'title' => t('Mime Detect Settings'), 'callback' => 'drupal_get_form', 'callback arguments' => array('mimedetect_settings'), ); } // Add settings form. return $items; } function mimedetect_requirements($phase) { $requirements = array(); $t = get_t(); $requirement = array( 'title' => $t('Mime Detection'), ); if (extension_loaded('fileinfo')) { $requirement['value'] = $t('PHP FileInfo Extension'); if (!$finfo = @finfo_open(FILEINFO_MIME, drupal_get_path('module', 'mimedetect') .'/magic')) { $requirement['description'] = $t('FileInfo could not load the magic file. It could be corrupted. Try reinstalling the magic file distributed with the MimeDetect module.'); $requirement['severity'] = REQUIREMENT_ERROR; } } elseif(variable_get('mimedetect_enable_file_binary', FALSE)) { $binary = variable_get('mimedetect_file_binary', '/usr/bin/file'); $requirement['value'] = $t("UNIX 'file' Command"); if (!is_executable($binary)){ if (!file_exists($binary)) { $requirement['description'] = $t("The file %path does not exist or is not readable by your webserver. ", array('%path' => $binary)); } else { $requirement['description'] .= t("The file %path is not executable by your webserver.", array('%path' => $binary)); } $requirement['severity'] = REQUIREMENT_ERROR; } } else { $requirement['value'] = $t('Browser & Extension'); $requirement['description'] = $t("MimeDetect is using the browser supplied mime type or file extension lookups. It is strongly recommended that you install and configure the PHP FileInfo Extension or the UNIX 'file' command to provide more accurate severside mime type detection."); $requirement['severity'] = REQUIREMENT_WARNING; } return array('mimedetect' => $requirement); } function mimedetect_settings() { $form[] = array(); $form['mimedetect_enable_file_binary'] = array( '#type' => 'checkbox', '#title' => t("Use UNIX 'file' command to detect mime type?"), '#description' => t("The the UNIX 'file' command will be used for mime detection only if the php fileinfo extension is not installed or fails to load."), '#default_value' => variable_get('mimedetect_enable_file_binary', FALSE), ); $form['mimedetect_file_binary'] = array( '#type' => 'textfield', '#title' => t("Path to the 'file' command"), '#description' => t("The path to the executable 'file' binary."), '#default_value' => variable_get('mimedetect_file_binary','/usr/bin/file'), ); return system_settings_form($form); } function mimedetect_settings_validate($form_id, $form_values) { // Test file binary settings. if ($form_values['mimedetect_enable_file_binary']) { if (empty($form_values['mimedetect_file_binary'])) { form_set_error('mimedetect_file_binary', t("You must specify the path to the 'file' binary if it is enabled.")); } if (!is_executable($form_values['mimedetect_file_binary'])) { if (!file_exists($form_values['mimedetect_file_binary'])) { form_set_error('mimedetect_file_binary', t("The path %path does not exist or is not readable by your webserver.", array('%path' => $form_values['mimedetect_file_binary']))); } else { form_set_error('mimedetect_file_binary', t("%path is not executable by your webserver.", array('%path' => $form_values['mimedetect_file_binary']))); } } } } /** * Detect File Mime Type... * */ function mimedetect_mime($file) { $file = (object)$file; $mime = FALSE; // Lookup array, Just in case we can't figure out the mime type. $extension_mimes = array( // MS ASF derived multimedia filetypes 'asx' => 'video/x-ms-asf', 'wma' => 'audio/x-ms-wma', 'wax' => 'audio/x-ms-wax', 'wmv' => 'video/x-ms-wmv', 'wvx' => ' video/x-ms-wvx', 'wm' => 'video/x-ms-wm', 'wmx' => 'video/x-ms-wmx', 'wmz' => 'application/x-ms-wmz', 'wmd' => 'application/x-ms-wmd', // Stanard Audio Types 'au' => 'audio/basic', 'snd' => 'audio/basic', 'mid' => 'audio/mid', 'rmi' => 'audio/mid', 'mp3' => 'audio/mpeg', 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff', 'aidff' => 'audio/x-aiff', 'm3u' => 'audio/x-mpegurl', 'ra' => 'audio/x-pn-realaudio', 'ram' => 'audio/x-pn-realaudio', 'wav' => 'audio/x-wav', // Standard Image Types 'bmp' => 'image/bmp', 'cod' => 'image/cis-cod', 'gif' => 'image/gif', 'ief' => 'image/ief', 'jpe' => 'image/jpeg', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jfif' => 'image/pipeg', 'svg' => 'image/svg+xml', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'ras' => 'image/x-cmu-raster', 'cmx' => 'image/x-cmx', 'ico' => 'image/x-icon', 'pnm' => 'image/x-portable-anymap', 'pbm' => 'image/x-portable-bitmap', 'pgm' => 'image/x-portable-graymap', 'ppm' => 'image/x-portable-pixmap', 'rbg' => 'image/x-rgb', 'xbm' => 'image/x-xbitmap', 'xpm' => 'image/x-xpixmap', 'xwd' => 'image/x-xwindowdump', // Standard Video types 'mp2' => 'video/mpeg', 'mpa' => 'video/mpeg', 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mpv2' => 'video/mpeg', 'mov' => 'video/quicktime', 'qt' => 'video/quicktime', 'lsf' => 'video/x-la-asf', 'lsx' => 'video/x-la-asf', 'asf' => 'video/x-ms-asf', 'asr' => 'video/x-ms-asf', 'asx' => 'video/x-ms-asf', 'avi' => 'video/x-msvideo', 'movie' => 'video/x-sgi-movie', ); // Try to use the fileinfo extension first. if (!$mime && extension_loaded('fileinfo')) { static $finfo = FALSE; if ($finfo || $finfo = @finfo_open(FILEINFO_MIME, drupal_get_path('module', 'mimedetect') .'/magic')) { $mime = finfo_file($finfo, $file->filepath); } } // Try the 'file' binary. if (!$mime && $filebin = variable_get('filefield_file_binary', '/usr/bin/file') && is_executable($filebin)) { $command = $filebin .' -bi '. escapeshellarg($file->filepath); $mime = trim(exec($command, $out)); } // Trust the browser... ack! if (!$mime) { $mime = $file->filemime; } // ASF derived media formats are hard to detect with magic. They're // typically all reported as video/x-ms-asf or application/octet-stream // These aren't really informative about the media type, so we attempt to // figure it out by extension. // I expect OGG to present similar difficulties in determining how it should be played... if (!$mime || $mime == 'application/octet-stream') { $parts = explode('.', $file->filename); $extension = array_pop($parts); if (isset($extension_mimes[$extension])) { $mime = $extension_mimes[$extension]; } } if (!$mime) { $mime = 'application/octet-stream'; } return $mime; }