From b0993040465d063af75de5b7ec3cebccdeeb191e Mon Sep 17 00:00:00 2001
From: Gisle Hannemyr <gisle@hannemyr.no>
Date: Tue, 27 Nov 2012 23:46:05 +0100
Subject: [PATCH] #825860 by gisle: Making redirect from 403 page work.

---
 customerror.module |   20 ++++++++++++++++++--
 1 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/customerror.module b/customerror.module
index dcb7f6b..4682d13 100644
--- a/customerror.module
+++ b/customerror.module
@@ -174,7 +174,13 @@ function customerror_page() {
     case 403:
       $internal_path = substr(request_uri(), strlen(base_path()));
       if ($internal_path) {
-        $_GET['destination'] = $internal_path;
+        $dest = drupal_parse_url($internal_path);
+        if (isset($dest['query']['destination'])) {
+          $_GET['destination'] = ($dest['query']['destination']);
+        }
+        else {
+          $_GET['destination'] = $internal_path;
+        }
       }
       else {
         $_GET['destination'] = variable_get('site_frontpage', 'node');
@@ -255,7 +261,17 @@ function customerror_user_login(&$edit, $account) {
   // Check if we have a destination saved in the session
   if (!empty($_SESSION['destination'])) {
     // If there is one, then set the REQUEST destination to it
-    $_GET['destination'] = $_SESSION['destination'];
+    $dest = drupal_parse_url($_SESSION['destination']);
+    if (isset($dest['query']['destination'])) {
+      $dnode = $dest['query']['destination'];
+    }
+    elseif (isset($dest['path'])) {
+      $dnode = $dest['path'];
+    }
+    else {
+      $dnode = variable_get('site_frontpage', 'node');
+    }
+    $_GET['destination'] = $dnode;
     // And clear the one in the session
     unset($_SESSION['destination']);
     // user.module then does a drupal_goto() for us after we return from here
-- 
1.7.4.1

