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
;
12 abstract class ShaarliVisitorController
14 /** @var ShaarliContainer */
17 /** @param ShaarliContainer $container Slim container (extended for attribute completion). */
18 public function __construct(ShaarliContainer
$container)
20 $this->container
= $container;
24 * Assign variables to RainTPL template through the PageBuilder.
26 * @param mixed $value Value to assign to the template
28 protected function assignView(string $name, $value): self
30 $this->container
->pageBuilder
->assign($name, $value);
36 * Assign variables to RainTPL template through the PageBuilder.
38 * @param mixed $data Values to assign to the template and their keys
40 protected function assignAllView(array $data): self
42 foreach ($data as $key => $value) {
43 $this->assignView($key, $value);
49 protected function render(string $template): string
51 $this->assignView('linkcount', $this->container
->bookmarkService
->count(BookmarkFilter
::$ALL));
52 $this->assignView('privateLinkcount', $this->container
->bookmarkService
->count(BookmarkFilter
::$PRIVATE));
53 $this->assignView('plugin_errors', $this->container
->pluginManager
->getErrors());
55 $this->executeDefaultHooks($template);
57 return $this->container
->pageBuilder
->render($template);
61 * Call plugin hooks for header, footer and includes, specifying which page will be rendered.
62 * Then assign generated data to RainTPL.
64 protected function executeDefaultHooks(string $template): void
72 foreach ($common_hooks as $name) {
74 $this->container
->pluginManager
->executeHooks(
78 'target' => $template,
79 'loggedin' => $this->container
->loginManager
->isLoggedIn()
82 $this->assignView('plugins_' . $name, $plugin_data);
87 * Generates a redirection to the previous page, based on the HTTP_REFERER.
88 * It fails back to the home page.
90 * @param array $loopTerms Terms to remove from path and query string to prevent direction loop.
91 * @param array $clearParams List of parameter to remove from the query string of the referrer.
93 protected function redirectFromReferer(
96 array $loopTerms = [],
97 array $clearParams = []
99 $defaultPath = $request->getUri()->getBasePath();
100 $referer = $this->container
->environment
['HTTP_REFERER'] ?? null;
102 if (null !== $referer) {
103 $currentUrl = parse_url($referer);
104 parse_str($currentUrl['query'] ?? '', $params);
105 $path = $currentUrl['path'] ?? $defaultPath;
108 $path = $defaultPath;
111 // Prevent redirection loop
112 if (isset($currentUrl)) {
113 foreach ($clearParams as $value) {
114 unset($params[$value]);
117 $checkQuery = implode('', array_keys($params));
118 foreach ($loopTerms as $value) {
119 if (strpos($path . $checkQuery, $value) !== false) {
121 $path = $defaultPath;
127 $queryString = count($params) > 0 ? '?'. http_build_query($params) : '';
129 return $response->withRedirect($path . $queryString);