From 1cf17dccf9ffaeb72a0da9a7bb0b6a286dd4a80c Mon Sep 17 00:00:00 2001
From: Capi Etheriel <barraponto@gmail.com>
Date: Sat, 18 Aug 2012 10:42:54 -0300
Subject: [PATCH 1/2] Issue #905914 by jenlamptom, fasdalf, pillarsdotnet:
 re-enable global redirections.

---
 redirect.admin.inc |  5 +----
 redirect.module    | 66 +++++++++++++++++++++++++++++++++---------------------
 2 files changed, 42 insertions(+), 29 deletions(-)

diff --git a/redirect.admin.inc b/redirect.admin.inc
index 3d70ebf..d4a203f 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,
   );
   $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 dfbe029..73f43eb 100644
--- a/redirect.module
+++ b/redirect.module
@@ -206,20 +206,34 @@ function redirect_menu() {
  * Implements hook_url_inbound_alter().
  */
 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', TRUE) && drupal_is_front_page() && (base_path() != $_SERVER['REQUEST_URI']) ) {
+    return redirect_redirect((object)array('redirect' => '', 'type' => 'global'));
+  }
 
-    // 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'));
-        }
-      }
+  $alias = drupal_get_path_alias($path, $path_language);
+
+  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'));
     }
   }
 }
@@ -252,8 +266,8 @@ function redirect_init() {
   $current_path = current_path();
   $current_langcode = $GLOBALS['language']->language;
   $current_query = drupal_get_query_parameters();
-  if ($redirect = redirect_load_by_source($current_path, $current_langcode, $current_query)) {
-    redirect_redirect($redirect);
+  if ($redirects = redirect_load_by_source($current_path, $current_langcode, $current_query)) {
+    redirect_redirect(reset($redirects));
   }
 
   $redirect_global = FALSE;
@@ -261,23 +275,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().
  */
@@ -527,7 +543,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;
     }
   }
 
@@ -642,7 +658,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',
@@ -909,7 +925,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)) {
-- 
1.7.11.5


From 9d55b937f7fe1c97de17e5534549446ab2cbb2aa Mon Sep 17 00:00:00 2001
From: Capi Etheriel <barraponto@gmail.com>
Date: Sat, 18 Aug 2012 11:15:20 -0300
Subject: [PATCH 2/2] Issue #905914 by jenlamptom, fasdalf, pillarsdotnet,
 barraponto: re-enable global redirections.

---
 redirect.admin.inc | 2 +-
 redirect.module    | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/redirect.admin.inc b/redirect.admin.inc
index d4a203f..89ab4f8 100644
--- a/redirect.admin.inc
+++ b/redirect.admin.inc
@@ -642,7 +642,7 @@ function redirect_settings_form($form, &$form_state) {
   $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),
+    '#default_value' => variable_get('redirect_global_home', FALSE),
   );
   $form['globals']['redirect_global_clean'] = array(
     '#type' => 'checkbox',
diff --git a/redirect.module b/redirect.module
index 73f43eb..7876a54 100644
--- a/redirect.module
+++ b/redirect.module
@@ -219,7 +219,7 @@ function redirect_url_inbound_alter(&$path, $original_path, $path_language) {
     return;
   }
   // Redirect to front page if requested.
-  if (variable_get('redirect_global_home', TRUE) && drupal_is_front_page() && (base_path() != $_SERVER['REQUEST_URI']) ) {
+  if (variable_get('redirect_global_home', FALSE) && drupal_is_front_page() && (base_path() != $_SERVER['REQUEST_URI']) ) {
     return redirect_redirect((object)array('redirect' => '', 'type' => 'global'));
   }
 
@@ -1251,7 +1251,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.11.5

