You are here

function domain_alias_lookup in Domain Access 6.2

Same name and namespace in other branches
  1. 7.3 domain_alias/domain_alias.module \domain_alias_lookup()
  2. 7.2 domain_alias/domain_alias.module \domain_alias_lookup()

Runs a lookup against the {domain_alias} table. One of the two values must be present. The database result is limited to one row.

Parameters

$subdomain: The domain to match the patterns in the {domain_alias} table against. Optional.

$alias_id: The alias_id taken from {domain_alias}. Optional

$reset: A boolean flag to clear the static variable if necessary.

Return value

An array containing the requested row from the {domain_alias} table. Returns -1 on failure.

2 calls to domain_alias_lookup()
domain_alias_domain_bootstrap_lookup in domain_alias/domain_alias.module
Implement hook_domain_bootstrap_lookup().
domain_alias_form_validate in domain_alias/domain_alias.admin.inc
FAPI for domain_alias_form()

File

domain_alias/domain_alias.module, line 156
Interface for advanced domain matching for Domain Access.

Code

function domain_alias_lookup($subdomain = NULL, $alias_id = NULL, $reset = FALSE) {
  static $aliases;

  // If both are NULL, no lookup can be run.
  if (is_null($subdomain) && is_null($alias_id)) {
    return -1;
  }

  // Create a unique key so we can static cache all requests.
  $key = $alias_id . '_' . $subdomain;

  // Run the lookup, if needed.
  if (!isset($aliases[$key]) || $reset) {
    if (is_string($subdomain)) {

      // First, look for an exact match.
      $alias = array();
      $alias = db_fetch_array(db_query("SELECT alias_id, domain_id, pattern, redirect FROM {domain_alias} WHERE pattern = '%s'", $subdomain));

      // If that fails, find the closest pattern(s).
      if (empty($alias)) {
        $result = db_query("SELECT alias_id, domain_id, pattern, redirect FROM {domain_alias} WHERE '%s' LIKE pattern", $subdomain);

        // If we returned more than one match, we have to find the most appropriate one.
        $patterns = array();
        while ($alias = db_fetch_array($result)) {
          $patterns[] = $alias;
        }
        if (empty($patterns)) {
          $alias = array();
        }
        else {
          if (count($patterns) == 1) {
            $alias = current($patterns);
          }
          else {
            $alias = domain_alias_best_match($subdomain, $patterns);
          }
        }
      }
    }
    else {
      if (intval($alias_id)) {
        $alias = db_fetch_array(db_query("SELECT alias_id, domain_id, pattern, redirect FROM {domain_alias} WHERE alias_id = %d", $alias_id));
      }
    }

    // If we found a match, prepare the readable version.
    if (isset($alias['alias_id'])) {
      $alias['pattern'] = _domain_alias_placeholders_from_sql($alias['pattern']);
      $aliases[$key] = $alias;
      $key2 = $alias['alias_id'] . '_';
      $aliases[$key2] =& $aliases[$key];
    }
    else {
      $aliases[$key] = -1;
    }
  }
  return $aliases[$key];
}