]>
Commit | Line | Data |
---|---|---|
c22fa57a A |
1 | <?php |
2 | ||
3 | declare(strict_types=1); | |
4 | ||
5 | namespace Shaarli\Front\Controller\Admin; | |
6 | ||
7 | use Shaarli\Bookmark\Bookmark; | |
8 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; | |
9 | use Shaarli\Formatter\BookmarkMarkdownFormatter; | |
1a8ac737 | 10 | use Shaarli\Render\TemplatePage; |
c22fa57a A |
11 | use Shaarli\Thumbnailer; |
12 | use Slim\Http\Request; | |
13 | use Slim\Http\Response; | |
14 | ||
15 | /** | |
16 | * Class PostBookmarkController | |
17 | * | |
18 | * Slim controller used to handle Shaarli create or edit bookmarks. | |
19 | */ | |
baa69791 | 20 | class ManageShaareController extends ShaarliAdminController |
c22fa57a A |
21 | { |
22 | /** | |
9c75f877 | 23 | * GET /admin/add-shaare - Displays the form used to create a new bookmark from an URL |
c22fa57a A |
24 | */ |
25 | public function addShaare(Request $request, Response $response): Response | |
26 | { | |
27 | $this->assignView( | |
28 | 'pagetitle', | |
29 | t('Shaare a new link') .' - '. $this->container->conf->get('general.title', 'Shaarli') | |
30 | ); | |
31 | ||
1a8ac737 | 32 | return $response->write($this->render(TemplatePage::ADDLINK)); |
c22fa57a A |
33 | } |
34 | ||
35 | /** | |
9c75f877 | 36 | * GET /admin/shaare - Displays the bookmark form for creation. |
baa69791 | 37 | * Note that if the URL is found in existing bookmarks, then it will be in edit mode. |
c22fa57a A |
38 | */ |
39 | public function displayCreateForm(Request $request, Response $response): Response | |
40 | { | |
41 | $url = cleanup_url($request->getParam('post')); | |
42 | ||
43 | $linkIsNew = false; | |
44 | // Check if URL is not already in database (in this case, we will edit the existing link) | |
45 | $bookmark = $this->container->bookmarkService->findByUrl($url); | |
46 | if (null === $bookmark) { | |
47 | $linkIsNew = true; | |
48 | // Get shaare data if it was provided in URL (e.g.: by the bookmarklet). | |
49 | $title = $request->getParam('title'); | |
50 | $description = $request->getParam('description'); | |
51 | $tags = $request->getParam('tags'); | |
52 | $private = filter_var($request->getParam('private'), FILTER_VALIDATE_BOOLEAN); | |
53 | ||
54 | // If this is an HTTP(S) link, we try go get the page to extract | |
55 | // the title (otherwise we will to straight to the edit form.) | |
4cf3564d A |
56 | if (true !== $this->container->conf->get('general.enable_async_metadata', true) |
57 | && empty($title) | |
58 | && strpos(get_url_scheme($url) ?: '', 'http') !== false | |
59 | ) { | |
60 | $metadata = $this->container->metadataRetriever->retrieve($url); | |
c22fa57a A |
61 | } |
62 | ||
4cf3564d A |
63 | if (empty($url)) { |
64 | $metadata['title'] = $this->container->conf->get('general.default_note_title', t('Note: ')); | |
c22fa57a A |
65 | } |
66 | ||
72fbbcd6 | 67 | $link = [ |
4cf3564d | 68 | 'title' => $title ?? $metadata['title'] ?? '', |
c22fa57a | 69 | 'url' => $url ?? '', |
4cf3564d A |
70 | 'description' => $description ?? $metadata['description'] ?? '', |
71 | 'tags' => $tags ?? $metadata['tags'] ?? '', | |
c22fa57a | 72 | 'private' => $private, |
72fbbcd6 | 73 | ]; |
c22fa57a A |
74 | } else { |
75 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
76 | $link = $formatter->format($bookmark); | |
77 | } | |
78 | ||
79 | return $this->displayForm($link, $linkIsNew, $request, $response); | |
80 | } | |
81 | ||
82 | /** | |
9c75f877 | 83 | * GET /admin/shaare/{id} - Displays the bookmark form in edition mode. |
c22fa57a A |
84 | */ |
85 | public function displayEditForm(Request $request, Response $response, array $args): Response | |
86 | { | |
baa69791 | 87 | $id = $args['id'] ?? ''; |
c22fa57a A |
88 | try { |
89 | if (false === ctype_digit($id)) { | |
90 | throw new BookmarkNotFoundException(); | |
91 | } | |
baa69791 | 92 | $bookmark = $this->container->bookmarkService->get((int) $id); // Read database |
c22fa57a | 93 | } catch (BookmarkNotFoundException $e) { |
baa69791 A |
94 | $this->saveErrorMessage(sprintf( |
95 | t('Bookmark with identifier %s could not be found.'), | |
96 | $id | |
97 | )); | |
c22fa57a | 98 | |
9c75f877 | 99 | return $this->redirect($response, '/'); |
c22fa57a A |
100 | } |
101 | ||
102 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
103 | $link = $formatter->format($bookmark); | |
104 | ||
105 | return $this->displayForm($link, false, $request, $response); | |
106 | } | |
107 | ||
108 | /** | |
9c75f877 | 109 | * POST /admin/shaare |
c22fa57a A |
110 | */ |
111 | public function save(Request $request, Response $response): Response | |
112 | { | |
113 | $this->checkToken($request); | |
114 | ||
115 | // lf_id should only be present if the link exists. | |
80a3efe1 | 116 | $id = $request->getParam('lf_id') !== null ? intval(escape($request->getParam('lf_id'))) : null; |
c22fa57a A |
117 | if (null !== $id && true === $this->container->bookmarkService->exists($id)) { |
118 | // Edit | |
119 | $bookmark = $this->container->bookmarkService->get($id); | |
120 | } else { | |
121 | // New link | |
122 | $bookmark = new Bookmark(); | |
123 | } | |
124 | ||
125 | $bookmark->setTitle($request->getParam('lf_title')); | |
126 | $bookmark->setDescription($request->getParam('lf_description')); | |
127 | $bookmark->setUrl($request->getParam('lf_url'), $this->container->conf->get('security.allowed_protocols', [])); | |
128 | $bookmark->setPrivate(filter_var($request->getParam('lf_private'), FILTER_VALIDATE_BOOLEAN)); | |
129 | $bookmark->setTagsString($request->getParam('lf_tags')); | |
130 | ||
131 | if ($this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE | |
21e72da9 A |
132 | && true !== $this->container->conf->get('general.enable_async_metadata', true) |
133 | && $bookmark->shouldUpdateThumbnail() | |
c22fa57a A |
134 | ) { |
135 | $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl())); | |
136 | } | |
137 | $this->container->bookmarkService->addOrSet($bookmark, false); | |
138 | ||
139 | // To preserve backward compatibility with 3rd parties, plugins still use arrays | |
140 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
141 | $data = $formatter->format($bookmark); | |
9fbc4229 | 142 | $this->executePageHooks('save_link', $data); |
c22fa57a A |
143 | |
144 | $bookmark->fromArray($data); | |
145 | $this->container->bookmarkService->set($bookmark); | |
146 | ||
147 | // If we are called from the bookmarklet, we must close the popup: | |
148 | if ($request->getParam('source') === 'bookmarklet') { | |
149 | return $response->write('<script>self.close();</script>'); | |
150 | } | |
151 | ||
152 | if (!empty($request->getParam('returnurl'))) { | |
153 | $this->container->environment['HTTP_REFERER'] = escape($request->getParam('returnurl')); | |
154 | } | |
155 | ||
156 | return $this->redirectFromReferer( | |
157 | $request, | |
158 | $response, | |
2785d85e | 159 | ['/admin/add-shaare', '/admin/shaare'], ['addlink', 'post', 'edit_link'], |
c22fa57a A |
160 | $bookmark->getShortUrl() |
161 | ); | |
162 | } | |
163 | ||
9c75f877 | 164 | /** |
7b8a6f28 | 165 | * GET /admin/shaare/delete - Delete one or multiple bookmarks (depending on `id` query parameter). |
9c75f877 | 166 | */ |
c22fa57a A |
167 | public function deleteBookmark(Request $request, Response $response): Response |
168 | { | |
169 | $this->checkToken($request); | |
170 | ||
baa69791 A |
171 | $ids = escape(trim($request->getParam('id') ?? '')); |
172 | if (empty($ids) || strpos($ids, ' ') !== false) { | |
c22fa57a | 173 | // multiple, space-separated ids provided |
baa69791 | 174 | $ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit')); |
c22fa57a A |
175 | } else { |
176 | $ids = [$ids]; | |
177 | } | |
178 | ||
179 | // assert at least one id is given | |
180 | if (0 === count($ids)) { | |
181 | $this->saveErrorMessage(t('Invalid bookmark ID provided.')); | |
182 | ||
183 | return $this->redirectFromReferer($request, $response, [], ['delete-shaare']); | |
184 | } | |
185 | ||
186 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
baa69791 | 187 | $count = 0; |
c22fa57a | 188 | foreach ($ids as $id) { |
baa69791 A |
189 | try { |
190 | $bookmark = $this->container->bookmarkService->get((int) $id); | |
191 | } catch (BookmarkNotFoundException $e) { | |
192 | $this->saveErrorMessage(sprintf( | |
193 | t('Bookmark with identifier %s could not be found.'), | |
194 | $id | |
195 | )); | |
196 | ||
197 | continue; | |
198 | } | |
199 | ||
c22fa57a | 200 | $data = $formatter->format($bookmark); |
9fbc4229 | 201 | $this->executePageHooks('delete_link', $data); |
c22fa57a | 202 | $this->container->bookmarkService->remove($bookmark, false); |
baa69791 | 203 | ++ $count; |
c22fa57a A |
204 | } |
205 | ||
baa69791 A |
206 | if ($count > 0) { |
207 | $this->container->bookmarkService->save(); | |
208 | } | |
c22fa57a A |
209 | |
210 | // If we are called from the bookmarklet, we must close the popup: | |
211 | if ($request->getParam('source') === 'bookmarklet') { | |
212 | return $response->write('<script>self.close();</script>'); | |
213 | } | |
214 | ||
215 | // Don't redirect to where we were previously because the datastore has changed. | |
9c75f877 | 216 | return $this->redirect($response, '/'); |
c22fa57a A |
217 | } |
218 | ||
7b8a6f28 A |
219 | /** |
220 | * GET /admin/shaare/visibility | |
221 | * | |
222 | * Change visibility (public/private) of one or multiple bookmarks (depending on `id` query parameter). | |
223 | */ | |
224 | public function changeVisibility(Request $request, Response $response): Response | |
225 | { | |
226 | $this->checkToken($request); | |
227 | ||
228 | $ids = trim(escape($request->getParam('id') ?? '')); | |
229 | if (empty($ids) || strpos($ids, ' ') !== false) { | |
230 | // multiple, space-separated ids provided | |
231 | $ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit')); | |
232 | } else { | |
233 | // only a single id provided | |
234 | $ids = [$ids]; | |
235 | } | |
236 | ||
237 | // assert at least one id is given | |
238 | if (0 === count($ids)) { | |
239 | $this->saveErrorMessage(t('Invalid bookmark ID provided.')); | |
240 | ||
241 | return $this->redirectFromReferer($request, $response, [], ['change_visibility']); | |
242 | } | |
243 | ||
244 | // assert that the visibility is valid | |
245 | $visibility = $request->getParam('newVisibility'); | |
246 | if (null === $visibility || false === in_array($visibility, ['public', 'private'], true)) { | |
247 | $this->saveErrorMessage(t('Invalid visibility provided.')); | |
248 | ||
249 | return $this->redirectFromReferer($request, $response, [], ['change_visibility']); | |
250 | } else { | |
251 | $isPrivate = $visibility === 'private'; | |
252 | } | |
253 | ||
254 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
255 | $count = 0; | |
256 | ||
257 | foreach ($ids as $id) { | |
258 | try { | |
259 | $bookmark = $this->container->bookmarkService->get((int) $id); | |
260 | } catch (BookmarkNotFoundException $e) { | |
261 | $this->saveErrorMessage(sprintf( | |
262 | t('Bookmark with identifier %s could not be found.'), | |
263 | $id | |
264 | )); | |
265 | ||
266 | continue; | |
267 | } | |
268 | ||
269 | $bookmark->setPrivate($isPrivate); | |
270 | ||
271 | // To preserve backward compatibility with 3rd parties, plugins still use arrays | |
272 | $data = $formatter->format($bookmark); | |
9fbc4229 | 273 | $this->executePageHooks('save_link', $data); |
7b8a6f28 A |
274 | $bookmark->fromArray($data); |
275 | ||
276 | $this->container->bookmarkService->set($bookmark, false); | |
277 | ++$count; | |
278 | } | |
279 | ||
280 | if ($count > 0) { | |
281 | $this->container->bookmarkService->save(); | |
282 | } | |
283 | ||
284 | return $this->redirectFromReferer($request, $response, ['/visibility'], ['change_visibility']); | |
285 | } | |
286 | ||
3447d888 A |
287 | /** |
288 | * GET /admin/shaare/{id}/pin - Pin or unpin a bookmark. | |
289 | */ | |
290 | public function pinBookmark(Request $request, Response $response, array $args): Response | |
291 | { | |
292 | $this->checkToken($request); | |
293 | ||
294 | $id = $args['id'] ?? ''; | |
295 | try { | |
296 | if (false === ctype_digit($id)) { | |
297 | throw new BookmarkNotFoundException(); | |
298 | } | |
299 | $bookmark = $this->container->bookmarkService->get((int) $id); // Read database | |
300 | } catch (BookmarkNotFoundException $e) { | |
301 | $this->saveErrorMessage(sprintf( | |
302 | t('Bookmark with identifier %s could not be found.'), | |
303 | $id | |
304 | )); | |
305 | ||
306 | return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']); | |
307 | } | |
308 | ||
309 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
310 | ||
311 | $bookmark->setSticky(!$bookmark->isSticky()); | |
312 | ||
313 | // To preserve backward compatibility with 3rd parties, plugins still use arrays | |
314 | $data = $formatter->format($bookmark); | |
9fbc4229 | 315 | $this->executePageHooks('save_link', $data); |
3447d888 A |
316 | $bookmark->fromArray($data); |
317 | ||
318 | $this->container->bookmarkService->set($bookmark); | |
319 | ||
320 | return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']); | |
321 | } | |
322 | ||
9c04921a A |
323 | /** |
324 | * GET /admin/shaare/private/{hash} - Attach a private key to given bookmark, then redirect to the sharing URL. | |
325 | */ | |
326 | public function sharePrivate(Request $request, Response $response, array $args): Response | |
327 | { | |
328 | $this->checkToken($request); | |
329 | ||
330 | $hash = $args['hash'] ?? ''; | |
331 | $bookmark = $this->container->bookmarkService->findByHash($hash); | |
332 | ||
333 | if ($bookmark->isPrivate() !== true) { | |
334 | return $this->redirect($response, '/shaare/' . $hash); | |
335 | } | |
336 | ||
337 | if (empty($bookmark->getAdditionalContentEntry('private_key'))) { | |
338 | $privateKey = bin2hex(random_bytes(16)); | |
339 | $bookmark->addAdditionalContentEntry('private_key', $privateKey); | |
340 | $this->container->bookmarkService->set($bookmark); | |
341 | } | |
342 | ||
343 | return $this->redirect( | |
344 | $response, | |
345 | '/shaare/' . $hash . '?key=' . $bookmark->getAdditionalContentEntry('private_key') | |
346 | ); | |
347 | } | |
348 | ||
baa69791 A |
349 | /** |
350 | * Helper function used to display the shaare form whether it's a new or existing bookmark. | |
351 | * | |
352 | * @param array $link data used in template, either from parameters or from the data store | |
353 | */ | |
c22fa57a A |
354 | protected function displayForm(array $link, bool $isNew, Request $request, Response $response): Response |
355 | { | |
356 | $tags = $this->container->bookmarkService->bookmarksCountPerTag(); | |
357 | if ($this->container->conf->get('formatter') === 'markdown') { | |
358 | $tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1; | |
359 | } | |
360 | ||
72fbbcd6 | 361 | $data = escape([ |
c22fa57a A |
362 | 'link' => $link, |
363 | 'link_is_new' => $isNew, | |
72fbbcd6 | 364 | 'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '', |
c22fa57a A |
365 | 'source' => $request->getParam('source') ?? '', |
366 | 'tags' => $tags, | |
367 | 'default_private_links' => $this->container->conf->get('privacy.default_private_links', false), | |
4cf3564d A |
368 | 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true), |
369 | 'retrieve_description' => $this->container->conf->get('general.retrieve_description', false), | |
72fbbcd6 | 370 | ]); |
c22fa57a | 371 | |
9fbc4229 | 372 | $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK); |
c22fa57a A |
373 | |
374 | foreach ($data as $key => $value) { | |
375 | $this->assignView($key, $value); | |
376 | } | |
377 | ||
378 | $editLabel = false === $isNew ? t('Edit') .' ' : ''; | |
379 | $this->assignView( | |
380 | 'pagetitle', | |
381 | $editLabel . t('Shaare') .' - '. $this->container->conf->get('general.title', 'Shaarli') | |
382 | ); | |
383 | ||
1a8ac737 | 384 | return $response->write($this->render(TemplatePage::EDIT_LINK)); |
c22fa57a | 385 | } |
c22fa57a | 386 | } |