You are here

function webform_client_form_submit in Webform 5.2

Same name and namespace in other branches
  1. 5 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 1481

Code

function webform_client_form_submit($form_id, &$form_values) {
  global $user, $base_url;
  include_once drupal_get_path('module', 'webform') . '/webform_submissions.inc';
  webform_load_components();
  $node = node_load(array(
    'nid' => $form_values['details']['nid'],
  ));
  $session_key = 'webform_form_' . $node->nid;

  // Check for a multi-page form that is not yet complete.
  if ($form_values['op'] != (empty($node->webform['submit_text']) ? t('Submit') : $node->webform['submit_text'])) {

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

    // This is a multi-page form that is not yet complete.
    // Save the order of the current post into a copied array.
    $original_post = is_array($_POST['submitted']) ? $_POST['submitted'] : array();
    $_POST['submitted'] = array();

    // 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;
      }
    }

    // Restore the newly added values either in the previous position.
    foreach ($original_post as $key => $val) {
      $_POST['submitted'][$key] = $val;
    }

    // Remove the variable so it doesn't show up in the additional processing.
    unset($original_post);

    // 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.
    $original_values = is_array($form_values['submitted']) ? $form_values['submitted'] : array();
    $form_values['submitted'] = array();
    foreach ($_SESSION[$session_key] as $key => $val) {
      $form_values['submitted'][$key] = $val;
    }
    foreach ($original_values as $key => $val) {
      $form_values['submitted'][$key] = $val;
    }

    // Remove the variable so it doesn't show up in the additional processing.
    unset($original_values);
    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'];
  $form_values['submitted'] = _webform_client_form_submit_flatten($node, $form_values['submitted']);

  // Convert additional email addresses into actual values.
  foreach ($node->webform['additional_emails'] as $cid => $value) {
    if (is_array($form_values['submitted'][$cid])) {
      $node->webform['additional_emails'][$cid] = array();
      foreach ($form_values['submitted'][$cid] as $submitted_value) {
        if ($submitted_value) {
          $node->webform['additional_emails'][$cid][] = $submitted_value;
        }
      }
    }
    else {
      $node->webform['additional_emails'][$cid] = $form_values['submitted'][$cid];
    }
    if (empty($node->webform['additional_emails'][$cid])) {
      unset($node->webform['additional_emails'][$cid]);
    }
  }

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

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

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

    // No sid was found thus insert it in the datatabase.
    $form_values['details']['sid'] = webform_submission_insert($node, $form_values['submitted']);
    $form_values['details']['is_new'] = TRUE;

    // Set a cookie including the server's submission time.
    // The cookie expires in the length of the interval plus a day to compensate for different timezones.
    if (variable_get('webform_use_cookies', 0)) {
      $cookie_name = 'webform-' . $node->nid;
      $time = time();
      setcookie($cookie_name . '[' . $time . ']', $time, $time + $node->webform['submit_interval'] + 86400);
    }
  }
  else {

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

  // Check if this form is sending an email.
  if ((!empty($node->webform['email']) || !empty($node->webform['additional_emails'])) && $form_values['details']['is_new']) {

    // Set values for the name, address, and subject for the email.
    $email_from_name = $node->webform['email_from_name'];
    $email_from_address = $node->webform['email_from_address'];
    $email_subject = $node->webform['email_subject'];
    foreach (array(
      'from_name',
      'from_address',
      'subject',
    ) as $field) {
      if ($node->webform['email_' . $field] == 'default') {
        ${'email_' . $field} = _webform_filter_values(webform_variable_get('webform_default_' . $field), $node, $form_values['submitted'], FALSE, TRUE);
      }
      elseif (is_numeric($node->webform['email_' . $field])) {
        if (is_array($form_values['submitted'][${'email_' . $field}])) {
          $values = array();
          foreach ($form_values['submitted'][${'email_' . $field}] as $key => $value) {
            $values[] = _webform_filter_values($value, $node, $form_values['submitted'], FALSE, TRUE);
          }
          ${'email_' . $field} = implode(', ', $values);
        }
        else {
          ${'email_' . $field} = _webform_filter_values($form_values['submitted'][${'email_' . $field}], $node, $form_values['submitted'], FALSE, TRUE);
        }
      }
      else {
        ${'email_' . $field} = _webform_filter_values(${'email_' . $field}, $node, $form_values['submitted'], FALSE, TRUE);
      }
    }

    // Create a themed message for mailing.
    // Check for a node-specific message:
    $emails = $node->webform['additional_emails'];
    if ($node->webform['email']) {
      $emails['default'] = $node->webform['email'];
    }
    $messages = array();
    $headers = array();
    $froms = array();
    $subjects = array();
    foreach ($emails as $cid => $email) {
      $messages[$cid] = theme('webform_mail_message_' . $node->nid, $form_values, $node, $sid, $cid);
      $headers[$cid] = theme('webform_mail_headers_' . $node->nid, $form_values, $node, $sid, $cid);

      // Otherwise use the generic message or headers:
      if (empty($messages[$cid])) {
        $messages[$cid] = theme('webform_mail_message', $form_values, $node, $sid, $cid);
      }
      if (empty($headers[$cid])) {
        $headers[$cid] = theme('webform_mail_headers', $form_values, $node, $sid, $cid);
      }

      // Assemble the FROM string.
      if (isset($headers[$cid]['From'])) {
        $froms[$cid] = $headers[$cid]['From'];
        unset($headers[$cid]['From']);
      }
      elseif (drupal_strlen($email_from_name) > 0) {
        $froms[$cid] = '"' . mime_header_encode($email_from_name) . '" <' . $email_from_address . '>';
      }
      else {
        $froms[$cid] = $email_from_address;
      }

      // Update the subject if set in the themed headers.
      if (isset($headers[$cid]['Subject'])) {
        $subjects[$cid] = $headers[$cid]['Subject'];
        unset($headers[$cid]['Subject']);
      }
      else {
        $subjects[$cid] = $email_subject;
      }

      // Update the to e-mail if set in the themed headers.
      if (isset($headers[$cid]['To'])) {
        $emails[$cid] = $headers[$cid]['To'];
        unset($headers[$cid]['To']);
      }
    }

    // Verify that this submission is not attempting to send any spam hacks.
    if (_webform_submission_spam_check($emails['default'], $subjects['default'], $froms['default'], $headers['default'])) {
      $ip_address = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
      watchdog('webform', t('Possible spam attempt from @remote_addr', array(
        '@remote_add' => $ip_address,
      )) . "<br />\n" . nl2br(htmlentities($messages['default'])));
      drupal_set_message(t('Illegal information. Data not submitted.'), 'error');
      return FALSE;
    }

    // Mail the webform results.
    foreach ($emails as $cid => $address) {

      // In the case of checkboxes or multiple select, multiple e-mails may need
      // to be sent out.
      if (is_array($address)) {
        foreach ($address as $single_address) {
          drupal_mail('webform-submission', $single_address, $subjects[$cid], $messages[$cid], $froms[$cid], $headers[$cid]);

          // Debugging output for email.
          if (variable_get('webform_debug', 0) >= 2) {
            drupal_set_message('E-mail Headers: <pre>' . htmlentities(print_r($headers[$cid], TRUE)) . '</pre>To: ' . $single_address . '<br />From: ' . htmlentities($froms[$cid]) . '<br />Subject: ' . $subjects[$cid] . '<br />E-mail Body: <pre>' . $messages[$cid] . '</pre>');
          }
        }
      }
      else {
        drupal_mail('webform-submission', $address, $subjects[$cid], $messages[$cid], $froms[$cid], $headers[$cid]);

        // Debugging output for email.
        if (variable_get('webform_debug', 0) >= 2) {
          drupal_set_message('E-mail Headers: <pre>' . htmlentities(print_r($headers[$cid], TRUE)) . '</pre>To: ' . $address . '<br />From: ' . htmlentities($froms[$cid]) . '<br />Subject: ' . $subjects[$cid] . '<br />E-mail Body: <pre>' . $messages[$cid] . '</pre>');
        }
      }
    }
  }

  // More debugging output.
  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>');
  }

  // Log to watchdog if normal debug is on.
  if (variable_get('webform_debug', 0) >= 1) {
    watchdog('webform', t('Submission posted to %title', array(
      '%title' => $node->title,
    )) . l(t('Results'), 'node/' . $node->nid . '/submission/' . $sid) . "<br />\n<pre>" . htmlentities(print_r($form_values, TRUE)) . "</pre>");
  }

  // Check confirmation field to see if redirect should be to another node or a message.
  if ($form_values['submission']) {
    drupal_set_message(t('Submission updated.'));
    $redirect = NULL;
  }
  elseif (valid_url(trim($node->webform['confirmation']), TRUE)) {
    $redirect = trim($node->webform['confirmation']);
  }
  elseif (preg_match('/^internal:/', trim(strip_tags($node->webform['confirmation'])))) {
    $path = preg_replace('/^internal:/', '', trim(strip_tags($node->webform['confirmation'])));
    $redirect = array(
      trim($path),
      'sid=' . $sid,
    );
  }
  elseif (preg_match('/^message:/', $node->webform['confirmation'])) {
    $message = preg_replace('/^message:/', '', $node->webform['confirmation']);
    drupal_set_message($message);
    $redirect = NULL;
  }
  else {
    $redirect = array(
      'node/' . $node->nid . '/done',
      'sid=' . $sid,
    );
  }
  return $redirect;
}