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
);
36 $formatter->addContextData('index_url', index_url($this->container
->environment
));
38 $searchTags = normalize_spaces($request->getParam('searchtags') ?? '');
39 $searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? ''));
41 // Filter bookmarks according search parameters.
42 $visibility = $this->container
->sessionManager
->getSessionParameter('visibility');
44 'searchtags' => $searchTags,
45 'searchterm' => $searchTerm,
48 // Select articles according to paging.
49 $page = (int) ($request->getParam('page') ?? 1);
50 $page = $page < 1 ? 1 : $page;
51 $linksPerPage = $this->container
->sessionManager
->getSessionParameter('LINKS_PER_PAGE', 20) ?: 20;
53 $searchResult = $this->container
->bookmarkService
->search(
57 !!$this->container
->sessionManager
->getSessionParameter('untaggedonly'),
59 ['offset' => $linksPerPage * ($page - 1), 'limit' => $linksPerPage]
64 foreach ($searchResult->getBookmarks() as $key => $bookmark) {
65 $save = $this->updateThumbnail($bookmark, false) || $save;
66 $links[$key] = $formatter->format($bookmark);
70 $this->container
->bookmarkService
->save();
73 // Compute paging navigation
74 $searchtagsUrl = $searchTags === '' ? '' : '&searchtags=' . urlencode($searchTags);
75 $searchtermUrl = $searchTerm === '' ? '' : '&searchterm=' . urlencode($searchTerm);
76 $page = $searchResult->getPage();
78 $previousPageUrl = !$searchResult->isLastPage() ? '?page=' . ($page +
1) . $searchtermUrl . $searchtagsUrl : '';
79 $nextPageUrl = !$searchResult->isFirstPage() ? '?page=' . ($page - 1) . $searchtermUrl . $searchtagsUrl : '';
81 $tagsSeparator = $this->container
->conf
->get('general.tags_separator', ' ');
82 $searchTagsUrlEncoded = array_map('urlencode', tags_str2array($searchTags, $tagsSeparator));
83 $searchTags = !empty($searchTags) ? trim($searchTags, $tagsSeparator) . $tagsSeparator : '';
85 // Fill all template fields.
87 $this->initializeTemplateVars(),
89 'previous_page_url' => $previousPageUrl,
90 'next_page_url' => $nextPageUrl,
91 'page_current' => $page,
92 'page_max' => $searchResult->getLastPage(),
93 'result_count' => $searchResult->getTotalCount(),
94 'search_term' => escape($searchTerm),
95 'search_tags' => escape($searchTags),
96 'search_tags_url' => $searchTagsUrlEncoded,
97 'visibility' => $visibility,
102 if (!empty($searchTerm) || !empty($searchTags)) {
103 $data['pagetitle'] = t('Search: ');
104 $data['pagetitle'] .= ! empty($searchTerm) ? $searchTerm . ' ' : '';
105 $bracketWrap = function ($tag) {
106 return '[' . $tag . ']';
108 $data['pagetitle'] .= ! empty($searchTags)
109 ? implode(' ', array_map($bracketWrap, tags_str2array($searchTags, $tagsSeparator))) . ' '
112 $data['pagetitle'] .= '- ';
115 $data['pagetitle'] = ($data['pagetitle'] ?? '') . $this->container
->conf
->get('general.title', 'Shaarli');
117 $this->executePageHooks('render_linklist', $data, TemplatePage
::LINKLIST
);
118 $this->assignAllView($data);
120 return $response->write($this->render(TemplatePage
::LINKLIST
));
124 * GET /shaare/{hash} - Display a single shaare
126 public function permalink(Request
$request, Response
$response, array $args): Response
128 $privateKey = $request->getParam('key');
131 $bookmark = $this->container
->bookmarkService
->findByHash($args['hash'], $privateKey);
132 } catch (BookmarkNotFoundException
$e) {
133 $this->assignView('error_message', $e->getMessage());
135 return $response->write($this->render(TemplatePage
::ERROR_404
));
138 $this->updateThumbnail($bookmark);
140 $formatter = $this->container
->formatterFactory
->getFormatter();
141 $formatter->addContextData('base_path', $this->container
->basePath
);
142 $formatter->addContextData('index_url', index_url($this->container
->environment
));
145 $this->initializeTemplateVars(),
147 'pagetitle' => $bookmark->getTitle() . ' - ' . $this->container
->conf
->get('general.title', 'Shaarli'),
148 'links' => [$formatter->format($bookmark)],
152 $this->executePageHooks('render_linklist', $data, TemplatePage
::LINKLIST
);
153 $this->assignAllView($data);
155 return $response->write($this->render(TemplatePage
::LINKLIST
));
159 * Update the thumbnail of a single bookmark if necessary.
161 protected function updateThumbnail(Bookmark
$bookmark, bool $writeDatastore = true): bool
163 if (false === $this->container
->loginManager
->isLoggedIn()) {
167 // If thumbnail should be updated, we reset it to null
168 if ($bookmark->shouldUpdateThumbnail()) {
169 $bookmark->setThumbnail(null);
171 // Requires an update, not async retrieval, thumbnails enabled
173 $bookmark->shouldUpdateThumbnail()
174 && true !== $this->container
->conf
->get('general.enable_async_metadata', true)
175 && $this->container
->conf
->get('thumbnails.mode', Thumbnailer
::MODE_NONE
) !== Thumbnailer
::MODE_NONE
177 $bookmark->setThumbnail($this->container
->thumbnailer
->get($bookmark->getUrl()));
178 $this->container
->bookmarkService
->set($bookmark, $writeDatastore);
188 * @return string[] Default template variables without values.
190 protected function initializeTemplateVars(): array
193 'previous_page_url' => '',
194 'next_page_url' => '',
197 'result_count' => '',
198 'async_metadata' => $this->container
->conf
->get('general.enable_async_metadata', true)
203 * Process legacy routes if necessary. They used query parameters.
204 * If no legacy routes is passed, return null.
206 protected function processLegacyController(Request
$request, Response
$response): ?Response
208 // Legacy smallhash filter
209 $queryString = $this->container
->environment
['QUERY_STRING'] ?? null;
210 if (null !== $queryString && 1 === preg_match('/^([a-zA-Z0-9-_@]{6})($|&|#)/', $queryString, $match)) {
211 return $this->redirect($response, '/shaare/' . $match[1]);
214 // Legacy controllers (mostly used for redirections)
215 if (null !== $request->getQueryParam('do')) {
216 $legacyController = new LegacyController($this->container
);
219 return $legacyController->process($request, $response, $request->getQueryParam('do'));
220 } catch (UnknowLegacyRouteException
$e) {
221 // We ignore legacy 404
226 // Legacy GET admin routes
227 $legacyGetRoutes = array_intersect(
228 LegacyController
::LEGACY_GET_ROUTES
,
229 array_keys($request->getQueryParams() ?? [])
231 if (1 === count($legacyGetRoutes)) {
232 $legacyController = new LegacyController($this->container
);
234 return $legacyController->process($request, $response, $legacyGetRoutes[0]);