You are here

function http_response_headers_get_rules in HTTP Response Headers 7

Provides applicable rules for current page.

Return value

mixed An array of matching header rules indexed with rule ID, NULL otherwise.

1 call to http_response_headers_get_rules()
http_response_headers_init in ./http_response_headers.module
Implements hook_init().

File

./http_response_headers.module, line 261
Contains HTTP response headers.

Code

function http_response_headers_get_rules() {
  global $user;

  // Convert the Drupal path to lowercase.
  $path = drupal_strtolower(drupal_get_path_alias());

  // Cache key for page.
  $cid = 'http_response_headers_page:' . _http_response_headers_get_page_cache_id($path);

  // Get rules from cache for this page, if exist.
  if ($rule_cached = cache_get($cid, HTTP_RESPONSE_HEADERS_CACHE_BIN)) {
    return $rule_cached->data;
  }
  $rules = http_response_headers_rule_load_all();
  foreach ($rules as $key => $rule) {

    // If a rule has no roles associated, it is displayed for every role.
    // For rules with roles associated, if none of the user's roles matches
    // the settings from this rule, remove it from the rule list.
    if ($rule
      ->hasRoles() && $rule
      ->rolesExist($user->roles)) {

      // No match.
      unset($rules[$key]);
      continue;
    }
    $enabled = TRUE;

    // Limited visibility rules must list at least one page.
    if ($rule
      ->getVisibility() == HTTP_RESPONSE_HEADERS_VISIBILITY_LISTED && $rule
      ->hasPages() === FALSE) {
      $enabled = FALSE;
    }
    if (!$enabled) {
      unset($rules[$key]);
      continue;
    }

    // Match path if necessary.
    if ($rule
      ->hasPages()) {

      // Convert path to lowercase. This allows comparison of the same path
      // with different case. Ex: /Page, /page, /PAGE.
      $pages = drupal_strtolower($rule
        ->getPages());
      $rule_visibility = $rule
        ->getVisibility();
      if ($rule_visibility <= HTTP_RESPONSE_HEADERS_VISIBILITY_LISTED) {

        // Compare the lowercase internal and lowercase path alias (if any).
        $page_match = drupal_match_path($path, $pages);
        $current_path = request_path();
        if ($path != $current_path) {
          $page_match = $page_match || drupal_match_path($current_path, $pages);
        }

        // When $rule->getVisibility() has a value of 0
        // (HTTP_RESPONSE_HEADERS_VISIBILITY_NOTLISTED),the header is set on
        // all pages except those listed in $rule->pages.
        // When set to 1 (HTTP_RESPONSE_HEADERS_VISIBILITY_LISTED), it is set
        // only on those pages listed in $rule->pages.
        $page_match = !($rule_visibility xor $page_match);
      }
      else {
        $page_match = FALSE;
      }
    }
    else {
      $page_match = TRUE;
    }
    if (!$page_match) {
      unset($rules[$key]);
    }

    // Match content types.
    $node = menu_get_object();
    $node_types = node_type_get_types();
    if (arg(0) == 'node' && arg(1) == 'add' && arg(2)) {
      $node_add_arg = strtr(arg(2), '-', '_');
    }

    // If a rule has no node types associated, it is displayed for every type.
    // For rules with node types associated, if the node type does not match
    // the settings from this rule, remove it from the list.
    if ($rule_node_types = $rule
      ->getTypeArray()) {
      if (!empty($node)) {

        // This is a node or node edit page.
        if (!in_array($node->type, $rule_node_types)) {

          // This rule should not be set for this node type.
          unset($rules[$key]);
          continue;
        }
      }
      elseif (isset($node_add_arg) && isset($node_types[$node_add_arg])) {

        // This is a node creation page.
        if (!in_array($node_add_arg, $rule_node_types)) {

          // This header should not be set for this node type.
          unset($rules[$key]);
          continue;
        }
      }
      else {

        // This is not a node page, remove the rule.
        unset($rules[$key]);
        continue;
      }
    }
  }

  // Set it to cache to use next time.
  cache_set($cid, $rules, HTTP_RESPONSE_HEADERS_CACHE_BIN);
  return $rules;
}