You are here

function protected_node_set_protected in Protected Node 6

Same name and namespace in other branches
  1. 5 protected_node.module \protected_node_set_protected()
  2. 7 protected_node.module \protected_node_set_protected()
  3. 1.0.x protected_node.module \protected_node_set_protected()

This method marks the specified node as protected.

The method accepts a password. It is legal to not pass a password in which case the previously defined password is used or the global password. If no password is available, then the node gets locked until edited by the author or the administrator (UID=1) and a password is added.

If the \p $passwd parameter is set, then the change is marked in the database. In other words, all users who had previously enter a password will be kicked out.

@param[in] $param The node identifier or whatever valid $param passed to node_load. @param[in] $passwd The node password.

Return value

TRUE if the node is protection on return.

File

./protected_node.module, line 873

Code

function protected_node_set_protected($param, $passwd = NULL) {

  // get the existing node
  $node = node_load($param);
  if ($node == FALSE) {

    // not even a valid node identifier?!
    return FALSE;
  }
  if (empty($node->protected_node_is_protected)) {

    // node exists in our table?
    $r = db_result(db_query("SELECT nid FROM {protected_nodes} WHERE nid = %d", $node->nid));
    if ($r) {
      if (empty($passwd)) {

        // in this case, an empty password is fine
        $sql = "UPDATE {protected_nodes} SET protected_node_is_protected = 1 WHERE nid = %d";
        $result = db_query($sql, $node->nid) !== FALSE;
      }
      else {

        // we have to also update the password in this case
        $sql = "UPDATE {protected_nodes} SET protected_node_is_protected = 1," . " protected_node_passwd = '%s', protected_node_passwd_changed = %d WHERE nid = %d";
        $result = db_query($sql, sha1($passwd), time(), $node->nid) !== FALSE;
      }
    }
    else {

      // no entry in the database yet, add it now
      if (empty($passwd)) {
        $passwd = '';
      }
      else {
        $passwd = sha1($passwd);
      }
      $sql = "INSERT INTO {protected_nodes} (protected_node_is_protected," . " protected_node_passwd, protected_node_show_title, nid)" . " VALUES (1, '%s', %d, %d)";
      $result = db_query($sql, $passwd, variable_get('protected_node_show_node_titles', FALSE), $node->nid) !== FALSE;
    }
  }
  else {

    // the node is already protected, change the password if necessary
    if (empty($passwd)) {

      // it is protected; we're done (the password is not to be changed)
      return TRUE;
    }
    $sql = "UPDATE {protected_nodes} SET protected_node_passwd = '%s', protected_node_passwd_changed = %d WHERE nid = %d";
    $result = db_query($sql, sha1($passwd), time(), $node->nid) !== FALSE;
  }
  return $result;
}