Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Utility;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\UrlHelper;
|
Chris@0
|
6 use Drupal\Core\GeneratedUrl;
|
Chris@0
|
7 use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
|
Chris@0
|
8 use Symfony\Component\HttpFoundation\RequestStack;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Provides a way to build external or non Drupal local domain URLs.
|
Chris@0
|
12 *
|
Chris@0
|
13 * It takes into account configured safe HTTP protocols.
|
Chris@0
|
14 */
|
Chris@0
|
15 class UnroutedUrlAssembler implements UnroutedUrlAssemblerInterface {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * A request stack object.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var \Symfony\Component\HttpFoundation\RequestStack
|
Chris@0
|
21 */
|
Chris@0
|
22 protected $requestStack;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * The outbound path processor.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var \Drupal\Core\PathProcessor\OutboundPathProcessorInterface
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $pathProcessor;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Constructs a new unroutedUrlAssembler object.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
|
Chris@0
|
35 * A request stack object.
|
Chris@0
|
36 * @param \Drupal\Core\PathProcessor\OutboundPathProcessorInterface $path_processor
|
Chris@0
|
37 * The output path processor.
|
Chris@0
|
38 * @param string[] $filter_protocols
|
Chris@0
|
39 * (optional) An array of protocols allowed for URL generation.
|
Chris@0
|
40 */
|
Chris@0
|
41 public function __construct(RequestStack $request_stack, OutboundPathProcessorInterface $path_processor, array $filter_protocols = ['http', 'https']) {
|
Chris@0
|
42 UrlHelper::setAllowedProtocols($filter_protocols);
|
Chris@0
|
43 $this->requestStack = $request_stack;
|
Chris@0
|
44 $this->pathProcessor = $path_processor;
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * {@inheritdoc}
|
Chris@0
|
49 *
|
Chris@0
|
50 * This is a helper function that calls buildExternalUrl() or buildLocalUrl()
|
Chris@0
|
51 * based on a check of whether the path is a valid external URL.
|
Chris@0
|
52 */
|
Chris@0
|
53 public function assemble($uri, array $options = [], $collect_bubbleable_metadata = FALSE) {
|
Chris@0
|
54 // Note that UrlHelper::isExternal will return FALSE if the $uri has a
|
Chris@0
|
55 // disallowed protocol. This is later made safe since we always add at
|
Chris@0
|
56 // least a leading slash.
|
Chris@0
|
57 if (parse_url($uri, PHP_URL_SCHEME) === 'base') {
|
Chris@0
|
58 return $this->buildLocalUrl($uri, $options, $collect_bubbleable_metadata);
|
Chris@0
|
59 }
|
Chris@0
|
60 elseif (UrlHelper::isExternal($uri)) {
|
Chris@0
|
61 // UrlHelper::isExternal() only returns true for safe protocols.
|
Chris@0
|
62 return $this->buildExternalUrl($uri, $options, $collect_bubbleable_metadata);
|
Chris@0
|
63 }
|
Chris@0
|
64 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.");
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * {@inheritdoc}
|
Chris@0
|
69 */
|
Chris@0
|
70 protected function buildExternalUrl($uri, array $options = [], $collect_bubbleable_metadata = FALSE) {
|
Chris@0
|
71 $this->addOptionDefaults($options);
|
Chris@0
|
72 // Split off the fragment.
|
Chris@0
|
73 if (strpos($uri, '#') !== FALSE) {
|
Chris@0
|
74 list($uri, $old_fragment) = explode('#', $uri, 2);
|
Chris@0
|
75 // If $options contains no fragment, take it over from the path.
|
Chris@0
|
76 if (isset($old_fragment) && !$options['fragment']) {
|
Chris@0
|
77 $options['fragment'] = '#' . $old_fragment;
|
Chris@0
|
78 }
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 if (isset($options['https'])) {
|
Chris@0
|
82 if ($options['https'] === TRUE) {
|
Chris@0
|
83 $uri = str_replace('http://', 'https://', $uri);
|
Chris@0
|
84 }
|
Chris@0
|
85 elseif ($options['https'] === FALSE) {
|
Chris@0
|
86 $uri = str_replace('https://', 'http://', $uri);
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|
Chris@0
|
89 // Append the query.
|
Chris@0
|
90 if ($options['query']) {
|
Chris@0
|
91 $uri .= (strpos($uri, '?') !== FALSE ? '&' : '?') . UrlHelper::buildQuery($options['query']);
|
Chris@0
|
92 }
|
Chris@0
|
93 // Reassemble.
|
Chris@0
|
94 $url = $uri . $options['fragment'];
|
Chris@0
|
95 return $collect_bubbleable_metadata ? (new GeneratedUrl())->setGeneratedUrl($url) : $url;
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 /**
|
Chris@0
|
99 * {@inheritdoc}
|
Chris@0
|
100 */
|
Chris@0
|
101 protected function buildLocalUrl($uri, array $options = [], $collect_bubbleable_metadata = FALSE) {
|
Chris@0
|
102 $generated_url = $collect_bubbleable_metadata ? new GeneratedUrl() : NULL;
|
Chris@0
|
103
|
Chris@0
|
104 $this->addOptionDefaults($options);
|
Chris@0
|
105 $request = $this->requestStack->getCurrentRequest();
|
Chris@0
|
106
|
Chris@0
|
107 // Remove the base: scheme.
|
Chris@0
|
108 // @todo Consider using a class constant for this in
|
Chris@0
|
109 // https://www.drupal.org/node/2417459
|
Chris@0
|
110 $uri = substr($uri, 5);
|
Chris@0
|
111
|
Chris@0
|
112 // Allow (outbound) path processing, if needed. A valid use case is the path
|
Chris@0
|
113 // alias overview form:
|
Chris@0
|
114 // @see \Drupal\path\Controller\PathController::adminOverview().
|
Chris@0
|
115 if (!empty($options['path_processing'])) {
|
Chris@0
|
116 // Do not pass the request, since this is a special case and we do not
|
Chris@0
|
117 // want to include e.g. the request language in the processing.
|
Chris@0
|
118 $uri = $this->pathProcessor->processOutbound($uri, $options, NULL, $generated_url);
|
Chris@0
|
119 }
|
Chris@0
|
120 // Strip leading slashes from internal paths to prevent them becoming
|
Chris@0
|
121 // external URLs without protocol. /example.com should not be turned into
|
Chris@0
|
122 // //example.com.
|
Chris@0
|
123 $uri = ltrim($uri, '/');
|
Chris@0
|
124
|
Chris@0
|
125 // Add any subdirectory where Drupal is installed.
|
Chris@0
|
126 $current_base_path = $request->getBasePath() . '/';
|
Chris@0
|
127
|
Chris@0
|
128 if ($options['absolute']) {
|
Chris@0
|
129 $current_base_url = $request->getSchemeAndHttpHost() . $current_base_path;
|
Chris@0
|
130 if (isset($options['https'])) {
|
Chris@0
|
131 if (!empty($options['https'])) {
|
Chris@0
|
132 $base = str_replace('http://', 'https://', $current_base_url);
|
Chris@0
|
133 $options['absolute'] = TRUE;
|
Chris@0
|
134 }
|
Chris@0
|
135 else {
|
Chris@0
|
136 $base = str_replace('https://', 'http://', $current_base_url);
|
Chris@0
|
137 $options['absolute'] = TRUE;
|
Chris@0
|
138 }
|
Chris@0
|
139 }
|
Chris@0
|
140 else {
|
Chris@0
|
141 $base = $current_base_url;
|
Chris@0
|
142 }
|
Chris@0
|
143 if ($collect_bubbleable_metadata) {
|
Chris@0
|
144 $generated_url->addCacheContexts(['url.site']);
|
Chris@0
|
145 }
|
Chris@0
|
146 }
|
Chris@0
|
147 else {
|
Chris@0
|
148 $base = $current_base_path;
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 $prefix = empty($uri) ? rtrim($options['prefix'], '/') : $options['prefix'];
|
Chris@0
|
152
|
Chris@0
|
153 $uri = str_replace('%2F', '/', rawurlencode($prefix . $uri));
|
Chris@0
|
154 $query = $options['query'] ? ('?' . UrlHelper::buildQuery($options['query'])) : '';
|
Chris@0
|
155 $url = $base . $options['script'] . $uri . $query . $options['fragment'];
|
Chris@0
|
156 return $collect_bubbleable_metadata ? $generated_url->setGeneratedUrl($url) : $url;
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 /**
|
Chris@0
|
160 * Merges in default defaults
|
Chris@0
|
161 *
|
Chris@0
|
162 * @param array $options
|
Chris@0
|
163 * The options to merge in the defaults.
|
Chris@0
|
164 */
|
Chris@0
|
165 protected function addOptionDefaults(array &$options) {
|
Chris@0
|
166 $request = $this->requestStack->getCurrentRequest();
|
Chris@0
|
167 $current_base_path = $request->getBasePath() . '/';
|
Chris@0
|
168 $current_script_path = '';
|
Chris@0
|
169 $base_path_with_script = $request->getBaseUrl();
|
Chris@0
|
170
|
Chris@0
|
171 // If the current request was made with the script name (eg, index.php) in
|
Chris@0
|
172 // it, then extract it, making sure the leading / is gone, and a trailing /
|
Chris@0
|
173 // is added, to allow simple string concatenation with other parts.
|
Chris@0
|
174 if (!empty($base_path_with_script)) {
|
Chris@0
|
175 $script_name = $request->getScriptName();
|
Chris@0
|
176 if (strpos($base_path_with_script, $script_name) !== FALSE) {
|
Chris@0
|
177 $current_script_path = ltrim(substr($script_name, strlen($current_base_path)), '/') . '/';
|
Chris@0
|
178 }
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 // Merge in defaults.
|
Chris@0
|
182 $options += [
|
Chris@0
|
183 'fragment' => '',
|
Chris@0
|
184 'query' => [],
|
Chris@0
|
185 'absolute' => FALSE,
|
Chris@0
|
186 'prefix' => '',
|
Chris@0
|
187 'script' => $current_script_path,
|
Chris@0
|
188 ];
|
Chris@0
|
189
|
Chris@0
|
190 if (isset($options['fragment']) && $options['fragment'] !== '') {
|
Chris@0
|
191 $options['fragment'] = '#' . $options['fragment'];
|
Chris@0
|
192 }
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 }
|