]>
Commit | Line | Data |
---|---|---|
1a8ac737 A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Visitor; | |
6 | ||
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; | |
15 | ||
16 | /** | |
17 | * Class BookmarkListController | |
18 | * | |
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. | |
21 | */ | |
22 | class BookmarkListController extends ShaarliVisitorController | |
23 | { | |
24 | /** | |
25 | * GET / - Displays the bookmark list, with optional filter parameters. | |
26 | */ | |
27 | public function index(Request $request, Response $response): Response | |
28 | { | |
29 | $legacyResponse = $this->processLegacyController($request, $response); | |
30 | if (null !== $legacyResponse) { | |
31 | return $legacyResponse; | |
32 | } | |
33 | ||
34 | $formatter = $this->container->formatterFactory->getFormatter(); | |
301c7ab1 | 35 | $formatter->addContextData('base_path', $this->container->basePath); |
1a8ac737 | 36 | |
72fbbcd6 | 37 | $searchTags = normalize_spaces($request->getParam('searchtags') ?? ''); |
1a8ac737 A |
38 | $searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? ''));; |
39 | ||
40 | // Filter bookmarks according search parameters. | |
41 | $visibility = $this->container->sessionManager->getSessionParameter('visibility'); | |
42 | $search = [ | |
43 | 'searchtags' => $searchTags, | |
44 | 'searchterm' => $searchTerm, | |
45 | ]; | |
46 | $linksToDisplay = $this->container->bookmarkService->search( | |
47 | $search, | |
48 | $visibility, | |
49 | false, | |
50 | !!$this->container->sessionManager->getSessionParameter('untaggedonly') | |
51 | ) ?? []; | |
52 | ||
53 | // ---- Handle paging. | |
54 | $keys = []; | |
55 | foreach ($linksToDisplay as $key => $value) { | |
56 | $keys[] = $key; | |
57 | } | |
58 | ||
59 | $linksPerPage = $this->container->sessionManager->getSessionParameter('LINKS_PER_PAGE', 20) ?: 20; | |
60 | ||
61 | // Select articles according to paging. | |
62 | $pageCount = (int) ceil(count($keys) / $linksPerPage) ?: 1; | |
63 | $page = (int) $request->getParam('page') ?? 1; | |
64 | $page = $page < 1 ? 1 : $page; | |
65 | $page = $page > $pageCount ? $pageCount : $page; | |
66 | ||
67 | // Start index. | |
68 | $i = ($page - 1) * $linksPerPage; | |
69 | $end = $i + $linksPerPage; | |
70 | ||
71 | $linkDisp = []; | |
72 | $save = false; | |
73 | while ($i < $end && $i < count($keys)) { | |
74 | $save = $this->updateThumbnail($linksToDisplay[$keys[$i]], false) || $save; | |
75 | $link = $formatter->format($linksToDisplay[$keys[$i]]); | |
76 | ||
77 | $linkDisp[$keys[$i]] = $link; | |
78 | $i++; | |
79 | } | |
80 | ||
81 | if ($save) { | |
82 | $this->container->bookmarkService->save(); | |
83 | } | |
84 | ||
85 | // Compute paging navigation | |
86 | $searchtagsUrl = $searchTags === '' ? '' : '&searchtags=' . urlencode($searchTags); | |
87 | $searchtermUrl = $searchTerm === '' ? '' : '&searchterm=' . urlencode($searchTerm); | |
88 | ||
89 | $previous_page_url = ''; | |
90 | if ($i !== count($keys)) { | |
91 | $previous_page_url = '?page=' . ($page + 1) . $searchtermUrl . $searchtagsUrl; | |
92 | } | |
93 | $next_page_url = ''; | |
94 | if ($page > 1) { | |
95 | $next_page_url = '?page=' . ($page - 1) . $searchtermUrl . $searchtagsUrl; | |
96 | } | |
97 | ||
b3bd8c3e A |
98 | $tagsSeparator = $this->container->conf->get('general.tags_separator', ' '); |
99 | $searchTagsUrlEncoded = array_map('urlencode', tags_str2array($searchTags, $tagsSeparator)); | |
100 | $searchTags = !empty($searchTags) ? trim($searchTags, $tagsSeparator) . $tagsSeparator : ''; | |
101 | ||
1a8ac737 A |
102 | // Fill all template fields. |
103 | $data = array_merge( | |
104 | $this->initializeTemplateVars(), | |
105 | [ | |
106 | 'previous_page_url' => $previous_page_url, | |
107 | 'next_page_url' => $next_page_url, | |
108 | 'page_current' => $page, | |
109 | 'page_max' => $pageCount, | |
110 | 'result_count' => count($linksToDisplay), | |
72fbbcd6 A |
111 | 'search_term' => escape($searchTerm), |
112 | 'search_tags' => escape($searchTags), | |
b3bd8c3e | 113 | 'search_tags_url' => $searchTagsUrlEncoded, |
1a8ac737 A |
114 | 'visibility' => $visibility, |
115 | 'links' => $linkDisp, | |
116 | ] | |
117 | ); | |
118 | ||
119 | if (!empty($searchTerm) || !empty($searchTags)) { | |
120 | $data['pagetitle'] = t('Search: '); | |
121 | $data['pagetitle'] .= ! empty($searchTerm) ? $searchTerm . ' ' : ''; | |
122 | $bracketWrap = function ($tag) { | |
123 | return '[' . $tag . ']'; | |
124 | }; | |
125 | $data['pagetitle'] .= ! empty($searchTags) | |
b3bd8c3e A |
126 | ? implode(' ', array_map($bracketWrap, tags_str2array($searchTags, $tagsSeparator))) . ' ' |
127 | : '' | |
128 | ; | |
1a8ac737 A |
129 | $data['pagetitle'] .= '- '; |
130 | } | |
131 | ||
132 | $data['pagetitle'] = ($data['pagetitle'] ?? '') . $this->container->conf->get('general.title', 'Shaarli'); | |
133 | ||
9fbc4229 | 134 | $this->executePageHooks('render_linklist', $data, TemplatePage::LINKLIST); |
1a8ac737 A |
135 | $this->assignAllView($data); |
136 | ||
137 | return $response->write($this->render(TemplatePage::LINKLIST)); | |
138 | } | |
139 | ||
140 | /** | |
141 | * GET /shaare/{hash} - Display a single shaare | |
142 | */ | |
143 | public function permalink(Request $request, Response $response, array $args): Response | |
144 | { | |
9c04921a A |
145 | $privateKey = $request->getParam('key'); |
146 | ||
1a8ac737 | 147 | try { |
9c04921a | 148 | $bookmark = $this->container->bookmarkService->findByHash($args['hash'], $privateKey); |
1a8ac737 A |
149 | } catch (BookmarkNotFoundException $e) { |
150 | $this->assignView('error_message', $e->getMessage()); | |
151 | ||
152 | return $response->write($this->render(TemplatePage::ERROR_404)); | |
153 | } | |
154 | ||
155 | $this->updateThumbnail($bookmark); | |
156 | ||
301c7ab1 A |
157 | $formatter = $this->container->formatterFactory->getFormatter(); |
158 | $formatter->addContextData('base_path', $this->container->basePath); | |
159 | ||
1a8ac737 A |
160 | $data = array_merge( |
161 | $this->initializeTemplateVars(), | |
162 | [ | |
163 | 'pagetitle' => $bookmark->getTitle() .' - '. $this->container->conf->get('general.title', 'Shaarli'), | |
301c7ab1 | 164 | 'links' => [$formatter->format($bookmark)], |
1a8ac737 A |
165 | ] |
166 | ); | |
167 | ||
9fbc4229 | 168 | $this->executePageHooks('render_linklist', $data, TemplatePage::LINKLIST); |
1a8ac737 A |
169 | $this->assignAllView($data); |
170 | ||
171 | return $response->write($this->render(TemplatePage::LINKLIST)); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Update the thumbnail of a single bookmark if necessary. | |
176 | */ | |
177 | protected function updateThumbnail(Bookmark $bookmark, bool $writeDatastore = true): bool | |
178 | { | |
0cf76ccb A |
179 | if (false === $this->container->loginManager->isLoggedIn()) { |
180 | return false; | |
181 | } | |
182 | ||
183 | // If thumbnail should be updated, we reset it to null | |
184 | if ($bookmark->shouldUpdateThumbnail()) { | |
185 | $bookmark->setThumbnail(null); | |
186 | ||
187 | // Requires an update, not async retrieval, thumbnails enabled | |
188 | if ($bookmark->shouldUpdateThumbnail() | |
189 | && true !== $this->container->conf->get('general.enable_async_metadata', true) | |
190 | && $this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE | |
191 | ) { | |
192 | $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl())); | |
193 | $this->container->bookmarkService->set($bookmark, $writeDatastore); | |
194 | ||
195 | return true; | |
196 | } | |
1a8ac737 A |
197 | } |
198 | ||
199 | return false; | |
200 | } | |
201 | ||
1a8ac737 A |
202 | /** |
203 | * @return string[] Default template variables without values. | |
204 | */ | |
205 | protected function initializeTemplateVars(): array | |
206 | { | |
207 | return [ | |
208 | 'previous_page_url' => '', | |
209 | 'next_page_url' => '', | |
210 | 'page_max' => '', | |
211 | 'search_tags' => '', | |
212 | 'result_count' => '', | |
21e72da9 | 213 | 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true) |
1a8ac737 A |
214 | ]; |
215 | } | |
216 | ||
217 | /** | |
218 | * Process legacy routes if necessary. They used query parameters. | |
219 | * If no legacy routes is passed, return null. | |
220 | */ | |
221 | protected function processLegacyController(Request $request, Response $response): ?Response | |
222 | { | |
223 | // Legacy smallhash filter | |
224 | $queryString = $this->container->environment['QUERY_STRING'] ?? null; | |
225 | if (null !== $queryString && 1 === preg_match('/^([a-zA-Z0-9-_@]{6})($|&|#)/', $queryString, $match)) { | |
226 | return $this->redirect($response, '/shaare/' . $match[1]); | |
227 | } | |
228 | ||
229 | // Legacy controllers (mostly used for redirections) | |
230 | if (null !== $request->getQueryParam('do')) { | |
231 | $legacyController = new LegacyController($this->container); | |
232 | ||
233 | try { | |
234 | return $legacyController->process($request, $response, $request->getQueryParam('do')); | |
235 | } catch (UnknowLegacyRouteException $e) { | |
236 | // We ignore legacy 404 | |
237 | return null; | |
238 | } | |
239 | } | |
240 | ||
241 | // Legacy GET admin routes | |
242 | $legacyGetRoutes = array_intersect( | |
243 | LegacyController::LEGACY_GET_ROUTES, | |
244 | array_keys($request->getQueryParams() ?? []) | |
245 | ); | |
246 | if (1 === count($legacyGetRoutes)) { | |
247 | $legacyController = new LegacyController($this->container); | |
248 | ||
249 | return $legacyController->process($request, $response, $legacyGetRoutes[0]); | |
250 | } | |
251 | ||
252 | return null; | |
253 | } | |
254 | } |