]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
redirect to previous search (if any) when deleting a link 114/head
authorfeula <mr.pikzen@gmail.com>
Sun, 15 Feb 2015 01:24:26 +0000 (02:24 +0100)
committernodiscc <nodiscc@gmail.com>
Tue, 17 Feb 2015 20:03:22 +0000 (21:03 +0100)
 * Fixes https://github.com/shaarli/Shaarli/issues/110

index.php

index 818fa680a347009c5596d55fa1472fa4185787a9..3af38695e1bbe6534f4ef25a890721f5cfd0cc7f 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1550,7 +1550,37 @@ function renderPage()
 
         // If we are called from the bookmarklet, we must close the popup:
         if (isset($_GET['source']) && $_GET['source']=='bookmarklet') { echo '<script>self.close();</script>'; exit; }
-        header('Location: ?'); // After deleting the link, redirect to the home page.
+        // Pick where we're going to redirect
+        // =============================================================
+        // Basically, we can't redirect to where we were previously if it was a permalink
+        // or an edit_link, because it would 404.
+        // Cases:
+        //    - /             : nothing in $_GET, redirect to self
+        //    - /?page        : redirect to self
+        //    - /?searchterm  : redirect to self (there might be other links) 
+        //    - /?searchtags  : redirect to self
+        //    - /permalink    : redirect to / (the link does not exist anymore)
+        //    - /?edit_link   : redirect to / (the link does not exist anymore)
+        // PHP treats the permalink as a $_GET variable, so we need to check if every condition for self
+        // redirect is not satisfied, and only then redirect to /
+        $location = "?";
+        // Self redirection
+        if (count($_GET) == 0    ||
+            isset($_GET['page']) ||
+            isset($_GET['searchterm']) ||
+            isset($_GET['searchtags'])) {
+
+            if (isset($_POST['returnurl'])) {
+                $location = $_POST['returnurl']; // Handle redirects given by the form
+            }
+
+            if ($location === "?" &&
+                isset($_SERVER['HTTP_REFERER'])) { // Handle HTTP_REFERER in case we're not coming from the same place.
+                $location = $_SERVER['HTTP_REFERER'];
+            }
+        }
+
+        header('Location: ' . $location); // After deleting the link, redirect to appropriate location
         exit;
     }