diff --git a/core/includes/file.inc b/core/includes/file.inc index a26f21f1ee7fc8d3b98780da91d62c99b567b265..c0cefa5a064451c5fa85ce86223a6df7c3be4beb 100644 --- a/core/includes/file.inc +++ b/core/includes/file.inc @@ -5,6 +5,7 @@ * API for handling file uploads and server file management. */ +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\StreamWrapper\LocalStream; use Drupal\Component\PhpStorage\FileStorage; use Drupal\Component\Utility\Settings; @@ -472,7 +473,7 @@ function file_create_url($uri) { else { // If this is not a properly formatted stream, then it is a shipped file. // Therefore, return the urlencoded URI with the base URL prepended. - return $GLOBALS['base_url'] . '/' . drupal_encode_path($uri); + return $GLOBALS['base_url'] . '/' . UrlHelper::encodePath($uri); } } elseif ($scheme == 'http' || $scheme == 'https') { diff --git a/core/includes/form.inc b/core/includes/form.inc index bd6d35205cdfb63d946bfbc25d71c06d7ffd38a5..1c6af1b92e79ff1d6455539367e9ef551fc70373 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -2624,7 +2624,7 @@ function form_validate_url(&$element, &$form_state) { $value = trim($element['#value']); form_set_value($element, $value, $form_state); - if ($value !== '' && !valid_url($value, TRUE)) { + if ($value !== '' && !UrlHelper::isValid($value, TRUE)) { form_error($element, $form_state, t('The URL %url is not valid.', array('%url' => $value))); } } diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 09c340aec8040e06a052f30dc6fb7be9aeee6285..d66bb304b12386dfb3010ea2995f643733d37f2f 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -1,5 +1,6 @@ getTarget()); - return $GLOBALS['base_url'] . '/' . self::getDirectoryPath() . '/' . drupal_encode_path($path); + return $GLOBALS['base_url'] . '/' . self::getDirectoryPath() . '/' . UrlHelper::encodePath($path); } /** diff --git a/core/modules/contextual/lib/Drupal/contextual/Plugin/views/field/ContextualLinks.php b/core/modules/contextual/lib/Drupal/contextual/Plugin/views/field/ContextualLinks.php index 21ff3242ba99a0cb70adad3b94cca263bd79258b..f2d45a2df99c44c7ab50901f72e3a5e556d9c786 100644 --- a/core/modules/contextual/lib/Drupal/contextual/Plugin/views/field/ContextualLinks.php +++ b/core/modules/contextual/lib/Drupal/contextual/Plugin/views/field/ContextualLinks.php @@ -7,6 +7,7 @@ namespace Drupal\contextual\Plugin\views\field; +use Drupal\Component\Utility\UrlHelper; use Drupal\views\Plugin\views\field\FieldPluginBase; use Drupal\views\ResultRow; use Drupal\Component\Utility\Json; @@ -115,7 +116,7 @@ public function render(ResultRow $values) { '', array(), array( - 'contextual-views-field-links' => drupal_encode_path(Json::encode($links)), + 'contextual-views-field-links' => UrlHelper::encodePath(Json::encode($links)), ) ) ); diff --git a/core/modules/locale/locale.module b/core/modules/locale/locale.module index 881ca9a65f7c1af985f9b776ac5e1f083fe0f96a..f9e17ebf44988c835f4b5fa1560904908c6c0517 100644 --- a/core/modules/locale/locale.module +++ b/core/modules/locale/locale.module @@ -11,6 +11,7 @@ */ use Drupal\Component\Utility\Json; +use Drupal\Component\Utility\UrlHelper; use Drupal\Component\Utility\Xss; use Drupal\Core\Cache\Cache; use Drupal\Core\Language\Language; @@ -1121,7 +1122,7 @@ function _locale_strip_quotes($string) { function _locale_parse_js_file($filepath) { // The file path might contain a query string, so make sure we only use the // actual file. - $parsed_url = drupal_parse_url($filepath); + $parsed_url = UrlHelper::parse($filepath); $filepath = $parsed_url['path']; // If there is still a protocol component in the path, reject that. diff --git a/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php b/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php index 7a2af0e30246c3d35222843442dcbef4c0736742..b500b484fa57526c2f459d9188c13d7ae246e685 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/Entity/MenuLink.php @@ -7,6 +7,7 @@ namespace Drupal\menu_link\Entity; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Cache\Cache; use Drupal\Core\Entity\Entity; use Drupal\Core\Entity\EntityStorageInterface; @@ -476,7 +477,7 @@ public function preSave(EntityStorageInterface $storage) { // This is the easiest way to handle the unique internal path '', // since a path marked as external does not need to match a route. - $this->external = (url_is_external($this->link_path) || $this->link_path == '') ? 1 : 0; + $this->external = (UrlHelper::isExternal($this->link_path) || $this->link_path == '') ? 1 : 0; // Try to find a parent link. If found, assign it and derive its menu. $parent = $this->findParent($storage); diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkFormController.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkFormController.php index 0c4ac19b96a984b4b36e5d2886bdffca9cf623fb..5c50cbee07cfc4e577439c3926ffe85e36ad6e3f 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkFormController.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkFormController.php @@ -7,6 +7,7 @@ namespace Drupal\menu_link; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Entity\EntityFormController; use Drupal\Core\Language\Language; use Drupal\Core\Path\AliasManagerInterface; @@ -212,7 +213,7 @@ public function validate(array $form, array &$form_state) { $menu_link->link_path = $normal_path; $form_state['values']['link_path'] = $normal_path; } - if (!url_is_external($menu_link->link_path)) { + if (!UrlHelper::isExternal($menu_link->link_path)) { $parsed_link = parse_url($menu_link->link_path); if (isset($parsed_link['query'])) { $menu_link->options['query'] = array(); diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index b6e055b095a71f222c931f63a650bf0b505313dc..870bd8af390536913f5745e7dbff183403c8583c 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -5,6 +5,7 @@ * Allows users to manage customizable lists of shortcut links. */ +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Routing\UrlMatcher; use Drupal\Core\Url; use Drupal\shortcut\ShortcutSetInterface; @@ -278,7 +279,7 @@ function shortcut_valid_link($path) { } // An empty path is valid too and will be converted to . - return (!url_is_external($path) && (\Drupal::service('router.route_provider')->getRoutesByPattern('/' . $path)->count() > 0)) || empty($path) || $path == ''; + return (!UrlHelper::isExternal($path) && (\Drupal::service('router.route_provider')->getRoutesByPattern('/' . $path)->count() > 0)) || empty($path) || $path == ''; } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/UrlTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/UrlTest.php index 3487fd4b700744cd23c3ee3b0ec5c9c42eeee6db..f3db2461cfebf46007ef5666b072a3bd3aea5653 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Common/UrlTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Common/UrlTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Common; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Language\Language; use Drupal\simpletest\WebTestBase; use Symfony\Component\HttpFoundation\Request; @@ -25,7 +26,7 @@ class UrlTest extends WebTestBase { public static function getInfo() { return array( 'name' => 'URL generation tests', - 'description' => 'Confirm that url(), drupal_get_query_parameters(), drupal_http_build_query(), and l() work correctly with various input.', + 'description' => 'Confirm that url(), drupal_get_query_parameters(), \Drupal\Component\Utility\UrlHelper::buildQuery(), and l() work correctly with various input.', 'group' => 'Common', ); } @@ -219,7 +220,7 @@ function testDrupalGetQueryParameters() { } /** - * Tests drupal_parse_url(). + * Tests UrlHelper::parse(). */ function testDrupalParseUrl() { // Relative, absolute, and external URLs, without/with explicit script path, @@ -233,7 +234,7 @@ function testDrupalParseUrl() { 'query' => array('foo' => 'bar', 'bar' => 'baz', 'baz' => ''), 'fragment' => 'foo', ); - $this->assertEqual(drupal_parse_url($url), $expected, 'URL parsed correctly.'); + $this->assertEqual(UrlHelper::parse($url), $expected, 'URL parsed correctly.'); } } } @@ -245,15 +246,15 @@ function testDrupalParseUrl() { 'query' => array(), 'fragment' => '', ); - $this->assertEqual(drupal_parse_url($url), $result, 'Relative URL parsed correctly.'); + $this->assertEqual(UrlHelper::parse($url), $result, 'Relative URL parsed correctly.'); // Test that drupal can recognize an absolute URL. Used to prevent attack vectors. $url = 'http://drupal.org/foo/bar?foo=bar&bar=baz&baz#foo'; - $this->assertTrue(url_is_external($url), 'Correctly identified an external URL.'); + $this->assertTrue(UrlHelper::isExternal($url), 'Correctly identified an external URL.'); - // Test that drupal_parse_url() does not allow spoofing a URL to force a malicious redirect. - $parts = drupal_parse_url('forged:http://cwe.mitre.org/data/definitions/601.html'); - $this->assertFalse(valid_url($parts['path'], TRUE), 'drupal_parse_url() correctly parsed a forged URL.'); + // Test that UrlHelper::parse() does not allow spoofing a URL to force a malicious redirect. + $parts = UrlHelper::parse('forged:http://cwe.mitre.org/data/definitions/601.html'); + $this->assertFalse(UrlHelper::isValid($parts['path'], TRUE), '\Drupal\Component\Utility\UrlHelper::isValid() correctly parsed a forged URL.'); } /** diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php index b3c85c1bdea35b4cc800264f292fb372f8762a47..482a032f7e9439df4ff0e9a27127d7a70c07ee8e 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\Html; use Drupal\Component\Utility\String; +use Drupal\Component\Utility\UrlHelper; use Drupal\Component\Utility\Xss; use Drupal\views\Plugin\views\HandlerBase; use Drupal\views\Plugin\views\display\DisplayPluginBase; @@ -1424,7 +1425,7 @@ protected function renderAsLink($alter, $text, $tokens) { if (isset($alter['query'])) { // Convert the query to a string, perform token replacement, and then // convert back to an array form for l(). - $options['query'] = drupal_http_build_query($alter['query']); + $options['query'] = UrlHelper::buildQuery($alter['query']); $options['query'] = strtr($options['query'], $tokens); $query = array(); parse_str($options['query'], $query); diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php index dc9e3e89b64d57aacb6e9b431908aca552c458e6..d4b178136da11b4ac7fb3856d6858d87b42f6133 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php @@ -7,6 +7,7 @@ namespace Drupal\views\Tests\Handler; +use Drupal\Component\Utility\UrlHelper; use Drupal\views\Views; /** @@ -284,11 +285,11 @@ public function testAlterUrl() { $id_field->options['alter']['path_case'] = 'ucfirst'; $id_field->options['alter']['path'] = 'drupal has a great community'; $output = $id_field->theme($row); - $this->assertSubString($output, drupal_encode_path('Drupal has a great community')); + $this->assertSubString($output, UrlHelper::encodePath('Drupal has a great community')); $id_field->options['alter']['path_case'] = 'ucwords'; $output = $id_field->theme($row); - $this->assertSubString($output, drupal_encode_path('Drupal Has A Great Community')); + $this->assertSubString($output, UrlHelper::encodePath('Drupal Has A Great Community')); unset($id_field->options['alter']['path_case']); // Tests the linkclass setting and see whether it actuall exists in the output.