]>
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 | |
132 | && false === $bookmark->isNote() | |
133 | ) { | |
134 | $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl())); | |
135 | } | |
136 | $this->container->bookmarkService->addOrSet($bookmark, false); | |
137 | ||
138 | // To preserve backward compatibility with 3rd parties, plugins still use arrays | |
139 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
140 | $data = $formatter->format($bookmark); | |
9fbc4229 | 141 | $this->executePageHooks('save_link', $data); |
c22fa57a A |
142 | |
143 | $bookmark->fromArray($data); | |
144 | $this->container->bookmarkService->set($bookmark); | |
145 | ||
146 | // If we are called from the bookmarklet, we must close the popup: | |
147 | if ($request->getParam('source') === 'bookmarklet') { | |
148 | return $response->write('<script>self.close();</script>'); | |
149 | } | |
150 | ||
151 | if (!empty($request->getParam('returnurl'))) { | |
152 | $this->container->environment['HTTP_REFERER'] = escape($request->getParam('returnurl')); | |
153 | } | |
154 | ||
155 | return $this->redirectFromReferer( | |
156 | $request, | |
157 | $response, | |
2785d85e | 158 | ['/admin/add-shaare', '/admin/shaare'], ['addlink', 'post', 'edit_link'], |
c22fa57a A |
159 | $bookmark->getShortUrl() |
160 | ); | |
161 | } | |
162 | ||
9c75f877 | 163 | /** |
7b8a6f28 | 164 | * GET /admin/shaare/delete - Delete one or multiple bookmarks (depending on `id` query parameter). |
9c75f877 | 165 | */ |
c22fa57a A |
166 | public function deleteBookmark(Request $request, Response $response): Response |
167 | { | |
168 | $this->checkToken($request); | |
169 | ||
baa69791 A |
170 | $ids = escape(trim($request->getParam('id') ?? '')); |
171 | if (empty($ids) || strpos($ids, ' ') !== false) { | |
c22fa57a | 172 | // multiple, space-separated ids provided |
baa69791 | 173 | $ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit')); |
c22fa57a A |
174 | } else { |
175 | $ids = [$ids]; | |
176 | } | |
177 | ||
178 | // assert at least one id is given | |
179 | if (0 === count($ids)) { | |
180 | $this->saveErrorMessage(t('Invalid bookmark ID provided.')); | |
181 | ||
182 | return $this->redirectFromReferer($request, $response, [], ['delete-shaare']); | |
183 | } | |
184 | ||
185 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
baa69791 | 186 | $count = 0; |
c22fa57a | 187 | foreach ($ids as $id) { |
baa69791 A |
188 | try { |
189 | $bookmark = $this->container->bookmarkService->get((int) $id); | |
190 | } catch (BookmarkNotFoundException $e) { | |
191 | $this->saveErrorMessage(sprintf( | |
192 | t('Bookmark with identifier %s could not be found.'), | |
193 | $id | |
194 | )); | |
195 | ||
196 | continue; | |
197 | } | |
198 | ||
c22fa57a | 199 | $data = $formatter->format($bookmark); |
9fbc4229 | 200 | $this->executePageHooks('delete_link', $data); |
c22fa57a | 201 | $this->container->bookmarkService->remove($bookmark, false); |
baa69791 | 202 | ++ $count; |
c22fa57a A |
203 | } |
204 | ||
baa69791 A |
205 | if ($count > 0) { |
206 | $this->container->bookmarkService->save(); | |
207 | } | |
c22fa57a A |
208 | |
209 | // If we are called from the bookmarklet, we must close the popup: | |
210 | if ($request->getParam('source') === 'bookmarklet') { | |
211 | return $response->write('<script>self.close();</script>'); | |
212 | } | |
213 | ||
214 | // Don't redirect to where we were previously because the datastore has changed. | |
9c75f877 | 215 | return $this->redirect($response, '/'); |
c22fa57a A |
216 | } |
217 | ||
7b8a6f28 A |
218 | /** |
219 | * GET /admin/shaare/visibility | |
220 | * | |
221 | * Change visibility (public/private) of one or multiple bookmarks (depending on `id` query parameter). | |
222 | */ | |
223 | public function changeVisibility(Request $request, Response $response): Response | |
224 | { | |
225 | $this->checkToken($request); | |
226 | ||
227 | $ids = trim(escape($request->getParam('id') ?? '')); | |
228 | if (empty($ids) || strpos($ids, ' ') !== false) { | |
229 | // multiple, space-separated ids provided | |
230 | $ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit')); | |
231 | } else { | |
232 | // only a single id provided | |
233 | $ids = [$ids]; | |
234 | } | |
235 | ||
236 | // assert at least one id is given | |
237 | if (0 === count($ids)) { | |
238 | $this->saveErrorMessage(t('Invalid bookmark ID provided.')); | |
239 | ||
240 | return $this->redirectFromReferer($request, $response, [], ['change_visibility']); | |
241 | } | |
242 | ||
243 | // assert that the visibility is valid | |
244 | $visibility = $request->getParam('newVisibility'); | |
245 | if (null === $visibility || false === in_array($visibility, ['public', 'private'], true)) { | |
246 | $this->saveErrorMessage(t('Invalid visibility provided.')); | |
247 | ||
248 | return $this->redirectFromReferer($request, $response, [], ['change_visibility']); | |
249 | } else { | |
250 | $isPrivate = $visibility === 'private'; | |
251 | } | |
252 | ||
253 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
254 | $count = 0; | |
255 | ||
256 | foreach ($ids as $id) { | |
257 | try { | |
258 | $bookmark = $this->container->bookmarkService->get((int) $id); | |
259 | } catch (BookmarkNotFoundException $e) { | |
260 | $this->saveErrorMessage(sprintf( | |
261 | t('Bookmark with identifier %s could not be found.'), | |
262 | $id | |
263 | )); | |
264 | ||
265 | continue; | |
266 | } | |
267 | ||
268 | $bookmark->setPrivate($isPrivate); | |
269 | ||
270 | // To preserve backward compatibility with 3rd parties, plugins still use arrays | |
271 | $data = $formatter->format($bookmark); | |
9fbc4229 | 272 | $this->executePageHooks('save_link', $data); |
7b8a6f28 A |
273 | $bookmark->fromArray($data); |
274 | ||
275 | $this->container->bookmarkService->set($bookmark, false); | |
276 | ++$count; | |
277 | } | |
278 | ||
279 | if ($count > 0) { | |
280 | $this->container->bookmarkService->save(); | |
281 | } | |
282 | ||
283 | return $this->redirectFromReferer($request, $response, ['/visibility'], ['change_visibility']); | |
284 | } | |
285 | ||
3447d888 A |
286 | /** |
287 | * GET /admin/shaare/{id}/pin - Pin or unpin a bookmark. | |
288 | */ | |
289 | public function pinBookmark(Request $request, Response $response, array $args): Response | |
290 | { | |
291 | $this->checkToken($request); | |
292 | ||
293 | $id = $args['id'] ?? ''; | |
294 | try { | |
295 | if (false === ctype_digit($id)) { | |
296 | throw new BookmarkNotFoundException(); | |
297 | } | |
298 | $bookmark = $this->container->bookmarkService->get((int) $id); // Read database | |
299 | } catch (BookmarkNotFoundException $e) { | |
300 | $this->saveErrorMessage(sprintf( | |
301 | t('Bookmark with identifier %s could not be found.'), | |
302 | $id | |
303 | )); | |
304 | ||
305 | return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']); | |
306 | } | |
307 | ||
308 | $formatter = $this->container->formatterFactory->getFormatter('raw'); | |
309 | ||
310 | $bookmark->setSticky(!$bookmark->isSticky()); | |
311 | ||
312 | // To preserve backward compatibility with 3rd parties, plugins still use arrays | |
313 | $data = $formatter->format($bookmark); | |
9fbc4229 | 314 | $this->executePageHooks('save_link', $data); |
3447d888 A |
315 | $bookmark->fromArray($data); |
316 | ||
317 | $this->container->bookmarkService->set($bookmark); | |
318 | ||
319 | return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']); | |
320 | } | |
321 | ||
baa69791 A |
322 | /** |
323 | * Helper function used to display the shaare form whether it's a new or existing bookmark. | |
324 | * | |
325 | * @param array $link data used in template, either from parameters or from the data store | |
326 | */ | |
c22fa57a A |
327 | protected function displayForm(array $link, bool $isNew, Request $request, Response $response): Response |
328 | { | |
329 | $tags = $this->container->bookmarkService->bookmarksCountPerTag(); | |
330 | if ($this->container->conf->get('formatter') === 'markdown') { | |
331 | $tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1; | |
332 | } | |
333 | ||
72fbbcd6 | 334 | $data = escape([ |
c22fa57a A |
335 | 'link' => $link, |
336 | 'link_is_new' => $isNew, | |
72fbbcd6 | 337 | 'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '', |
c22fa57a A |
338 | 'source' => $request->getParam('source') ?? '', |
339 | 'tags' => $tags, | |
340 | 'default_private_links' => $this->container->conf->get('privacy.default_private_links', false), | |
4cf3564d A |
341 | 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true), |
342 | 'retrieve_description' => $this->container->conf->get('general.retrieve_description', false), | |
72fbbcd6 | 343 | ]); |
c22fa57a | 344 | |
9fbc4229 | 345 | $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK); |
c22fa57a A |
346 | |
347 | foreach ($data as $key => $value) { | |
348 | $this->assignView($key, $value); | |
349 | } | |
350 | ||
351 | $editLabel = false === $isNew ? t('Edit') .' ' : ''; | |
352 | $this->assignView( | |
353 | 'pagetitle', | |
354 | $editLabel . t('Shaare') .' - '. $this->container->conf->get('general.title', 'Shaarli') | |
355 | ); | |
356 | ||
1a8ac737 | 357 | return $response->write($this->render(TemplatePage::EDIT_LINK)); |
c22fa57a | 358 | } |
c22fa57a | 359 | } |