You are here

function _pages_restriction_restrict_access in Pages Restriction Access 7

Restriction rules feature for Pages Restriction.

Parameters

string $path: String current URL Path that users access.

string $method: String with method that the access is restricting it.

Return value

bool Return a TRUE bool value if the access is restricted.

1 call to _pages_restriction_restrict_access()
pages_restriction_page_alter in ./pages_restriction.module
Implements hook_page_alter().

File

includes/pages_restriction.helpers.inc, line 172
Contains the helpers functions for the Pages Restriction.

Code

function _pages_restriction_restrict_access(&$path, $method = 'direct function') {

  // Check if the user is 'administrator' or 'Content Editor'.
  if (user_is_logged_in()) {
    global $user;
    $is_admin = in_array('administrator', array_values($user->roles));
    $is_content_editor = in_array('Content Editor', array_values($user->roles));
    if ($is_admin || $is_content_editor) {
      return FALSE;
    }
  }

  // Get data from Cache.
  // Module Load is being added here for performance purposes.
  module_load_include('inc', 'pages_restriction', 'includes/pages_restriction.cache');
  $data = _pages_restriction_get_cached_data();

  // Explode all lines in variable to a item in array.
  $rules = _pages_restriction_get_rules_by_path($path);
  $total_matches = 0;
  foreach ($rules as $key => $rule) {
    $pages = $rule;
    foreach ($pages as $key_page => $page) {

      // If path matches an item on the line, verify it.
      if (preg_match('@^' . str_replace('%', '\\w*', trim($page)) . '$@', $path)) {

        // Increase number of total matches. This will be used for multirules.
        $total_matches++;

        // If it is the first item don't verify.
        if ($key_page === 0) {
          return FALSE;
        }

        // Define watchdog message/params for future use during debug.
        $watchdog_message = t('URL Change From @path to @new_path from @method');
        $watchdog_values = array(
          '@path' => $path,
          '@new_path' => $pages[0],
          '@method' => $method,
        );

        // If there is no session defined, send to first page.
        if (!isset($_SESSION['pages_restriction_id_session_' . $key])) {
          _pages_restriction_debug_info($watchdog_message, $watchdog_values);
          $path = $pages[0];
          return TRUE;
        }

        // If data for this id_session is empty, send to first page.
        if (!isset($data[$_SESSION['pages_restriction_id_session_' . $key]][$key])) {
          _pages_restriction_debug_info($watchdog_message, $watchdog_values);
          $path = $pages[0];
          return TRUE;
        }
        $cached_page = trim($data[$_SESSION['pages_restriction_id_session_' . $key]][$key]);

        // If page matches, stop it.
        if (trim($page) === $cached_page) {

          // If it is the last page,
          // unset the session so a new session ID can be generated.
          if ($key_page === count($pages) - 1) {
            _pages_restriction_unset_sections_with_path($path);
          }
          return FALSE;
        }
      }
    }
  }

  // If at least 1 match happened, we should redirect it to the stored page.
  if ($total_matches > 0) {

    // Consider that the first stored rule is the only important here.
    $key = array_keys($rules)[0];
    $path = $data[$_SESSION['pages_restriction_id_session_' . $key]][$key];
    $watchdog_values['@new_path'] = $path;
    _pages_restriction_debug_info($watchdog_message, $watchdog_values);
    return TRUE;
  }

  // Return FALSE if no condition are matched.
  return FALSE;
}