You are here

function protected_node_enter_any_password in Protected Node 7

Same name and namespace in other branches
  1. 6 protected_node.fork.inc \protected_node_enter_any_password()
  2. 1.0.x protected_node.fork.inc \protected_node_enter_any_password()

Create the form asking the end users for the node password.

The function expects a $_GET with the variable named protected_pages. This parameter is a list of comma separated NIDs.

The function accepts a $_GET named 'back'. This is a URL used for the Cancel link shown next to the OK button.

@important The function does NOT use check_plain() on the $info, $description, etc. because the administrator is the only one who can enter that information.

@todo It would be a good idea to transform this function in a theme() call instead.

1 string reference to 'protected_node_enter_any_password'
protected_node_menu_array in ./protected_node.settings.inc
Actual implementation of the protected_node_menu() function.

File

./protected_node.fork.inc, line 27
Redirected page callback file for the protected_node module.

Code

function protected_node_enter_any_password() {

  // Global $user is used for token replacement, should it be the node author?
  global $user;
  if (isset($_GET['destination'])) {

    // Illegal call.
    watchdog('protected_node', 'Illegal call to /protected-nodes: destination parameter specified.', array(), WATCHDOG_WARNING);
    drupal_access_denied();
  }
  $nids = protected_node_get_nids_from_protected_pages_parameter();

  // Check, in reverse order, whether a node password was specified.
  // If so, use that node.
  // @todo: why reverse order?
  foreach (array_reverse($nids) as $nid) {
    if (!empty($_SESSION['_protected_node']['passwords'][$nid])) {
      $when = $_SESSION['_protected_node']['passwords'][$nid];

      // Global reset time.
      if ($when > variable_get('protected_node_session_timelimit', 0)) {
        $node = node_load($nid);

        // This page reset time.
        if ($when > $node->protected_node_passwd_changed) {

          // The password is still known, go to the page.
          drupal_goto('node/' . $nid);
        }
      }

      // If the "best" node had a password and it's gone, we don't test the
      // other pages; instead we accept this one as is...
      break;
    }
  }

  // Make sure the first node exists, the others we don't care as much at this
  // point they are tested when the user submits the password and used as the
  // destination.
  $node = node_load($nids[0]);
  if (!is_object($node)) {

    // Illegal nid.
    watchdog('protected_node', 'Illegal nids[0] (@nid) to /protected-nodes: node does not exist.', array(
      '@nid' => $nids[0],
    ), WATCHDOG_WARNING);
    drupal_access_denied();
  }

  // Some variable initialization.
  $types = node_type_get_types();
  $node_type = $types[$node->type];
  $token_module_enabled = module_exists('token');

  // Set the title of this page.
  $title = variable_get('protected_node_title', NULL);
  if (!empty($title)) {
    if ($token_module_enabled) {
      $title = token_replace($title, array(
        'node' => $node,
      ));
      $title = token_replace($title, array(
        'user' => $user,
      ));
    }
    drupal_set_title($title);
  }

  // Set the information that appears between the title and the password form.
  $info = variable_get('protected_node_info', '');
  if ($token_module_enabled) {
    $info = token_replace($info, array(
      'node' => $node,
    ));
    $info = token_replace($info, array(
      'user' => $user,
    ));
  }
  if ($info) {
    $form['protected_node'] = array(
      '#type' => 'fieldset',
      '#description' => filter_xss_admin($info),
      '#collapsible' => FALSE,
    );
  }

  // Enter the detailed description of the protected node password.
  $description = variable_get('protected_node_description_' . $node->type, '');
  if (!$description) {
    $description = variable_get('protected_node_description', '');
  }
  if (!$description) {
    if ($node->protected_node_show_title) {

      // Embellish the title with double quotes.
      $node_title = '"' . $node->title . '"';
    }
    else {
      $node_title = '';
    }
    $description = t('The @node_type @node_title you are trying to view is password protected. Please enter the password below to proceed.', array(
      '@node_type' => $node_type->name,
      '@node_title' => $node_title,
    ));
  }
  elseif ($token_module_enabled) {
    $description = token_replace($description, array(
      'node' => $node,
    ));
    $description = token_replace($description, array(
      'user' => $user,
    ));
  }
  $form['protected_node_enterpassword'] = array(
    '#type' => 'fieldset',
    '#description' => filter_xss_admin($description),
    '#collapsible' => FALSE,
  );

  // Create the password widget.
  $label = variable_get('protected_node_password_label', '');
  if ($token_module_enabled) {
    $label = token_replace($label, array(
      'node' => $node,
    ));
    $label = token_replace($label, array(
      'user' => $user,
    ));
  }
  else {
    $label = t('@node_type password', array(
      '@node_type' => $node_type->name,
    ));
  }
  $form['protected_node_enterpassword']['password'] = array(
    '#type' => 'password',
    '#title' => $label,
    '#size' => 20,
  );

  // Add a submit button.
  $form['protected_node_enterpassword']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('OK'),
  );

  // Add a cancel link when 'back' is defined (i.e. refers on the previous
  // page).
  if (isset($_GET['back'])) {
    $cancel = urldecode($_GET['back']);
  }
  elseif (variable_get('protected_node_cancel', 0)) {
    $cancel = '<front>';
  }
  if (isset($cancel) && $cancel) {
    $form['protected_node_enterpassword']['cancel'] = array(
      '#type' => 'markup',
      '#markup' => l(t('Cancel'), $cancel),
    );
  }
  return $form;
}