From 00e3d9dfa5acc4a8aa887c5b4e018f56b1e7cbe8 Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Sat, 13 Oct 2012 10:34:10 -0400
Subject: [PATCH] Issue #905914 by swentel, pillarsdotnet, fasdalf, jenlampton, barraponto: Re-enable global redirections.

---
 redirect.admin.inc |    7 +----
 redirect.module    |   65 ++++++++++++++++++++++++++++++++--------------------
 2 files changed, 42 insertions(+), 30 deletions(-)

diff --git a/redirect.admin.inc b/redirect.admin.inc
index 176f105..5f6c212 100644
--- a/redirect.admin.inc
+++ b/redirect.admin.inc
@@ -521,6 +521,7 @@ function redirect_edit_form_validate($form, &$form_state) {
 
   if (empty($form_state['values']['override'])) {
     if ($existing = redirect_load_by_source($redirect->source, $redirect->language)) {
+      $existing = $existing[1];
       if ($redirect->rid != $existing->rid && $redirect->language == $existing->language) {
         // The "from" path should not conflict with another redirect
         $form_state['storage']['override_messages']['redirect-conflict'] = t('The base source path %source is already being redirected. Do you want to <a href="@edit-page">edit the existing redirect</a>?', array('%source' => $redirect->source, '@edit-page' => url('admin/config/search/redirect/edit/'. $existing->rid)));
@@ -637,20 +638,17 @@ function redirect_settings_form($form, &$form_state) {
     '#type' => 'fieldset',
     '#title' => t('Always enabled redirections'),
     '#description' => t('(formerly Global Redirect features)'),
-    '#access' => FALSE,
   );
   $form['globals']['redirect_global_home'] = array(
     '#type' => 'checkbox',
     '#title' => t('Redirect from paths like index.php and /node to the root directory.'),
-    '#default_value' => variable_get('redirect_global_home', 1),
-    '#access' => FALSE,
+    '#default_value' => variable_get('redirect_global_home', FALSE),
   );
   $form['globals']['redirect_global_clean'] = array(
     '#type' => 'checkbox',
     '#title' => t('Redirect from non-clean URLs to clean URLs.'),
     '#default_value' => variable_get('redirect_global_clean', 1),
     '#disabled' => !variable_get('clean_url', 0),
-    '#access' => FALSE,
   );
   $form['globals']['redirect_global_canonical'] = array(
     '#type' => 'checkbox',
@@ -661,7 +659,6 @@ function redirect_settings_form($form, &$form_state) {
     '#type' => 'checkbox',
     '#title' => t('Remove trailing slashes from paths.'),
     '#default_value' => variable_get('redirect_global_deslash', 0),
-    '#access' => FALSE,
   );
   $form['globals']['redirect_global_admin_paths'] = array(
     '#type' => 'checkbox',
diff --git a/redirect.module b/redirect.module
index a99bd9b..b79eeee 100644
--- a/redirect.module
+++ b/redirect.module
@@ -244,21 +244,34 @@ function redirect_url_inbound_alter(&$path, $original_path, $path_language) {
     }
   }
 
-  // Redirect to canonical URLs.
-  if ($path && variable_get('redirect_canonical', 1)) {
-    $alias = drupal_get_path_alias($path, $path_language);
-    if ($alias != $path && $alias != $original_path) {
-      //return redirect_redirect(array('redirect' => $alias, 'type' => 'global'));
-    }
+  // Check for empty path or disabled canonical redirects.
+  if (empty($path) || !variable_get('redirect_global_canonical', 1)) {
+    return;
+  }
+  // Do not redirect if $original_path does not match the requested url.
+  if ($original_path != $_GET['q']) {
+    return;
+  }
+  // Do not redirect if disallowed.
+  if (!redirect_can_redirect()) {
+    return;
+  }
+  // Redirect to front page if requested.
+  if (variable_get('redirect_global_home', FALSE) && drupal_is_front_page() && (base_path() != $_SERVER['REQUEST_URI']) ) {
+    return redirect_redirect((object)array('redirect' => '', 'type' => 'global'));
+  }
+
+  $alias = drupal_get_path_alias($path, $path_language);
 
-    // Redirect from default entity paths to the proper entity path.
-    //if ($path_entity = redirect_load_entity_from_path($path)) {
-    //  if ($uri = entity_uri($path_entity['entity_type'], $path_entity['entity'])) {
-    //    if ($path != $uri['path']) {
-    //      return redirect_redirect(array('redirect' => $uri['path'], 'redirect_options' => $uri['options'], 'type' => 'global'));
-    //    }
-    //  }
-    //}
+  if ($alias != $path && $alias != $original_path) {
+    return redirect_redirect((object)array('redirect' => $alias, 'type' => 'global'));
+  }
+
+  // Redirect from default entity paths to the proper entity path.
+  if ($path_entity = redirect_load_entity_from_path($path)) {
+    $uri = entity_uri($path_entity['entity_type'], $path_entity['entity']);
+    if ($path != $uri['path']) {
+      return redirect_redirect((object)array('redirect' => $uri['path'], 'redirect_options' => $uri['options'], 'type' => 'global'));
   }
 }
 
@@ -331,23 +344,25 @@ function redirect_init() {
 
   // Redirect from non-clean URLs to clean URLs.
   if (variable_get('redirect_global_clean', 1) && variable_get('clean_url', 0) && strpos($request_uri, '?q=') !== FALSE) {
-    //$redirect_global = TRUE;
-    //$request_uri = str_replace('?q=', '', $request_uri);
+    $redirect_global = TRUE;
+    $request_uri = str_replace('?q=', '', $request_uri);
   }
 
+  // Remove index.php from the request uri.
   if (strpos($request_uri, 'index.php') !== FALSE) {
-    //$redirect_global = TRUE;
-    //$request_uri = str_replace('index.php', '', $request_uri);
+    $redirect_global = TRUE;
+    $request_uri = str_replace('index.php', '', $request_uri);
   }
 
-  //$request_uri = ltrim($request_uri, '/');
-  //$parsed = parse_url($request_uri);
+  $request_uri = ltrim($request_uri, '/');
+  $parsed = parse_url($request_uri);
 
   if ($redirect_global && $request_uri != $original_uri) {
-    redirect_redirect(array(/*'redirect' => $request_uri,*/ 'type' => 'global'));
+    redirect_redirect((object)array('redirect' => $request_uri, 'type' => 'global'));
   }
 }
 
+
 /**
  * Implements hook_cron().
  */
@@ -596,7 +611,7 @@ function redirect_load_by_source($source, $language = LANGUAGE_NONE, array $quer
       $context = array('language' => $language, 'query' => $query);
       drupal_alter('redirect_load_by_source', $redirects, $source, $context);
 
-      return !empty($redirects) ? reset($redirects) : FALSE;
+      return !empty($redirects) ? $redirects : FALSE;
     }
   }
 
@@ -711,7 +726,7 @@ function redirect_validate($redirect, $form, &$form_state) {
   }
 }
 
-function redirect_object_prepare($redirect, $defaults = array()) {
+function redirect_object_prepare(stdClass $redirect, $defaults = array()) {
   $defaults += array(
     'rid' => NULL,
     'type' => 'redirect',
@@ -978,7 +993,7 @@ function redirect_purge_inactive_redirects(array $types = array('redirect'), $in
  *
  * @ingroup redirect_api
  */
-function redirect_redirect($redirect = NULL) {
+function redirect_redirect(stdClass $redirect = NULL) {
   // First check if we're in an infinite loop.
   $session_id = session_id();
   if (flood_is_allowed('redirection', 5, 15, $session_id ? $session_id : NULL)) {
@@ -1309,7 +1324,7 @@ function redirect_variables() {
     'redirect_passthrough_querystring' => 1,
     'redirect_page_cache' => 0,
     'redirect_purge_inactive' => 0,
-    'redirect_global_home' => 1,
+    'redirect_global_home' => FALSE,
     'redirect_global_clean' => 1,
     'redirect_global_canonical' => 1,
     'redirect_global_admin_paths' => 0,
-- 
1.7.1

