mimedetect.install in MimeDetect 7
Same filename and directory in other branches
Install, update, and uninstall functions for the MimeDetect module.
File
mimedetect.installView source
<?php
/**
* @file
* Install, update, and uninstall functions for the MimeDetect module.
*/
/**
* Implements hook_install().
*/
function mimedetect_install() {
// Enable PHP fileinfo engine by default.
variable_set('mimedetect_enable_file_info', TRUE);
}
/**
* Implements hook_uninstall().
*/
function mimedetect_uninstall() {
variable_del('mimedetect_enable_file_info');
variable_del('mimedetect_enable_file_binary');
variable_del('mimedetect_file_binary');
variable_del('mimedetect_enable_file_extension');
variable_del('mimedetect_magic');
}
/**
* Implements hook_requirements().
*/
function mimedetect_requirements($phase) {
$t = get_t();
$requirement = array(
'title' => $t('MIME type enabled detection engines'),
'description' => '',
'severity' => REQUIREMENT_OK,
);
$magic_path = variable_get('mimedetect_magic', '');
// Test PHP fileinfo engine.
if ($fileinfo_enabled = variable_get('mimedetect_enable_file_info', FALSE)) {
if ($fileinfo_error = !extension_loaded('fileinfo')) {
$requirement['description'] = $t('PHP file information extension not found.');
}
elseif ($fileinfo_error = !@finfo_open(FILEINFO_MIME, $magic_path)) {
if (!empty($magic_path)) {
$requirement['description'] = $t('Fileinfo cannot load the configured magic file <em>@file</em>. It could be corrupted. Try another magic file or remove your magic file path settings to use defaults.', array(
'@file' => $magic_path,
));
}
else {
$requirement['description'] = $t('Fileinfo could not load magic information. Check the MAGIC environment variable on your system and that fileinfo PHP extension is properly installed.');
}
}
if ($fileinfo_error) {
$requirement['description'] .= ' ' . $t('Fileinfo detection engine cannot be enabled.') . ' ';
$requirement['severity'] = REQUIREMENT_ERROR;
$fileinfo_enabled = FALSE;
}
}
// Test UNIX 'file' command engine.
if ($filebin_enabled = variable_get('mimedetect_enable_file_binary', FALSE)) {
$binary = variable_get('mimedetect_file_binary', '/usr/bin/file');
if (!($filebin_enabled = basename($binary) == 'file')) {
$requirement['description'] .= $t("For security reasons, the basename of the UNIX file command executable must be 'file'.");
}
elseif (!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,
));
}
$filebin_enabled = FALSE;
}
else {
// Execution test.
$exit_code = 0;
$filebin_version = NULL;
$output = NULL;
exec($binary . (!empty($magic_path) ? ' --magic-file=' . escapeshellarg($magic_path) : '') . ' --version', $output, $exit_code);
if ($exit_code !== 0) {
$filebin_enabled = FALSE;
if (!empty($magic_path)) {
$requirement['description'] .= $t('File command execution failed with exit code <em>@code</em>. Could not load the magic file <em>@file</em>.', array(
'@code' => $exit_code,
'@file' => $magic_path,
));
}
else {
$requirement['description'] .= $t('File command execution failed with exit code <em>@code</em>.', array(
'@code' => $exit_code,
));
}
}
elseif (is_array($output) && count($output)) {
// Expected output: "file-x.xx".
$output_parts = explode('-', $output[0]);
if (count($output_parts) == 2 && $output_parts[0] == 'file') {
$filebin_version = $output_parts[1];
}
}
if ($filebin_enabled && !$filebin_version) {
// Unable to determine file bin version.
$filebin_enabled = FALSE;
$requirement['description'] .= $t('Unable to determine the UNIX file command version.');
}
}
// Enabled but error on tests.
if (!$filebin_enabled) {
$requirement['description'] .= ' ' . $t("UNIX 'file' command detection engine disabled.") . ' ';
$requirement['severity'] = REQUIREMENT_ERROR;
}
}
// Enabled engines + default.
$enabled_engines = '';
$enabled_engines .= $fileinfo_enabled ? 'fileinfo, ' : '';
$enabled_engines .= $filebin_enabled ? 'UNIX file command v. ' . $filebin_version . ', ' : '';
$enabled_engines .= 'filename extension';
$requirement['value'] = $enabled_engines;
// No real MIME detection engine enabled.
if ($fileinfo_enabled + $filebin_enabled == 0 && $requirement['severity'] == REQUIREMENT_OK) {
$requirement['description'] = $t("MimeDetect is using the browser supplied filename for 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 sever-side mime type detection.");
$requirement['severity'] = REQUIREMENT_WARNING;
}
return array(
'mimetype' => $requirement,
);
}
Functions
Name | Description |
---|---|
mimedetect_install | Implements hook_install(). |
mimedetect_requirements | Implements hook_requirements(). |
mimedetect_uninstall | Implements hook_uninstall(). |