You are here

function print_mail_form_validate in Printer, email and PDF versions 6

Same name and namespace in other branches
  1. 5.4 print_mail/print_mail.inc \print_mail_form_validate()
  2. 5.3 print_mail/print_mail.inc \print_mail_form_validate()
  3. 7.2 print_mail/print_mail.inc \print_mail_form_validate()
  4. 7 print_mail/print_mail.inc \print_mail_form_validate()
  5. 5.x print_mail/print_mail.inc \print_mail_form_validate()

Validate the send by-email form submission.

File

print_mail/print_mail.inc, line 206

Code

function print_mail_form_validate($form, &$form_state) {
  if (array_key_exists('cancel', $form['#post'])) {
    form_set_error(NULL, '', TRUE);
    drupal_get_messages('error');
    return;
  }
  $from_addr = trim($form_state['values']['fld_from_addr']);
  $test = user_validate_mail($from_addr);
  if ($test) {
    form_set_error('fld_from_addr', $test);
  }

  // All new-lines are replaced by commas
  $to_addrs = preg_replace('![\\r|\\n|,]+!', ',', trim($form_state['values']['txt_to_addrs']));

  // Create an array from the string
  $to_array = explode(',', $to_addrs);

  // Verify each element of the array
  foreach ($to_array as $key => $address) {
    $address = trim($address);
    if (preg_match('/(.*?) <(.*)>/s', $address, $matches)) {

      // Address is of the type User Name <user@domain.tld>
      $test = user_validate_mail($matches[2]);
      $to_array[$key] = trim($matches[1]) . ' <' . $matches[2] . '>';
    }
    else {

      // Address must be user@domain.tld
      $test = user_validate_mail($address);
    }
    if ($test) {
      form_set_error('txt_to_addrs', $test);
    }
  }
  $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
  if (!user_access('send unlimited emails') && !flood_is_allowed('print_mail', $print_mail_hourly_threshold - count($to_array) + 1)) {
    form_set_error('txt_to_addrs', t('You cannot send more than %number messages per hour. Please reduce the number of recipients.', array(
      '%number' => $print_mail_hourly_threshold,
    )));
  }

  // In all fields, prevent insertion of custom headers
  foreach ($form_state['values'] as $key => $string) {
    if (drupal_substr($key, 0, 4) == 'fld_' && (strpos($string, "\n") !== FALSE || strpos($string, "\r") !== FALSE)) {
      form_set_error($key, 'Found invalid character');
    }
  }
  $form_state['values']['fld_from_addr'] = $from_addr;
  $form_state['values']['fld_from_name'] = trim($form_state['values']['fld_from_name']);

  // Re-create the string from the re-organized array
  $form_state['values']['txt_to_addrs'] = implode(', ', $to_array);
}