You are here

function gardens_site_data_get_site_from_server_info in Acquia Cloud Site Factory Connector 8.2

Same name and namespace in other branches
  1. 8 acsf_init/lib/sites/g/sites.inc \gardens_site_data_get_site_from_server_info()

Checks for a registered ACSF site based on Apache server variables.

Prerequisite: $_SERVER and $_ENV are both populated as per Acquia practices.

Return value

array|int|null 0 if no site was found for the given domain; NULL if a sites.json read failure was encountered; otherwise, an array of site data as constructed by gardens_site_data_build_data() - with an added key 'dir_key' containing the key where sites.php would expect to set this site's directory in its '$sites' variable.

See also

gardens_site_data_build_data()

1 call to gardens_site_data_get_site_from_server_info()
sites.php in acsf_init/lib/sites/sites.php
Drupal multi-site configuration file for sites on Acquia Cloud Site Factory.

File

acsf_init/lib/sites/g/sites.inc, line 116
ACSF helper functions for Drupal's multi-site directory aliasing feature.

Code

function gardens_site_data_get_site_from_server_info() {

  // First derive the 'site uri' (the base domain with possibly a sub path).
  if (PHP_SAPI === 'cli' && class_exists('\\Drush\\Drush') && Drush::hasContainer()) {
    $acsf_drush_boot_manager = Drush::bootstrapManager();
    if ($acsf_drush_boot_manager
      ->getUri()) {
      $acsf_uri = $acsf_drush_boot_manager
        ->getUri();
    }

    // Drush site-install gets confused about the uri when we specify the
    // --sites-subdir option. By specifying the --acsf-install-uri option with
    // the value of the standard domain, we can catch that here and correct the
    // uri argument for drush site installs.
    $acsf_drush_input = Drush::input();
    try {
      $acsf_install_uri = $acsf_drush_input
        ->getOption('acsf-install-uri');
      if ($acsf_install_uri) {
        $acsf_uri = $acsf_install_uri;
      }
    } catch (InvalidArgumentException $e) {
    }
    if (!preg_match('|https?://|', $acsf_uri)) {
      $acsf_uri = 'http://' . $acsf_uri;
    }
    $host = $_SERVER['HTTP_HOST'] = parse_url($acsf_uri, PHP_URL_HOST);
    $path = parse_url($acsf_uri, PHP_URL_PATH);
  }
  else {
    $host = rtrim($_SERVER['HTTP_HOST'], '.');
    $path = $_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['SCRIPT_FILENAME'];

    // Path based domains are implemented by symlinking a subdirectory back to
    // the docroot. To support these, we need to know which part of the script
    // path is the domain sub path, and which is a URL path inside the website.
    // (Note our only concern in 'supporting path based domains' is locating
    // the right site. How the request would derive the right URL inside that
    // site is not up to us, and the only thing that is actually known to work
    // with path based domains -through HTTP requests or Drush- is the standard
    // index.php.)
    if (substr($path, -10) === '/index.php') {

      // Assume the requested index.php is in the root, meaning that the full
      // script path leading up to it is the domain sub path. In other words:
      // we do not support index.php being anywhere else except the docroot.
      // This goes for every customer, not just those using path based domains.
      $path = substr($path, 0, strlen($path) - 10);
    }
    else {

      // For any non-index.php path, assume the sub path is empty. In other
      // words: simply do not support path based domains for these.
      $path = '';
    }
  }

  // Convert host/path to lower case because our registry stores data this way;
  // upper cased paths may still not be recognized (by Drush commands, unless
  // there is a symlink matching the specific case) though.
  $host = strtolower($host);

  // The path may have a trailing slash (because PHP_URL_PATH can contain one,
  // and because a script may have set SCRIPT_NAME to '//index.php'). Unify, so
  // the result is either empty or a path with a leading slash.
  $path = strtolower(rtrim($path, '/'));

  // Get data for the 'site uri' from APC.
  if (GARDENS_SITE_DATA_USE_APC && GARDENS_SITE_JSON_LEGACY) {

    // Check for data in APC: FALSE means no; 0 means "not found" cached in
    // APC; NULL means "sites.json read failure" cached in APC.
    $data = gardens_site_data_cache_get($host . $path);
    if ($data === FALSE) {

      // There's no guarantee about how many times this will be called, because
      // Core doesn't do any caching around the 'conf path'. For instance Drush
      // 7 would will call this file and logic around 19 times during each
      // command; Drush 8 only once and Drush 9.6 increases this to around 4. To
      // minimise gluster access, cache data to a local static cache. We assume
      // that *if* we have data for a given domain at any point during a request
      // on the command line then that data remains the same for the duration of
      // the command execution.
      // For some drush commands (that have bootstrap levels lower than
      // 'configuration', only when run with drush6/drush7), we don't have
      // drupal_static() available. We'll skip caching for those few commands,
      // so sites.json will be read always (which used to be the case for all
      // commands before we implemented this cache).
      if (function_exists('drupal_static')) {
        $static_cache =& drupal_static('acsf_sites_php_site_data');
      }
      else {
        $static_cache = [];
      }
      if (isset($static_cache[$host . $path])) {
        $data = $static_cache[$host . $path];
      }
      else {
        $data = gardens_site_data_refresh_one($host . $path);
        if ($data) {

          // We only cache truthy data ourselves, because we don't want to
          // assume that if a domain is not found (or there's a gluster error),
          // that will stay the same during command execution. Note we also set
          // the static cache if APC is active (which isn't necessary) because
          // it doesn't really matter and because otherwise, we would have to
          // - either replicate the check for APC which is now abstracted away
          //   into gardens_site_data_cache_get()
          // - or move $static_cache into gardens_site_data_cache_get() &
          //   gardens_site_data_cache_set(), which isn't wrong but we prefer
          //   not caching every domain in memory when the full file gets read.
          $static_cache[$host . $path] = $data;
        }
      }
    }
  }
  else {
    $data = gardens_site_data_refresh_one($host . $path);
  }
  if (is_array($data)) {

    // Generate the expected drupal sites.php key for this domain.
    $data['dir_key'] = str_replace('/', '.', $host . $path);
  }
  return $data;
}