You are here

function background_process_determine_default_service_host in Background Process 8

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

Implements to Determine host for current installation.

1 call to background_process_determine_default_service_host()
background_process_determine_and_save_default_service_host in ./background_process.module
Implements to Determines the default service host.

File

./background_process.module, line 779
This module implements a framework for calling funtions in the background.

Code

function background_process_determine_default_service_host() {
  $token = md5(session_id() . md5(uniqid(mt_rand(), TRUE)) . md5(uniqid(mt_rand(), TRUE)));
  \Drupal::configFactory()
    ->getEditable('background_process.settings')
    ->set('background_process_token', $token)
    ->save();
  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 = [
    [
      'base_url' => $base_url,
    ],
    [
      'base_url' => $scheme . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $path,
      'http_host' => $_SERVER['HTTP_HOST'],
    ],
    [
      'base_url' => $scheme . '127.0.0.1:' . $_SERVER['SERVER_PORT'] . $path,
      'http_host' => $_SERVER['HTTP_HOST'],
    ],
    [
      'base_url' => $scheme . (!array_key_exists('SERVER_ADDR', $_SERVER) ? $_SERVER['LOCAL_ADDR'] : $_SERVER['SERVER_ADDR']) . ':' . $_SERVER['SERVER_PORT'] . $path,
      'http_host' => $_SERVER['HTTP_HOST'],
    ],
    [
      'base_url' => $scheme . $auth . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $path,
      'http_host' => $_SERVER['HTTP_HOST'],
    ],
    [
      'base_url' => $scheme . $auth . '127.0.0.1:' . $_SERVER['SERVER_PORT'] . $path,
      'http_host' => $_SERVER['HTTP_HOST'],
    ],
    [
      '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, [
        '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;
}