]>
Commit | Line | Data |
---|---|---|
6c50a6cc A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
2899ebb5 | 5 | namespace Shaarli\Front\Controller\Visitor; |
6c50a6cc | 6 | |
0498b209 | 7 | use Shaarli\Bookmark\BookmarkFilter; |
6c50a6cc | 8 | use Shaarli\Container\ShaarliContainer; |
2899ebb5 | 9 | use Slim\Http\Request; |
af290059 | 10 | use Slim\Http\Response; |
6c50a6cc | 11 | |
ef00f9d2 A |
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 | */ | |
2899ebb5 | 20 | abstract class ShaarliVisitorController |
6c50a6cc A |
21 | { |
22 | /** @var ShaarliContainer */ | |
27ceea2a | 23 | protected $container; |
6c50a6cc | 24 | |
27ceea2a A |
25 | /** @param ShaarliContainer $container Slim container (extended for attribute completion). */ |
26 | public function __construct(ShaarliContainer $container) | |
6c50a6cc | 27 | { |
27ceea2a | 28 | $this->container = $container; |
6c50a6cc A |
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 | { | |
27ceea2a | 38 | $this->container->pageBuilder->assign($name, $value); |
6c50a6cc A |
39 | |
40 | return $this; | |
41 | } | |
0498b209 | 42 | |
7b2ba6ef A |
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 | ||
0498b209 A |
57 | protected function render(string $template): string |
58 | { | |
ccd1862d A |
59 | // Legacy key that used to be injected by PluginManager |
60 | $this->assignView('_PAGE_', $template); | |
61 | $this->assignView('template', $template); | |
62 | ||
27ceea2a A |
63 | $this->assignView('linkcount', $this->container->bookmarkService->count(BookmarkFilter::$ALL)); |
64 | $this->assignView('privateLinkcount', $this->container->bookmarkService->count(BookmarkFilter::$PRIVATE)); | |
0498b209 A |
65 | |
66 | $this->executeDefaultHooks($template); | |
67 | ||
7e3dc0ba A |
68 | $this->assignView('plugin_errors', $this->container->pluginManager->getErrors()); |
69 | ||
9fbc4229 | 70 | return $this->container->pageBuilder->render($template, $this->container->basePath); |
0498b209 A |
71 | } |
72 | ||
73 | /** | |
74 | * Call plugin hooks for header, footer and includes, specifying which page will be rendered. | |
75 | * Then assign generated data to RainTPL. | |
76 | */ | |
77 | protected function executeDefaultHooks(string $template): void | |
78 | { | |
79 | $common_hooks = [ | |
80 | 'includes', | |
81 | 'header', | |
82 | 'footer', | |
83 | ]; | |
84 | ||
80b708a8 A |
85 | $parameters = $this->buildPluginParameters($template); |
86 | ||
0498b209 | 87 | foreach ($common_hooks as $name) { |
c22fa57a | 88 | $pluginData = []; |
27ceea2a | 89 | $this->container->pluginManager->executeHooks( |
0498b209 | 90 | 'render_' . $name, |
c22fa57a | 91 | $pluginData, |
80b708a8 | 92 | $parameters |
0498b209 | 93 | ); |
c22fa57a | 94 | $this->assignView('plugins_' . $name, $pluginData); |
0498b209 A |
95 | } |
96 | } | |
af290059 | 97 | |
9fbc4229 A |
98 | protected function executePageHooks(string $hook, array &$data, string $template = null): void |
99 | { | |
9fbc4229 A |
100 | $this->container->pluginManager->executeHooks( |
101 | $hook, | |
102 | $data, | |
80b708a8 | 103 | $this->buildPluginParameters($template) |
9fbc4229 A |
104 | ); |
105 | } | |
106 | ||
80b708a8 A |
107 | protected function buildPluginParameters(?string $template): array |
108 | { | |
109 | return [ | |
110 | 'target' => $template, | |
111 | 'loggedin' => $this->container->loginManager->isLoggedIn(), | |
112 | 'basePath' => $this->container->basePath, | |
3adbdc2a | 113 | 'rootPath' => preg_replace('#/index\.php$#', '', $this->container->basePath), |
80b708a8 A |
114 | 'bookmarkService' => $this->container->bookmarkService |
115 | ]; | |
116 | } | |
117 | ||
9c75f877 A |
118 | /** |
119 | * Simple helper which prepend the base path to redirect path. | |
120 | * | |
121 | * @param Response $response | |
122 | * @param string $path Absolute path, e.g.: `/`, or `/admin/shaare/123` regardless of install directory | |
123 | * | |
124 | * @return Response updated | |
125 | */ | |
126 | protected function redirect(Response $response, string $path): Response | |
127 | { | |
128 | return $response->withRedirect($this->container->basePath . $path); | |
129 | } | |
130 | ||
af290059 A |
131 | /** |
132 | * Generates a redirection to the previous page, based on the HTTP_REFERER. | |
133 | * It fails back to the home page. | |
134 | * | |
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. | |
137 | */ | |
2899ebb5 A |
138 | protected function redirectFromReferer( |
139 | Request $request, | |
140 | Response $response, | |
141 | array $loopTerms = [], | |
c22fa57a A |
142 | array $clearParams = [], |
143 | string $anchor = null | |
2899ebb5 | 144 | ): Response { |
818b3193 | 145 | $defaultPath = $this->container->basePath . '/'; |
af290059 A |
146 | $referer = $this->container->environment['HTTP_REFERER'] ?? null; |
147 | ||
148 | if (null !== $referer) { | |
149 | $currentUrl = parse_url($referer); | |
abe033be | 150 | // If the referer is not related to Shaarli instance, redirect to default |
53054b2b A |
151 | if ( |
152 | isset($currentUrl['host']) | |
abe033be A |
153 | && strpos(index_url($this->container->environment), $currentUrl['host']) === false |
154 | ) { | |
155 | return $response->withRedirect($defaultPath); | |
156 | } | |
157 | ||
af290059 A |
158 | parse_str($currentUrl['query'] ?? '', $params); |
159 | $path = $currentUrl['path'] ?? $defaultPath; | |
160 | } else { | |
161 | $params = []; | |
162 | $path = $defaultPath; | |
163 | } | |
164 | ||
165 | // Prevent redirection loop | |
166 | if (isset($currentUrl)) { | |
167 | foreach ($clearParams as $value) { | |
168 | unset($params[$value]); | |
169 | } | |
170 | ||
171 | $checkQuery = implode('', array_keys($params)); | |
172 | foreach ($loopTerms as $value) { | |
173 | if (strpos($path . $checkQuery, $value) !== false) { | |
174 | $params = []; | |
175 | $path = $defaultPath; | |
176 | break; | |
177 | } | |
178 | } | |
179 | } | |
180 | ||
53054b2b | 181 | $queryString = count($params) > 0 ? '?' . http_build_query($params) : ''; |
c22fa57a | 182 | $anchor = $anchor ? '#' . $anchor : ''; |
af290059 | 183 | |
c22fa57a | 184 | return $response->withRedirect($path . $queryString . $anchor); |
af290059 | 185 | } |
6c50a6cc | 186 | } |