You are here

function mimedetect_settings_validate in MimeDetect 7

Same name and namespace in other branches
  1. 5 mimedetect.module \mimedetect_settings_validate()
  2. 6 mimedetect.admin.inc \mimedetect_settings_validate()

Validation handler for the MimeDetect settings form.

File

./mimedetect.admin.inc, line 87
Admin page callbacks for the mimedetect module.

Code

function mimedetect_settings_validate($form_id, &$form_state) {

  // Test custom magic file path.
  $magic_file = $form_state['values']['mimedetect_magic'];
  if (!empty($magic_file) && !file_exists($magic_file)) {
    form_set_error('mimedetect_magic', t("The path %path does not exist or is not readable by your webserver.", array(
      '%path' => $magic_file,
    )));
    return;
  }

  // Test fileinfo settings.
  if ($form_state['values']['mimedetect_enable_file_info']) {
    if (!extension_loaded('fileinfo') || !@finfo_open(FILEINFO_MIME, $magic_file)) {
      if (!empty($magic_file)) {
        form_set_error('mimedetect_magic', 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_file,
        )));
      }
      else {
        form_set_error('mimedetect_enable_file_info', t('Fileinfo could not load magic information. Check the MAGIC environment variable on your system and that fileinfo PHP extension is properly installed.'));
      }
    }
  }

  // Test file binary settings.
  if ($form_state['values']['mimedetect_enable_file_binary']) {
    if (empty($form_state['values']['mimedetect_file_binary'])) {
      form_set_error('mimedetect_file_binary', t("You must specify the path to the 'file' binary if it is enabled."));
    }
    $filebin = $form_state['values']['mimedetect_file_binary'];

    // For security reasons, the basename of the UNIX file command executable
    // must be 'file'
    if (basename($filebin) != 'file') {
      form_set_error('mimedetect_file_binary', t("For security reasons, the basename of the UNIX file command executable must be 'file'."));
    }
    elseif (!is_executable($filebin)) {
      if (!file_exists($form_state['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' => $filebin,
        )));
      }
      else {
        form_set_error('mimedetect_file_binary', t("%path is not executable by your webserver.", array(
          '%path' => $filebin,
        )));
      }
    }
    else {

      // Execution test.
      $exit_code = 0;
      $output = NULL;
      exec($filebin . (!empty($magic_file) ? ' --magic-file=' . escapeshellarg($magic_file) : '') . ' --version', $output, $exit_code);
      if ($exit_code !== 0) {
        if (!empty($magic_file)) {
          form_set_error('mimedetect_enable_file_binary', 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_file,
          )));
        }
        else {
          form_set_error('mimedetect_enable_file_binary', t('File command execution failed with exit code <em>@code</em>.', array(
            '@code' => $exit_code,
          )));
        }
      }
      elseif (!is_array($output) || !count($output) || strpos($output[0], 'file-') !== 0) {
        form_set_error('mimedetect_file_binary', t('Unable to determine the UNIX file command version.'));
      }
    }
  }
  if (!$form_state['values']['mimedetect_enable_file_info'] && !$form_state['values']['mimedetect_enable_file_binary']) {
    drupal_set_message(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."), 'warning');
  }
}