]>
Commit | Line | Data |
---|---|---|
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 | $this->assignView('plugin_errors', $this->container->pluginManager->getErrors()); | |
62 | ||
63 | /* | |
64 | * Define base path (if Shaarli is installed in a domain's subfolder, e.g. `/shaarli`) | |
65 | * and the asset path (subfolder/tpl/default for default theme). | |
66 | * These MUST be used to create an internal link or to include an asset in templates. | |
67 | */ | |
68 | $this->assignView('base_path', $this->container->basePath); | |
69 | $this->assignView( | |
70 | 'asset_path', | |
71 | $this->container->basePath . '/' . | |
72 | rtrim($this->container->conf->get('resource.raintpl_tpl', 'tpl'), '/') . '/' . | |
73 | $this->container->conf->get('resource.theme', 'default') | |
74 | ); | |
75 | ||
76 | $this->executeDefaultHooks($template); | |
77 | ||
78 | return $this->container->pageBuilder->render($template); | |
79 | } | |
80 | ||
81 | /** | |
82 | * Call plugin hooks for header, footer and includes, specifying which page will be rendered. | |
83 | * Then assign generated data to RainTPL. | |
84 | */ | |
85 | protected function executeDefaultHooks(string $template): void | |
86 | { | |
87 | $common_hooks = [ | |
88 | 'includes', | |
89 | 'header', | |
90 | 'footer', | |
91 | ]; | |
92 | ||
93 | foreach ($common_hooks as $name) { | |
94 | $pluginData = []; | |
95 | $this->container->pluginManager->executeHooks( | |
96 | 'render_' . $name, | |
97 | $pluginData, | |
98 | [ | |
99 | 'target' => $template, | |
100 | 'loggedin' => $this->container->loginManager->isLoggedIn() | |
101 | ] | |
102 | ); | |
103 | $this->assignView('plugins_' . $name, $pluginData); | |
104 | } | |
105 | } | |
106 | ||
107 | /** | |
108 | * Simple helper which prepend the base path to redirect path. | |
109 | * | |
110 | * @param Response $response | |
111 | * @param string $path Absolute path, e.g.: `/`, or `/admin/shaare/123` regardless of install directory | |
112 | * | |
113 | * @return Response updated | |
114 | */ | |
115 | protected function redirect(Response $response, string $path): Response | |
116 | { | |
117 | return $response->withRedirect($this->container->basePath . $path); | |
118 | } | |
119 | ||
120 | /** | |
121 | * Generates a redirection to the previous page, based on the HTTP_REFERER. | |
122 | * It fails back to the home page. | |
123 | * | |
124 | * @param array $loopTerms Terms to remove from path and query string to prevent direction loop. | |
125 | * @param array $clearParams List of parameter to remove from the query string of the referrer. | |
126 | */ | |
127 | protected function redirectFromReferer( | |
128 | Request $request, | |
129 | Response $response, | |
130 | array $loopTerms = [], | |
131 | array $clearParams = [], | |
132 | string $anchor = null | |
133 | ): Response { | |
134 | $defaultPath = $this->container->basePath . '/'; | |
135 | $referer = $this->container->environment['HTTP_REFERER'] ?? null; | |
136 | ||
137 | if (null !== $referer) { | |
138 | $currentUrl = parse_url($referer); | |
139 | parse_str($currentUrl['query'] ?? '', $params); | |
140 | $path = $currentUrl['path'] ?? $defaultPath; | |
141 | } else { | |
142 | $params = []; | |
143 | $path = $defaultPath; | |
144 | } | |
145 | ||
146 | // Prevent redirection loop | |
147 | if (isset($currentUrl)) { | |
148 | foreach ($clearParams as $value) { | |
149 | unset($params[$value]); | |
150 | } | |
151 | ||
152 | $checkQuery = implode('', array_keys($params)); | |
153 | foreach ($loopTerms as $value) { | |
154 | if (strpos($path . $checkQuery, $value) !== false) { | |
155 | $params = []; | |
156 | $path = $defaultPath; | |
157 | break; | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
162 | $queryString = count($params) > 0 ? '?'. http_build_query($params) : ''; | |
163 | $anchor = $anchor ? '#' . $anchor : ''; | |
164 | ||
165 | return $response->withRedirect($path . $queryString . $anchor); | |
166 | } | |
167 | } |