3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller
;
7 use Shaarli\Bookmark\BookmarkFilter
;
8 use Shaarli\Container\ShaarliContainer
;
9 use Slim\Http\Response
;
11 abstract class ShaarliController
13 /** @var ShaarliContainer */
16 /** @param ShaarliContainer $container Slim container (extended for attribute completion). */
17 public function __construct(ShaarliContainer
$container)
19 $this->container
= $container;
23 * Assign variables to RainTPL template through the PageBuilder.
25 * @param mixed $value Value to assign to the template
27 protected function assignView(string $name, $value): self
29 $this->container
->pageBuilder
->assign($name, $value);
35 * Assign variables to RainTPL template through the PageBuilder.
37 * @param mixed $data Values to assign to the template and their keys
39 protected function assignAllView(array $data): self
41 foreach ($data as $key => $value) {
42 $this->assignView($key, $value);
48 protected function render(string $template): string
50 $this->assignView('linkcount', $this->container
->bookmarkService
->count(BookmarkFilter
::$ALL));
51 $this->assignView('privateLinkcount', $this->container
->bookmarkService
->count(BookmarkFilter
::$PRIVATE));
52 $this->assignView('plugin_errors', $this->container
->pluginManager
->getErrors());
54 $this->executeDefaultHooks($template);
56 return $this->container
->pageBuilder
->render($template);
60 * Call plugin hooks for header, footer and includes, specifying which page will be rendered.
61 * Then assign generated data to RainTPL.
63 protected function executeDefaultHooks(string $template): void
71 foreach ($common_hooks as $name) {
73 $this->container
->pluginManager
->executeHooks(
77 'target' => $template,
78 'loggedin' => $this->container
->loginManager
->isLoggedIn()
81 $this->assignView('plugins_' . $name, $plugin_data);
86 * Generates a redirection to the previous page, based on the HTTP_REFERER.
87 * It fails back to the home page.
89 * @param array $loopTerms Terms to remove from path and query string to prevent direction loop.
90 * @param array $clearParams List of parameter to remove from the query string of the referrer.
92 protected function redirectFromReferer(Response
$response, array $loopTerms = [], array $clearParams = []): Response
95 $referer = $this->container
->environment
['HTTP_REFERER'] ?? null;
97 if (null !== $referer) {
98 $currentUrl = parse_url($referer);
99 parse_str($currentUrl['query'] ?? '', $params);
100 $path = $currentUrl['path'] ?? $defaultPath;
103 $path = $defaultPath;
106 // Prevent redirection loop
107 if (isset($currentUrl)) {
108 foreach ($clearParams as $value) {
109 unset($params[$value]);
112 $checkQuery = implode('', array_keys($params));
113 foreach ($loopTerms as $value) {
114 if (strpos($path . $checkQuery, $value) !== false) {
116 $path = $defaultPath;
122 $queryString = count($params) > 0 ? '?'. http_build_query($params) : '';
124 return $response->withRedirect($path . $queryString);