Index: checkout.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/checkout/checkout.module,v
retrieving revision 1.11.2.11
diff -u -p -r1.11.2.11 checkout.module
--- checkout.module	24 Apr 2009 13:08:57 -0000	1.11.2.11
+++ checkout.module	10 Dec 2009 11:50:28 -0000
@@ -182,22 +182,34 @@ function checkout_cron() {
  *   The user id to (un)lock nodes for.
  */
 function checkout_handle_request($uid) {
-  global $base_path;
-  
-  // Build referer path
-  $referer_uri = parse_url(referer_uri());
-  if (variable_get('clean_url', 0)) {
-    $referer = substr($referer_uri['path'], strlen($base_path));
+  global $base_path, $base_url;
+
+  $current_nid = checkout_get_nid($_GET['q']);
+  if ($uid && $current_nid) {
+    // Store the current request URI for future referer lookups.
+    $_SESSION['checkout_path'][$base_url . request_uri()][] = array($_GET['q'], time());
   }
-  else {
-    $vars = array();
-    if (isset($referer_uri['query'])) {
-      parse_str($referer_uri['query'], $vars);
+
+  // Look up the path for the current referer.
+  $referer_uri = referer_uri();
+  if (is_array($_SESSION['checkout_path'][$referer_uri])) {
+    list($referer, ) = array_shift($_SESSION['checkout_path'][$referer_uri]);
+    if (empty($_SESSION['checkout_path'][$referer_uri])) {
+      unset($_SESSION['checkout_path'][$referer_uri]);
     }
-    $referer = isset($vars['q']) ? $vars['q'] : '';
   }
-  if ($referer = rtrim($referer, '/')) {
-    $referer = drupal_get_normal_path($referer);
+
+  // Tidy up the session storage.
+  if (is_array($_SESSION['checkout_path'])) {
+    foreach ($_SESSION['checkout_path'] as $uri => $history) {
+      while (list($path, $time) = array_shift($_SESSION['checkout_path'][$uri])) {
+        if ($time >= time() - 86400) { // 24 hours
+          $_SESSION['checkout_path'][$uri] = array_merge(array(array($path, $time)), $_SESSION['checkout_path'][$uri]);
+          break 2; // escape both the while and the foreach
+        }
+      }
+      unset($_SESSION['checkout_path'][$uri]);
+    }
   }
 
   // If refering and current paths match we can abort, since there can't be any
@@ -208,7 +220,6 @@ function checkout_handle_request($uid) {
 
   // Otherwise try to extract nid from path.
   $previous_nid = checkout_get_nid($referer);
-  $current_nid = checkout_get_nid($_GET['q']);
 
   // Check whether to release a previously edited node.
   if ($previous_nid && (!$current_nid || $current_nid != $previous_nid)) {
@@ -219,7 +230,9 @@ function checkout_handle_request($uid) {
   if ($current_nid && (!$previous_nid || $previous_nid != $current_nid)) {
     // Try to lock the node.
     if (!checkout_node($current_nid, $uid)) {
-      // Node already locked: send back to refering page.
+      // Node already locked; remove the path we just added.
+      array_pop($_SESSION['checkout_path'][$base_url . request_uri()]);
+      // Send back to refering page.
       drupal_goto(referer_uri());
     }
   }
