You are here

function protected_node_set_protected in Protected Node 7

Same name and namespace in other branches
  1. 5 protected_node.module \protected_node_set_protected()
  2. 6 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

bool TRUE if the node is protection on return.

File

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

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?
    $select = db_select('protected_nodes')
      ->fields('protected_nodes', array(
      'nid',
    ))
      ->condition('nid', $node->nid)
      ->execute()
      ->fetchField();
    if ($select) {
      if (empty($passwd)) {

        // In this case, an empty password is fine.
        $result = db_update('protected_nodes')
          ->fields(array(
          'protected_node_is_protected' => 1,
        ))
          ->condition('nid', $node->nid)
          ->execute() !== FALSE;
      }
      else {

        // We have to also update the password in this case.
        $result = db_update('protected_nodes')
          ->fields(array(
          'protected_node_is_protected' => 1,
          'protected_node_passwd' => hash('sha256', $passwd),
          'protected_node_passwd_changed' => REQUEST_TIME,
        ))
          ->condition('nid', $node->nid)
          ->execute() !== FALSE;
      }
    }
    else {

      // No entry in the database yet, add it now.
      if (empty($passwd)) {
        $passwd = '';
      }
      else {
        $passwd = hash('sha256', $passwd);
      }
      $result = db_insert('protected_nodes')
        ->fields(array(
        'nid' => $node->nid,
        'protected_node_is_protected' => 1,
        'protected_node_passwd' => $passwd,
        'protected_node_show_title' => variable_get('protected_node_show_node_titles', FALSE),
      ))
        ->execute() !== 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;
    }
    $result = db_update('protected_nodes')
      ->fields(array(
      'protected_node_passwd' => hash('sha256', $passwd),
      'protected_node_passwd_changed' => REQUEST_TIME,
    ))
      ->condition('nid', $node->nid)
      ->execute() !== FALSE;
  }
  return $result;
}