diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-05-22 13:20:31 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 2899ebb5b5e82890c877151f5c02045266ac9973 (patch) | |
tree | 0c4e2684c7f6d161f92a21181bfa4b2f78d6a82f /application/front/controller/visitor/ShaarliVisitorController.php | |
parent | af290059d10319e76d1e7d78b592cab99c26d91a (diff) | |
download | Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.tar.gz Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.tar.zst Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.zip |
Initialize admin Slim controllers
- Reorganize visitor controllers
- Fix redirection with Slim's requests base path
- Fix daily links
Diffstat (limited to 'application/front/controller/visitor/ShaarliVisitorController.php')
-rw-r--r-- | application/front/controller/visitor/ShaarliVisitorController.php | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/application/front/controller/visitor/ShaarliVisitorController.php b/application/front/controller/visitor/ShaarliVisitorController.php new file mode 100644 index 00000000..655b3baa --- /dev/null +++ b/application/front/controller/visitor/ShaarliVisitorController.php | |||
@@ -0,0 +1,131 @@ | |||
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 | abstract class ShaarliVisitorController | ||
13 | { | ||
14 | /** @var ShaarliContainer */ | ||
15 | protected $container; | ||
16 | |||
17 | /** @param ShaarliContainer $container Slim container (extended for attribute completion). */ | ||
18 | public function __construct(ShaarliContainer $container) | ||
19 | { | ||
20 | $this->container = $container; | ||
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 | { | ||
30 | $this->container->pageBuilder->assign($name, $value); | ||
31 | |||
32 | return $this; | ||
33 | } | ||
34 | |||
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 | |||
49 | protected function render(string $template): string | ||
50 | { | ||
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()); | ||
54 | |||
55 | $this->executeDefaultHooks($template); | ||
56 | |||
57 | return $this->container->pageBuilder->render($template); | ||
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 = []; | ||
74 | $this->container->pluginManager->executeHooks( | ||
75 | 'render_' . $name, | ||
76 | $plugin_data, | ||
77 | [ | ||
78 | 'target' => $template, | ||
79 | 'loggedin' => $this->container->loginManager->isLoggedIn() | ||
80 | ] | ||
81 | ); | ||
82 | $this->assignView('plugins_' . $name, $plugin_data); | ||
83 | } | ||
84 | } | ||
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 | */ | ||
93 | protected function redirectFromReferer( | ||
94 | Request $request, | ||
95 | Response $response, | ||
96 | array $loopTerms = [], | ||
97 | array $clearParams = [] | ||
98 | ): Response { | ||
99 | $defaultPath = $request->getUri()->getBasePath(); | ||
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 | } | ||
131 | } | ||