You are here

function protected_node_init in Protected Node 7

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

Implements hook_init().

File

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

Code

function protected_node_init() {

  // Let Drush bypass password protection.
  if (function_exists('drush_main')) {
    return;
  }

  // Are we about to display a node?
  // Can user see all nodes anyway?
  if (user_access('edit protected content')) {
    return;
  }
  if (variable_get('protected_node_use_global_password', PROTECTED_NODE_PER_NODE_PASSWORD) == PROTECTED_NODE_GLOBAL_PASSWORD && isset($_SESSION['has_entered_global_password'])) {
    return;
  }
  $nid = FALSE;
  $param2 = arg(2);
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    if ($param2 === NULL) {
      $nid = protected_node_is_locked(arg(1), 'view');
      if ($nid === -1) {
        return;
      }
    }
    elseif ($param2 == 'edit' || $param2 == 'delete') {
      $nid = protected_node_is_locked(arg(1), $param2);
    }
    else {

      // Any other sub-path.
      $nid = protected_node_is_locked(arg(1), 'view');
    }
    if ($nid === TRUE || $nid === -1) {
      drupal_access_denied();
      exit;
    }
  }
  elseif (arg(0) == 'system' && arg(1) == 'files') {
    $requested_url = drupal_parse_url(request_uri());
    $path = urldecode(str_replace('system/files', '', $requested_url['path']));
    if (!empty($path)) {
      $nid = protected_node_and_attachment($path);
    }
  }
  if ($nid) {
    $query = drupal_get_destination();
    if (!empty($_SERVER['HTTP_REFERER'])) {
      $query['back'] = urlencode($_SERVER['HTTP_REFERER']);
    }
    $query['protected_page'] = $nid;
    drupal_goto('protected-node', array(
      'query' => $query,
    ));
  }
}