diff options
Diffstat (limited to 'application/front/controllers/ShaarliController.php')
-rw-r--r-- | application/front/controllers/ShaarliController.php | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/application/front/controllers/ShaarliController.php b/application/front/controllers/ShaarliController.php index 0c5d363e..bfff5fcf 100644 --- a/application/front/controllers/ShaarliController.php +++ b/application/front/controllers/ShaarliController.php | |||
@@ -6,6 +6,7 @@ namespace Shaarli\Front\Controller; | |||
6 | 6 | ||
7 | use Shaarli\Bookmark\BookmarkFilter; | 7 | use Shaarli\Bookmark\BookmarkFilter; |
8 | use Shaarli\Container\ShaarliContainer; | 8 | use Shaarli\Container\ShaarliContainer; |
9 | use Slim\Http\Response; | ||
9 | 10 | ||
10 | abstract class ShaarliController | 11 | abstract class ShaarliController |
11 | { | 12 | { |
@@ -80,4 +81,46 @@ abstract class ShaarliController | |||
80 | $this->assignView('plugins_' . $name, $plugin_data); | 81 | $this->assignView('plugins_' . $name, $plugin_data); |
81 | } | 82 | } |
82 | } | 83 | } |
84 | |||
85 | /** | ||
86 | * Generates a redirection to the previous page, based on the HTTP_REFERER. | ||
87 | * It fails back to the home page. | ||
88 | * | ||
89 | * @param array $loopTerms Terms to remove from path and query string to prevent direction loop. | ||
90 | * @param array $clearParams List of parameter to remove from the query string of the referrer. | ||
91 | */ | ||
92 | protected function redirectFromReferer(Response $response, array $loopTerms = [], array $clearParams = []): Response | ||
93 | { | ||
94 | $defaultPath = './'; | ||
95 | $referer = $this->container->environment['HTTP_REFERER'] ?? null; | ||
96 | |||
97 | if (null !== $referer) { | ||
98 | $currentUrl = parse_url($referer); | ||
99 | parse_str($currentUrl['query'] ?? '', $params); | ||
100 | $path = $currentUrl['path'] ?? $defaultPath; | ||
101 | } else { | ||
102 | $params = []; | ||
103 | $path = $defaultPath; | ||
104 | } | ||
105 | |||
106 | // Prevent redirection loop | ||
107 | if (isset($currentUrl)) { | ||
108 | foreach ($clearParams as $value) { | ||
109 | unset($params[$value]); | ||
110 | } | ||
111 | |||
112 | $checkQuery = implode('', array_keys($params)); | ||
113 | foreach ($loopTerms as $value) { | ||
114 | if (strpos($path . $checkQuery, $value) !== false) { | ||
115 | $params = []; | ||
116 | $path = $defaultPath; | ||
117 | break; | ||
118 | } | ||
119 | } | ||
120 | } | ||
121 | |||
122 | $queryString = count($params) > 0 ? '?'. http_build_query($params) : ''; | ||
123 | |||
124 | return $response->withRedirect($path . $queryString); | ||
125 | } | ||
83 | } | 126 | } |