3 namespace Drupal\Core\Utility;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Component\Utility\UrlHelper;
7 use Drupal\Core\GeneratedUrl;
8 use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
9 use Symfony\Component\HttpFoundation\RequestStack;
12 * Provides a way to build external or non Drupal local domain URLs.
14 * It takes into account configured safe HTTP protocols.
16 class UnroutedUrlAssembler implements UnroutedUrlAssemblerInterface {
19 * A request stack object.
21 * @var \Symfony\Component\HttpFoundation\RequestStack
23 protected $requestStack;
26 * The outbound path processor.
28 * @var \Drupal\Core\PathProcessor\OutboundPathProcessorInterface
30 protected $pathProcessor;
33 * Constructs a new unroutedUrlAssembler object.
35 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
36 * A request stack object.
37 * @param \Drupal\Core\PathProcessor\OutboundPathProcessorInterface $path_processor
38 * The output path processor.
39 * @param string[] $filter_protocols
40 * (optional) An array of protocols allowed for URL generation.
42 public function __construct(RequestStack $request_stack, OutboundPathProcessorInterface $path_processor, array $filter_protocols = ['http', 'https']) {
43 UrlHelper::setAllowedProtocols($filter_protocols);
44 $this->requestStack = $request_stack;
45 $this->pathProcessor = $path_processor;
51 * This is a helper function that calls buildExternalUrl() or buildLocalUrl()
52 * based on a check of whether the path is a valid external URL.
54 public function assemble($uri, array $options = [], $collect_bubbleable_metadata = FALSE) {
55 // Note that UrlHelper::isExternal will return FALSE if the $uri has a
56 // disallowed protocol. This is later made safe since we always add at
57 // least a leading slash.
58 if (parse_url($uri, PHP_URL_SCHEME) === 'base') {
59 return $this->buildLocalUrl($uri, $options, $collect_bubbleable_metadata);
61 elseif (UrlHelper::isExternal($uri)) {
62 // UrlHelper::isExternal() only returns true for safe protocols.
63 return $this->buildExternalUrl($uri, $options, $collect_bubbleable_metadata);
65 throw new \InvalidArgumentException("The URI '$uri' is invalid. You must use a valid URI scheme. Use base: for a path, e.g., to a Drupal file that needs the base path. Do not use this for internal paths controlled by Drupal.");
71 protected function buildExternalUrl($uri, array $options = [], $collect_bubbleable_metadata = FALSE) {
72 $this->addOptionDefaults($options);
73 // Split off the query & fragment.
74 $parsed = UrlHelper::parse($uri);
75 $uri = $parsed['path'];
77 $parsed += ['query' => []];
78 $options += ['query' => []];
80 $options['query'] = NestedArray::mergeDeep($parsed['query'], $options['query']);
82 if ($parsed['fragment'] && !$options['fragment']) {
83 $options['fragment'] = '#' . $parsed['fragment'];
86 if (isset($options['https'])) {
87 if ($options['https'] === TRUE) {
88 $uri = str_replace('http://', 'https://', $uri);
90 elseif ($options['https'] === FALSE) {
91 $uri = str_replace('https://', 'http://', $uri);
95 if ($options['query']) {
96 $uri .= '?' . UrlHelper::buildQuery($options['query']);
99 $url = $uri . $options['fragment'];
100 return $collect_bubbleable_metadata ? (new GeneratedUrl())->setGeneratedUrl($url) : $url;
106 protected function buildLocalUrl($uri, array $options = [], $collect_bubbleable_metadata = FALSE) {
107 $generated_url = $collect_bubbleable_metadata ? new GeneratedUrl() : NULL;
109 $this->addOptionDefaults($options);
110 $request = $this->requestStack->getCurrentRequest();
112 // Remove the base: scheme.
113 // @todo Consider using a class constant for this in
114 // https://www.drupal.org/node/2417459
115 $uri = substr($uri, 5);
117 // Allow (outbound) path processing, if needed. A valid use case is the path
118 // alias overview form:
119 // @see \Drupal\path\Controller\PathController::adminOverview().
120 if (!empty($options['path_processing'])) {
121 // Do not pass the request, since this is a special case and we do not
122 // want to include e.g. the request language in the processing.
123 $uri = $this->pathProcessor->processOutbound($uri, $options, NULL, $generated_url);
125 // Strip leading slashes from internal paths to prevent them becoming
126 // external URLs without protocol. /example.com should not be turned into
128 $uri = ltrim($uri, '/');
130 // Add any subdirectory where Drupal is installed.
131 $current_base_path = $request->getBasePath() . '/';
133 if ($options['absolute']) {
134 $current_base_url = $request->getSchemeAndHttpHost() . $current_base_path;
135 if (isset($options['https'])) {
136 if (!empty($options['https'])) {
137 $base = str_replace('http://', 'https://', $current_base_url);
138 $options['absolute'] = TRUE;
141 $base = str_replace('https://', 'http://', $current_base_url);
142 $options['absolute'] = TRUE;
146 $base = $current_base_url;
148 if ($collect_bubbleable_metadata) {
149 $generated_url->addCacheContexts(['url.site']);
153 $base = $current_base_path;
156 $prefix = empty($uri) ? rtrim($options['prefix'], '/') : $options['prefix'];
158 $uri = str_replace('%2F', '/', rawurlencode($prefix . $uri));
159 $query = $options['query'] ? ('?' . UrlHelper::buildQuery($options['query'])) : '';
160 $url = $base . $options['script'] . $uri . $query . $options['fragment'];
161 return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url;
165 * Merges in default defaults
167 * @param array $options
168 * The options to merge in the defaults.
170 protected function addOptionDefaults(array &$options) {
171 $request = $this->requestStack->getCurrentRequest();
172 $current_base_path = $request->getBasePath() . '/';
173 $current_script_path = '';
174 $base_path_with_script = $request->getBaseUrl();
176 // If the current request was made with the script name (eg, index.php) in
177 // it, then extract it, making sure the leading / is gone, and a trailing /
178 // is added, to allow simple string concatenation with other parts.
179 if (!empty($base_path_with_script)) {
180 $script_name = $request->getScriptName();
181 if (strpos($base_path_with_script, $script_name) !== FALSE) {
182 $current_script_path = ltrim(substr($script_name, strlen($current_base_path)), '/') . '/';
186 // Merge in defaults.
192 'script' => $current_script_path,
195 if (isset($options['fragment']) && $options['fragment'] !== '') {
196 $options['fragment'] = '#' . $options['fragment'];