You are here

function mimedetect_requirements in MimeDetect 8

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

Implements hook_requirements().

File

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

Code

function mimedetect_requirements($phase) {
  if ($phase != 'runtime') {
    return [];
  }
  $requirement = [
    'title' => t('MIME type enabled detection engines'),
    'description' => '',
    'severity' => REQUIREMENT_OK,
  ];
  $msg = '';
  $config = \Drupal::config('mimedetect.settings');
  $mimedetect_service = \Drupal::service('mimedetect');
  $magic_file = $config
    ->get('magicfile');

  // Test PHP fileinfo engine.
  if (($fileinfo_enabled = $config
    ->get('fileinfo.enable')) && !$mimedetect_service
    ->checkFileinfo($magic_file, $msg)) {
    $requirement['description'] = $msg . ' ' . t('Fileinfo detection engine cannot be enabled.') . ' ';
    $requirement['severity'] = REQUIREMENT_ERROR;
    $fileinfo_enabled = FALSE;
  }

  // Test UNIX 'file' command engine.
  if (($fileunix_enabled = $config
    ->get('unixfile.enable')) && !$mimedetect_service
    ->checkUnixfile($config
    ->get('unixfile.binary'), $magic_file, $msg)) {
    $requirement['description'] = $msg . ' ' . t("UNIX 'file' command detection engine disabled.") . ' ';
    $requirement['severity'] = REQUIREMENT_ERROR;
    $fileunix_enabled = FALSE;
  }
  else {
    $filebin_version = $msg;
  }

  // Enabled engines + default.
  $enabled_engines = '';
  $enabled_engines .= $fileinfo_enabled ? 'fileinfo, ' : '';
  $enabled_engines .= $fileunix_enabled ? 'UNIX file command v. ' . $filebin_version . ', ' : '';
  $enabled_engines .= 'filename extension';
  $requirement['value'] = $enabled_engines;

  // No real MIME detection engine enabled.
  if ($fileinfo_enabled + $fileunix_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 [
    'mimetype' => $requirement,
  ];
}