diff --git globalredirect.module globalredirect.module index b799157..97253db 100644 --- globalredirect.module +++ globalredirect.module @@ -51,7 +51,7 @@ function globalredirect_init() { } // Get the Query String (minus the 'q'). If none set, set to NULL - $query_string = drupal_get_query_parameters($_GET, array('q')); + $query_string = drupal_get_query_parameters(); if (empty($query_string)) { $query_string = NULL; } @@ -66,13 +66,20 @@ function globalredirect_init() { 'prefix' => '', 'external' => FALSE, ); - if (function_exists('language_url_rewrite')) { - // Note 1 : the language_url_rewrite() takes path (by reference) as the + + // Get the request path. We cannot use request_path() as it uses a completely trimmed path. + // We need just left trimmed. + $request_path = globalredirect_request_path(); + + + // Let the language_url_rewrite_url do it's magic, if preset. + // TODO: Is this needed anymore? + if (function_exists('language_url_rewrite_url')) { + // Note 1 : the language_url_rewrite_url() 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'] : ''; - language_url_rewrite($path, $options); + // Note 2 : We use $request_path here as we want the path in an untouched + // form (current_path() gets modified by core) + language_url_rewrite_url($request_path, $options); } $prefix = rtrim($options['prefix'], '/'); @@ -80,7 +87,7 @@ function globalredirect_init() { 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 ($request_path != $prefix) { drupal_goto('', $options, 301); } elseif ($settings['nonclean_to_clean'] && @@ -93,32 +100,36 @@ function globalredirect_init() { return; } - // Trim any trailing slash off the end (eg, 'node/1/' to 'node/1') - $request = $settings['deslash'] ? trim($_GET['q'], '/') : $_GET['q']; + + // Get the current page - it's already "deslashed" + $current_path = current_path(); + + // Optional stripping of "/0". Disabled by default. switch ($settings['trailing_zero']) { case 2 : // If 'taxonomy/term/*' only. If not, break out. - if (drupal_substr($request, 0, 14) != 'taxonomy/term/') { + if (drupal_substr($current_path, 0, 14) != 'taxonomy/term/') { break; } // If it is, fall through to general trailing zero method case 1 : // If last 2 characters of URL are /0 then trim them off - if (drupal_substr($request, -2) == '/0') { - $request = rtrim($request, '/0'); + if (drupal_substr($current_path, -2) == '/0') { + $current_path = rtrim($current_path, '/0'); } } + // If the feature is enabled, check and redirect taxonomy/term/* requests to their proper handler defined by hook_term_path(). - if ($settings['term_path_handler'] && module_exists('taxonomy') && preg_match('/taxonomy\/term\/([0-9]+)$/', $request, $matches)) { + if ($settings['term_path_handler'] && module_exists('taxonomy') && preg_match('/taxonomy\/term\/([0-9]+)$/', $current_path, $matches)) { // So the feature is enabled, as is taxonomy module and the current request is a taxonomy term page. // NOTE: This will only match taxonomy term pages WITHOUT a depth modifier $term = taxonomy_term_load($matches[1]); // Get the term path for this term (handler is defined in the vocab table under module). If it differs from the request, then redirect. - if (($term_path = entity_uri('taxonomy_term', $term)) && $term_path['path'] != $request) { - $request = $term_path['path']; + if (($term_path = entity_uri('taxonomy_term', $term)) && $term_path['path'] != $current_path) { + $current_path = $term_path['path']; } } @@ -153,9 +164,9 @@ function globalredirect_init() { foreach ($all_languages as $l => $lang) { // Only test for languages other than the current one. if ($lang->language != $language->language) { - $alias = drupal_get_path_alias($request, $lang->language); + $alias = drupal_get_path_alias($current_path, $lang->language); // There is a matching language for this alias - if ($alias != $request) { + if ($alias != $current_path) { if (isset($lang->domain)) { drupal_goto($lang->domain . '/' . $alias, $options, 301); } @@ -170,20 +181,24 @@ function globalredirect_init() { // Find an alias (if any) for the request $langcode = isset($options['language']->language) ? $options['language']->language : ''; - $alias = drupal_get_path_alias($request, $langcode); + $alias = drupal_get_path_alias($current_path, $langcode); // TODO: This looks wrong for D7... maybe a hook? if (function_exists('custom_url_rewrite_outbound')) { // Modules may alter outbound links by reference. - custom_url_rewrite_outbound($alias, $options, $request); + custom_url_rewrite_outbound($alias, $options, $current_path); } if ($prefix && $alias) { $prefix .= '/'; } // Alias case sensitivity check. - // NOTE: This has changed. In D6 the $alias matched the request (in terms of case), however in D7 $alias is already a true alias (accurate in case), and therefore not the "true" request... - // So, if the alias and the request path are case-insensitive the same then, if Case Senitive URL's are enabled, the alias SHOULD be the accurate $alias from above, otherwise it should be the request_path()... + // NOTE: This has changed. In D6 the $alias matched the request (in terms of + // case). However in D7 $alias is already a true alias (accurate in case), + // and therefore not the "true" request... So, if the alias and the request + // path are case-insensitive the same then, if Case Sensitive URL's are + // enabled, the alias SHOULD be the accurate $alias from above, otherwise it + // should be the request_path()... // TODO: Test if this breaks the language checking above! if (strcasecmp($alias, request_path()) == 0) { // The alias and the request are identical (case insensitive)... Therefore... @@ -192,9 +207,9 @@ function globalredirect_init() { // 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_path != $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_path) != '/' || $settings['deslash']) { drupal_goto($alias, $options, 301); } } @@ -202,7 +217,7 @@ function globalredirect_init() { // If no alias was returned, the final check is to direct non-clean to // clean - if clean is enabled if ($settings['nonclean_to_clean'] && ((bool)variable_get('clean_url', 0)) && strpos(request_uri(), '?q=')) { - drupal_goto($request, $options, 301); + drupal_goto($current_path, $options, 301); } // Restore the destination from earlier so its available in other places. @@ -215,13 +230,13 @@ function globalredirect_init() { if ($settings['canonical']) { drupal_add_html_head_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_path, array('absolute' => TRUE, 'query' => $query_string)), )); } // Add the Content-Location header to the page if ($settings['content_location_header']) { - drupal_add_http_header('Content-Location', url(drupal_is_front_page() ? '' : $_REQUEST['q'], array('absolute' => TRUE, 'query' => $query_string))); + drupal_add_http_header('Content-Location', url(drupal_is_front_page() ? '' : $request_path, array('absolute' => TRUE, 'query' => $query_string))); } } @@ -278,7 +293,7 @@ function _globalredirect_is_active($settings) { * If the site is in offline mode there is little point doing any of this as * you might end up redirecting to a 503. */ - if (variable_get('maintenance_mode', 0)) { + if (variable_get('site_offline', 0) == 1) { return FALSE; } @@ -297,7 +312,8 @@ function _globalredirect_is_active($settings) { } /** - * If menu_check is enabled AND the menu_get_item function is missing, GlobalRedirect is disabled + * If menu_check is enabled AND the menu_get_item function is missing, + * GlobalRedirect is disabled. */ if ($settings['menu_check'] && !function_exists('menu_get_item')) { return FALSE; @@ -331,3 +347,33 @@ function _globalredirect_get_settings($default_only = FALSE) { return variable_get('globalredirect_settings', array()) + $defaults; } + + +/** + * globalredirect_request_path() is borrowed from request_uri(), but it only ltrim's.. + */ +function globalredirect_request_path() { + if (isset($_SERVER['REQUEST_URI'])) { + if (isset($_REQUEST['q'])) { + $path = $_REQUEST['q']; + } + else { + // This is a request using a clean URL. 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); + } + } + 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 = ltrim($path, '/'); + + return $path; +}