X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2Ffront%2Fcontroller%2Fvisitor%2FShaarliVisitorController.php;h=55c075a2a87f7ae8c7cad95c5134bf78c53e3cf5;hb=054e03f37fa29da8066f1a637919f13c7e7dc5d2;hp=f12915c135caaf92f57a5a44a2d4ffee11c335f0;hpb=ef00f9d2033f6de11e71bf3a909399cae6f73a9f;p=github%2Fshaarli%2FShaarli.git diff --git a/application/front/controller/visitor/ShaarliVisitorController.php b/application/front/controller/visitor/ShaarliVisitorController.php index f12915c1..55c075a2 100644 --- a/application/front/controller/visitor/ShaarliVisitorController.php +++ b/application/front/controller/visitor/ShaarliVisitorController.php @@ -58,11 +58,12 @@ abstract class ShaarliVisitorController { $this->assignView('linkcount', $this->container->bookmarkService->count(BookmarkFilter::$ALL)); $this->assignView('privateLinkcount', $this->container->bookmarkService->count(BookmarkFilter::$PRIVATE)); - $this->assignView('plugin_errors', $this->container->pluginManager->getErrors()); $this->executeDefaultHooks($template); - return $this->container->pageBuilder->render($template); + $this->assignView('plugin_errors', $this->container->pluginManager->getErrors()); + + return $this->container->pageBuilder->render($template, $this->container->basePath); } /** @@ -77,20 +78,51 @@ abstract class ShaarliVisitorController 'footer', ]; + $parameters = $this->buildPluginParameters($template); + foreach ($common_hooks as $name) { - $plugin_data = []; + $pluginData = []; $this->container->pluginManager->executeHooks( 'render_' . $name, - $plugin_data, - [ - 'target' => $template, - 'loggedin' => $this->container->loginManager->isLoggedIn() - ] + $pluginData, + $parameters ); - $this->assignView('plugins_' . $name, $plugin_data); + $this->assignView('plugins_' . $name, $pluginData); } } + protected function executePageHooks(string $hook, array &$data, string $template = null): void + { + $this->container->pluginManager->executeHooks( + $hook, + $data, + $this->buildPluginParameters($template) + ); + } + + protected function buildPluginParameters(?string $template): array + { + return [ + 'target' => $template, + 'loggedin' => $this->container->loginManager->isLoggedIn(), + 'basePath' => $this->container->basePath, + 'bookmarkService' => $this->container->bookmarkService + ]; + } + + /** + * Simple helper which prepend the base path to redirect path. + * + * @param Response $response + * @param string $path Absolute path, e.g.: `/`, or `/admin/shaare/123` regardless of install directory + * + * @return Response updated + */ + protected function redirect(Response $response, string $path): Response + { + return $response->withRedirect($this->container->basePath . $path); + } + /** * Generates a redirection to the previous page, based on the HTTP_REFERER. * It fails back to the home page. @@ -102,13 +134,21 @@ abstract class ShaarliVisitorController Request $request, Response $response, array $loopTerms = [], - array $clearParams = [] + array $clearParams = [], + string $anchor = null ): Response { - $defaultPath = $request->getUri()->getBasePath(); + $defaultPath = $this->container->basePath . '/'; $referer = $this->container->environment['HTTP_REFERER'] ?? null; if (null !== $referer) { $currentUrl = parse_url($referer); + // If the referer is not related to Shaarli instance, redirect to default + if (isset($currentUrl['host']) + && strpos(index_url($this->container->environment), $currentUrl['host']) === false + ) { + return $response->withRedirect($defaultPath); + } + parse_str($currentUrl['query'] ?? '', $params); $path = $currentUrl['path'] ?? $defaultPath; } else { @@ -133,7 +173,8 @@ abstract class ShaarliVisitorController } $queryString = count($params) > 0 ? '?'. http_build_query($params) : ''; + $anchor = $anchor ? '#' . $anchor : ''; - return $response->withRedirect($path . $queryString); + return $response->withRedirect($path . $queryString . $anchor); } }