You are here

function mimedetect_requirements in MimeDetect 7

Same name and namespace in other branches
  1. 8 mimedetect.install \mimedetect_requirements()
  2. 5 mimedetect.module \mimedetect_requirements()
  3. 6 mimedetect.install \mimedetect_requirements()

Implements hook_requirements().

File

./mimedetect.install, line 30
Install, update, and uninstall functions for the MimeDetect module.

Code

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,
  );
}