Index: globalredirect.module ========================================================= --- globalredirect.module (revision 1.1.2.4.2.5.2.32) +++ globalredirect.module Thu Nov 04 21:58:23 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. */ @@ -70,18 +74,38 @@ 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 + // 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'] : ''; + $path = isset($request_q) ? $request_q : ''; language_url_rewrite($path, $options); + $request_q = preg_replace('`^'. $options['prefix'] .'`', '', $request_q); } $prefix = rtrim($options['prefix'], '/'); + // If purl being used, remove purl modifiers + if (function_exists('purl_get_normal_path')) { + // Get our clean url from PURL + $request_q = globalredirect_purl_fix($request_q); + if (drupal_is_front_page()) { + // If it is a front page, set $request_q to what Drupal would normally do + // Which is to remove the "Drupal path" and only leave prefixes + // In our case, the front page path is the same as $get_q + $request_q = preg_replace('`'. $get_q .'$`', '', $request_q); + // We can safely add the lenguage prefix with no check. If no lenguage is being used, + // it will be empty + $request_q = $prefix . $request_q; + } + else { + // We can safely add the lenguage prefix with no check. If no lenguage is being used, + // it will be empty + $request_q = $options['prefix'] . $request_q; + } + } // 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 +119,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 +179,10 @@ // Only test for languages other than the current one. if ($lang->language != $language->language) { $alias = drupal_get_path_alias($request, $lang->language); + // If purl being used, remove purl modifiers + if (function_exists('purl_get_normal_path')) { + $alias = globalredirect_purl_fix($alias); + } // There is a matching language for this alias if ($alias != $request) { if (isset($lang->domain)) { @@ -179,6 +207,10 @@ if ($prefix && $alias) { $prefix .= '/'; } + // If purl being used, remove purl modifiers + if (function_exists('purl_get_normal_path')) { + $alias = globalredirect_purl_fix($alias); + } // Alias case sensitivity check. If there is an alias from the previous // lookup, do a query to test for case. @@ -193,9 +225,9 @@ // 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 +247,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 +370,64 @@ return variable_get('globalredirect_settings', array()) + $defaults; } + +/** + * 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, $activate = FALSE, $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; + } + } + + // This is the difference with the original + // We need no path alias + +/* + // Now that the path has been fully rewritten, check to see if there is + // an available path alias. + if ($src = drupal_lookup_path('source', $adjusted[$q])) { + $adjusted[$q] = $src; + } + } +*/ + // If the activate flag has been thrown, activate provider callbacks. + if ($activate) { + foreach ($cache[$q] as $method => $elements) { + foreach ($elements as $element) { + purl_active()->add($method, $element)->set($element); + } + } + } + // If at the end of all this the adjusted query string is empty, use the + // site frontpage. + if (!empty($q)) { + return !empty($adjusted[$q]) ? $adjusted[$q] : variable_get('site_frontpage', 'node'); + } + // If the actual requested string was empty, we need to leave it alone. + return $adjusted[$q]; +} \ No newline at end of file