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 // Legacy key that used to be injected by PluginManager
60 $this->assignView('_PAGE_', $template);
61 $this->assignView('template', $template);
63 $this->assignView('linkcount', $this->container
->bookmarkService
->count(BookmarkFilter
::$ALL));
64 $this->assignView('privateLinkcount', $this->container
->bookmarkService
->count(BookmarkFilter
::$PRIVATE));
66 $this->executeDefaultHooks($template);
68 $this->assignView('plugin_errors', $this->container
->pluginManager
->getErrors());
70 return $this->container
->pageBuilder
->render($template, $this->container
->basePath
);
74 * Call plugin hooks for header, footer and includes, specifying which page will be rendered.
75 * Then assign generated data to RainTPL.
77 protected function executeDefaultHooks(string $template): void
85 $parameters = $this->buildPluginParameters($template);
87 foreach ($common_hooks as $name) {
89 $this->container
->pluginManager
->executeHooks(
94 $this->assignView('plugins_' . $name, $pluginData);
98 protected function executePageHooks(string $hook, array &$data, string $template = null): void
100 $this->container
->pluginManager
->executeHooks(
103 $this->buildPluginParameters($template)
107 protected function buildPluginParameters(?string $template): array
110 'target' => $template,
111 'loggedin' => $this->container
->loginManager
->isLoggedIn(),
112 'basePath' => $this->container
->basePath
,
113 'rootPath' => preg_replace('#/index\.php$#', '', $this->container
->basePath
),
114 'bookmarkService' => $this->container
->bookmarkService
119 * Simple helper which prepend the base path to redirect path.
121 * @param Response $response
122 * @param string $path Absolute path, e.g.: `/`, or `/admin/shaare/123` regardless of install directory
124 * @return Response updated
126 protected function redirect(Response
$response, string $path): Response
128 return $response->withRedirect($this->container
->basePath
. $path);
132 * Generates a redirection to the previous page, based on the HTTP_REFERER.
133 * It fails back to the home page.
135 * @param array $loopTerms Terms to remove from path and query string to prevent direction loop.
136 * @param array $clearParams List of parameter to remove from the query string of the referrer.
138 protected function redirectFromReferer(
141 array $loopTerms = [],
142 array $clearParams = [],
143 string $anchor = null
145 $defaultPath = $this->container
->basePath
. '/';
146 $referer = $this->container
->environment
['HTTP_REFERER'] ?? null;
148 if (null !== $referer) {
149 $currentUrl = parse_url($referer);
150 // If the referer is not related to Shaarli instance, redirect to default
152 isset($currentUrl['host'])
153 && strpos(index_url($this->container
->environment
), $currentUrl['host']) === false
155 return $response->withRedirect($defaultPath);
158 parse_str($currentUrl['query'] ?? '', $params);
159 $path = $currentUrl['path'] ?? $defaultPath;
162 $path = $defaultPath;
165 // Prevent redirection loop
166 if (isset($currentUrl)) {
167 foreach ($clearParams as $value) {
168 unset($params[$value]);
171 $checkQuery = implode('', array_keys($params));
172 foreach ($loopTerms as $value) {
173 if (strpos($path . $checkQuery, $value) !== false) {
175 $path = $defaultPath;
181 $queryString = count($params) > 0 ? '?' . http_build_query($params) : '';
182 $anchor = $anchor ? '#' . $anchor : '';
184 return $response->withRedirect($path . $queryString . $anchor);