You are here

public static function MimeDetectService::checkUnixfile in MimeDetect 8

Checks UNIX 'file' command system avilability.

Parameters

string $bin_file: Optional file path to the UNIX file command binary.

string $magic_file: An optional 'magic' information file.

string $msg: Optional string variable reference to place the UNIX file command version if available or error message.

Return value

bool Test result. TRUE if OK, FALSE on error.

2 calls to MimeDetectService::checkUnixfile()
MimeDetectService::__construct in src/MimeDetectService.php
Construct the MimeDetectService object.
MimeDetectSettingsForm::validateForm in src/Form/MimeDetectSettingsForm.php
Form validation handler.

File

src/MimeDetectService.php, line 211

Class

MimeDetectService
MimeDetect service.

Namespace

Drupal\mimedetect

Code

public static function checkUnixfile($bin_file = '/usr/bin/file', $magic_file = '', &$msg = '') {

  // Test magic file if not empty.
  $errors = !empty($magic_file) ? !MimeDetectService::checkMagicfile($magic_file, $msg) : FALSE;
  if ($errors) {
    return FALSE;
  }
  if ($errors = empty($bin_file)) {
    $msg = t("You must specify the path to the 'file' binary if it is enabled.");
  }
  elseif ($errors = basename($bin_file) != 'file') {
    $msg = t("Base name %basename for the 'file' binary not allowed. Must be 'file'.", [
      '%basename' => basename($bin_file),
    ]);
  }
  elseif ($errors = !file_exists($bin_file)) {
    $msg = t("The path %path does not exist or is not readable by your webserver.", [
      '%path' => $bin_file,
    ]);
  }
  elseif ($errors = !is_executable($bin_file)) {
    $msg = t("%path is not executable by your webserver.", [
      '%path' => $bin_file,
    ]);
  }
  else {

    // Execution test.
    $exit_code = 0;
    $output = NULL;
    exec($bin_file . (!empty($magic_file) ? ' --magic-file=' . escapeshellarg($magic_file) : '') . ' --version', $output, $exit_code);
    if ($errors = $exit_code !== 0) {
      if (!empty($magic_file)) {
        $msg = t('File command execution failed with exit code %code. Could not load the magic file %file.', [
          '%code' => $exit_code,
          '%file' => $magic_file,
        ]);
      }
      else {
        $msg = t('File command execution failed with exit code %code.', [
          '%code' => $exit_code,
        ]);
      }
    }
    elseif ($errors = !is_array($output) || !count($output) || strpos($output[0], 'file-') !== 0) {

      // Expected output: "file-x.xx".
      $msg = t('Unable to determine the UNIX file command version.');
    }
    else {
      $msg = substr($output[0], strlen('file-'));
    }
  }
  return !$errors;
}