From 674dae32c7ab6b67693df62841293e12a710d0a5 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Sat, 31 Mar 2012 20:08:15 -0500 Subject: [PATCH 1/3] Register the request object in the installer so that the utility functions that now depend on it work. --- core/includes/install.core.inc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 07e25a0110b..019d78a8dee 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -3,6 +3,8 @@ use Drupal\Core\Database\Database; use Drupal\Core\Database\Install\TaskException; +use Symfony\Component\HttpFoundation\Request; + /** * @file * API functions for installing Drupal. @@ -261,6 +263,14 @@ function install_begin_request(&$install_state) { drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION); + // A request object from the HTTPFoundation to tell us about the request. + $request = Request::createFromGlobals(); + + // Set the global $request object. This is a temporary measure to + // keep legacy utility functions working. It should be moved to a dependency + // injection container at some point. + request($request); + // This must go after drupal_bootstrap(), which unsets globals! global $conf; From a764bd3fbb6263ddd0dc64b92077c913aa4ef698 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Sat, 31 Mar 2012 20:17:44 -0500 Subject: [PATCH 2/3] Revert "Convert more path utility functions to just wrap the request object." We're not ready for this yet. This reverts commit a2f83000b40f47976ead0cbe8219d68f58942ce3. --- core/includes/bootstrap.inc | 52 +++++++++++++++++++++++++++++++++---- core/includes/common.inc | 6 +---- core/includes/path.inc | 5 +--- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 3f6355a3b9d..7673d23ba31 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -551,6 +551,13 @@ function drupal_environment_initialize() { $_SERVER['HTTP_HOST'] = ''; } + // When clean URLs are enabled, emulate ?q=foo/bar using REQUEST_URI. It is + // not possible to append the query string using mod_rewrite without the B + // flag (this was added in Apache 2.2.8), because mod_rewrite unescapes the + // path before passing it on to PHP. This is a problem when the path contains + // e.g. "&" or "%" that have special meanings in URLs and must be encoded. + $_GET['q'] = request_path(); + // Enforce E_STRICT, but allow users to set levels not part of E_STRICT. error_reporting(E_STRICT | E_ALL | error_reporting()); @@ -1512,7 +1519,7 @@ function request_uri() { * @param Request $new_request * The new request object to store. If you are not index.php, you probably * should not be using this parameter. - * @return Symfony\Component\HttpFoundation\Request + * @return Request * The current request object. */ function request(Request $new_request = NULL) { @@ -2610,9 +2617,6 @@ function language_default() { /** * Returns the requested URL path of the page being viewed. * - * @todo Eliminate this function in favor of direct access to the request - * object. - * * Examples: * - http://example.com/node/306 returns "node/306". * - http://example.com/drupalfolder/node/306 returns "node/306" while @@ -2628,7 +2632,45 @@ function language_default() { * @see current_path() */ function request_path() { - return request()->getPathInfo(); + static $path; + + if (isset($path)) { + return $path; + } + + if (isset($_GET['q'])) { + // This is a request with a ?q=foo/bar query string. $_GET['q'] is + // overwritten in drupal_path_initialize(), but request_path() is called + // very early in the bootstrap process, so the original value is saved in + // $path and returned in later calls. + $path = $_GET['q']; + } + elseif (isset($_SERVER['REQUEST_URI'])) { + // This request is either a clean URL, or 'index.php', or nonsense. + // Extract the path from REQUEST_URI. + $request_path = strtok($_SERVER['REQUEST_URI'], '?'); + $base_path_len = strlen(rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/')); + // Unescape and strip $base_path prefix, leaving q without a leading slash. + $path = substr(urldecode($request_path), $base_path_len + 1); + // If the path equals the script filename, either because 'index.php' was + // explicitly provided in the URL, or because the server added it to + // $_SERVER['REQUEST_URI'] even when it wasn't provided in the URL (some + // versions of Microsoft IIS do this), the front page should be served. + if ($path == basename($_SERVER['PHP_SELF'])) { + $path = ''; + } + } + else { + // This is the front page. + $path = ''; + } + + // Under certain conditions Apache's RewriteRule directive prepends the value + // assigned to $_GET['q'] with a slash. Moreover we can always have a trailing + // slash in place, hence we need to normalize $_GET['q']. + $path = trim($path, '/'); + + return $path; } /** diff --git a/core/includes/common.inc b/core/includes/common.inc index a7dbee348b5..c15bd1c87a2 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -2735,10 +2735,6 @@ function drupal_get_path($type, $name) { /** * Returns the base URL path (i.e., directory) of the Drupal installation. * - * @todo Eliminate this function in favor of direct access to the request. Or, - * better yet, eliminate the need for this function outside of the - * URL Generator. - * * base_path() adds a "/" to the beginning and end of the returned path if the * path is not empty. At the very least, this will return "/". * @@ -2747,7 +2743,7 @@ function drupal_get_path($type, $name) { * - http://example.com/drupal/folder returns "/drupal/folder/". */ function base_path() { - return request()->getBasePath() . '/'; + return $GLOBALS['base_path']; } /** diff --git a/core/includes/path.inc b/core/includes/path.inc index 9bdd4a14911..2903930229f 100644 --- a/core/includes/path.inc +++ b/core/includes/path.inc @@ -334,10 +334,7 @@ function drupal_match_path($path, $patterns) { /** * Return the current URL path of the page being viewed. * - * @todo Eliminate this function in favor of direct access to the request - * object. - * - * Examples: + * Examples: * - http://example.com/node/306 returns "node/306". * - http://example.com/drupalfolder/node/306 returns "node/306" while * base_path() returns "/drupalfolder/". From b7ff1162795d16e675e1cd8e2aab6ad563f0efa4 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Sat, 31 Mar 2012 20:20:28 -0500 Subject: [PATCH 3/3] Revert the other use of the request object on utility functions. --- core/includes/path.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/includes/path.inc b/core/includes/path.inc index 2903930229f..223ab0473d8 100644 --- a/core/includes/path.inc +++ b/core/includes/path.inc @@ -352,7 +352,7 @@ function drupal_match_path($path, $patterns) { * @see request_path() */ function current_path() { - return request()->attributes->get('system_path'); + return $_GET['q']; } /**