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));
61 $this->assignView('plugin_errors', $this->container
->pluginManager
->getErrors());
63 $this->executeDefaultHooks($template);
65 return $this->container
->pageBuilder
->render($template);
69 * Call plugin hooks for header, footer and includes, specifying which page will be rendered.
70 * Then assign generated data to RainTPL.
72 protected function executeDefaultHooks(string $template): void
80 foreach ($common_hooks as $name) {
82 $this->container
->pluginManager
->executeHooks(
86 'target' => $template,
87 'loggedin' => $this->container
->loginManager
->isLoggedIn()
90 $this->assignView('plugins_' . $name, $pluginData);
95 * Generates a redirection to the previous page, based on the HTTP_REFERER.
96 * It fails back to the home page.
98 * @param array $loopTerms Terms to remove from path and query string to prevent direction loop.
99 * @param array $clearParams List of parameter to remove from the query string of the referrer.
101 protected function redirectFromReferer(
104 array $loopTerms = [],
105 array $clearParams = [],
106 string $anchor = null
108 $defaultPath = rtrim($request->getUri()->getBasePath(), '/') . '/';
109 $referer = $this->container
->environment
['HTTP_REFERER'] ?? null;
111 if (null !== $referer) {
112 $currentUrl = parse_url($referer);
113 parse_str($currentUrl['query'] ?? '', $params);
114 $path = $currentUrl['path'] ?? $defaultPath;
117 $path = $defaultPath;
120 // Prevent redirection loop
121 if (isset($currentUrl)) {
122 foreach ($clearParams as $value) {
123 unset($params[$value]);
126 $checkQuery = implode('', array_keys($params));
127 foreach ($loopTerms as $value) {
128 if (strpos($path . $checkQuery, $value) !== false) {
130 $path = $defaultPath;
136 $queryString = count($params) > 0 ? '?'. http_build_query($params) : '';
137 $anchor = $anchor ? '#' . $anchor : '';
139 return $response->withRedirect($path . $queryString . $anchor);