aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/front/controllers
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-22 13:20:31 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit2899ebb5b5e82890c877151f5c02045266ac9973 (patch)
tree0c4e2684c7f6d161f92a21181bfa4b2f78d6a82f /application/front/controllers
parentaf290059d10319e76d1e7d78b592cab99c26d91a (diff)
downloadShaarli-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')
-rw-r--r--application/front/controllers/DailyController.php210
-rw-r--r--application/front/controllers/FeedController.php79
-rw-r--r--application/front/controllers/LoginController.php48
-rw-r--r--application/front/controllers/LogoutController.php31
-rw-r--r--application/front/controllers/OpenSearchController.php28
-rw-r--r--application/front/controllers/PictureWallController.php72
-rw-r--r--application/front/controllers/SessionFilterController.php81
-rw-r--r--application/front/controllers/ShaarliController.php126
-rw-r--r--application/front/controllers/TagCloudController.php133
-rw-r--r--application/front/controllers/TagController.php120
10 files changed, 0 insertions, 928 deletions
diff --git a/application/front/controllers/DailyController.php b/application/front/controllers/DailyController.php
deleted file mode 100644
index 4a0735aa..00000000
--- a/application/front/controllers/DailyController.php
+++ /dev/null
@@ -1,210 +0,0 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use DateTime;
8use DateTimeImmutable;
9use Shaarli\Bookmark\Bookmark;
10use Slim\Http\Request;
11use Slim\Http\Response;
12
13/**
14 * Class DailyController
15 *
16 * Slim controller used to render the daily page.
17 *
18 * @package Front\Controller
19 */
20class DailyController extends ShaarliController
21{
22 public static $DAILY_RSS_NB_DAYS = 8;
23
24 /**
25 * Controller displaying all bookmarks published in a single day.
26 * It take a `day` date query parameter (format YYYYMMDD).
27 */
28 public function index(Request $request, Response $response): Response
29 {
30 $day = $request->getQueryParam('day') ?? date('Ymd');
31
32 $availableDates = $this->container->bookmarkService->days();
33 $nbAvailableDates = count($availableDates);
34 $index = array_search($day, $availableDates);
35
36 if ($index === false) {
37 // no bookmarks for day, but at least one day with bookmarks
38 $day = $availableDates[$nbAvailableDates - 1] ?? $day;
39 $previousDay = $availableDates[$nbAvailableDates - 2] ?? '';
40 } else {
41 $previousDay = $availableDates[$index - 1] ?? '';
42 $nextDay = $availableDates[$index + 1] ?? '';
43 }
44
45 if ($day === date('Ymd')) {
46 $this->assignView('dayDesc', t('Today'));
47 } elseif ($day === date('Ymd', strtotime('-1 days'))) {
48 $this->assignView('dayDesc', t('Yesterday'));
49 }
50
51 try {
52 $linksToDisplay = $this->container->bookmarkService->filterDay($day);
53 } catch (\Exception $exc) {
54 $linksToDisplay = [];
55 }
56
57 $formatter = $this->container->formatterFactory->getFormatter();
58 // We pre-format some fields for proper output.
59 foreach ($linksToDisplay as $key => $bookmark) {
60 $linksToDisplay[$key] = $formatter->format($bookmark);
61 // This page is a bit specific, we need raw description to calculate the length
62 $linksToDisplay[$key]['formatedDescription'] = $linksToDisplay[$key]['description'];
63 $linksToDisplay[$key]['description'] = $bookmark->getDescription();
64 }
65
66 $dayDate = DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000');
67 $data = [
68 'linksToDisplay' => $linksToDisplay,
69 'day' => $dayDate->getTimestamp(),
70 'dayDate' => $dayDate,
71 'previousday' => $previousDay ?? '',
72 'nextday' => $nextDay ?? '',
73 ];
74
75 // Hooks are called before column construction so that plugins don't have to deal with columns.
76 $this->executeHooks($data);
77
78 $data['cols'] = $this->calculateColumns($data['linksToDisplay']);
79
80 foreach ($data as $key => $value) {
81 $this->assignView($key, $value);
82 }
83
84 $mainTitle = $this->container->conf->get('general.title', 'Shaarli');
85 $this->assignView(
86 'pagetitle',
87 t('Daily') .' - '. format_date($dayDate, false) . ' - ' . $mainTitle
88 );
89
90 return $response->write($this->render('daily'));
91 }
92
93 /**
94 * Daily RSS feed: 1 RSS entry per day giving all the bookmarks on that day.
95 * Gives the last 7 days (which have bookmarks).
96 * This RSS feed cannot be filtered and does not trigger plugins yet.
97 */
98 public function rss(Request $request, Response $response): Response
99 {
100 $response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8');
101
102 $pageUrl = page_url($this->container->environment);
103 $cache = $this->container->pageCacheManager->getCachePage($pageUrl);
104
105 $cached = $cache->cachedVersion();
106 if (!empty($cached)) {
107 return $response->write($cached);
108 }
109
110 $days = [];
111 foreach ($this->container->bookmarkService->search() as $bookmark) {
112 $day = $bookmark->getCreated()->format('Ymd');
113
114 // Stop iterating after DAILY_RSS_NB_DAYS entries
115 if (count($days) === static::$DAILY_RSS_NB_DAYS && !isset($days[$day])) {
116 break;
117 }
118
119 $days[$day][] = $bookmark;
120 }
121
122 // Build the RSS feed.
123 $indexUrl = escape(index_url($this->container->environment));
124
125 $formatter = $this->container->formatterFactory->getFormatter();
126 $formatter->addContextData('index_url', $indexUrl);
127
128 $dataPerDay = [];
129
130 /** @var Bookmark[] $bookmarks */
131 foreach ($days as $day => $bookmarks) {
132 $dayDatetime = DateTimeImmutable::createFromFormat(Bookmark::LINK_DATE_FORMAT, $day.'_000000');
133 $dataPerDay[$day] = [
134 'date' => $dayDatetime,
135 'date_rss' => $dayDatetime->format(DateTime::RSS),
136 'date_human' => format_date($dayDatetime, false, true),
137 'absolute_url' => $indexUrl . '/daily?day=' . $day,
138 'links' => [],
139 ];
140
141 foreach ($bookmarks as $key => $bookmark) {
142 $dataPerDay[$day]['links'][$key] = $formatter->format($bookmark);
143
144 // Make permalink URL absolute
145 if ($bookmark->isNote()) {
146 $dataPerDay[$day]['links'][$key]['url'] = $indexUrl . $bookmark->getUrl();
147 }
148 }
149 }
150
151 $this->assignView('title', $this->container->conf->get('general.title', 'Shaarli'));
152 $this->assignView('index_url', $indexUrl);
153 $this->assignView('page_url', $pageUrl);
154 $this->assignView('hide_timestamps', $this->container->conf->get('privacy.hide_timestamps', false));
155 $this->assignView('days', $dataPerDay);
156
157 $rssContent = $this->render('dailyrss');
158
159 $cache->cache($rssContent);
160
161 return $response->write($rssContent);
162 }
163
164 /**
165 * We need to spread the articles on 3 columns.
166 * did not want to use a JavaScript lib like http://masonry.desandro.com/
167 * so I manually spread entries with a simple method: I roughly evaluate the
168 * height of a div according to title and description length.
169 */
170 protected function calculateColumns(array $links): array
171 {
172 // Entries to display, for each column.
173 $columns = [[], [], []];
174 // Rough estimate of columns fill.
175 $fill = [0, 0, 0];
176 foreach ($links as $link) {
177 // Roughly estimate length of entry (by counting characters)
178 // Title: 30 chars = 1 line. 1 line is 30 pixels height.
179 // Description: 836 characters gives roughly 342 pixel height.
180 // This is not perfect, but it's usually OK.
181 $length = strlen($link['title'] ?? '') + (342 * strlen($link['description'] ?? '')) / 836;
182 if (! empty($link['thumbnail'])) {
183 $length += 100; // 1 thumbnails roughly takes 100 pixels height.
184 }
185 // Then put in column which is the less filled:
186 $smallest = min($fill); // find smallest value in array.
187 $index = array_search($smallest, $fill); // find index of this smallest value.
188 array_push($columns[$index], $link); // Put entry in this column.
189 $fill[$index] += $length;
190 }
191
192 return $columns;
193 }
194
195 /**
196 * @param mixed[] $data Variables passed to the template engine
197 *
198 * @return mixed[] Template data after active plugins render_picwall hook execution.
199 */
200 protected function executeHooks(array $data): array
201 {
202 $this->container->pluginManager->executeHooks(
203 'render_daily',
204 $data,
205 ['loggedin' => $this->container->loginManager->isLoggedIn()]
206 );
207
208 return $data;
209 }
210}
diff --git a/application/front/controllers/FeedController.php b/application/front/controllers/FeedController.php
deleted file mode 100644
index 78d826d9..00000000
--- a/application/front/controllers/FeedController.php
+++ /dev/null
@@ -1,79 +0,0 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Shaarli\Feed\FeedBuilder;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Class FeedController
13 *
14 * Slim controller handling ATOM and RSS feed.
15 *
16 * @package Front\Controller
17 */
18class FeedController extends ShaarliController
19{
20 public function atom(Request $request, Response $response): Response
21 {
22 return $this->processRequest(FeedBuilder::$FEED_ATOM, $request, $response);
23 }
24
25 public function rss(Request $request, Response $response): Response
26 {
27 return $this->processRequest(FeedBuilder::$FEED_RSS, $request, $response);
28 }
29
30 protected function processRequest(string $feedType, Request $request, Response $response): Response
31 {
32 $response = $response->withHeader('Content-Type', 'application/'. $feedType .'+xml; charset=utf-8');
33
34 $pageUrl = page_url($this->container->environment);
35 $cache = $this->container->pageCacheManager->getCachePage($pageUrl);
36
37 $cached = $cache->cachedVersion();
38 if (!empty($cached)) {
39 return $response->write($cached);
40 }
41
42 // Generate data.
43 $this->container->feedBuilder->setLocale(strtolower(setlocale(LC_COLLATE, 0)));
44 $this->container->feedBuilder->setHideDates($this->container->conf->get('privacy.hide_timestamps', false));
45 $this->container->feedBuilder->setUsePermalinks(
46 null !== $request->getParam('permalinks') || !$this->container->conf->get('feed.rss_permalinks')
47 );
48
49 $data = $this->container->feedBuilder->buildData($feedType, $request->getParams());
50
51 $this->executeHooks($data, $feedType);
52 $this->assignAllView($data);
53
54 $content = $this->render('feed.'. $feedType);
55
56 $cache->cache($content);
57
58 return $response->write($content);
59 }
60
61 /**
62 * @param mixed[] $data Template data
63 *
64 * @return mixed[] Template data after active plugins hook execution.
65 */
66 protected function executeHooks(array $data, string $feedType): array
67 {
68 $this->container->pluginManager->executeHooks(
69 'render_feed',
70 $data,
71 [
72 'loggedin' => $this->container->loginManager->isLoggedIn(),
73 'target' => $feedType,
74 ]
75 );
76
77 return $data;
78 }
79}
diff --git a/application/front/controllers/LoginController.php b/application/front/controllers/LoginController.php
deleted file mode 100644
index ae3599e0..00000000
--- a/application/front/controllers/LoginController.php
+++ /dev/null
@@ -1,48 +0,0 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Shaarli\Front\Exception\LoginBannedException;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Class LoginController
13 *
14 * Slim controller used to render the login page.
15 *
16 * The login page is not available if the user is banned
17 * or if open shaarli setting is enabled.
18 *
19 * @package Front\Controller
20 */
21class LoginController extends ShaarliController
22{
23 public function index(Request $request, Response $response): Response
24 {
25 if ($this->container->loginManager->isLoggedIn()
26 || $this->container->conf->get('security.open_shaarli', false)
27 ) {
28 return $response->withRedirect('./');
29 }
30
31 $userCanLogin = $this->container->loginManager->canLogin($request->getServerParams());
32 if ($userCanLogin !== true) {
33 throw new LoginBannedException();
34 }
35
36 if ($request->getParam('username') !== null) {
37 $this->assignView('username', escape($request->getParam('username')));
38 }
39
40 $this
41 ->assignView('returnurl', escape($request->getServerParam('HTTP_REFERER')))
42 ->assignView('remember_user_default', $this->container->conf->get('privacy.remember_user_default', true))
43 ->assignView('pagetitle', t('Login') .' - '. $this->container->conf->get('general.title', 'Shaarli'))
44 ;
45
46 return $response->write($this->render('loginform'));
47 }
48}
diff --git a/application/front/controllers/LogoutController.php b/application/front/controllers/LogoutController.php
deleted file mode 100644
index aba078c3..00000000
--- a/application/front/controllers/LogoutController.php
+++ /dev/null
@@ -1,31 +0,0 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Shaarli\Security\LoginManager;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Class LogoutController
13 *
14 * Slim controller used to logout the user.
15 * It invalidates page cache and terminate the user session. Then it redirects to the homepage.
16 *
17 * @package Front\Controller
18 */
19class LogoutController extends ShaarliController
20{
21 public function index(Request $request, Response $response): Response
22 {
23 $this->container->pageCacheManager->invalidateCaches();
24 $this->container->sessionManager->logout();
25
26 // TODO: switch to a simple Cookie manager allowing to check the session, and create mocks.
27 setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, 'false', 0, $this->container->webPath);
28
29 return $response->withRedirect('./');
30 }
31}
diff --git a/application/front/controllers/OpenSearchController.php b/application/front/controllers/OpenSearchController.php
deleted file mode 100644
index fa32c5f1..00000000
--- a/application/front/controllers/OpenSearchController.php
+++ /dev/null
@@ -1,28 +0,0 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Slim\Http\Request;
8use Slim\Http\Response;
9
10/**
11 * Class OpenSearchController
12 *
13 * Slim controller used to render open search template.
14 * This allows to add Shaarli as a search engine within the browser.
15 *
16 * @package front\controllers
17 */
18class OpenSearchController extends ShaarliController
19{
20 public function index(Request $request, Response $response): Response
21 {
22 $response = $response->withHeader('Content-Type', 'application/opensearchdescription+xml; charset=utf-8');
23
24 $this->assignView('serverurl', index_url($this->container->environment));
25
26 return $response->write($this->render('opensearch'));
27 }
28}
diff --git a/application/front/controllers/PictureWallController.php b/application/front/controllers/PictureWallController.php
deleted file mode 100644
index 08d31b29..00000000
--- a/application/front/controllers/PictureWallController.php
+++ /dev/null
@@ -1,72 +0,0 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Shaarli\Front\Exception\ThumbnailsDisabledException;
8use Shaarli\Thumbnailer;
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12/**
13 * Class PicturesWallController
14 *
15 * Slim controller used to render the pictures wall page.
16 * If thumbnails mode is set to NONE, we just render the template without any image.
17 *
18 * @package Front\Controller
19 */
20class PictureWallController extends ShaarliController
21{
22 public function index(Request $request, Response $response): Response
23 {
24 if ($this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) === Thumbnailer::MODE_NONE) {
25 throw new ThumbnailsDisabledException();
26 }
27
28 $this->assignView(
29 'pagetitle',
30 t('Picture wall') .' - '. $this->container->conf->get('general.title', 'Shaarli')
31 );
32
33 // Optionally filter the results:
34 $links = $this->container->bookmarkService->search($request->getQueryParams());
35 $linksToDisplay = [];
36
37 // Get only bookmarks which have a thumbnail.
38 // Note: we do not retrieve thumbnails here, the request is too heavy.
39 $formatter = $this->container->formatterFactory->getFormatter('raw');
40 foreach ($links as $key => $link) {
41 if (!empty($link->getThumbnail())) {
42 $linksToDisplay[] = $formatter->format($link);
43 }
44 }
45
46 $data = $this->executeHooks($linksToDisplay);
47 foreach ($data as $key => $value) {
48 $this->assignView($key, $value);
49 }
50
51 return $response->write($this->render('picwall'));
52 }
53
54 /**
55 * @param mixed[] $linksToDisplay List of formatted bookmarks
56 *
57 * @return mixed[] Template data after active plugins render_picwall hook execution.
58 */
59 protected function executeHooks(array $linksToDisplay): array
60 {
61 $data = [
62 'linksToDisplay' => $linksToDisplay,
63 ];
64 $this->container->pluginManager->executeHooks(
65 'render_picwall',
66 $data,
67 ['loggedin' => $this->container->loginManager->isLoggedIn()]
68 );
69
70 return $data;
71 }
72}
diff --git a/application/front/controllers/SessionFilterController.php b/application/front/controllers/SessionFilterController.php
deleted file mode 100644
index a021dc37..00000000
--- a/application/front/controllers/SessionFilterController.php
+++ /dev/null
@@ -1,81 +0,0 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Shaarli\Bookmark\BookmarkFilter;
8use Shaarli\Security\SessionManager;
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12/**
13 * Class SessionFilterController
14 *
15 * Slim controller used to handle filters stored in the user session, such as visibility, links per page, etc.
16 *
17 * @package Shaarli\Front\Controller
18 */
19class SessionFilterController extends ShaarliController
20{
21 /**
22 * GET /links-per-page: set the number of bookmarks to display per page in homepage
23 */
24 public function linksPerPage(Request $request, Response $response): Response
25 {
26 $linksPerPage = $request->getParam('nb') ?? null;
27 if (null === $linksPerPage || false === is_numeric($linksPerPage)) {
28 $linksPerPage = $this->container->conf->get('general.links_per_page', 20);
29 }
30
31 $this->container->sessionManager->setSessionParameter(
32 SessionManager::KEY_LINKS_PER_PAGE,
33 abs(intval($linksPerPage))
34 );
35
36 return $this->redirectFromReferer($response, ['linksperpage'], ['nb']);
37 }
38
39 /**
40 * GET /visibility: allows to display only public or only private bookmarks in linklist
41 */
42 public function visibility(Request $request, Response $response, array $args): Response
43 {
44 if (false === $this->container->loginManager->isLoggedIn()) {
45 return $this->redirectFromReferer($response, ['visibility']);
46 }
47
48 $newVisibility = $args['visibility'] ?? null;
49 if (false === in_array($newVisibility, [BookmarkFilter::$PRIVATE, BookmarkFilter::$PUBLIC], true)) {
50 $newVisibility = null;
51 }
52
53 $currentVisibility = $this->container->sessionManager->getSessionParameter(SessionManager::KEY_VISIBILITY);
54
55 // Visibility not set or not already expected value, set expected value, otherwise reset it
56 if ($newVisibility !== null && (null === $currentVisibility || $currentVisibility !== $newVisibility)) {
57 // See only public bookmarks
58 $this->container->sessionManager->setSessionParameter(
59 SessionManager::KEY_VISIBILITY,
60 $newVisibility
61 );
62 } else {
63 $this->container->sessionManager->deleteSessionParameter(SessionManager::KEY_VISIBILITY);
64 }
65
66 return $this->redirectFromReferer($response, ['visibility']);
67 }
68
69 /**
70 * GET /untagged-only: allows to display only bookmarks without any tag
71 */
72 public function untaggedOnly(Request $request, Response $response): Response
73 {
74 $this->container->sessionManager->setSessionParameter(
75 SessionManager::KEY_UNTAGGED_ONLY,
76 empty($this->container->sessionManager->getSessionParameter(SessionManager::KEY_UNTAGGED_ONLY))
77 );
78
79 return $this->redirectFromReferer($response, ['untaggedonly', 'untagged-only']);
80 }
81}
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
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Shaarli\Bookmark\BookmarkFilter;
8use Shaarli\Container\ShaarliContainer;
9use Slim\Http\Response;
10
11abstract 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}
diff --git a/application/front/controllers/TagCloudController.php b/application/front/controllers/TagCloudController.php
deleted file mode 100644
index 1ff7c2e6..00000000
--- a/application/front/controllers/TagCloudController.php
+++ /dev/null
@@ -1,133 +0,0 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Slim\Http\Request;
8use Slim\Http\Response;
9
10/**
11 * Class TagCloud
12 *
13 * Slim controller used to render the tag cloud and tag list pages.
14 *
15 * @package Front\Controller
16 */
17class TagCloudController extends ShaarliController
18{
19 protected const TYPE_CLOUD = 'cloud';
20 protected const TYPE_LIST = 'list';
21
22 /**
23 * Display the tag cloud through the template engine.
24 * This controller a few filters:
25 * - Visibility stored in the session for logged in users
26 * - `searchtags` query parameter: will return tags associated with filter in at least one bookmark
27 */
28 public function cloud(Request $request, Response $response): Response
29 {
30 return $this->processRequest(static::TYPE_CLOUD, $request, $response);
31 }
32
33 /**
34 * Display the tag list through the template engine.
35 * This controller a few filters:
36 * - Visibility stored in the session for logged in users
37 * - `searchtags` query parameter: will return tags associated with filter in at least one bookmark
38 * - `sort` query parameters:
39 * + `usage` (default): most used tags first
40 * + `alpha`: alphabetical order
41 */
42 public function list(Request $request, Response $response): Response
43 {
44 return $this->processRequest(static::TYPE_LIST, $request, $response);
45 }
46
47 /**
48 * Process the request for both tag cloud and tag list endpoints.
49 */
50 protected function processRequest(string $type, Request $request, Response $response): Response
51 {
52 if ($this->container->loginManager->isLoggedIn() === true) {
53 $visibility = $this->container->sessionManager->getSessionParameter('visibility');
54 }
55
56 $sort = $request->getQueryParam('sort');
57 $searchTags = $request->getQueryParam('searchtags');
58 $filteringTags = $searchTags !== null ? explode(' ', $searchTags) : [];
59
60 $tags = $this->container->bookmarkService->bookmarksCountPerTag($filteringTags, $visibility ?? null);
61
62 if (static::TYPE_CLOUD === $type || 'alpha' === $sort) {
63 // TODO: the sorting should be handled by bookmarkService instead of the controller
64 alphabetical_sort($tags, false, true);
65 }
66
67 if (static::TYPE_CLOUD === $type) {
68 $tags = $this->formatTagsForCloud($tags);
69 }
70
71 $searchTags = implode(' ', escape($filteringTags));
72 $data = [
73 'search_tags' => $searchTags,
74 'tags' => $tags,
75 ];
76 $data = $this->executeHooks('tag' . $type, $data);
77 foreach ($data as $key => $value) {
78 $this->assignView($key, $value);
79 }
80
81 $searchTags = !empty($searchTags) ? $searchTags .' - ' : '';
82 $this->assignView(
83 'pagetitle',
84 $searchTags . t('Tag '. $type) .' - '. $this->container->conf->get('general.title', 'Shaarli')
85 );
86
87 return $response->write($this->render('tag.'. $type));
88 }
89
90 /**
91 * Format the tags array for the tag cloud template.
92 *
93 * @param array<string, int> $tags List of tags as key with count as value
94 *
95 * @return mixed[] List of tags as key, with count and expected font size in a subarray
96 */
97 protected function formatTagsForCloud(array $tags): array
98 {
99 // We sort tags alphabetically, then choose a font size according to count.
100 // First, find max value.
101 $maxCount = count($tags) > 0 ? max($tags) : 0;
102 $logMaxCount = $maxCount > 1 ? log($maxCount, 30) : 1;
103 $tagList = [];
104 foreach ($tags as $key => $value) {
105 // Tag font size scaling:
106 // default 15 and 30 logarithm bases affect scaling,
107 // 2.2 and 0.8 are arbitrary font sizes in em.
108 $size = log($value, 15) / $logMaxCount * 2.2 + 0.8;
109 $tagList[$key] = [
110 'count' => $value,
111 'size' => number_format($size, 2, '.', ''),
112 ];
113 }
114
115 return $tagList;
116 }
117
118 /**
119 * @param mixed[] $data Template data
120 *
121 * @return mixed[] Template data after active plugins hook execution.
122 */
123 protected function executeHooks(string $template, array $data): array
124 {
125 $this->container->pluginManager->executeHooks(
126 'render_'. $template,
127 $data,
128 ['loggedin' => $this->container->loginManager->isLoggedIn()]
129 );
130
131 return $data;
132 }
133}
diff --git a/application/front/controllers/TagController.php b/application/front/controllers/TagController.php
deleted file mode 100644
index a1d5ad5b..00000000
--- a/application/front/controllers/TagController.php
+++ /dev/null
@@ -1,120 +0,0 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller;
6
7use Slim\Http\Request;
8use Slim\Http\Response;
9
10/**
11 * Class TagController
12 *
13 * Slim controller handle tags.
14 *
15 * @package Front\Controller
16 */
17class TagController extends ShaarliController
18{
19 /**
20 * Add another tag in the current search through an HTTP redirection.
21 *
22 * @param array $args Should contain `newTag` key as tag to add to current search
23 */
24 public function addTag(Request $request, Response $response, array $args): Response
25 {
26 $newTag = $args['newTag'] ?? null;
27 $referer = $this->container->environment['HTTP_REFERER'] ?? null;
28
29 // In case browser does not send HTTP_REFERER, we search a single tag
30 if (null === $referer) {
31 if (null !== $newTag) {
32 return $response->withRedirect('./?searchtags='. urlencode($newTag));
33 }
34
35 return $response->withRedirect('./');
36 }
37
38 $currentUrl = parse_url($referer);
39 parse_str($currentUrl['query'] ?? '', $params);
40
41 if (null === $newTag) {
42 return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params));
43 }
44
45 // Prevent redirection loop
46 if (isset($params['addtag'])) {
47 unset($params['addtag']);
48 }
49
50 // Check if this tag is already in the search query and ignore it if it is.
51 // Each tag is always separated by a space
52 $currentTags = isset($params['searchtags']) ? explode(' ', $params['searchtags']) : [];
53
54 $addtag = true;
55 foreach ($currentTags as $value) {
56 if ($value === $newTag) {
57 $addtag = false;
58 break;
59 }
60 }
61
62 // Append the tag if necessary
63 if (true === $addtag) {
64 $currentTags[] = trim($newTag);
65 }
66
67 $params['searchtags'] = trim(implode(' ', $currentTags));
68
69 // We also remove page (keeping the same page has no sense, since the results are different)
70 unset($params['page']);
71
72 return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params));
73 }
74
75 /**
76 * Remove a tag from the current search through an HTTP redirection.
77 *
78 * @param array $args Should contain `tag` key as tag to remove from current search
79 */
80 public function removeTag(Request $request, Response $response, array $args): Response
81 {
82 $referer = $this->container->environment['HTTP_REFERER'] ?? null;
83
84 // If the referrer is not provided, we can update the search, so we failback on the bookmark list
85 if (empty($referer)) {
86 return $response->withRedirect('./');
87 }
88
89 $tagToRemove = $args['tag'] ?? null;
90 $currentUrl = parse_url($referer);
91 parse_str($currentUrl['query'] ?? '', $params);
92
93 if (null === $tagToRemove) {
94 return $response->withRedirect(($currentUrl['path'] ?? './') .'?'. http_build_query($params));
95 }
96
97 // Prevent redirection loop
98 if (isset($params['removetag'])) {
99 unset($params['removetag']);
100 }
101
102 if (isset($params['searchtags'])) {
103 $tags = explode(' ', $params['searchtags']);
104 // Remove value from array $tags.
105 $tags = array_diff($tags, [$tagToRemove]);
106 $params['searchtags'] = implode(' ', $tags);
107
108 if (empty($params['searchtags'])) {
109 unset($params['searchtags']);
110 }
111
112 // We also remove page (keeping the same page has no sense, since the results are different)
113 unset($params['page']);
114 }
115
116 $queryParams = count($params) > 0 ? '?' . http_build_query($params) : '';
117
118 return $response->withRedirect(($currentUrl['path'] ?? './') . $queryParams);
119 }
120}