You are here

function _privatemsg_validate_message in Privatemsg 6

Same name and namespace in other branches
  1. 6.2 privatemsg.module \_privatemsg_validate_message()
  2. 7.2 privatemsg.module \_privatemsg_validate_message()
  3. 7 privatemsg.module \_privatemsg_validate_message()
3 calls to _privatemsg_validate_message()
pm_send_validate in ./privatemsg.module
privatemsg_new_thread in ./privatemsg.module
Send a new message.
privatemsg_reply in ./privatemsg.module
Send a reply message

File

./privatemsg.module, line 1743
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'])) {

    // no need to do further checks in this case...
    if ($form) {
      form_set_error('author', t('User @user is not allowed to write messages', array(
        '@user' => $message['author']->name,
      )));
      return array(
        'success' => FALSE,
        'messages' => $messages,
      );
    }
    else {
      $messages['error'][] = t('User @user is not allowed to write messages', array(
        '@user' => $message['author']->name,
      ));
      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('Disallowed to send a message without subject'));
    }
    else {
      $messages['error'][] = t('Disallowed to send a message without subject');
    }
  }

  // Don't allow replies without a body.
  if (!empty($message['thread_id']) && ($message['body'] === NULL || $message['body'] === '')) {
    if ($form) {
      form_set_error('body', t('Disallowed to send reply without a message.'));
    }
    else {
      $messages['error'][] = t('Disallowed to send reply without a message.');
    }
  }

  // Check if an allowed format is used. global $user needs to be changed since
  // it is not possible to do the check for a specific user.
  global $user;
  $original_user = drupal_clone($user);
  session_save_session(FALSE);
  $user = $message['author'];
  if (!filter_access($message['format'])) {
    if ($form) {
      form_set_error('format', t('You are not allowed to use the specified input format.'));
    }
    else {
      $messages['error'][] = t('User @user is not allowed to use the specified input format.', array(
        '@user' => $message['author']->name,
      ));
    }
  }
  $user = $original_user;
  session_save_session(TRUE);
  if (empty($message['recipients']) || !is_array($message['recipients'])) {
    if ($form) {
      form_set_error('to', t('Disallowed to send a message without at least one valid recipient'));
    }
    else {
      $messages['error'][] = t('Disallowed to send a message without at least one valid recipient');
    }
  }
  if (!empty($message['recipients']) && is_array($message['recipients'])) {
    foreach (module_invoke_all('privatemsg_block_message', $message['author'], $message['recipients']) as $blocked) {
      unset($message['recipients'][$blocked['uid']]);
      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('to', t('Disallowed to send message because all recipients are blocked'));
    }
    else {
      $messages['error'][] = t('Disallowed to send message because all recipients are blocked');
    }
  }
  $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,
  );
}