Index: globalredirect.module ========================================================= --- globalredirect.module (revision 1.1.2.4.2.5.2.32) +++ globalredirect.module Fri Nov 05 02:29:30 EST 2010 @@ -23,6 +23,10 @@ function globalredirect_init() { global $language; + // Set variables to be able to modify them if needed + $get_q = $_GET['q']; + $request_q = $_REQUEST['q']; + /** * Get the settings. */ @@ -59,6 +63,7 @@ // Establish the language prefix that should be used, ie. the one that // drupal_goto() would use + $prefix = ''; $options = array( 'fragment' => '', 'query' => '', @@ -70,18 +75,35 @@ if (function_exists('language_url_rewrite')) { // Note 1 : the language_url_rewrite() takes path (by reference) as the // first argument but does not use it at all - // Note 2 : We use $_REQUEST['q'] here as we want the path in an untouched - // form ($_GET['q] gets modified by core) - $path = isset($_REQUEST['q']) ? $_REQUEST['q'] : ''; + // Note 2 : We use $request_q here as we want the path in an untouched + // form ($get_q gets modified by core) + $path = isset($request_q) ? $request_q : ''; language_url_rewrite($path, $options); + $prefix = rtrim($options['prefix'], '/'); + // We remove the lenguage prefix and the triling slash if it is the only thing we have + // apart form the lenguage prefix + // Will be put back once we get our PURL clean url + $request_q = preg_replace('`^'. $prefix . '(/(?=.))?`', '', $request_q); + } + + // Clean our path from modifications made by other modules + $request_q = globalredirect_clean_path($request_q); + + // We put the lenguage prefix back + if ($prefix) { + if($request_q == '/' || empty($request_q)) { + $request_q = $prefix . $request_q; + } + else { + $request_q = $prefix . '/' . $request_q; + } } - $prefix = rtrim($options['prefix'], '/'); // Do a check if this is a front page if (drupal_is_front_page()) { // Redirect if the current request does not refer to the front page in the // configured fashion (with or without a prefix) - if (isset($_REQUEST['q']) && $_REQUEST['q'] != $prefix) { + if (isset($request_q) && $request_q != $prefix) { drupal_goto('', $query_string, NULL, 301); } elseif ($settings['nonclean_to_clean'] && @@ -95,7 +117,7 @@ } // Trim any trailing slash off the end (eg, 'node/1/' to 'node/1') - $request = $settings['deslash'] ? trim($_GET['q'], '/') : $_GET['q']; + $request = $settings['deslash'] ? trim($get_q, '/') : $get_q; // Optional stripping of "/0". Disabled by default. switch ($settings['trailing_zero']) { @@ -155,6 +177,8 @@ // Only test for languages other than the current one. if ($lang->language != $language->language) { $alias = drupal_get_path_alias($request, $lang->language); + // Clean our path from modifications made by other modules + $alias = globalredirect_clean_path($alias); // There is a matching language for this alias if ($alias != $request) { if (isset($lang->domain)) { @@ -191,11 +215,14 @@ } } + // Clean our path from modifications made by other modules + $alias = globalredirect_clean_path($alias); + // Compare the request to the alias. This also works as a 'deslashing' // agent. If we have a language prefix then prefix the alias - if ($_REQUEST['q'] != $prefix . $alias) { + if ($request_q != $prefix . $alias) { // If it's not just a slash or user has deslash on, redirect - if (str_replace($prefix . $alias, '', $_REQUEST['q']) != '/' || $settings['deslash']) { + if (str_replace($prefix . $alias, '', $request_q) != '/' || $settings['deslash']) { drupal_goto($alias, $query_string, NULL, 301); } } @@ -215,13 +242,13 @@ if ($settings['canonical']) { drupal_add_link(array( 'rel' => 'canonical', - 'href' => url(drupal_is_front_page() ? '' : $_REQUEST['q'], array('absolute' => TRUE, 'query' => $query_string)), + 'href' => url(drupal_is_front_page() ? '' : $request_q, array('absolute' => TRUE, 'query' => $query_string)), )); } // Add the Content-Location header to the page if ($settings['content_location_header']) { - drupal_set_header('Content-Location: '. url(drupal_is_front_page() ? '' : $_REQUEST['q'], array('absolute' => TRUE, 'query' => $query_string))); + drupal_set_header('Content-Location: '. url(drupal_is_front_page() ? '' : $request_q, array('absolute' => TRUE, 'query' => $query_string))); } } @@ -338,3 +365,54 @@ return variable_get('globalredirect_settings', array()) + $defaults; } + +/** + * Wrapper for globalredirect_purl_fix() + * + * Could be replaced by a hook to let modules modify the path before + * globalredirect does the magic. + */ +function globalredirect_clean_path($path) { + // If purl being used, remove purl modifiers + if (function_exists('purl_get_normal_path')) { + // Get our clean url from PURL + $path = globalredirect_purl_fix($path); + } + return $path; +} + +/** + * same as purl_get_normal_path() but without the alias lookup. + * Used to fix the redirect issue. More info here: http://drupal.org/node/346911 + * + * Can be removed once purl_get_normal_path() receives an extra parameter + * to avoid the alias lookup + */ +function globalredirect_purl_fix($q, $reset = FALSE) { + static $cache = array(); + static $adjusted = array(); + if (!isset($cache[$q]) || $reset) { + $cache[$q] = array(); + $adjusted[$q] = $q; + // Detect all modifiers, parse them into their respective provider values, + // and allow processors to adjust the query string. + foreach (array_keys(_purl_options()) as $method) { + $processor = purl_get_processor($method); + + // Pass the requested Drupal query string for modifier detection. + // Processors that don't use/affect the query string can access + // the $_REQUEST or $_GET objects directly. + $value = $processor->detect($adjusted[$q]); + $elements = purl_parse($processor, $value); + + // Allow adjustment of page request globals. + if (is_array($elements)) { + foreach ($elements as $element) { + $processor->adjust($value, $element, $adjusted[$q]); + } + $cache[$q][$method] = $elements; + } + } + } + return $adjusted[$q]; +} \ No newline at end of file