You are here

function protected_node_node_view in Protected Node 7

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

Implements hook_node_view().

File

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

Code

function protected_node_node_view($node, $view_mode, $langcode) {
  global $user;
  if (!empty($node->protected_node_is_protected)) {

    // Accessed for search indexing? (usually by cron.php).
    if ($view_mode == 'search_index') {

      // "user" could see the node, but at this time, not its contents
      // (the current user is Anonymous, so that statement is not exactly true,
      // but at the time of the search index building we cannot know who will
      // be searching so we let go without the access denied error).
      protected_node_invoke('protected_node_hide', $node);
    }
    elseif (!user_access('view protected content') && _protected_node_check_view_mode($view_mode)) {
      if (!$user->uid && variable_get('cache', 1)) {

        // Prevent caching (do NOT use variable_set() since this is temporary
        // for this session).
        $GLOBALS['conf']['cache'] = 0;
      }
      $global_reset_time = variable_get('protected_node_session_timelimit', 0);
      $this_page_reset_time = $node->protected_node_passwd_changed;
      if ($node->uid !== $user->uid) {

        // Is there a global password?
        if (isset($_SESSION['_protected_node']['passwords']['global'])) {

          // Is password out of date?
          $when = $_SESSION['_protected_node']['passwords']['global'];
          if ($when <= $global_reset_time || $when <= $this_page_reset_time) {
            unset($_SESSION['_protected_node']['passwords']['global']);
          }
        }

        // Is there a password?
        if (isset($_SESSION['_protected_node']['passwords'][$node->nid])) {

          // Is password out of date?
          $when = $_SESSION['_protected_node']['passwords'][$node->nid];
          if ($when <= $global_reset_time || $when <= $this_page_reset_time) {
            unset($_SESSION['_protected_node']['passwords'][$node->nid]);
          }
        }
        if (!isset($_SESSION['_protected_node']['passwords'][$node->nid]) && !isset($_SESSION['_protected_node']['passwords']['global'])) {
          if (!user_access('access protected node password form')) {

            // User will never be given access (no drupal_goto() call
            // necessary).
            drupal_access_denied();
            exit;
          }

          // User could see the node, but at this time, not its contents.
          protected_node_invoke('protected_node_hide', $node);
        }
      }
    }
  }
}