You are here

protected function Request::prepareBaseUrl in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-foundation/Request.php \Symfony\Component\HttpFoundation\Request::prepareBaseUrl()

Prepares the base URL.

Return value

string

1 call to Request::prepareBaseUrl()
Request::getBaseUrl in vendor/symfony/http-foundation/Request.php
Returns the root URL from which this request is executed.
1 method overrides Request::prepareBaseUrl()
ApacheRequest::prepareBaseUrl in vendor/symfony/http-foundation/ApacheRequest.php
Prepares the base URL.

File

vendor/symfony/http-foundation/Request.php, line 1722

Class

Request
Request represents an HTTP request.

Namespace

Symfony\Component\HttpFoundation

Code

protected function prepareBaseUrl() {
  $filename = basename($this->server
    ->get('SCRIPT_FILENAME'));
  if (basename($this->server
    ->get('SCRIPT_NAME')) === $filename) {
    $baseUrl = $this->server
      ->get('SCRIPT_NAME');
  }
  elseif (basename($this->server
    ->get('PHP_SELF')) === $filename) {
    $baseUrl = $this->server
      ->get('PHP_SELF');
  }
  elseif (basename($this->server
    ->get('ORIG_SCRIPT_NAME')) === $filename) {
    $baseUrl = $this->server
      ->get('ORIG_SCRIPT_NAME');

    // 1and1 shared hosting compatibility
  }
  else {

    // Backtrack up the script_filename to find the portion matching
    // php_self
    $path = $this->server
      ->get('PHP_SELF', '');
    $file = $this->server
      ->get('SCRIPT_FILENAME', '');
    $segs = explode('/', trim($file, '/'));
    $segs = array_reverse($segs);
    $index = 0;
    $last = count($segs);
    $baseUrl = '';
    do {
      $seg = $segs[$index];
      $baseUrl = '/' . $seg . $baseUrl;
      ++$index;
    } while ($last > $index && false !== ($pos = strpos($path, $baseUrl)) && 0 != $pos);
  }

  // Does the baseUrl have anything in common with the request_uri?
  $requestUri = $this
    ->getRequestUri();
  if ($baseUrl && false !== ($prefix = $this
    ->getUrlencodedPrefix($requestUri, $baseUrl))) {

    // full $baseUrl matches
    return $prefix;
  }
  if ($baseUrl && false !== ($prefix = $this
    ->getUrlencodedPrefix($requestUri, rtrim(dirname($baseUrl), '/' . DIRECTORY_SEPARATOR) . '/'))) {

    // directory portion of $baseUrl matches
    return rtrim($prefix, '/' . DIRECTORY_SEPARATOR);
  }
  $truncatedRequestUri = $requestUri;
  if (false !== ($pos = strpos($requestUri, '?'))) {
    $truncatedRequestUri = substr($requestUri, 0, $pos);
  }
  $basename = basename($baseUrl);
  if (empty($basename) || !strpos(rawurldecode($truncatedRequestUri), $basename)) {

    // no match whatsoever; set it blank
    return '';
  }

  // If using mod_rewrite or ISAPI_Rewrite strip the script filename
  // out of baseUrl. $pos !== 0 makes sure it is not matching a value
  // from PATH_INFO or QUERY_STRING
  if (strlen($requestUri) >= strlen($baseUrl) && false !== ($pos = strpos($requestUri, $baseUrl)) && $pos !== 0) {
    $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
  }
  return rtrim($baseUrl, '/' . DIRECTORY_SEPARATOR);
}