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