You are here

function _protected_node_save in Protected Node 7

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

Sets the given node to protected with the provided password.

The password cannot be empty.

If the node already password protected this method changes the password to the one you provided as $password parameter.

@param[in,out] object $node The node to be saved.

1 call to _protected_node_save()
_protected_node_node_create_or_update in ./protected_node.module
Helper function.

File

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

Code

function _protected_node_save(&$node) {

  // The node type is never protected if $node->protected_node_is_protected
  // is not set.
  if (!isset($node->protected_node_is_protected)) {
    return;
  }

  // We first test whether a protected_nodes entry exist so we can use UPDATE
  // or INSERT accordingly (UPDATE does not always properly report working
  // with MySQL).
  // We also retrieve nid because protected_node_passwd may exist and be empty.
  $result = db_select('protected_nodes')
    ->fields('protected_nodes', array(
    'nid',
    'protected_node_passwd',
    'protected_node_emails',
  ))
    ->condition('nid', $node->nid)
    ->execute()
    ->fetchAssoc();
  if (!empty($result)) {

    // Note: the following test prevents the user from using "0" as a password.
    if (isset($node->protected_node_passwd)) {
      $changed = $node->protected_node_passwd != $result['protected_node_passwd'];
      if ($changed) {
        if (empty($node->protected_node_passwd)) {

          // Keep result if it's empty ...
          $node->protected_node_passwd = $result['protected_node_passwd'];
          $changed = FALSE;
        }
        else {
          $node->protected_node_clear_passwd = $node->protected_node_passwd;
          $node->protected_node_passwd = hash('sha256', $node->protected_node_passwd);
        }
      }
    }
    else {
      $changed = FALSE;
      $node->protected_node_passwd = $result['protected_node_passwd'];
    }

    // Check if the email addresses is empty.
    if (empty($node->protected_node_emails)) {
      if (!empty($result['protected_node_emails'])) {

        // Keep the addresses.
        $saved_emails = $result['protected_node_emails'];
      }
      else {
        $saved_emails = '';
      }
    }
    else {
      $saved_emails = $node->protected_node_emails;
    }
    $args = array(
      'protected_node_is_protected' => (int) $node->protected_node_is_protected,
      'protected_node_passwd' => $node->protected_node_passwd,
      'protected_node_show_title' => (int) $node->protected_node_show_title,
      'protected_node_emails' => $saved_emails,
      'protected_node_hint' => isset($node->protected_node_hint) ? $node->protected_node_hint : '',
    );
    if ($changed) {
      $args['protected_node_passwd_changed'] = REQUEST_TIME;
    }
    db_update('protected_nodes')
      ->fields($args)
      ->condition('nid', $node->nid)
      ->execute();
  }
  else {
    if (!isset($node->protected_node_passwd)) {

      // This happens when the global password is to be used.
      $node->protected_node_passwd = '';
    }
    elseif ($node->protected_node_passwd) {
      $node->protected_node_clear_passwd = $node->protected_node_passwd;
      $node->protected_node_passwd = hash('sha256', $node->protected_node_passwd);
    }

    // We don't need to set the protected_node_passwd_changed since no
    // one has ever entered a password for this node.
    db_insert('protected_nodes')
      ->fields(array(
      'protected_node_is_protected' => (int) $node->protected_node_is_protected,
      'protected_node_passwd' => $node->protected_node_passwd,
      'protected_node_show_title' => (int) $node->protected_node_show_title,
      'nid' => $node->nid,
      'protected_node_emails' => isset($node->protected_node_emails) ? $node->protected_node_emails : '',
      'protected_node_hint' => isset($node->protected_node_hint) ? $node->protected_node_hint : '',
    ))
      ->execute();
  }
}