You are here

function protected_node_node_validate in Protected Node 7

Same name and namespace in other branches
  1. 1.0.x protected_node.module \protected_node_node_validate()

Implements hook_node_validate().

File

./protected_node.module, line 547
Protected Node module.

Code

function protected_node_node_validate($node, $form, &$form_state) {

  // The node type is never protected if $node->protected_node_is_protected is
  // not set.
  if (!isset($node->protected_node_is_protected)) {
    return;
  }
  global $_protected_node_emails;
  global $_protected_node_random_passwd;
  $_protected_node_emails = '';
  $_protected_node_random_passwd = '';
  if ($node->protected_node_is_protected && (user_access('edit any protected node password') || user_access('edit ' . $node->type . ' password'))) {
    $missing_password = FALSE;
    if (empty($node->protected_node_passwd)) {

      // Global content type password exists ?
      $global_content_type_password = variable_get('protected_node_node_type_password_' . $node->type, '');
      if ($global_content_type_password != '') {

        // it's ok.
      }
      else {
        $result = db_select('protected_nodes')
          ->fields('protected_nodes', array(
          'protected_node_passwd',
        ))
          ->condition('nid', $node->nid)
          ->execute()
          ->fetchField();

        // Getting "    " (40 spaces) when empty.
        $result = trim($result);
        if (empty($result)) {
          $missing_password = TRUE;
        }
      }
    }
    if (!empty($node->protected_node_emails)) {
      if ($node->status) {

        // Verify each email address.
        $emails = explode(',', str_replace(array(
          "\r",
          "\n",
        ), ',', $node->protected_node_emails));
        foreach ($emails as $k => $m) {
          $m = trim($m);
          if ($m) {
            if (!valid_email_address($m)) {
              form_error($form['protected_node']['protected_node_emails'], t('Invalid email address: @m. Please correct this mistake and try again.', array(
                '@m' => $m,
              )));

              // Unset just in case; should be useless though.
              unset($emails[$k]);
            }
            else {
              $emails[$k] = $m;
            }
          }
          else {

            // Ignore empty entries.
            unset($emails[$k]);
          }
        }
        $_protected_node_emails = implode(', ', $emails);
        if ($_protected_node_emails && $missing_password && variable_get('protected_node_random_password', FALSE)) {

          // Automatically generate a password for the email. Note that means
          // the author won't know the password!
          $_protected_node_random_passwd = user_password();

          // Not missing anymore.
          $missing_password = FALSE;
          drupal_set_message(t('A random password was generated in order to send the email about this page. Remember that changing the password will prevent users you just emailed from accessing this page.'), 'warning');
        }
      }
      else {

        // The node is not published, forget about emails!
        form_error($form['protected_node']['protected_node_emails'], t('The node is not published. Therefore no email will be sent.'));
      }
    }
    if ($missing_password) {
      global $user;
      if ($user->uid == 0) {

        // If anonymous user, then global password is not an option otherwise
        // all the nodes could be edited by all the anonymous users!
        $global_password = PROTECTED_NODE_PER_NODE_PASSWORD;
      }
      else {
        $global_password = variable_get('protected_node_use_global_password', PROTECTED_NODE_PER_NODE_PASSWORD);
      }
      switch ($global_password) {
        case PROTECTED_NODE_PER_NODE_PASSWORD:
          form_error($form['protected_node']['protected_node_passwd'], t('To protect this page, please enter a password.'));
          break;
      }
    }
  }
  elseif (isset($node->protected_node_emails) && trim($node->protected_node_emails)) {
    form_error($form['protected_node']['protected_node_emails'], t('No email can be sent by the protected node module when the node is not protected or you do not have permission to set a password.'));
  }
}