]>
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 | { | |
27ceea2a A |
59 | $this->assignView('linkcount', $this->container->bookmarkService->count(BookmarkFilter::$ALL)); |
60 | $this->assignView('privateLinkcount', $this->container->bookmarkService->count(BookmarkFilter::$PRIVATE)); | |
0498b209 A |
61 | |
62 | $this->executeDefaultHooks($template); | |
63 | ||
7e3dc0ba A |
64 | $this->assignView('plugin_errors', $this->container->pluginManager->getErrors()); |
65 | ||
9fbc4229 | 66 | return $this->container->pageBuilder->render($template, $this->container->basePath); |
0498b209 A |
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 | ||
80b708a8 A |
81 | $parameters = $this->buildPluginParameters($template); |
82 | ||
0498b209 | 83 | foreach ($common_hooks as $name) { |
c22fa57a | 84 | $pluginData = []; |
27ceea2a | 85 | $this->container->pluginManager->executeHooks( |
0498b209 | 86 | 'render_' . $name, |
c22fa57a | 87 | $pluginData, |
80b708a8 | 88 | $parameters |
0498b209 | 89 | ); |
c22fa57a | 90 | $this->assignView('plugins_' . $name, $pluginData); |
0498b209 A |
91 | } |
92 | } | |
af290059 | 93 | |
9fbc4229 A |
94 | protected function executePageHooks(string $hook, array &$data, string $template = null): void |
95 | { | |
9fbc4229 A |
96 | $this->container->pluginManager->executeHooks( |
97 | $hook, | |
98 | $data, | |
80b708a8 | 99 | $this->buildPluginParameters($template) |
9fbc4229 A |
100 | ); |
101 | } | |
102 | ||
80b708a8 A |
103 | protected function buildPluginParameters(?string $template): array |
104 | { | |
105 | return [ | |
106 | 'target' => $template, | |
107 | 'loggedin' => $this->container->loginManager->isLoggedIn(), | |
108 | 'basePath' => $this->container->basePath, | |
3adbdc2a | 109 | 'rootPath' => preg_replace('#/index\.php$#', '', $this->container->basePath), |
80b708a8 A |
110 | 'bookmarkService' => $this->container->bookmarkService |
111 | ]; | |
112 | } | |
113 | ||
9c75f877 A |
114 | /** |
115 | * Simple helper which prepend the base path to redirect path. | |
116 | * | |
117 | * @param Response $response | |
118 | * @param string $path Absolute path, e.g.: `/`, or `/admin/shaare/123` regardless of install directory | |
119 | * | |
120 | * @return Response updated | |
121 | */ | |
122 | protected function redirect(Response $response, string $path): Response | |
123 | { | |
124 | return $response->withRedirect($this->container->basePath . $path); | |
125 | } | |
126 | ||
af290059 A |
127 | /** |
128 | * Generates a redirection to the previous page, based on the HTTP_REFERER. | |
129 | * It fails back to the home page. | |
130 | * | |
131 | * @param array $loopTerms Terms to remove from path and query string to prevent direction loop. | |
132 | * @param array $clearParams List of parameter to remove from the query string of the referrer. | |
133 | */ | |
2899ebb5 A |
134 | protected function redirectFromReferer( |
135 | Request $request, | |
136 | Response $response, | |
137 | array $loopTerms = [], | |
c22fa57a A |
138 | array $clearParams = [], |
139 | string $anchor = null | |
2899ebb5 | 140 | ): Response { |
818b3193 | 141 | $defaultPath = $this->container->basePath . '/'; |
af290059 A |
142 | $referer = $this->container->environment['HTTP_REFERER'] ?? null; |
143 | ||
144 | if (null !== $referer) { | |
145 | $currentUrl = parse_url($referer); | |
abe033be | 146 | // If the referer is not related to Shaarli instance, redirect to default |
53054b2b A |
147 | if ( |
148 | isset($currentUrl['host']) | |
abe033be A |
149 | && strpos(index_url($this->container->environment), $currentUrl['host']) === false |
150 | ) { | |
151 | return $response->withRedirect($defaultPath); | |
152 | } | |
153 | ||
af290059 A |
154 | parse_str($currentUrl['query'] ?? '', $params); |
155 | $path = $currentUrl['path'] ?? $defaultPath; | |
156 | } else { | |
157 | $params = []; | |
158 | $path = $defaultPath; | |
159 | } | |
160 | ||
161 | // Prevent redirection loop | |
162 | if (isset($currentUrl)) { | |
163 | foreach ($clearParams as $value) { | |
164 | unset($params[$value]); | |
165 | } | |
166 | ||
167 | $checkQuery = implode('', array_keys($params)); | |
168 | foreach ($loopTerms as $value) { | |
169 | if (strpos($path . $checkQuery, $value) !== false) { | |
170 | $params = []; | |
171 | $path = $defaultPath; | |
172 | break; | |
173 | } | |
174 | } | |
175 | } | |
176 | ||
53054b2b | 177 | $queryString = count($params) > 0 ? '?' . http_build_query($params) : ''; |
c22fa57a | 178 | $anchor = $anchor ? '#' . $anchor : ''; |
af290059 | 179 | |
c22fa57a | 180 | return $response->withRedirect($path . $queryString . $anchor); |
af290059 | 181 | } |
6c50a6cc | 182 | } |