]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/front/controller/visitor/ShaarliVisitorController.php
Merge pull request #1511 from ArthurHoaro/wip-slim-routing
[github/shaarli/Shaarli.git] / application / front / controller / visitor / ShaarliVisitorController.php
CommitLineData
6c50a6cc
A
1<?php
2
3declare(strict_types=1);
4
2899ebb5 5namespace Shaarli\Front\Controller\Visitor;
6c50a6cc 6
0498b209 7use Shaarli\Bookmark\BookmarkFilter;
6c50a6cc 8use Shaarli\Container\ShaarliContainer;
2899ebb5 9use Slim\Http\Request;
af290059 10use Slim\Http\Response;
6c50a6cc 11
ef00f9d2
A
12/**
13 * Class ShaarliVisitorController
14 *
15 * All controllers accessible by visitors (non logged in users) should extend this abstract class.
16 * Contains a few helper function for template rendering, plugins, etc.
17 *
18 * @package Shaarli\Front\Controller\Visitor
19 */
2899ebb5 20abstract class ShaarliVisitorController
6c50a6cc
A
21{
22 /** @var ShaarliContainer */
27ceea2a 23 protected $container;
6c50a6cc 24
27ceea2a
A
25 /** @param ShaarliContainer $container Slim container (extended for attribute completion). */
26 public function __construct(ShaarliContainer $container)
6c50a6cc 27 {
27ceea2a 28 $this->container = $container;
6c50a6cc
A
29 }
30
31 /**
32 * Assign variables to RainTPL template through the PageBuilder.
33 *
34 * @param mixed $value Value to assign to the template
35 */
36 protected function assignView(string $name, $value): self
37 {
27ceea2a 38 $this->container->pageBuilder->assign($name, $value);
6c50a6cc
A
39
40 return $this;
41 }
0498b209 42
7b2ba6ef
A
43 /**
44 * Assign variables to RainTPL template through the PageBuilder.
45 *
46 * @param mixed $data Values to assign to the template and their keys
47 */
48 protected function assignAllView(array $data): self
49 {
50 foreach ($data as $key => $value) {
51 $this->assignView($key, $value);
52 }
53
54 return $this;
55 }
56
0498b209
A
57 protected function render(string $template): string
58 {
27ceea2a
A
59 $this->assignView('linkcount', $this->container->bookmarkService->count(BookmarkFilter::$ALL));
60 $this->assignView('privateLinkcount', $this->container->bookmarkService->count(BookmarkFilter::$PRIVATE));
61 $this->assignView('plugin_errors', $this->container->pluginManager->getErrors());
0498b209
A
62
63 $this->executeDefaultHooks($template);
64
9fbc4229 65 return $this->container->pageBuilder->render($template, $this->container->basePath);
0498b209
A
66 }
67
68 /**
69 * Call plugin hooks for header, footer and includes, specifying which page will be rendered.
70 * Then assign generated data to RainTPL.
71 */
72 protected function executeDefaultHooks(string $template): void
73 {
74 $common_hooks = [
75 'includes',
76 'header',
77 'footer',
78 ];
79
80 foreach ($common_hooks as $name) {
c22fa57a 81 $pluginData = [];
27ceea2a 82 $this->container->pluginManager->executeHooks(
0498b209 83 'render_' . $name,
c22fa57a 84 $pluginData,
0498b209
A
85 [
86 'target' => $template,
9fbc4229
A
87 'loggedin' => $this->container->loginManager->isLoggedIn(),
88 'basePath' => $this->container->basePath,
0498b209
A
89 ]
90 );
c22fa57a 91 $this->assignView('plugins_' . $name, $pluginData);
0498b209
A
92 }
93 }
af290059 94
9fbc4229
A
95 protected function executePageHooks(string $hook, array &$data, string $template = null): void
96 {
97 $params = [
98 'target' => $template,
99 'loggedin' => $this->container->loginManager->isLoggedIn(),
100 'basePath' => $this->container->basePath,
101 ];
102
103 $this->container->pluginManager->executeHooks(
104 $hook,
105 $data,
106 $params
107 );
108 }
109
9c75f877
A
110 /**
111 * Simple helper which prepend the base path to redirect path.
112 *
113 * @param Response $response
114 * @param string $path Absolute path, e.g.: `/`, or `/admin/shaare/123` regardless of install directory
115 *
116 * @return Response updated
117 */
118 protected function redirect(Response $response, string $path): Response
119 {
120 return $response->withRedirect($this->container->basePath . $path);
121 }
122
af290059
A
123 /**
124 * Generates a redirection to the previous page, based on the HTTP_REFERER.
125 * It fails back to the home page.
126 *
127 * @param array $loopTerms Terms to remove from path and query string to prevent direction loop.
128 * @param array $clearParams List of parameter to remove from the query string of the referrer.
129 */
2899ebb5
A
130 protected function redirectFromReferer(
131 Request $request,
132 Response $response,
133 array $loopTerms = [],
c22fa57a
A
134 array $clearParams = [],
135 string $anchor = null
2899ebb5 136 ): Response {
818b3193 137 $defaultPath = $this->container->basePath . '/';
af290059
A
138 $referer = $this->container->environment['HTTP_REFERER'] ?? null;
139
140 if (null !== $referer) {
141 $currentUrl = parse_url($referer);
142 parse_str($currentUrl['query'] ?? '', $params);
143 $path = $currentUrl['path'] ?? $defaultPath;
144 } else {
145 $params = [];
146 $path = $defaultPath;
147 }
148
149 // Prevent redirection loop
150 if (isset($currentUrl)) {
151 foreach ($clearParams as $value) {
152 unset($params[$value]);
153 }
154
155 $checkQuery = implode('', array_keys($params));
156 foreach ($loopTerms as $value) {
157 if (strpos($path . $checkQuery, $value) !== false) {
158 $params = [];
159 $path = $defaultPath;
160 break;
161 }
162 }
163 }
164
165 $queryString = count($params) > 0 ? '?'. http_build_query($params) : '';
c22fa57a 166 $anchor = $anchor ? '#' . $anchor : '';
af290059 167
c22fa57a 168 return $response->withRedirect($path . $queryString . $anchor);
af290059 169 }
6c50a6cc 170}