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/controllers/ShaarliController.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/controllers/ShaarliController.php')
-rw-r--r-- | application/front/controllers/ShaarliController.php | 126 |
1 files changed, 0 insertions, 126 deletions
diff --git a/application/front/controllers/ShaarliController.php b/application/front/controllers/ShaarliController.php deleted file mode 100644 index bfff5fcf..00000000 --- a/application/front/controllers/ShaarliController.php +++ /dev/null | |||
@@ -1,126 +0,0 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller; | ||
6 | |||
7 | use Shaarli\Bookmark\BookmarkFilter; | ||
8 | use Shaarli\Container\ShaarliContainer; | ||
9 | use Slim\Http\Response; | ||
10 | |||
11 | abstract class ShaarliController | ||
12 | { | ||
13 | /** @var ShaarliContainer */ | ||
14 | protected $container; | ||
15 | |||
16 | /** @param ShaarliContainer $container Slim container (extended for attribute completion). */ | ||
17 | public function __construct(ShaarliContainer $container) | ||
18 | { | ||
19 | $this->container = $container; | ||
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 | { | ||
29 | $this->container->pageBuilder->assign($name, $value); | ||
30 | |||
31 | return $this; | ||
32 | } | ||
33 | |||
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 | |||
48 | protected function render(string $template): string | ||
49 | { | ||
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()); | ||
53 | |||
54 | $this->executeDefaultHooks($template); | ||
55 | |||
56 | return $this->container->pageBuilder->render($template); | ||
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 = []; | ||
73 | $this->container->pluginManager->executeHooks( | ||
74 | 'render_' . $name, | ||
75 | $plugin_data, | ||
76 | [ | ||
77 | 'target' => $template, | ||
78 | 'loggedin' => $this->container->loginManager->isLoggedIn() | ||
79 | ] | ||
80 | ); | ||
81 | $this->assignView('plugins_' . $name, $plugin_data); | ||
82 | } | ||
83 | } | ||
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 | } | ||
126 | } | ||