You are here

function protected_node_save in Protected Node 6

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.

Return value

boolean TRUE if the action was successful, FALSE otherwise.

1 call to protected_node_save()
protected_node_nodeapi in ./protected_node.module
Implementation of hook_nodeapi(). @link http://api.drupal.org/api/function/hook_nodeapi/6

File

./protected_node.module, line 606

Code

function protected_node_save(&$node) {

  // 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 retrive nid because protected_node_passwd may exist and be empty
  $result = db_fetch_array(db_query("SELECT nid, protected_node_passwd FROM {protected_nodes} WHERE nid = %d", $node->nid));
  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 (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 = sha1($node->protected_node_passwd);
      }
    }
    else {
      $changed = FALSE;
      $node->protected_node_passwd = $result['protected_node_passwd'];
    }
    $sql = "UPDATE {protected_nodes} SET protected_node_is_protected = %d," . " protected_node_passwd = '%s', protected_node_show_title = %d," . " protected_node_hint = '%s'";
    $args = array(
      $node->protected_node_is_protected,
      $node->protected_node_passwd,
      $node->protected_node_show_title,
      isset($node->protected_node_hint) ? $node->protected_node_hint : '',
    );
    if ($changed) {
      $sql .= ", protected_node_passwd_changed = %d";
      $args[] = time();

      // last time the password was changed (i.e. invalidate all existing sessions)
    }
    $sql .= " WHERE nid = %d";
    $args[] = $node->nid;
    db_query($sql, $args);
  }
  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 = sha1($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
    $sql = "INSERT INTO {protected_nodes} (protected_node_is_protected," . " protected_node_passwd, protected_node_show_title, nid," . " protected_node_hint)" . " VALUES (%d, '%s', %d, %d, '%s')";
    db_query($sql, $node->protected_node_is_protected, $node->protected_node_passwd, $node->protected_node_show_title, $node->nid, isset($node->protected_node_hint) ? $node->protected_node_hint : '');
  }
}