You are here

function access_filter_check_access in Access Filter 7

Check the access matches to filter.

Parameters

object $filter: An object of filter.

string $path: String of path to check.

string $ip: String of IP address.

Return value

bool Boolean TRUE if access is allowed.

2 calls to access_filter_check_access()
access_filter_boot in ./access_filter.module
Implements hook_boot().
access_filter_form_filter_callback_test in ./access_filter.admin.inc
Ajax callback function for access_filter_form_filter(). Test filter and show result.

File

./access_filter.module, line 215
Allows users to manage access filters.

Code

function access_filter_check_access($filter, $path = NULL, $ip = NULL) {

  // Pass checking if disabled.
  // In fast mode, $filter->status is not set and passed only enabled filters.
  if (isset($filter->status) && $filter->status < ACCESS_FILTER_STATUS_ENABLED) {
    return TRUE;
  }

  // Check path.
  if (is_null($path)) {
    $path = current_path();
    $uri = ltrim(request_uri(), '/');
  }
  else {

    // Test mode.
    $uri = $path;
    if (!empty($GLOBALS['conf']['clean_url'])) {
      $path = strstr($uri, '?', TRUE);
    }
    else {
      $parsed_url = drupal_parse_url($path);
      $path = $parsed_url['path'];
    }
    if ($path === FALSE) {
      $path = $uri;
    }
  }

  // Use cache data to admin paths to avoid breaking admin pages.
  $admin_paths_cache = cache_get('access_filter_admin_paths');
  if ($admin_paths_cache) {
    $admin_paths = $admin_paths_cache->data;
  }
  else {
    $admin_paths = array(
      'admin' => 'admin',
      'non admin' => '',
    );
  }
  $path_matched = FALSE;
  $path_aliases = $uri_aliases = NULL;
  foreach ($filter->parsed_paths as $parsed) {
    if (!$parsed->is_blind) {

      // Get all aliases once.
      if (is_null($path_aliases)) {
        $path_aliases = access_filter_get_all_aliases($path);
        $uri_aliases = array(
          $uri,
        );
        $query = strval(strstr($uri, '?'));
        foreach ($path_aliases as $path_alias) {
          $uri_aliases[] = $path_alias . $query;
        }
      }
    }
    if ($parsed->is_uri) {
      $check_uris = $parsed->is_blind ? array(
        $uri,
      ) : $uri_aliases;
      if ($parsed->is_regex) {
        foreach ($check_uris as $check_uri) {
          if (preg_match('/' . $parsed->pattern . '/i', $check_uri)) {
            $path_matched = TRUE;
            break 2;
          }
        }
      }
      else {
        foreach ($check_uris as $check_uri) {
          if ($parsed->pattern == $check_uri) {
            $path_matched = TRUE;
            break 2;
          }
        }
      }
    }
    else {
      $check_paths = $parsed->is_blind ? array(
        $path,
      ) : $path_aliases;
      if ($parsed->is_regex) {
        foreach ($check_paths as $check_path) {
          if (preg_match('/' . $parsed->pattern . '/i', $check_path)) {
            $path_matched = TRUE;
            break 2;
          }
        }
      }
      else {
        if ($parsed->pattern == '<admin>') {
          foreach ($check_paths as $check_path) {
            if (drupal_match_path($check_path, $admin_paths['admin']) && !drupal_match_path($check_path, $admin_paths['non_admin'])) {
              $path_matched = TRUE;
              break 2;
            }
          }
        }
        else {
          foreach ($check_paths as $check_path) {
            if (drupal_match_path($check_path, $parsed->pattern)) {
              $path_matched = TRUE;
              break 2;
            }
          }
        }
      }
    }
  }
  if (!$path_matched) {
    return TRUE;
  }

  // Check IP address.
  if (is_null($ip)) {
    $ip = ip_address();
  }
  $is_allowed = TRUE;
  foreach (explode("\n", $filter->rules) as $line) {
    $line = trim($line);
    if (!strlen($line)) {
      continue;
    }
    list($type, $pattern) = explode(':', $line, 2);
    if (access_filter_ip_match($pattern, $ip)) {
      $is_allowed = $type == ACCESS_FILTER_PROCESS_TYPE_ALLOW;
    }
  }
  return $is_allowed;
}