function _httprl_build_drupal_root in HTTP Parallel Request & Threading Library 6
Same name and namespace in other branches
- 7 httprl.module \_httprl_build_drupal_root()
Helper function to build an URL for asynchronous requests to self.
Parameters
int $level: How deep to go when setting the base path.
int $hostname_mode: Force a hostname type.
Return value
string URL pointing to this server.
1 call to _httprl_build_drupal_root()
- httprl_build_url_self in ./
httprl.module - Helper function to build an URL for asynchronous requests to self.
File
- ./
httprl.module, line 276 - HTTP Parallel Request Library module.
Code
function _httprl_build_drupal_root($level = 0, $hostname_mode = 0) {
static $webroot;
$root_path = '/';
if ($level > 0) {
// Work backwards from this file till we find drupal's index.php.
if (!isset($webroot)) {
$webroot = str_replace('\\', '/', dirname(__FILE__));
while (!empty($webroot)) {
if (file_exists($webroot . '/index.php') && strpos(file_get_contents($webroot . '/index.php'), 'menu_execute_active_handler();') !== FALSE) {
break;
}
$new_webroot = str_replace('\\', '/', dirname($webroot));
if ($new_webroot == $webroot) {
$webroot = str_replace('\\', '/', getcwd());
break;
}
$webroot = $new_webroot;
}
}
$root_path = '';
$webroot_array = explode('/', $webroot);
while ($level > 0 && count($webroot_array) != 0) {
$level--;
$root_path = array_pop($webroot_array) . '/' . $root_path;
}
$root_path = '/' . $root_path;
$root_path = str_replace('//', '/', $root_path);
}
else {
if (!empty($GLOBALS['base_path'])) {
$root_path = $GLOBALS['base_path'];
}
}
// Server auth.
$auth = '';
if (module_exists('shield')) {
$auth = variable_get('shield_user', '') . ':' . variable_get('shield_pass', '') . '@';
}
elseif (isset($_SERVER['AUTH_TYPE']) && $_SERVER['AUTH_TYPE'] == 'Basic' || isset($_SERVER['HTTP_AUTHORIZATION']) && preg_match('/Basic\\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION'])) {
$auth = $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'] . '@';
}
// Use static for gethostbyname lookup.
static $dns_cache;
$hostname = httprl_get_hostname();
// Get Host.
$ip = httprl_variable_get('httprl_server_addr', FALSE);
if ($ip == -1 || $hostname_mode == 1) {
$ip = $hostname;
// If the host is bad don't use it.
if (is_callable('drupal_is_cli') && drupal_is_cli() || !isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)) {
if (!isset($dns_cache[$hostname])) {
$dns_cache[$hostname] = gethostbyname($hostname);
}
if ($dns_cache[$hostname] == $hostname) {
$ip = '';
}
}
}
if (empty($ip) || $hostname_mode == 2) {
$ip = empty($_SERVER['SERVER_ADDR']) ? '127.0.0.1' : $_SERVER['SERVER_ADDR'];
// Check for IPv6. If IPv6 convert to IPv4 if possible.
if (strpos($ip, ':') !== FALSE) {
if ($_SERVER['SERVER_ADDR'] == '::1') {
$ip = "127.0.0.1";
}
elseif (preg_match('/^::\\d+.\\d+.\\d+.\\d+$/', $ip)) {
$ip = substr($ip, 2);
}
elseif (!empty($hostname)) {
// Last option is to use the IP from the host name.
if (!isset($dns_cache[$hostname])) {
$dns_cache[$hostname] = gethostbyname($hostname);
}
$ip = $dns_cache[$hostname];
if ($dns_cache[$hostname] == $hostname) {
$ip = '';
}
}
}
}
if ($hostname_mode == 3) {
$ip = httprl_variable_get('httprl_server_hostname', FALSE);
}
if (empty($ip) || $hostname_mode == 4) {
$ip = '127.0.0.1';
}
// Port.
$port = httprl_variable_get('httprl_server_port', HTTPRL_SERVER_PORT);
// Add the port separator if necessary.
if (!empty($port)) {
$port = ':' . $port;
}
elseif (isset($_SERVER['SERVER_PORT']) && is_numeric($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
$port = ':' . $_SERVER['SERVER_PORT'];
}
// URL schema http or https.
$schema_var = httprl_variable_get('httprl_server_schema', HTTPRL_SERVER_SCHEMA);
if ($schema_var == 0) {
$schema = httprl_get_server_schema() . '://';
}
elseif ($schema_var == 1) {
$schema = 'http://';
}
elseif ($schema_var == 2) {
$schema = 'https://';
}
// Special handling if clean urls are disabled.
if (!variable_get('clean_url', 0)) {
$path_parts = @parse_url('http://example.com/' . $path);
if (!empty($path_parts)) {
$path_parts_query = array();
if (isset($path_parts['query'])) {
parse_str($path_parts['query'], $path_parts_query);
}
$path_parts_query['q'] = ltrim($path_parts['path'], '/');
$path = '?' . http_build_query($path_parts_query, '', '&');
}
}
return $schema . $auth . $ip . $port . $root_path;
}