You are here

function print_mail_form_validate in Printer, email and PDF versions 7.2

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. 6 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()

Form validation handler for print_mail_form().

See also

print_mail_form()

File

print_mail/print_mail.inc, line 315
Displays and processes the mail send form.

Code

function print_mail_form_validate($form, &$form_state) {
  $print_mail_user_recipients_default = variable_get('print_mail_user_recipients', PRINT_MAIL_USER_RECIPIENTS_DEFAULT);
  if (array_key_exists('cancel', $form_state['input'])) {
    form_set_error(NULL, '', TRUE);
    drupal_get_messages('error');
    drupal_goto(preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']), array(
      'query' => $form_state['values']['query'],
    ));
    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);
  }
  $to_array = array();
  if (!empty($form_state['values']['txt_to']['users'])) {
    $to_array = array_values($form_state['values']['txt_to']['users']);
  }
  if (!empty($form_state['values']['txt_to']['addrs'])) {

    // 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 = array_merge($to_array, explode(',', $to_addrs));
  }
  if (empty($to_array) && $print_mail_user_recipients_default) {
    form_set_error('txt_to', t('You must specify at least one email address or user as a recipient.'));
  }

  // 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', $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', 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);
}