You are here

function site_disclaimer_init in Site Disclaimer 6

Same name and namespace in other branches
  1. 7 site_disclaimer.module \site_disclaimer_init()

Implementation of hook_init().

File

./site_disclaimer.module, line 296
This module adds Site Disclaimer to the registration page.

Code

function site_disclaimer_init() {
  global $user;

  // http://drupal.org/node/313272#comment-2786004
  // This is a performance hit. We need it here because $conf in settings.php
  // overrides database variable value in variable_init().
  // Faster performance will be achieved if the variables in
  // _site_disclaimer_i18n() are added to $conf in settings.php.
  _site_disclaimer_i18n();
  if ($user->uid > 1 && !user_access('administer site configuration')) {
    $site_disclaimer_enabled = _site_disclaimer_enabled();
    $site_disclaimer_version = variable_get('site_disclaimer_version', 1);
    if ($site_disclaimer_enabled && (!isset($user->site_disclaimer) || $user->site_disclaimer < $site_disclaimer_version)) {
      $allow_nodes = variable_get('site_disclaimer_allow_nodes', array());

      // Allowed paths, array path components mean logical or (any of)
      $allow_paths = array(
        explode('/', SITE_DISCLAIMER_ACCEPT_PATH),
        array(
          'logout',
        ),
      );

      // Any links in the Site Disclaimer node or in the checkbox should also be allowed.
      if (!empty($allow_nodes)) {
        $allow_paths[] = array(
          'node',
          $allow_nodes,
        );

        // Special case for print.module - allow the user to also print the allowed nodes.
        $allow_paths[] = array(
          'print',
          'node',
          $allow_nodes,
        );
        $allow_paths[] = array(
          'printpdf',
          'node',
          $allow_nodes,
        );
      }

      // @todo: implement hook_site_disclaimer_allowed_paths_alter() here, so paths like print, printpdf can belong to print module
      // Check if the path is in the allowed list
      foreach ($allow_paths as $allow_path) {

        // This while loop ends in "return" if the path matches.
        // If the path does not match, break 2 will loop to the next allowed path
        while (1) {
          $i = 0;

          // Array path components mean logical or (any of)
          foreach ($allow_path as $part) {
            if (is_array($part) && !in_array(arg($i), $part)) {
              break 2;
            }
            if (!is_array($part) && arg($i) != $part) {
              break 2;
            }
            $i++;
          }

          // The arg() path matches the one in the $allow_path
          return;
        }
      }
      drupal_goto(SITE_DISCLAIMER_ACCEPT_PATH, drupal_get_destination());
    }
  }
}