You are here

protected function LinkExtractorService::getAbsoluteContentPath in Link checker 8

Get the path of an URL.

Parameters

string $url: The http/https URL to parse.

Return value

string Full qualified URL with absolute path of the URL.

1 call to LinkExtractorService::getAbsoluteContentPath()
LinkExtractorService::getLinks in src/LinkExtractorService.php
Filters URL that do not need to check.

File

src/LinkExtractorService.php, line 434

Class

LinkExtractorService
Class LinkExtractor.

Namespace

Drupal\linkchecker

Code

protected function getAbsoluteContentPath($url) {

  // Parse the URL and make sure we can handle the schema.
  $uri = @parse_url($url);
  if ($uri == FALSE) {
    return NULL;
  }
  if (!isset($uri['scheme'])) {
    return NULL;
  }

  // Break if the schema is not supported.
  if (!in_array($uri['scheme'], [
    'http',
    'https',
  ])) {
    return NULL;
  }
  $scheme = isset($uri['scheme']) ? $uri['scheme'] . '://' : '';
  $user = isset($uri['user']) ? $uri['user'] . ($uri['pass'] ? ':' . $uri['pass'] : '') . '@' : '';
  $port = isset($uri['port']) ? $uri['port'] : 80;
  $host = $uri['host'] . ($port != 80 ? ':' . $port : '');
  $path = isset($uri['path']) ? $uri['path'] : '/';

  // Glue the URL variables.
  $absoluteUrl = $scheme . $user . $host . $path;

  // Find the last slash and remove all after the last slash to get the path.
  $lastSlash = strrpos($absoluteUrl, '/');
  $absoluteContentPath = mb_substr($absoluteUrl, 0, $lastSlash + 1);
  return $absoluteContentPath;
}