diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
commit | b6f678a5a1d15acf284ebcec16c905e976671ce1 (patch) | |
tree | 33c7da831482ed79c44896ef19c73c72ada84f2e /application/front/controller/visitor/BookmarkListController.php | |
parent | b14687036b9b800681197f51fdc47e62f0c88e2e (diff) | |
parent | 1c1520b6b98ab20201bfe15577782a52320339df (diff) | |
download | Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip |
Merge branch 'v0.12' into latest
Diffstat (limited to 'application/front/controller/visitor/BookmarkListController.php')
-rw-r--r-- | application/front/controller/visitor/BookmarkListController.php | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/application/front/controller/visitor/BookmarkListController.php b/application/front/controller/visitor/BookmarkListController.php new file mode 100644 index 00000000..18368751 --- /dev/null +++ b/application/front/controller/visitor/BookmarkListController.php | |||
@@ -0,0 +1,241 @@ | |||
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(); | ||
35 | $formatter->addContextData('base_path', $this->container->basePath); | ||
36 | |||
37 | $searchTags = normalize_spaces($request->getParam('searchtags') ?? ''); | ||
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 | |||
98 | // Fill all template fields. | ||
99 | $data = array_merge( | ||
100 | $this->initializeTemplateVars(), | ||
101 | [ | ||
102 | 'previous_page_url' => $previous_page_url, | ||
103 | 'next_page_url' => $next_page_url, | ||
104 | 'page_current' => $page, | ||
105 | 'page_max' => $pageCount, | ||
106 | 'result_count' => count($linksToDisplay), | ||
107 | 'search_term' => escape($searchTerm), | ||
108 | 'search_tags' => escape($searchTags), | ||
109 | 'search_tags_url' => array_map('urlencode', explode(' ', $searchTags)), | ||
110 | 'visibility' => $visibility, | ||
111 | 'links' => $linkDisp, | ||
112 | ] | ||
113 | ); | ||
114 | |||
115 | if (!empty($searchTerm) || !empty($searchTags)) { | ||
116 | $data['pagetitle'] = t('Search: '); | ||
117 | $data['pagetitle'] .= ! empty($searchTerm) ? $searchTerm . ' ' : ''; | ||
118 | $bracketWrap = function ($tag) { | ||
119 | return '[' . $tag . ']'; | ||
120 | }; | ||
121 | $data['pagetitle'] .= ! empty($searchTags) | ||
122 | ? implode(' ', array_map($bracketWrap, preg_split('/\s+/', $searchTags))) . ' ' | ||
123 | : ''; | ||
124 | $data['pagetitle'] .= '- '; | ||
125 | } | ||
126 | |||
127 | $data['pagetitle'] = ($data['pagetitle'] ?? '') . $this->container->conf->get('general.title', 'Shaarli'); | ||
128 | |||
129 | $this->executePageHooks('render_linklist', $data, TemplatePage::LINKLIST); | ||
130 | $this->assignAllView($data); | ||
131 | |||
132 | return $response->write($this->render(TemplatePage::LINKLIST)); | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * GET /shaare/{hash} - Display a single shaare | ||
137 | */ | ||
138 | public function permalink(Request $request, Response $response, array $args): Response | ||
139 | { | ||
140 | try { | ||
141 | $bookmark = $this->container->bookmarkService->findByHash($args['hash']); | ||
142 | } catch (BookmarkNotFoundException $e) { | ||
143 | $this->assignView('error_message', $e->getMessage()); | ||
144 | |||
145 | return $response->write($this->render(TemplatePage::ERROR_404)); | ||
146 | } | ||
147 | |||
148 | $this->updateThumbnail($bookmark); | ||
149 | |||
150 | $formatter = $this->container->formatterFactory->getFormatter(); | ||
151 | $formatter->addContextData('base_path', $this->container->basePath); | ||
152 | |||
153 | $data = array_merge( | ||
154 | $this->initializeTemplateVars(), | ||
155 | [ | ||
156 | 'pagetitle' => $bookmark->getTitle() .' - '. $this->container->conf->get('general.title', 'Shaarli'), | ||
157 | 'links' => [$formatter->format($bookmark)], | ||
158 | ] | ||
159 | ); | ||
160 | |||
161 | $this->executePageHooks('render_linklist', $data, TemplatePage::LINKLIST); | ||
162 | $this->assignAllView($data); | ||
163 | |||
164 | return $response->write($this->render(TemplatePage::LINKLIST)); | ||
165 | } | ||
166 | |||
167 | /** | ||
168 | * Update the thumbnail of a single bookmark if necessary. | ||
169 | */ | ||
170 | protected function updateThumbnail(Bookmark $bookmark, bool $writeDatastore = true): bool | ||
171 | { | ||
172 | // Logged in, thumbnails enabled, not a note, is HTTP | ||
173 | // and (never retrieved yet or no valid cache file) | ||
174 | if ($this->container->loginManager->isLoggedIn() | ||
175 | && $this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE | ||
176 | && false !== $bookmark->getThumbnail() | ||
177 | && !$bookmark->isNote() | ||
178 | && (null === $bookmark->getThumbnail() || !is_file($bookmark->getThumbnail())) | ||
179 | && startsWith(strtolower($bookmark->getUrl()), 'http') | ||
180 | ) { | ||
181 | $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl())); | ||
182 | $this->container->bookmarkService->set($bookmark, $writeDatastore); | ||
183 | |||
184 | return true; | ||
185 | } | ||
186 | |||
187 | return false; | ||
188 | } | ||
189 | |||
190 | /** | ||
191 | * @return string[] Default template variables without values. | ||
192 | */ | ||
193 | protected function initializeTemplateVars(): array | ||
194 | { | ||
195 | return [ | ||
196 | 'previous_page_url' => '', | ||
197 | 'next_page_url' => '', | ||
198 | 'page_max' => '', | ||
199 | 'search_tags' => '', | ||
200 | 'result_count' => '', | ||
201 | ]; | ||
202 | } | ||
203 | |||
204 | /** | ||
205 | * Process legacy routes if necessary. They used query parameters. | ||
206 | * If no legacy routes is passed, return null. | ||
207 | */ | ||
208 | protected function processLegacyController(Request $request, Response $response): ?Response | ||
209 | { | ||
210 | // Legacy smallhash filter | ||
211 | $queryString = $this->container->environment['QUERY_STRING'] ?? null; | ||
212 | if (null !== $queryString && 1 === preg_match('/^([a-zA-Z0-9-_@]{6})($|&|#)/', $queryString, $match)) { | ||
213 | return $this->redirect($response, '/shaare/' . $match[1]); | ||
214 | } | ||
215 | |||
216 | // Legacy controllers (mostly used for redirections) | ||
217 | if (null !== $request->getQueryParam('do')) { | ||
218 | $legacyController = new LegacyController($this->container); | ||
219 | |||
220 | try { | ||
221 | return $legacyController->process($request, $response, $request->getQueryParam('do')); | ||
222 | } catch (UnknowLegacyRouteException $e) { | ||
223 | // We ignore legacy 404 | ||
224 | return null; | ||
225 | } | ||
226 | } | ||
227 | |||
228 | // Legacy GET admin routes | ||
229 | $legacyGetRoutes = array_intersect( | ||
230 | LegacyController::LEGACY_GET_ROUTES, | ||
231 | array_keys($request->getQueryParams() ?? []) | ||
232 | ); | ||
233 | if (1 === count($legacyGetRoutes)) { | ||
234 | $legacyController = new LegacyController($this->container); | ||
235 | |||
236 | return $legacyController->process($request, $response, $legacyGetRoutes[0]); | ||
237 | } | ||
238 | |||
239 | return null; | ||
240 | } | ||
241 | } | ||