You are here

function webform_client_form_submit in Webform 5

Same name and namespace in other branches
  1. 5.2 webform.module \webform_client_form_submit()
  2. 6.3 webform.module \webform_client_form_submit()
  3. 6.2 webform.module \webform_client_form_submit()
  4. 7.4 webform.module \webform_client_form_submit()
  5. 7.3 webform.module \webform_client_form_submit()

File

./webform.module, line 1461

Code

function webform_client_form_submit($form_id, $form_values) {
  global $user, $base_url;
  include_once drupal_get_path('module', 'webform') . "/webform.inc";
  $node = node_load(array(
    'nid' => $form_values['details']['nid'],
  ));
  $session_key = 'webform_form_' . $node->nid;
  if ($form_values['op'] != t('Submit')) {

    // This is a multi-page form that is not yet complete.
    // Copy values stored during previous steps into $_POST because they are needed in form_builder() to repopulate the form.
    if (is_array($_SESSION[$session_key])) {
      foreach ($_SESSION[$session_key] as $key => $val) {
        $_POST['submitted'][$key] = $val;
      }
    }

    // Store values from an intermediate stage of a multistep form in $_SESSION.
    if (is_array($form_values['submitted'])) {
      foreach ($form_values['submitted'] as $key => $val) {
        $_SESSION[$session_key][$key] = $val;
      }
    }
    return;
  }
  if (is_array($_SESSION[$session_key])) {

    // Merge any submission data stored in $_SESSION for multistep forms.
    foreach ($_SESSION[$session_key] as $key => $val) {
      $form_values['submitted'][$key] = $val;
    }
    unset($_SESSION[$session_key]);
  }

  // Perform post processing by components.
  _webform_client_form_submit_process($node, $form_values['submitted']);

  // Flatten trees within the submission.
  $form_values['submitted_tree'] = $form_values['submitted'];
  _webform_client_form_submit_flatten($node, $form_values['submitted'], $form_values['submitted']);

  // Perform additional submit processing.
  if (trim($node->additional_submit)) {

    // We use eval here (rather than drupal_eval) because the user needs access to local variables.
    eval("?>" . $node->additional_submit);
  }

  // Save the submission to the database.
  if (!$form_values['details']['sid']) {

    // No sid was found thus insert it in the datatabase.
    $sid = _webform_save_submission($node, $form_values['submitted']);
  }
  else {

    // Sid was found thus update the existing sid in the datatbase.
    $sid = _webform_update_submission($node, $form_values['details']['sid'], $form_values['submitted']);
  }

  // Check if this form is sending an email.
  if (isset($node->email) && !$form_values['details']['sid']) {
    $node->email = strip_tags($node->email);
    if (!empty($node->email)) {

      // Create a themed message for mailing.
      // Check for a node-specific message:
      $message = theme("webform_create_mailmessage_{$node->nid}", $form_values, $node, $sid);
      if (!$message) {

        // Otherwise use the generic form:
        $message = theme("webform_create_mailmessage", $form_values, $node, $sid);
      }

      // Build arrays of possible return email addresses and email subject lines from elements on the form.
      // Default values:
      $email_from_name = variable_get('webform_default_from_name', variable_get('site_name', ''));
      $email_from_address = variable_get('webform_default_from_email', variable_get('site_mail', ini_get('sendmail_from')));
      $email_subject_string = variable_get('webform_default_subject', t('Form submission from: ')) . ' ' . $node->title;
      $headers = array();

      // Check for empty values.
      if ($node->email_from_name == 'none') {
        $email_from_name = '';
      }
      if ($node->email_from_address == 'none') {
        $email_from_address = '';
      }
      if ($node->email_subject == 'none') {
        $email_subject_string = '';
      }

      // Search for user set FROM and SUBJECT fields.
      if (is_array($node->webformcomponents) && !empty($node->webformcomponents)) {
        foreach ($node->webformcomponents as $cid => $component) {
          $type = $component['type'];

          // Find and set a custom FROM: field.
          if ($type == 'email' || $type == 'hidden' || $type == 'select') {
            if ($component['name'] == $node->email_from_address) {
              $email_from_address = check_plain($form_values['submitted'][$cid]);
            }
          }

          // Find and set a custom SUBJECT: field.
          if ($type == 'textfield' || $type == 'hidden' || $type == 'select') {
            if ($component['name'] == $node->email_from_name) {
              $email_from_name = check_plain($form_values['submitted'][$cid]);
            }
            if ($component['name'] == $node->email_subject) {
              $email_subject_string = check_plain($form_values['submitted'][$cid]);
            }
          }
          if ($type == 'email' && $component['extra']['carboncopy'] == 'Y') {
            $headers['Cc'] = check_plain($form_values['submitted'][$cid]);
          }
        }
      }

      // Assemble the FROM string.
      if (strlen($email_from_name) > 0) {
        $email_from_string = mime_header_encode($email_from_name) . ' <' . $email_from_address . '>';
      }
      else {
        $email_from_string = $email_from_address;
      }

      // Verify that this submission is not attempting to send any spam hacks.
      if (_webform_submission_spam_check($node->email, $email_subject_string, $message, $email_from_string, $headers)) {
        watchdog('webform', t('Possible spam attempt from ') . $_SERVER['REMOTE_ADDR'] . "<br />\n" . nl2br(htmlentities($message)), WATCHDOG_WARNING);
        drupal_set_message(t("Illegal information. Data not submitted."), 'error');
        return false;
      }

      // Additional headers.
      $headers['X-Mailer'] = 'Drupal Webform (PHP/' . phpversion() . ')';

      // Mail the webform results.
      drupal_mail('webform-submission', $node->email, $email_subject_string, $message, $email_from_string, $headers);

      // Debugging output.
      if (variable_get('webform_debug', 0) >= 2) {
        drupal_set_message("E-mail Headers: <pre>" . htmlentities(print_r($headers, true)) . "</pre>To: " . $node->email . "<br />From: " . htmlentities($email_from_string) . "<br />Subject: " . $email_subject_string . "<br />E-mail Body: <pre>" . $message . "</pre>");
      }
    }
  }
  if (variable_get('webform_debug', 0) >= 2) {
    drupal_set_message('$form_values are: <pre>' . htmlentities(print_r($form_values, true)) . '</pre>');
    drupal_set_message('$_SERVER is: <pre>' . htmlentities(print_r($_SERVER, true)) . '</pre>');
    drupal_set_message('$_POST is: <pre>' . htmlentities(print_r($_POST, true)) . '</pre>');
  }
  if (variable_get('webform_debug', 0) >= 1) {
    watchdog('webform', t('Submission posted to %title', array(
      '%title' => $node->title,
    )) . l(t('Results'), 'node/' . $node->nid, NULL, 'sid=' . $sid) . "<br />\n<pre>" . htmlentities(print_r($form_values, TRUE)) . "</pre>", WATCHDOG_NOTICE);
  }

  // Check confirmation field to see if redirect should be to another node or a message.
  if (valid_url(trim($node->confirmation), true)) {
    $redirect = trim($node->confirmation);
  }
  elseif (preg_match('/^internal:/', $node->confirmation) && $node->redirect_post) {
    $path = preg_replace('/^internal:/', $base_url . '/', $node->confirmation);
    $redirect = trim($path);
  }
  elseif (preg_match('/^internal:/', $node->confirmation)) {
    $path = preg_replace('/^internal:/', '', $node->confirmation);
    $redirect = array(
      trim($path),
      'sid=' . $sid,
    );
  }
  else {
    $redirect = array(
      'node/' . $node->nid . '/done',
      'sid=' . $sid,
    );
  }
  return $redirect;
}