You are here

function pardot_webform_components_form_validate in Pardot Integration 7

Same name and namespace in other branches
  1. 7.2 pardot_webform/pardot_webform.admin.inc \pardot_webform_components_form_validate()

Form validation handler for pardot_webform_components_form().

See also

pardot_webform_components_form_submit()

File

./pardot.admin.inc, line 236
Admin forms.

Code

function pardot_webform_components_form_validate($form, $form_state) {

  // Check if we should validate the form handler.
  if (variable_get('pardot_validate_url', 1)) {
    if (function_exists('curl_getinfo')) {

      // Just check if the URL is active. No validation done if it is not.
      if ($form_state['values']['details']['is_active'] == 1) {

        // If so, check if it's a valid url.
        $check_url = curl_init($form_state['values']['details']['url']);

        // Use curl_setopt to get the response.
        curl_setopt($check_url, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($check_url, CURLOPT_HEADER, TRUE);

        // Fetch the url.
        $response = curl_exec($check_url);
        if (!$response) {
          drupal_set_message(t('Could not connect to host, no validation done for post URL'), 'status');
        }
        else {

          // Get the pardot cookie if it exists.
          preg_match('/^Set-Cookie:\\s*([^;]*)/mi', $response, $cookie_result);
          parse_str($cookie_result[1], $cookie);

          // If the Pardot cookie exists.
          if (isset($cookie['pardot'])) {

            // Check for HTTP code, we need a 200.
            $http_code = curl_getinfo($check_url, CURLINFO_HTTP_CODE);
            if ($http_code != 200) {

              // Set form error.
              form_set_error('url', t('The Post url is not valid.'));
            }
          }
          else {
            form_set_error('url', t('This is not a pardot Post url.'));
          }

          // Always close an opened url.
          curl_close($check_url);
        }
      }
      else {

        // Print out message.
        drupal_set_message(t('No validation done for none active Post URL'), 'status');
      }
    }
  }
}