diff --git a/core/includes/common.inc b/core/includes/common.inc index 1379b4e..f46c9a9 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -639,6 +639,9 @@ function _format_date_callback(array $matches = NULL, $new_langcode = NULL) { * alternative than url(). * * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromPath(). + * + * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0. + * System paths should not be used - use route names and parameters. */ function url($path = NULL, array $options = array()) { return \Drupal::urlGenerator()->generateFromPath($path, $options); diff --git a/core/lib/Drupal/Core/Routing/UrlBuilder.php b/core/lib/Drupal/Core/Routing/UrlBuilder.php new file mode 100644 index 0000000..0eff5bd --- /dev/null +++ b/core/lib/Drupal/Core/Routing/UrlBuilder.php @@ -0,0 +1,153 @@ +mixedModeSessions = $settings->get('mixed_mode_sessions', FALSE); + $allowed_protocols = $config->get('system.filter')->get('protocols') ?: array('http', 'https'); + UrlHelper::setAllowedProtocols($allowed_protocols); + $this->requestStack = $request_stack; + } + + /** + * {@inheritdoc} + */ + public function buildFromPath($path= NULL, array $options = array()) { + // Note that UrlHelper::isExternal will return FALSE if the $path has a + // disallowed protocol. This is later made safe since we always add at + // least a leading slash. + if (UrlHelper::isExternal($path)) { + return $this->buildExternalUrl($path, $options); + } + return $this->buildLocalUrl($path, $options); + } + + /** + * {@inheritdoc} + */ + public function buildExternalUrl($path = NULL, array $options = array()) { + $this->addOptionDefaults($options); + // Split off the fragment. + if (strpos($path, '#') !== FALSE) { + list($path, $old_fragment) = explode('#', $path, 2); + // If $options contains no fragment, take it over from the path. + if (isset($old_fragment) && !$options['fragment']) { + $options['fragment'] = '#' . $old_fragment; + } + } + // Append the query. + if ($options['query']) { + $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . UrlHelper::buildQuery($options['query']); + } + if (isset($options['https']) && $this->mixedModeSessions) { + if ($options['https'] === TRUE) { + $path = str_replace('http://', 'https://', $path); + } + elseif ($options['https'] === FALSE) { + $path = str_replace('https://', 'http://', $path); + } + } + // Reassemble. + return $path . $options['fragment']; + } + + /** + * {@inheritdoc} + */ + public function buildLocalUrl($path = NULL, array $options = array()) { + $this->addOptionDefaults($options); + $request = $this->requestStack->getCurrentRequest(); + $current_base_path = $request->getBasePath() . '/'; + $current_base_url = $request->getSchemeAndHttpHost() . $current_base_path; + $current_script_path = ''; + $base_path_with_script = $request->getBaseUrl(); + if (!empty($base_path_with_script)) { + $script_name = $request->getScriptName(); + if (strpos($base_path_with_script, $script_name) !== FALSE) { + $current_script_path = ltrim(substr($script_name, strlen($current_base_path)), '/') . '/'; + } + } + + if (!isset($options['script'])) { + $options['script'] = $current_script_path; + } + // The base_url might be rewritten from the language rewrite in domain mode. + if (!isset($options['base_url'])) { + if (isset($options['https']) && $this->mixedModeSessions) { + if ($options['https'] === TRUE) { + $options['base_url'] = str_replace('http://', 'https://', $current_base_url); + $options['absolute'] = TRUE; + } + elseif ($options['https'] === FALSE) { + $options['base_url'] = str_replace('https://', 'http://', $current_base_url); + $options['absolute'] = TRUE; + } + } + else { + $options['base_url'] = $current_base_url; + } + } + elseif (rtrim($options['base_url'], '/') == $options['base_url']) { + $options['base_url'] .= '/'; + } + $base = $options['absolute'] ? $options['base_url'] : $current_base_path; + $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix']; + + $path = str_replace('%2F', '/', rawurlencode($prefix . $path)); + $query = $options['query'] ? ('?' . UrlHelper::buildQuery($options['query'])) : ''; + return $base . $options['script'] . $path . $query . $options['fragment']; + } + + protected function addOptionDefaults(&$options) { + // Merge in defaults. + $options += array( + 'fragment' => '', + 'query' => array(), + 'absolute' => FALSE, + 'prefix' => '', + ); + + if (isset($options['fragment']) && $options['fragment'] !== '') { + $options['fragment'] = '#' . $options['fragment']; + } + } +} diff --git a/core/lib/Drupal/Core/Routing/UrlBuilderInterface.php b/core/lib/Drupal/Core/Routing/UrlBuilderInterface.php new file mode 100644 index 0000000..3c94f05 --- /dev/null +++ b/core/lib/Drupal/Core/Routing/UrlBuilderInterface.php @@ -0,0 +1,17 @@ +