Commit 02750689 authored by Dries Buytaert's avatar Dries Buytaert
Browse files

- Patch #1183208 by effulgentsia, Niklas Fiekas, katbailey, no_commit_credit,...

- Patch #1183208 by effulgentsia, Niklas Fiekas, katbailey, no_commit_credit, catch: remove variable_get()('clean_url()') and switch to index.php/path pattern for dirty URL support.
parent 34ef955a
Loading
Loading
Loading
Loading
+0 −11
Changes for core/authorize.php: 0 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -103,17 +103,6 @@ function authorize_access_allowed() {
  // Load the code that drives the authorize process.
  require_once DRUPAL_ROOT . '/core/includes/authorize.inc';

  // For the sake of Batch API and a few other low-level functions, we need to
  // initialize the URL path into $_GET['q']. However, we do not want to raise
  // our bootstrap level, nor do we want to call drupal_initialize_path(),
  // since that is assuming that modules are loaded and invoking hooks.
  // However, all we really care is if we're in the middle of a batch, in which
  // case $_GET['q'] will already be set, we just initialize it to an empty
  // string if it's not already defined.
  if (!isset($_GET['q'])) {
    $_GET['q'] = '';
  }

  if (isset($_SESSION['authorize_operation']['page_title'])) {
    drupal_set_title($_SESSION['authorize_operation']['page_title']);
  }
+69 −40
Changes for core/includes/bootstrap.inc: 69 added lines, 40 removed lines.
Original line number Diff line number Diff line
@@ -551,12 +551,8 @@ 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();
  // @todo Refactor with the Symfony Request object.
  _current_path(request_path());

  // Enforce E_STRICT, but allow users to set levels not part of E_STRICT.
  error_reporting(E_STRICT | E_ALL | error_reporting());
@@ -597,7 +593,7 @@ function drupal_valid_http_host($host) {
 * Sets the base URL, cookie domain, and session name from configuration.
 */
function drupal_settings_initialize() {
  global $base_url, $base_path, $base_root;
  global $base_url, $base_path, $base_root, $script_path;

  // Export the following settings.php variables to the global namespace
  global $databases, $cookie_domain, $conf, $installed_profile, $update_free_access, $db_url, $db_prefix, $drupal_hash_salt, $is_https, $base_secure_url, $base_insecure_url, $config_directory_name, $config_signature_key;
@@ -626,8 +622,8 @@ function drupal_settings_initialize() {

    $base_url = $base_root;

    // $_SERVER['SCRIPT_NAME'] can, in contrast to $_SERVER['PHP_SELF'], not
    // be modified by a visitor.
    // For a request URI of '/index.php/foo', $_SERVER['SCRIPT_NAME'] is
    // '/index.php', whereas $_SERVER['PHP_SELF'] is '/index.php/foo'.
    if ($dir = rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/')) {
      // Remove "core" directory if present, allowing install.php, update.php,
      // cron.php and others to auto-detect a base path.
@@ -648,6 +644,32 @@ function drupal_settings_initialize() {
  $base_secure_url = str_replace('http://', 'https://', $base_url);
  $base_insecure_url = str_replace('https://', 'http://', $base_url);

  // Determine the path of the script relative to the base path, and add a
  // trailing slash. This is needed for creating URLs to Drupal pages.
  if (!isset($script_path)) {
    $script_path = '';
    // We don't expect scripts outside of the base path, but sanity check
    // anyway.
    if (strpos($_SERVER['SCRIPT_NAME'], $base_path) === 0) {
      $script_path = substr($_SERVER['SCRIPT_NAME'], strlen($base_path)) . '/';
      // If the request URI does not contain the script name, then clean URLs
      // are in effect and the script path can be similarly dropped from URL
      // generation. For servers that don't provide $_SERVER['REQUEST_URI'], we
      // do not know the actual URI requested by the client, and request_uri()
      // returns a URI with the script name, resulting in non-clean URLs unless
      // there's other code that intervenes.
      if (strpos(request_uri(TRUE) . '/', $base_path . $script_path) !== 0) {
        $script_path = '';
      }
      // @todo Temporary BC for install.php, update.php, and other scripts.
      //   - http://drupal.org/node/1547184
      //   - http://drupal.org/node/1546082
      if ($script_path !== 'index.php/') {
        $script_path = '';
      }
    }
  }

  if ($cookie_domain) {
    // If the user specifies the cookie domain, also use it for session name.
    $session_name = $cookie_domain;
@@ -1488,13 +1510,15 @@ function drupal_validate_utf8($text) {
 *
 * Because $_SERVER['REQUEST_URI'] is only available on Apache, we generate an
 * equivalent using other environment variables.
 *
 * @todo The above comment is incorrect: http://drupal.org/node/1547294.
 */
function request_uri() {
function request_uri($omit_query_string = FALSE) {
  if (isset($_SERVER['REQUEST_URI'])) {
    $uri = $_SERVER['REQUEST_URI'];
  }
  else {
    if (isset($_SERVER['argv'])) {
    if (isset($_SERVER['argv'][0])) {
      $uri = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['argv'][0];
    }
    elseif (isset($_SERVER['QUERY_STRING'])) {
@@ -1507,7 +1531,7 @@ function request_uri() {
  // Prevent multiple slashes to avoid cross site requests via the Form API.
  $uri = '/' . ltrim($uri, '/');

  return $uri;
  return $omit_query_string ? strtok($uri, '?') : $uri;
}

/**
@@ -2181,7 +2205,7 @@ function _drupal_bootstrap_page_cache() {
    if (is_object($cache)) {
      header('X-Drupal-Cache: HIT');
      // Restore the metadata cached with the page.
      $_GET['q'] = $cache->data['path'];
      _current_path($cache->data['path']);
      drupal_set_title($cache->data['title'], PASS_THROUGH);
      date_default_timezone_set(drupal_get_user_timezone());
      // If the skipping of the bootstrap hooks is not enforced, call
@@ -2418,9 +2442,9 @@ function drupal_maintenance_theme() {
 */
function drupal_fast_404() {
  $exclude_paths = variable_get('404_fast_paths_exclude', FALSE);
  if ($exclude_paths && !preg_match($exclude_paths, $_GET['q'])) {
  if ($exclude_paths && !preg_match($exclude_paths, request_path())) {
    $fast_paths = variable_get('404_fast_paths', FALSE);
    if ($fast_paths && preg_match($fast_paths, $_GET['q'])) {
    if ($fast_paths && preg_match($fast_paths, request_path())) {
      drupal_add_http_header('Status', '404 Not Found');
      $fast_404_html = variable_get('404_fast_html', '<html xmlns="http://www.w3.org/1999/xhtml"><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL "@path" was not found on this server.</p></body></html>');
      // Replace @path in the variable with the page path.
@@ -2655,41 +2679,44 @@ function request_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'], '?');
  // Get the part of the URI between the base path of the Drupal installation
  // and the query string, and unescape it.
  $request_path = request_uri(TRUE);
  $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'])) {

  // Depending on server configuration, the URI might or might not include the
  // script name. For example, the front page might be accessed as
  // http://example.com or as http://example.com/index.php, and the "user"
  // page might be accessed as http://example.com/user or as
  // http://example.com/index.php/user. Strip the script name from $path.
  $script = basename($_SERVER['SCRIPT_NAME']);
  if ($path == $script) {
    $path = '';
  }
  }
  else {
    // This is the front page.
    $path = '';
  elseif (strpos($path, $script . '/') === 0) {
    $path = substr($path, strlen($script) + 1);
  }

  // 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'].
  // Extra slashes can appear in URLs or under some conditions, added by Apache,
  // so normalize.
  $path = trim($path, '/');

  return $path;
}

/**
 * @todo This is a temporary function pending refactoring Drupal to use
 *   Symfony's Request object exclusively.
 */
function _current_path($path = NULL) {
  static $current_path = '';
  if (isset($path)) {
    $current_path = $path;
  }
  return $current_path;
}

/**
 * Returns a component of the current Drupal path.
 *
@@ -2726,7 +2753,9 @@ function arg($index = NULL, $path = NULL) {
  $arguments = &$drupal_static_fast['arguments'];

  if (!isset($path)) {
    $path = $_GET['q'];
    // @todo The public function current_path() is not available during early
    //   bootstrap.
    $path = _current_path();
  }
  if (!isset($arguments[$path])) {
    $arguments[$path] = explode('/', $path);
+47 −69
Changes for core/includes/common.inc: 47 added lines, 69 removed lines.
Original line number Diff line number Diff line
@@ -397,14 +397,14 @@ function drupal_get_feeds($delimiter = "\n") {
 *   (optional) An array to be processed. Defaults to $_GET.
 * @param $exclude
 *   (optional) A list of $query array keys to remove. Use "parent[child]" to
 *   exclude nested items. Defaults to array('q').
 *   exclude nested items.
 * @param $parent
 *   Internal use only. Used to build the $query array key for nested items.
 *
 * @return
 *   An array containing query parameters, which can be used for url().
 */
function drupal_get_query_parameters(array $query = NULL, array $exclude = array('q'), $parent = '') {
function drupal_get_query_parameters(array $query = NULL, array $exclude = array(), $parent = '') {
  // Set defaults, if none given.
  if (!isset($query)) {
    $query = $_GET;
@@ -517,7 +517,7 @@ function drupal_get_destination() {
    $destination = array('destination' => $_GET['destination']);
  }
  else {
    $path = $_GET['q'];
    $path = current_path();
    $query = drupal_http_build_query(drupal_get_query_parameters());
    if ($query != '') {
      $path .= '?' . $query;
@@ -598,13 +598,6 @@ function drupal_parse_url($url) {
      $options['fragment'] = $parts['fragment'];
    }
  }
  // The 'q' parameter contains the path of the current page if clean URLs are
  // disabled. It overrides the 'path' of the URL when present, even if clean
  // URLs are enabled, due to how Apache rewriting rules work.
  if (isset($options['query']['q'])) {
    $options['path'] = $options['query']['q'];
    unset($options['query']['q']);
  }

  return $options;
}
@@ -2138,14 +2131,9 @@ function _format_date_callback(array $matches = NULL, $new_langcode = NULL) {
 *     dependent URL requires so.
 *   - 'prefix': Only used internally, to modify the path when a language
 *     dependent URL requires so.
 *   - 'script': The script filename in Drupal's root directory to use when
 *     clean URLs are disabled, such as 'index.php'. Defaults to an empty
 *     string, as most modern web servers automatically find 'index.php'. If
 *     clean URLs are disabled, the value of $path is appended as query
 *     parameter 'q' to $options['script'] in the returned URL. When deploying
 *     Drupal on a web server that cannot be configured to automatically find
 *     index.php, then hook_url_outbound_alter() can be implemented to force
 *     this value to 'index.php'.
 *   - 'script': Added to the URL between the base path and the path prefix.
 *     Defaults to empty string when clean URLs are in effect, and to
 *     'index.php/' when they are not.
 *   - 'entity_type': The entity type of the object that called url(). Only
 *     set if url() is invoked by entity_uri().
 *   - 'entity': The entity object (such as a node) for which the URL is being
@@ -2161,7 +2149,8 @@ function url($path = NULL, array $options = array()) {
    'query' => array(),
    'absolute' => FALSE,
    'alias' => FALSE,
    'prefix' => ''
    'prefix' => '',
    'script' => $GLOBALS['script_path'],
  );

  if (!isset($options['external'])) {
@@ -2243,32 +2232,9 @@ function url($path = NULL, array $options = array()) {
  $base = $options['absolute'] ? $options['base_url'] . '/' : base_path();
  $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix'];

  // With Clean URLs.
  if (!empty($GLOBALS['conf']['clean_url'])) {
  $path = drupal_encode_path($prefix . $path);
    if ($options['query']) {
      return $base . $path . '?' . drupal_http_build_query($options['query']) . $options['fragment'];
    }
    else {
      return $base . $path . $options['fragment'];
    }
  }
  // Without Clean URLs.
  else {
    $path = $prefix . $path;
    $query = array();
    if (!empty($path)) {
      $query['q'] = $path;
    }
    if ($options['query']) {
      // We do not use array_merge() here to prevent overriding $path via query
      // parameters.
      $query += $options['query'];
    }
    $query = $query ? ('?' . drupal_http_build_query($query)) : '';
    $script = isset($options['script']) ? $options['script'] : '';
    return $base . $script . $query . $options['fragment'];
  }
  $query = $options['query'] ? ('?' . drupal_http_build_query($options['query'])) : '';
  return $base . $options['script'] . $path . $query . $options['fragment'];
}

/**
@@ -2404,7 +2370,7 @@ function l($text, $path, array $options = array()) {
  );

  // Append active class.
  if (($path == $_GET['q'] || ($path == '<front>' && drupal_is_front_page())) &&
  if (($path == current_path() || ($path == '<front>' && drupal_is_front_page())) &&
      (empty($options['language']) || $options['language']->langcode == drupal_container()->get(LANGUAGE_TYPE_URL)->langcode)) {
    $options['attributes']['class'][] = 'active';
  }
@@ -2530,7 +2496,7 @@ function drupal_deliver_page($page_callback_result, $default_delivery_callback =
    // If a delivery callback is specified, but doesn't exist as a function,
    // something is wrong, but don't print anything, since it's not known
    // what format the response needs to be in.
    watchdog('delivery callback not found', 'callback %callback not found: %q.', array('%callback' => $delivery_callback, '%q' => $_GET['q']), WATCHDOG_ERROR);
    watchdog('delivery callback not found', 'callback %callback not found: %path.', array('%callback' => $delivery_callback, '%path' => current_path()), WATCHDOG_ERROR);
  }
}

@@ -2571,18 +2537,18 @@ function drupal_deliver_html_page($page_callback_result) {
        // Print a 404 page.
        drupal_add_http_header('Status', '404 Not Found');

        watchdog('page not found', check_plain($_GET['q']), NULL, WATCHDOG_WARNING);
        watchdog('page not found', check_plain(current_path()), NULL, WATCHDOG_WARNING);

        // Check for and return a fast 404 page if configured.
        drupal_fast_404();

        // Keep old path for reference, and to allow forms to redirect to it.
        if (!isset($_GET['destination'])) {
          $_GET['destination'] = $_GET['q'];
          $_GET['destination'] = current_path();
        }

        $path = drupal_get_normal_path(variable_get('site_404', ''));
        if ($path && $path != $_GET['q']) {
        if ($path && $path != current_path()) {
          // Custom 404 handler. Set the active item in case there are tabs to
          // display, or other dependencies on the path.
          menu_set_active_item($path);
@@ -2603,15 +2569,15 @@ function drupal_deliver_html_page($page_callback_result) {
      case MENU_ACCESS_DENIED:
        // Print a 403 page.
        drupal_add_http_header('Status', '403 Forbidden');
        watchdog('access denied', check_plain($_GET['q']), NULL, WATCHDOG_WARNING);
        watchdog('access denied', check_plain(current_path()), NULL, WATCHDOG_WARNING);

        // Keep old path for reference, and to allow forms to redirect to it.
        if (!isset($_GET['destination'])) {
          $_GET['destination'] = $_GET['q'];
          $_GET['destination'] = current_path();
        }

        $path = drupal_get_normal_path(variable_get('site_403', ''));
        if ($path && $path != $_GET['q']) {
        if ($path && $path != current_path()) {
          // Custom 403 handler. Set the active item in case there are tabs to
          // display or other dependencies on the path.
          menu_set_active_item($path);
@@ -3570,11 +3536,15 @@ function drupal_build_css_cache($css) {
    if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) {
      return FALSE;
    }
    // If CSS gzip compression is enabled, clean URLs are enabled (which means
    // that rewrite rules are working) and the zlib extension is available then
    // create a gzipped version of this file. This file is served conditionally
    // to browsers that accept gzip using .htaccess rules.
    if (variable_get('css_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) {
    // If CSS gzip compression is enabled and the zlib extension is available
    // then create a gzipped version of this file. This file is served
    // conditionally to browsers that accept gzip using .htaccess rules.
    // It's possible that the rewrite rules in .htaccess aren't working on this
    // server, but there's no harm (other than the time spent generating the
    // file) in generating the file anyway. Sites on servers where rewrite rules
    // aren't working can set css_gzip_compression to FALSE in order to skip
    // generating a file that won't be used.
    if (variable_get('css_gzip_compression', TRUE) && extension_loaded('zlib')) {
      if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {
        return FALSE;
      }
@@ -4105,15 +4075,19 @@ function drupal_add_js($data = NULL, $options = NULL) {
    // Add jquery.js and drupal.js, as well as the basePath setting, the
    // first time a JavaScript file is added.
    if (empty($javascript)) {
      // url() generates the prefix using hook_url_outbound_alter(). Instead of
      // running the hook_url_outbound_alter() again here, extract the prefix
      // from url().
      url('', array('prefix' => &$prefix));
      // url() generates the script and prefix using hook_url_outbound_alter().
      // Instead of running the hook_url_outbound_alter() again here, extract
      // them from url().
      // @todo Make this less hacky: http://drupal.org/node/1547376.
      $scriptPath = $GLOBALS['script_path'];
      $pathPrefix = '';
      url('', array('script' => &$scriptPath, 'prefix' => &$pathPrefix));
      $javascript = array(
        'settings' => array(
          'data' => array(
            array('basePath' => base_path()),
            array('pathPrefix' => empty($prefix) ? '' : $prefix),
            array('scriptPath' => $scriptPath),
            array('pathPrefix' => $pathPrefix),
          ),
          'type' => 'setting',
          'scope' => 'header',
@@ -5056,11 +5030,15 @@ function drupal_build_js_cache($files) {
    if (!file_exists($uri) && !file_unmanaged_save_data($contents, $uri, FILE_EXISTS_REPLACE)) {
      return FALSE;
    }
    // If JS gzip compression is enabled, clean URLs are enabled (which means
    // that rewrite rules are working) and the zlib extension is available then
    // create a gzipped version of this file. This file is served conditionally
    // to browsers that accept gzip using .htaccess rules.
    if (variable_get('js_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) {
    // If JS gzip compression is enabled and the zlib extension is available
    // then create a gzipped version of this file. This file is served
    // conditionally to browsers that accept gzip using .htaccess rules.
    // It's possible that the rewrite rules in .htaccess aren't working on this
    // server, but there's no harm (other than the time spent generating the
    // file) in generating the file anyway. Sites on servers where rewrite rules
    // aren't working can set js_gzip_compression to FALSE in order to skip
    // generating a file that won't be used.
    if (variable_get('js_gzip_compression', TRUE) && extension_loaded('zlib')) {
      if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($contents, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) {
        return FALSE;
      }
@@ -5217,7 +5195,7 @@ function _drupal_bootstrap_full() {
    ini_set('error_log', 'public://error.log');
  }

  // Initialize $_GET['q'] prior to invoking hook_init().
  // Initialize current_path() prior to invoking hook_init().
  drupal_path_initialize();

  // Let all modules take action before the menu system handles the request.
@@ -5256,7 +5234,7 @@ function drupal_page_set_cache() {
    $cache = (object) array(
      'cid' => $base_root . request_uri(),
      'data' => array(
        'path' => $_GET['q'],
        'path' => current_path(),
        'body' => ob_get_clean(),
        'title' => drupal_get_title(),
        'headers' => array(),
+1 −1
Changes for core/includes/file.inc: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -2031,7 +2031,7 @@ function file_transfer($uri, $headers) {
 * @see system_menu()
 */
function file_download() {
  // Merge remainder of arguments from GET['q'], into relative file path.
  // Merge remaining path arguments into relative file path.
  $args = func_get_args();
  $scheme = array_shift($args);
  $target = implode('/', $args);
+2 −2
Changes for core/includes/form.inc: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -1248,7 +1248,7 @@ function drupal_redirect_form($form_state) {
        $function($form_state['redirect']);
      }
    }
    drupal_goto($_GET['q']);
    drupal_goto(current_path());
  }
}

@@ -4682,7 +4682,7 @@ function batch_process($redirect = NULL, $url = 'batch', $redirect_callback = 'd
      'progressive' => TRUE,
      'url' => $url,
      'url_options' => array(),
      'source_url' => $_GET['q'],
      'source_url' => current_path(),
      'redirect' => $redirect,
      'theme' => $GLOBALS['theme_key'],
      'redirect_callback' => $redirect_callback,
Loading