You are here

function protected_node_nodeapi in Protected Node 5

Same name and namespace in other branches
  1. 6 protected_node.module \protected_node_nodeapi()

Implementation of hook_nodeapi(). @link http://api.drupal.org/api/function/hook_nodeapi/5

File

./protected_node.module, line 118

Code

function protected_node_nodeapi(&$node, $op, $arg = 0, $page = 0) {
  global $user;
  switch ($op) {
    case 'load':
      $output['is_protected'] = protected_node_isset_protected($node->nid);
      $output['was_protected'] = $output['is_protected'];
      return $output;
      break;
    case 'delete':
      protected_node_unset_protected($node->nid);
      break;
    case 'insert':
      if ($node->is_protected && (!empty($node->password) || isset($_SESSION['_protected_node']['entered_password']))) {
        $pwd = isset($_SESSION['_protected_node']['entered_password']) ? $_SESSION['_protected_node']['entered_password'] : $node->password;
        unset($_SESSION['_protected_node']['entered_password']);
        protected_node_set_protected($node->nid, $pwd);
      }
      break;
    case 'validate':
      if ($node->is_protected && !empty($node->password)) {
        $_SESSION['_protected_node']['entered_password'] = $node->password;
      }
      break;
    case 'update':

      // Protected flag was switched off?
      if ($node->was_protected && !$node->is_protected) {
        protected_node_unset_protected($node->nid);
      }
      if ($node->is_protected && (!empty($node->password) || isset($_SESSION['_protected_node']['entered_password']))) {
        if (db_result(db_query('SELECT count(*) FROM {protected_nodes} WHERE nid = %d', $node->nid)) > 0) {
          $pwd = isset($_SESSION['_protected_node']['entered_password']) ? $_SESSION['_protected_node']['entered_password'] : $node->password;
          unset($_SESSION['_protected_node']['entered_password']);
          protected_node_set_protected($node->nid, $pwd);
        }
        else {
          protected_node_nodeapi($node, 'insert', $arg, $page);
        }
      }
      break;
    case 'view':
      if ($node->is_protected && !user_access('bypass password protection')) {

        // If we have been accessed from cron.php (f.e. search indexing)
        if (variable_get('cron_semaphore', FALSE)) {
          $node->title = '';
          $node->teaser = '';
          $node->body = '';
          $node->content = array();
        }
        else {
          if (!$user->uid && variable_get('cache', 0)) {
            $GLOBALS['conf']['cache'] = FALSE;
          }
          if ($node->uid !== $user->uid) {

            // If node is protected and not teaser nor page view and not owner of node
            if (!isset($_SESSION['_protected_node']['passwords'][$node->nid])) {
              if (!$arg) {
                $_SESSION['_protected_node']['current'] = $node->nid;
                drupal_goto('protected-node', 'destination=' . $_GET['q']);
              }
              else {
                $node->teaser = '';
                $node->body = '';
                $node->content = array();
              }
            }
          }
        }
      }
      break;
  }
}