3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 use Shaarli\Bookmark\BookmarkFilter
;
8 use Shaarli\Container\ShaarliContainer
;
10 use Slim\Http\Response
;
13 * Class ShaarliVisitorController
15 * All controllers accessible by visitors (non logged in users) should extend this abstract class.
16 * Contains a few helper function for template rendering, plugins, etc.
18 * @package Shaarli\Front\Controller\Visitor
20 abstract class ShaarliVisitorController
22 /** @var ShaarliContainer */
25 /** @param ShaarliContainer $container Slim container (extended for attribute completion). */
26 public function __construct(ShaarliContainer
$container)
28 $this->container
= $container;
32 * Assign variables to RainTPL template through the PageBuilder.
34 * @param mixed $value Value to assign to the template
36 protected function assignView(string $name, $value): self
38 $this->container
->pageBuilder
->assign($name, $value);
44 * Assign variables to RainTPL template through the PageBuilder.
46 * @param mixed $data Values to assign to the template and their keys
48 protected function assignAllView(array $data): self
50 foreach ($data as $key => $value) {
51 $this->assignView($key, $value);
57 protected function render(string $template): string
59 $this->assignView('linkcount', $this->container
->bookmarkService
->count(BookmarkFilter
::$ALL));
60 $this->assignView('privateLinkcount', $this->container
->bookmarkService
->count(BookmarkFilter
::$PRIVATE));
62 $this->executeDefaultHooks($template);
64 $this->assignView('plugin_errors', $this->container
->pluginManager
->getErrors());
66 return $this->container
->pageBuilder
->render($template, $this->container
->basePath
);
70 * Call plugin hooks for header, footer and includes, specifying which page will be rendered.
71 * Then assign generated data to RainTPL.
73 protected function executeDefaultHooks(string $template): void
81 $parameters = $this->buildPluginParameters($template);
83 foreach ($common_hooks as $name) {
85 $this->container
->pluginManager
->executeHooks(
90 $this->assignView('plugins_' . $name, $pluginData);
94 protected function executePageHooks(string $hook, array &$data, string $template = null): void
96 $this->container
->pluginManager
->executeHooks(
99 $this->buildPluginParameters($template)
103 protected function buildPluginParameters(?string $template): array
106 'target' => $template,
107 'loggedin' => $this->container
->loginManager
->isLoggedIn(),
108 'basePath' => $this->container
->basePath
,
109 'bookmarkService' => $this->container
->bookmarkService
114 * Simple helper which prepend the base path to redirect path.
116 * @param Response $response
117 * @param string $path Absolute path, e.g.: `/`, or `/admin/shaare/123` regardless of install directory
119 * @return Response updated
121 protected function redirect(Response
$response, string $path): Response
123 return $response->withRedirect($this->container
->basePath
. $path);
127 * Generates a redirection to the previous page, based on the HTTP_REFERER.
128 * It fails back to the home page.
130 * @param array $loopTerms Terms to remove from path and query string to prevent direction loop.
131 * @param array $clearParams List of parameter to remove from the query string of the referrer.
133 protected function redirectFromReferer(
136 array $loopTerms = [],
137 array $clearParams = [],
138 string $anchor = null
140 $defaultPath = $this->container
->basePath
. '/';
141 $referer = $this->container
->environment
['HTTP_REFERER'] ?? null;
143 if (null !== $referer) {
144 $currentUrl = parse_url($referer);
145 parse_str($currentUrl['query'] ?? '', $params);
146 $path = $currentUrl['path'] ?? $defaultPath;
149 $path = $defaultPath;
152 // Prevent redirection loop
153 if (isset($currentUrl)) {
154 foreach ($clearParams as $value) {
155 unset($params[$value]);
158 $checkQuery = implode('', array_keys($params));
159 foreach ($loopTerms as $value) {
160 if (strpos($path . $checkQuery, $value) !== false) {
162 $path = $defaultPath;
168 $queryString = count($params) > 0 ? '?'. http_build_query($params) : '';
169 $anchor = $anchor ? '#' . $anchor : '';
171 return $response->withRedirect($path . $queryString . $anchor);