You are here

function agreement_init in Agreement 6.2

Same name and namespace in other branches
  1. 6 agreement.module \agreement_init()
  2. 7.2 agreement.module \agreement_init()

Implementation of hook_init().

File

./agreement.module, line 29
agreement.module - Agreement module code

Code

function agreement_init() {

  // If the user hasn't already agreed, redirect them to the agreement page.
  global $user;

  // Check to make sure the user belongs to an agreement-restricted role.
  // Members with the 'skip agreement' permission are excluded from the agreement
  if (array_key_exists(variable_get('agreement_role', -1), $user->roles) && !user_access('skip agreement')) {
    $agreement_status = _agreement_status($user->uid);

    // We will not redirect to the agreement page from these URLs.
    $exceptions = array(
      check_plain(variable_get('agreement_page_url', AGREEMENT_PAGE_URL)),
      'logout',
      'admin/settings/agreement',
    );
    if ((!isset($agreement_status) || !$agreement_status) && !in_array($_GET['q'], $exceptions)) {

      // Match path if necessary.
      if ($visibility_pages = check_plain(variable_get('agreement_page_visibility_pages', FALSE))) {

        // Convert path to lowercase. This allows comparison of the same path
        // with different case. Ex: /Page, /page, /PAGE.
        $pages = drupal_strtolower($visibility_pages);

        // check_plain() converts <front> to &lt;front&gt; so need to convert
        // it back before matching
        $pages = str_replace('&lt;front&gt;', '<front>', $pages);

        // Convert the Drupal path to lowercase
        $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));

        // Compare the lowercase internal and lowercase path alias (if any).
        $page_match = drupal_match_path($path, $pages);
        if ($path != $_GET['q']) {
          $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
        }

        // When $visibility_settings has a value of 0, the agreement is displayed on all pages except
        // those listed in $pages. When set to 1, it is displayed only on those pages listed in $pages.
        $page_match = !(check_plain(variable_get('agreement_page_visibility_settings', 0)) xor $page_match);
      }
      else {
        $page_match = TRUE;
      }
      if ($page_match) {

        // Save intended destination
        if (!isset($_SESSION['agreement_destination'])) {
          if (preg_match('/^user\\/reset/', $_GET['q'])) {
            $_SESSION['agreement_destination'] = 'change password';
          }
          else {
            $_SESSION['agreement_destination'] = $_GET['q'];
          }
        }
        drupal_goto(check_plain(variable_get('agreement_page_url', AGREEMENT_PAGE_URL)));
        exit;
      }
    }
  }
}