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') ?? ''));
42 // Filter bookmarks according search parameters.
43 $visibility = $this->container
->sessionManager
->getSessionParameter('visibility');
45 'searchtags' => $searchTags,
46 'searchterm' => $searchTerm,
48 $linksToDisplay = $this->container
->bookmarkService
->search(
52 !!$this->container
->sessionManager
->getSessionParameter('untaggedonly')
55 // ---- Handle paging.
57 foreach ($linksToDisplay as $key => $value) {
61 $linksPerPage = $this->container
->sessionManager
->getSessionParameter('LINKS_PER_PAGE', 20) ?: 20;
63 // Select articles according to paging.
64 $pageCount = (int) ceil(count($keys) / $linksPerPage) ?: 1;
65 $page = (int) $request->getParam('page') ?? 1;
66 $page = $page < 1 ? 1 : $page;
67 $page = $page > $pageCount ? $pageCount : $page;
70 $i = ($page - 1) * $linksPerPage;
71 $end = $i +
$linksPerPage;
75 while ($i < $end && $i < count($keys)) {
76 $save = $this->updateThumbnail($linksToDisplay[$keys[$i]], false) || $save;
77 $link = $formatter->format($linksToDisplay[$keys[$i]]);
79 $linkDisp[$keys[$i]] = $link;
84 $this->container
->bookmarkService
->save();
87 // Compute paging navigation
88 $searchtagsUrl = $searchTags === '' ? '' : '&searchtags=' . urlencode($searchTags);
89 $searchtermUrl = $searchTerm === '' ? '' : '&searchterm=' . urlencode($searchTerm);
91 $previous_page_url = '';
92 if ($i !== count($keys)) {
93 $previous_page_url = '?page=' . ($page +
1) . $searchtermUrl . $searchtagsUrl;
97 $next_page_url = '?page=' . ($page - 1) . $searchtermUrl . $searchtagsUrl;
100 $tagsSeparator = $this->container
->conf
->get('general.tags_separator', ' ');
101 $searchTagsUrlEncoded = array_map('urlencode', tags_str2array($searchTags, $tagsSeparator));
102 $searchTags = !empty($searchTags) ? trim($searchTags, $tagsSeparator) . $tagsSeparator : '';
104 // Fill all template fields.
106 $this->initializeTemplateVars(),
108 'previous_page_url' => $previous_page_url,
109 'next_page_url' => $next_page_url,
110 'page_current' => $page,
111 'page_max' => $pageCount,
112 'result_count' => count($linksToDisplay),
113 'search_term' => escape($searchTerm),
114 'search_tags' => escape($searchTags),
115 'search_tags_url' => $searchTagsUrlEncoded,
116 'visibility' => $visibility,
117 'links' => $linkDisp,
121 if (!empty($searchTerm) || !empty($searchTags)) {
122 $data['pagetitle'] = t('Search: ');
123 $data['pagetitle'] .= ! empty($searchTerm) ? $searchTerm . ' ' : '';
124 $bracketWrap = function ($tag) {
125 return '[' . $tag . ']';
127 $data['pagetitle'] .= ! empty($searchTags)
128 ? implode(' ', array_map($bracketWrap, tags_str2array($searchTags, $tagsSeparator))) . ' '
131 $data['pagetitle'] .= '- ';
134 $data['pagetitle'] = ($data['pagetitle'] ?? '') . $this->container
->conf
->get('general.title', 'Shaarli');
136 $this->executePageHooks('render_linklist', $data, TemplatePage
::LINKLIST
);
137 $this->assignAllView($data);
139 return $response->write($this->render(TemplatePage
::LINKLIST
));
143 * GET /shaare/{hash} - Display a single shaare
145 public function permalink(Request
$request, Response
$response, array $args): Response
147 $privateKey = $request->getParam('key');
150 $bookmark = $this->container
->bookmarkService
->findByHash($args['hash'], $privateKey);
151 } catch (BookmarkNotFoundException
$e) {
152 $this->assignView('error_message', $e->getMessage());
154 return $response->write($this->render(TemplatePage
::ERROR_404
));
157 $this->updateThumbnail($bookmark);
159 $formatter = $this->container
->formatterFactory
->getFormatter();
160 $formatter->addContextData('base_path', $this->container
->basePath
);
161 $formatter->addContextData('index_url', index_url($this->container
->environment
));
164 $this->initializeTemplateVars(),
166 'pagetitle' => $bookmark->getTitle() . ' - ' . $this->container
->conf
->get('general.title', 'Shaarli'),
167 'links' => [$formatter->format($bookmark)],
171 $this->executePageHooks('render_linklist', $data, TemplatePage
::LINKLIST
);
172 $this->assignAllView($data);
174 return $response->write($this->render(TemplatePage
::LINKLIST
));
178 * Update the thumbnail of a single bookmark if necessary.
180 protected function updateThumbnail(Bookmark
$bookmark, bool $writeDatastore = true): bool
182 if (false === $this->container
->loginManager
->isLoggedIn()) {
186 // If thumbnail should be updated, we reset it to null
187 if ($bookmark->shouldUpdateThumbnail()) {
188 $bookmark->setThumbnail(null);
190 // Requires an update, not async retrieval, thumbnails enabled
192 $bookmark->shouldUpdateThumbnail()
193 && true !== $this->container
->conf
->get('general.enable_async_metadata', true)
194 && $this->container
->conf
->get('thumbnails.mode', Thumbnailer
::MODE_NONE
) !== Thumbnailer
::MODE_NONE
196 $bookmark->setThumbnail($this->container
->thumbnailer
->get($bookmark->getUrl()));
197 $this->container
->bookmarkService
->set($bookmark, $writeDatastore);
207 * @return string[] Default template variables without values.
209 protected function initializeTemplateVars(): array
212 'previous_page_url' => '',
213 'next_page_url' => '',
216 'result_count' => '',
217 'async_metadata' => $this->container
->conf
->get('general.enable_async_metadata', true)
222 * Process legacy routes if necessary. They used query parameters.
223 * If no legacy routes is passed, return null.
225 protected function processLegacyController(Request
$request, Response
$response): ?Response
227 // Legacy smallhash filter
228 $queryString = $this->container
->environment
['QUERY_STRING'] ?? null;
229 if (null !== $queryString && 1 === preg_match('/^([a-zA-Z0-9-_@]{6})($|&|#)/', $queryString, $match)) {
230 return $this->redirect($response, '/shaare/' . $match[1]);
233 // Legacy controllers (mostly used for redirections)
234 if (null !== $request->getQueryParam('do')) {
235 $legacyController = new LegacyController($this->container
);
238 return $legacyController->process($request, $response, $request->getQueryParam('do'));
239 } catch (UnknowLegacyRouteException
$e) {
240 // We ignore legacy 404
245 // Legacy GET admin routes
246 $legacyGetRoutes = array_intersect(
247 LegacyController
::LEGACY_GET_ROUTES
,
248 array_keys($request->getQueryParams() ?? [])
250 if (1 === count($legacyGetRoutes)) {
251 $legacyController = new LegacyController($this->container
);
253 return $legacyController->process($request, $response, $legacyGetRoutes[0]);