]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/front/controller/visitor/ShaarliVisitorController.php
55c075a2a87f7ae8c7cad95c5134bf78c53e3cf5
[github/shaarli/Shaarli.git] / application / front / controller / visitor / ShaarliVisitorController.php
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Shaarli\Front\Controller\Visitor;
6
7 use Shaarli\Bookmark\BookmarkFilter;
8 use Shaarli\Container\ShaarliContainer;
9 use Slim\Http\Request;
10 use Slim\Http\Response;
11
12 /**
13 * Class ShaarliVisitorController
14 *
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.
17 *
18 * @package Shaarli\Front\Controller\Visitor
19 */
20 abstract class ShaarliVisitorController
21 {
22 /** @var ShaarliContainer */
23 protected $container;
24
25 /** @param ShaarliContainer $container Slim container (extended for attribute completion). */
26 public function __construct(ShaarliContainer $container)
27 {
28 $this->container = $container;
29 }
30
31 /**
32 * Assign variables to RainTPL template through the PageBuilder.
33 *
34 * @param mixed $value Value to assign to the template
35 */
36 protected function assignView(string $name, $value): self
37 {
38 $this->container->pageBuilder->assign($name, $value);
39
40 return $this;
41 }
42
43 /**
44 * Assign variables to RainTPL template through the PageBuilder.
45 *
46 * @param mixed $data Values to assign to the template and their keys
47 */
48 protected function assignAllView(array $data): self
49 {
50 foreach ($data as $key => $value) {
51 $this->assignView($key, $value);
52 }
53
54 return $this;
55 }
56
57 protected function render(string $template): string
58 {
59 $this->assignView('linkcount', $this->container->bookmarkService->count(BookmarkFilter::$ALL));
60 $this->assignView('privateLinkcount', $this->container->bookmarkService->count(BookmarkFilter::$PRIVATE));
61
62 $this->executeDefaultHooks($template);
63
64 $this->assignView('plugin_errors', $this->container->pluginManager->getErrors());
65
66 return $this->container->pageBuilder->render($template, $this->container->basePath);
67 }
68
69 /**
70 * Call plugin hooks for header, footer and includes, specifying which page will be rendered.
71 * Then assign generated data to RainTPL.
72 */
73 protected function executeDefaultHooks(string $template): void
74 {
75 $common_hooks = [
76 'includes',
77 'header',
78 'footer',
79 ];
80
81 $parameters = $this->buildPluginParameters($template);
82
83 foreach ($common_hooks as $name) {
84 $pluginData = [];
85 $this->container->pluginManager->executeHooks(
86 'render_' . $name,
87 $pluginData,
88 $parameters
89 );
90 $this->assignView('plugins_' . $name, $pluginData);
91 }
92 }
93
94 protected function executePageHooks(string $hook, array &$data, string $template = null): void
95 {
96 $this->container->pluginManager->executeHooks(
97 $hook,
98 $data,
99 $this->buildPluginParameters($template)
100 );
101 }
102
103 protected function buildPluginParameters(?string $template): array
104 {
105 return [
106 'target' => $template,
107 'loggedin' => $this->container->loginManager->isLoggedIn(),
108 'basePath' => $this->container->basePath,
109 'bookmarkService' => $this->container->bookmarkService
110 ];
111 }
112
113 /**
114 * Simple helper which prepend the base path to redirect path.
115 *
116 * @param Response $response
117 * @param string $path Absolute path, e.g.: `/`, or `/admin/shaare/123` regardless of install directory
118 *
119 * @return Response updated
120 */
121 protected function redirect(Response $response, string $path): Response
122 {
123 return $response->withRedirect($this->container->basePath . $path);
124 }
125
126 /**
127 * Generates a redirection to the previous page, based on the HTTP_REFERER.
128 * It fails back to the home page.
129 *
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.
132 */
133 protected function redirectFromReferer(
134 Request $request,
135 Response $response,
136 array $loopTerms = [],
137 array $clearParams = [],
138 string $anchor = null
139 ): Response {
140 $defaultPath = $this->container->basePath . '/';
141 $referer = $this->container->environment['HTTP_REFERER'] ?? null;
142
143 if (null !== $referer) {
144 $currentUrl = parse_url($referer);
145 // If the referer is not related to Shaarli instance, redirect to default
146 if (isset($currentUrl['host'])
147 && strpos(index_url($this->container->environment), $currentUrl['host']) === false
148 ) {
149 return $response->withRedirect($defaultPath);
150 }
151
152 parse_str($currentUrl['query'] ?? '', $params);
153 $path = $currentUrl['path'] ?? $defaultPath;
154 } else {
155 $params = [];
156 $path = $defaultPath;
157 }
158
159 // Prevent redirection loop
160 if (isset($currentUrl)) {
161 foreach ($clearParams as $value) {
162 unset($params[$value]);
163 }
164
165 $checkQuery = implode('', array_keys($params));
166 foreach ($loopTerms as $value) {
167 if (strpos($path . $checkQuery, $value) !== false) {
168 $params = [];
169 $path = $defaultPath;
170 break;
171 }
172 }
173 }
174
175 $queryString = count($params) > 0 ? '?'. http_build_query($params) : '';
176 $anchor = $anchor ? '#' . $anchor : '';
177
178 return $response->withRedirect($path . $queryString . $anchor);
179 }
180 }