function _privatemsg_validate_message

1 privatemsg.module _privatemsg_validate_message(&$message, $form = FALSE)
7-1 privatemsg.module _privatemsg_validate_message(&$message, $form = FALSE)
7-2 privatemsg.module _privatemsg_validate_message(&$message, $form = FALSE)
6-2 privatemsg.module _privatemsg_validate_message(&$message, $form = FALSE)
3 calls to _privatemsg_validate_message()

File

./privatemsg.module, line 1772
Allows users to send private messages to other users.

Code

function _privatemsg_validate_message(&$message, $form = FALSE) {
  $messages = array(
    'error' => array(),
    'warning' => array(),
  );
  if (!(privatemsg_user_access('write privatemsg', $message->author) || (privatemsg_user_access('reply only privatemsg', $message->author) && isset($message->thread_id)))) {
    // no need to do further checks in this case...
    if ($form) {
      form_set_error('author', t('You are not allowed to write messages.'));
      return array(
        'success' => FALSE,
        'messages' => $messages,
      );
    }
    else {
      $messages['error'][] = t('@user is not allowed to write messages.', array('@user' => privatemsg_recipient_format($message->author, array('plain' => TRUE))));
      return array(
        'success' => FALSE,
        'messages' => $messages,
      );
    }
  }

  // Prevent subjects which only consist of a space as these can not be clicked.
  $message->subject = trim($message->subject);
  if (empty($message->subject)) {
    if ($form) {
      form_set_error('subject', t('You must include a subject line or a message.'));
    }
    else {
      $messages['error'][] = t('A subject or message must be included.');
    }
  }

  // Don't allow replies without a body.
  if (!empty($message->thread_id) && ($message->body === NULL || $message->body === '')) {
    if ($form) {
      form_set_error('body', t('You must include a message in your reply.'));
    }
    else {
      $messages['error'][] = t('A message must be included in your reply.');
    }
  }

  // Check if an allowed format is used.
  if (!filter_access(filter_format_load($message->format), $message->author)) {
    if ($form) {
      form_set_error('format', t('You are not allowed to use the specified format.'));
    }
    else {
      $messages['error'][] = t('@user is not allowed to use the specified input format.', array('@user' => privatemsg_recipient_format($message->author, array('plain' => TRUE))));
    }
  }

  if (empty($message->recipients) || !is_array($message->recipients)) {
    if ($form) {
      form_set_error('recipient', t('You must include at least one valid recipient.'));
    }
    else {
      $messages['error'][] = t('At least one valid recipient must be included with the message.');
    }
  }

  if (!empty($message->recipients) && is_array($message->recipients)) {
    foreach (module_invoke_all('privatemsg_block_message', $message->author, $message->recipients, (array) $message) as $blocked) {
      unset($message->recipients[$blocked['recipient']]);
      if ($form) {
        drupal_set_message($blocked['message'], 'warning');
      }
      else {
        $messages['warning'][] = $blocked['message'];
      }
    }
  }

  // Check again, give another error message if all recipients are blocked
  if (empty($message->recipients)) {
    if ($form) {
      form_set_error('recipient', t('You are not allowed to send this message because all recipients are blocked.'));
    }
    else {
      $messages['error'][] = t('The message cannot be sent because all recipients are blocked.');
    }
  }

  // Verify if message has tokens and user is allowed to use them.
  $message->has_tokens = privatemsg_user_access('use tokens in privatemsg', $message->author) && count(token_scan($message->subject . $message->body));

  $messages = array_merge_recursive(module_invoke_all('privatemsg_message_validate', $message, $form), $messages);

  // Check if there are errors in $messages or if $form is TRUE, there are form errors.
  $success = empty($messages['error']) || ($form && count((array) form_get_errors()) > 0);
  return array(
    'success' => $success,
    'messages' => $messages,
  );
}