You are here

function agreement_init in Agreement 6

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

Implementation of hook_init().

File

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

Code

function agreement_init() {

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

  // Get the user role the agreement is restricted to.
  $role = check_plain(variable_get('agreement_role', -1));

  // Check to make sure the user belongs to the above role.
  if (array_key_exists($role, $user->roles)) {
    $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)) {
      $agreement_page_visibility_settings = check_plain(variable_get('agreement_page_visibility_settings', 0));
      $agreement_page_visibility_pages = check_plain(variable_get('agreement_page_visibility_pages', ''));

      // Match path if necessary.
      if ($agreement_page_visibility_pages) {

        // Convert path to lowercase. This allows comparison of the same path
        // with different case. Ex: /Page, /page, /PAGE.
        $pages = drupal_strtolower($agreement_page_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 $agreement_page_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 = !($agreement_page_visibility_settings xor $page_match);
      }
      else {
        $page_match = TRUE;
      }
      if ($page_match) {

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