3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Visitor
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
9 use Shaarli\Legacy\LegacyController
;
10 use Shaarli\Legacy\UnknowLegacyRouteException
;
11 use Shaarli\Render\TemplatePage
;
12 use Shaarli\Thumbnailer
;
13 use Slim\Http\Request
;
14 use Slim\Http\Response
;
17 * Class BookmarkListController
19 * Slim controller used to render the bookmark list, the home page of Shaarli.
20 * It also displays permalinks, and process legacy routes based on GET parameters.
22 class BookmarkListController
extends ShaarliVisitorController
25 * GET / - Displays the bookmark list, with optional filter parameters.
27 public function index(Request
$request, Response
$response): Response
29 $legacyResponse = $this->processLegacyController($request, $response);
30 if (null !== $legacyResponse) {
31 return $legacyResponse;
34 $formatter = $this->container
->formatterFactory
->getFormatter();
35 $formatter->addContextData('base_path', $this->container
->basePath
);
37 $searchTags = normalize_spaces($request->getParam('searchtags') ?? '');
38 $searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? ''));
40 // Filter bookmarks according search parameters.
41 $visibility = $this->container
->sessionManager
->getSessionParameter('visibility');
43 'searchtags' => $searchTags,
44 'searchterm' => $searchTerm,
47 // Select articles according to paging.
48 $page = (int) ($request->getParam('page') ?? 1);
49 $page = $page < 1 ? 1 : $page;
50 $linksPerPage = $this->container
->sessionManager
->getSessionParameter('LINKS_PER_PAGE', 20) ?: 20;
52 $searchResult = $this->container
->bookmarkService
->search(
56 !!$this->container
->sessionManager
->getSessionParameter('untaggedonly'),
58 ['offset' => $linksPerPage * ($page - 1), 'limit' => $linksPerPage]
63 foreach ($searchResult->getBookmarks() as $key => $bookmark) {
64 $save = $this->updateThumbnail($bookmark, false) || $save;
65 $links[$key] = $formatter->format($bookmark);
69 $this->container
->bookmarkService
->save();
72 // Compute paging navigation
73 $searchtagsUrl = $searchTags === '' ? '' : '&searchtags=' . urlencode($searchTags);
74 $searchtermUrl = $searchTerm === '' ? '' : '&searchterm=' . urlencode($searchTerm);
75 $page = $searchResult->getPage();
77 $previousPageUrl = !$searchResult->isLastPage() ? '?page=' . ($page +
1) . $searchtermUrl . $searchtagsUrl : '';
78 $nextPageUrl = !$searchResult->isFirstPage() ? '?page=' . ($page - 1) . $searchtermUrl . $searchtagsUrl : '';
80 $tagsSeparator = $this->container
->conf
->get('general.tags_separator', ' ');
81 $searchTagsUrlEncoded = array_map('urlencode', tags_str2array($searchTags, $tagsSeparator));
82 $searchTags = !empty($searchTags) ? trim($searchTags, $tagsSeparator) . $tagsSeparator : '';
84 // Fill all template fields.
86 $this->initializeTemplateVars(),
88 'previous_page_url' => $previousPageUrl,
89 'next_page_url' => $nextPageUrl,
90 'page_current' => $page,
91 'page_max' => $searchResult->getLastPage(),
92 'result_count' => $searchResult->getTotalCount(),
93 'search_term' => escape($searchTerm),
94 'search_tags' => escape($searchTags),
95 'search_tags_url' => $searchTagsUrlEncoded,
96 'visibility' => $visibility,
101 if (!empty($searchTerm) || !empty($searchTags)) {
102 $data['pagetitle'] = t('Search: ');
103 $data['pagetitle'] .= ! empty($searchTerm) ? $searchTerm . ' ' : '';
104 $bracketWrap = function ($tag) {
105 return '[' . $tag . ']';
107 $data['pagetitle'] .= ! empty($searchTags)
108 ? implode(' ', array_map($bracketWrap, tags_str2array($searchTags, $tagsSeparator))) . ' '
111 $data['pagetitle'] .= '- ';
114 $data['pagetitle'] = ($data['pagetitle'] ?? '') . $this->container
->conf
->get('general.title', 'Shaarli');
116 $this->executePageHooks('render_linklist', $data, TemplatePage
::LINKLIST
);
117 $this->assignAllView($data);
119 return $response->write($this->render(TemplatePage
::LINKLIST
));
123 * GET /shaare/{hash} - Display a single shaare
125 public function permalink(Request
$request, Response
$response, array $args): Response
127 $privateKey = $request->getParam('key');
130 $bookmark = $this->container
->bookmarkService
->findByHash($args['hash'], $privateKey);
131 } catch (BookmarkNotFoundException
$e) {
132 $this->assignView('error_message', $e->getMessage());
134 return $response->write($this->render(TemplatePage
::ERROR_404
));
137 $this->updateThumbnail($bookmark);
139 $formatter = $this->container
->formatterFactory
->getFormatter();
140 $formatter->addContextData('base_path', $this->container
->basePath
);
143 $this->initializeTemplateVars(),
145 'pagetitle' => $bookmark->getTitle() . ' - ' . $this->container
->conf
->get('general.title', 'Shaarli'),
146 'links' => [$formatter->format($bookmark)],
150 $this->executePageHooks('render_linklist', $data, TemplatePage
::LINKLIST
);
151 $this->assignAllView($data);
153 return $response->write($this->render(TemplatePage
::LINKLIST
));
157 * Update the thumbnail of a single bookmark if necessary.
159 protected function updateThumbnail(Bookmark
$bookmark, bool $writeDatastore = true): bool
161 if (false === $this->container
->loginManager
->isLoggedIn()) {
165 // If thumbnail should be updated, we reset it to null
166 if ($bookmark->shouldUpdateThumbnail()) {
167 $bookmark->setThumbnail(null);
169 // Requires an update, not async retrieval, thumbnails enabled
171 $bookmark->shouldUpdateThumbnail()
172 && true !== $this->container
->conf
->get('general.enable_async_metadata', true)
173 && $this->container
->conf
->get('thumbnails.mode', Thumbnailer
::MODE_NONE
) !== Thumbnailer
::MODE_NONE
175 $bookmark->setThumbnail($this->container
->thumbnailer
->get($bookmark->getUrl()));
176 $this->container
->bookmarkService
->set($bookmark, $writeDatastore);
186 * @return string[] Default template variables without values.
188 protected function initializeTemplateVars(): array
191 'previous_page_url' => '',
192 'next_page_url' => '',
195 'result_count' => '',
196 'async_metadata' => $this->container
->conf
->get('general.enable_async_metadata', true)
201 * Process legacy routes if necessary. They used query parameters.
202 * If no legacy routes is passed, return null.
204 protected function processLegacyController(Request
$request, Response
$response): ?Response
206 // Legacy smallhash filter
207 $queryString = $this->container
->environment
['QUERY_STRING'] ?? null;
208 if (null !== $queryString && 1 === preg_match('/^([a-zA-Z0-9-_@]{6})($|&|#)/', $queryString, $match)) {
209 return $this->redirect($response, '/shaare/' . $match[1]);
212 // Legacy controllers (mostly used for redirections)
213 if (null !== $request->getQueryParam('do')) {
214 $legacyController = new LegacyController($this->container
);
217 return $legacyController->process($request, $response, $request->getQueryParam('do'));
218 } catch (UnknowLegacyRouteException
$e) {
219 // We ignore legacy 404
224 // Legacy GET admin routes
225 $legacyGetRoutes = array_intersect(
226 LegacyController
::LEGACY_GET_ROUTES
,
227 array_keys($request->getQueryParams() ?? [])
229 if (1 === count($legacyGetRoutes)) {
230 $legacyController = new LegacyController($this->container
);
232 return $legacyController->process($request, $response, $legacyGetRoutes[0]);