You are here

function background_process_determine_default_service_host in Background Process 7

Same name and namespace in other branches
  1. 8 background_process.module \background_process_determine_default_service_host()
  2. 6 background_process.module \background_process_determine_default_service_host()

Determine host for current installation.

Host is determined in the following order: <server name> <localhost> <server ip>

Return value

array Array of service host parameters for the default service host. FALSE if none could be determined.

1 call to background_process_determine_default_service_host()
background_process_determine_and_save_default_service_host in ./background_process.module
Determines the default service host and stores it in the variable storage.

File

./background_process.module, line 945

Code

function background_process_determine_default_service_host() {
  $token = md5(session_id() . md5(uniqid(mt_rand(), TRUE)) . md5(uniqid(mt_rand(), TRUE)));
  variable_set('background_process_token', $token);
  global $conf;
  $auth = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'] . '@' : '';
  $scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
  global $base_url;
  $url = parse_url($base_url);
  $path = empty($url['path']) ? '' : $url['path'];
  $candidates = array(
    array(
      'base_url' => $base_url,
    ),
    array(
      'base_url' => $scheme . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $path,
      'http_host' => $_SERVER['HTTP_HOST'],
    ),
    array(
      'base_url' => $scheme . '127.0.0.1:' . $_SERVER['SERVER_PORT'] . $path,
      'http_host' => $_SERVER['HTTP_HOST'],
    ),
    array(
      'base_url' => $scheme . (!array_key_exists('SERVER_ADDR', $_SERVER) ? $_SERVER['LOCAL_ADDR'] : $_SERVER['SERVER_ADDR']) . ':' . $_SERVER['SERVER_PORT'] . $path,
      'http_host' => $_SERVER['HTTP_HOST'],
    ),
    array(
      'base_url' => $scheme . $auth . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $path,
      'http_host' => $_SERVER['HTTP_HOST'],
    ),
    array(
      'base_url' => $scheme . $auth . '127.0.0.1:' . $_SERVER['SERVER_PORT'] . $path,
      'http_host' => $_SERVER['HTTP_HOST'],
    ),
    array(
      'base_url' => $scheme . $auth . (!array_key_exists('SERVER_ADDR', $_SERVER) ? $_SERVER['LOCAL_ADDR'] : $_SERVER['SERVER_ADDR']) . ':' . $_SERVER['SERVER_PORT'] . $path,
      'http_host' => $_SERVER['HTTP_HOST'],
    ),
  );
  $found = NULL;
  foreach ($candidates as $i => $candidate) {
    $conf['background_process_service_hosts']['__test'] = $candidate;
    list($url, $headers) = background_process_build_request('background-process/check-token', '__test');
    if (empty($results[$url])) {
      $results[$url] = background_process_http_request($url, array(
        'headers' => $headers,
        'postpone' => TRUE,
        'candidate' => $i,
        'method' => 'POST',
      ));
    }
  }
  background_process_http_request_process($results);
  foreach ($results as $result) {
    if ($result->code == 200) {
      if ($token === substr($result->data, 0, strlen($token))) {
        $found = $candidates[$result->options['candidate']];
        break;
      }
    }
  }
  if ($found) {
    return $found;
  }
  return FALSE;
}