Finds the appropriate configuration directory for a given host and path.
Finds a matching configuration directory file by stripping the website's hostname from left to right and pathname from right to left. By default, the directory must contain a 'settings.php' file for it to match. If the parameter $require_settings is set to FALSE, then a directory without a 'settings.php' file will match as well. The first configuration file found will be used and the remaining ones will be ignored. If no configuration file is found, returns a default value '$confdir/default'. See default.settings.php for examples on how the URL is converted to a directory.
If a file named sites.php is present in the $confdir, it will be loaded prior to scanning for directories. That file can define aliases in an associative array named $sites. The array is written in the format '<port>.<domain>.<path>' => 'directory'. As an example, to create a directory alias for http://www.drupal.org:8080/mysite/test whose configuration file is in sites/example.com, the array should be defined as:
$sites = array(
'8080.www.drupal.org.mysite.test' => 'example.com',
);
$http_host: The hostname and optional port number, e.g. "www.example.com" or "www.example.com:8080".
$script_name: The part of the URL following the hostname, including the leading slash.
$require_settings: Defaults to TRUE. If TRUE, then only match directories with a 'settings.php' file. Otherwise match any directory.
The path of the matching configuration directory.
function find_conf_path($http_host, $script_name, $require_settings = TRUE) {
// Determine whether multi-site functionality is enabled.
if (!file_exists(DRUPAL_ROOT . '/sites/sites.php')) {
return 'sites/default';
}
$sites = array();
include DRUPAL_ROOT . '/sites/sites.php';
$uri = explode('/', $script_name);
$server = explode('.', implode('.', array_reverse(explode(':', rtrim($http_host, '.')))));
for ($i = count($uri) - 1; $i > 0; $i--) {
for ($j = count($server); $j > 0; $j--) {
$dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
if (isset($sites[$dir]) && file_exists(DRUPAL_ROOT . '/sites/' . $sites[$dir])) {
$dir = $sites[$dir];
}
if (file_exists(DRUPAL_ROOT . '/sites/' . $dir . '/settings.php') || !$require_settings && file_exists(DRUPAL_ROOT . '/sites/' . $dir)) {
return "sites/{$dir}";
}
}
}
return 'sites/default';
}