You are here

function search_api_attachments_settings_form_validate in Search API attachments 7

Validation handler for the settings form.

File

./search_api_attachments.admin.inc, line 114
Admin settings.

Code

function search_api_attachments_settings_form_validate($form, &$form_state) {

  // We only need to validate the tika path if we're using local extraction.
  if ($form_state['values']['search_api_attachments_extract_using'] == 'tika') {

    // Tika extraction without tika jar error.
    if (empty($form_state['values']['search_api_attachments_tika_jar'])) {
      form_set_error('search_api_attachments_tika_jar', t('Tika jar is mandatory.'));
    }

    // Check that the file exists.
    $path = realpath($form_state['values']['search_api_attachments_tika_path']);
    $tika = $path . '/' . $form_state['values']['search_api_attachments_tika_jar'];
    if (!file_exists($tika)) {
      form_set_error('search_api_attachments_tika_path', t('Tika jar file not found at this path.'));
    }
    else {
      $cmd = escapeshellcmd(variable_get('search_api_attachments_java', 'java')) . ' -jar ' . escapeshellarg($tika) . ' -V';
      exec($cmd, $output, $return_code);

      // $return_code = 1 if it fails. 0 instead.
      if ($return_code) {
        form_set_error('search_api_attachments_tika_path', t('There may be a problem with tika path.'));
        form_set_error('search_api_attachments_tika_jar', t('Tika jar file is not an executable jar. Please check if you have not downloaded a corrupted jar file.'));
      }
    }
  }
  elseif ($form_state['values']['search_api_attachments_extract_using'] == 'python_pdf2txt') {

    // Pdf2txt extraction without pdf2txt python script error.
    if (empty($form_state['values']['search_api_attachments_python_pdf2txt_script'])) {
      form_set_error('search_api_attachments_python_pdf2txt_script', t('Pdf2txt Python script is mandatory.'));
    }

    // Check that the file exists.
    $path = realpath($form_state['values']['search_api_attachments_python_pdf2txt_path']);
    $pdf2txt = $path . '/' . $form_state['values']['search_api_attachments_python_pdf2txt_script'];
    if (!file_exists($pdf2txt)) {
      form_set_error('search_api_attachments_python_pdf2txt_path', t('Pdf2txt Python script not found at this path.'));
    }
    else {
      $cmd = escapeshellcmd('python') . ' ' . escapeshellarg($pdf2txt);
      exec($cmd, $output, $return_code);

      // $return_code = 1 if it fails. 100 instead.
      if ($return_code != 100) {
        form_set_error('search_api_attachments_python_pdf2txt_path', t('There may be a problem with python pdf2txt path.'));
        form_set_error('search_api_attachments_python_pdf2txt_script', t('Pdf2txt Python script file is not executable.'));
      }
    }
  }
  elseif ($form_state['values']['search_api_attachments_extract_using'] == 'tika_server') {

    // Set server URL.
    $server = 'http://' . $form_state['values']['search_api_attachments_tika_server_host'] . ':' . $form_state['values']['search_api_attachments_tika_server_port'] . '/tika';
    $ch = curl_init($server);

    // Check if server exists.
    curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($httpCode != 200) {
      form_set_error('search_api_attachments_tika_server_host', t('No tika server was found. Please check if host and server are correct'));
    }
    curl_close($ch);
  }
}