You are here

function print_mail_form_validate in Printer, email and PDF versions 5.3

Same name and namespace in other branches
  1. 5.4 print_mail/print_mail.inc \print_mail_form_validate()
  2. 6 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 150

Code

function print_mail_form_validate($form_id, $form_values, $form) {
  if (array_key_exists('cancel', $form['#post'])) {
    return;
  }
  $from_addr = trim($form_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 = str_replace(array(
    "\r\n",
    "\n",
    "\r",
  ), ',', $form_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);
    }
  }

  // In all fields, prevent insertion of custom headers
  foreach ($form_values as $key => $string) {
    if (substr($key, 0, 4) == 'fld_' && (strpos($string, "\n") !== FALSE || strpos($string, "\r") !== FALSE)) {
      form_set_error($key, 'Found invalid character');
    }
  }
  $subject = trim($form_values['fld_subject']);
  if (empty($subject)) {
    form_set_error('fld_subject', t('You must enter a subject.'));
  }
  $message = trim($form_values['txt_message']);
  if (empty($message)) {
    form_set_error('txt_message', t('You must enter a message.'));
  }
  form_set_value($form['fld_from_addr'], $from_addr);
  form_set_value($form['fld_from_name'], trim($form_values['fld_from_name']));

  // Re-create the string from the re-organized array
  form_set_value($form['txt_to_addrs'], implode(', ', $to_array));
  form_set_value($form['fld_subject'], $subject);
  form_set_value($form['txt_message'], $message);
}