]>
Commit | Line | Data |
---|---|---|
6c50a6cc A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller; | |
6 | ||
0498b209 | 7 | use Shaarli\Bookmark\BookmarkFilter; |
6c50a6cc | 8 | use Shaarli\Container\ShaarliContainer; |
af290059 | 9 | use Slim\Http\Response; |
6c50a6cc A |
10 | |
11 | abstract class ShaarliController | |
12 | { | |
13 | /** @var ShaarliContainer */ | |
27ceea2a | 14 | protected $container; |
6c50a6cc | 15 | |
27ceea2a A |
16 | /** @param ShaarliContainer $container Slim container (extended for attribute completion). */ |
17 | public function __construct(ShaarliContainer $container) | |
6c50a6cc | 18 | { |
27ceea2a | 19 | $this->container = $container; |
6c50a6cc A |
20 | } |
21 | ||
22 | /** | |
23 | * Assign variables to RainTPL template through the PageBuilder. | |
24 | * | |
25 | * @param mixed $value Value to assign to the template | |
26 | */ | |
27 | protected function assignView(string $name, $value): self | |
28 | { | |
27ceea2a | 29 | $this->container->pageBuilder->assign($name, $value); |
6c50a6cc A |
30 | |
31 | return $this; | |
32 | } | |
0498b209 | 33 | |
7b2ba6ef A |
34 | /** |
35 | * Assign variables to RainTPL template through the PageBuilder. | |
36 | * | |
37 | * @param mixed $data Values to assign to the template and their keys | |
38 | */ | |
39 | protected function assignAllView(array $data): self | |
40 | { | |
41 | foreach ($data as $key => $value) { | |
42 | $this->assignView($key, $value); | |
43 | } | |
44 | ||
45 | return $this; | |
46 | } | |
47 | ||
0498b209 A |
48 | protected function render(string $template): string |
49 | { | |
27ceea2a A |
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()); | |
0498b209 A |
53 | |
54 | $this->executeDefaultHooks($template); | |
55 | ||
27ceea2a | 56 | return $this->container->pageBuilder->render($template); |
0498b209 A |
57 | } |
58 | ||
59 | /** | |
60 | * Call plugin hooks for header, footer and includes, specifying which page will be rendered. | |
61 | * Then assign generated data to RainTPL. | |
62 | */ | |
63 | protected function executeDefaultHooks(string $template): void | |
64 | { | |
65 | $common_hooks = [ | |
66 | 'includes', | |
67 | 'header', | |
68 | 'footer', | |
69 | ]; | |
70 | ||
71 | foreach ($common_hooks as $name) { | |
72 | $plugin_data = []; | |
27ceea2a | 73 | $this->container->pluginManager->executeHooks( |
0498b209 A |
74 | 'render_' . $name, |
75 | $plugin_data, | |
76 | [ | |
77 | 'target' => $template, | |
27ceea2a | 78 | 'loggedin' => $this->container->loginManager->isLoggedIn() |
0498b209 A |
79 | ] |
80 | ); | |
81 | $this->assignView('plugins_' . $name, $plugin_data); | |
82 | } | |
83 | } | |
af290059 A |
84 | |
85 | /** | |
86 | * Generates a redirection to the previous page, based on the HTTP_REFERER. | |
87 | * It fails back to the home page. | |
88 | * | |
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. | |
91 | */ | |
92 | protected function redirectFromReferer(Response $response, array $loopTerms = [], array $clearParams = []): Response | |
93 | { | |
94 | $defaultPath = './'; | |
95 | $referer = $this->container->environment['HTTP_REFERER'] ?? null; | |
96 | ||
97 | if (null !== $referer) { | |
98 | $currentUrl = parse_url($referer); | |
99 | parse_str($currentUrl['query'] ?? '', $params); | |
100 | $path = $currentUrl['path'] ?? $defaultPath; | |
101 | } else { | |
102 | $params = []; | |
103 | $path = $defaultPath; | |
104 | } | |
105 | ||
106 | // Prevent redirection loop | |
107 | if (isset($currentUrl)) { | |
108 | foreach ($clearParams as $value) { | |
109 | unset($params[$value]); | |
110 | } | |
111 | ||
112 | $checkQuery = implode('', array_keys($params)); | |
113 | foreach ($loopTerms as $value) { | |
114 | if (strpos($path . $checkQuery, $value) !== false) { | |
115 | $params = []; | |
116 | $path = $defaultPath; | |
117 | break; | |
118 | } | |
119 | } | |
120 | } | |
121 | ||
122 | $queryString = count($params) > 0 ? '?'. http_build_query($params) : ''; | |
123 | ||
124 | return $response->withRedirect($path . $queryString); | |
125 | } | |
6c50a6cc | 126 | } |