]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/front/controllers/ShaarliController.php
Process session filters through Slim controllers
[github/shaarli/Shaarli.git] / application / front / controllers / ShaarliController.php
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 }