aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-10-10 17:40:26 +0200
committerArthurHoaro <arthur@hoa.ro>2020-10-27 20:11:30 +0100
commit5d8de7587d67b5c3e5d1fed8562d9b87ecde80c1 (patch)
tree2236e571035332a63f87a09222f2278b93f63515 /application
parentb8e5a253ab5521ce2be6c0d3e04e0101527df3c1 (diff)
downloadShaarli-5d8de7587d67b5c3e5d1fed8562d9b87ecde80c1.tar.gz
Shaarli-5d8de7587d67b5c3e5d1fed8562d9b87ecde80c1.tar.zst
Shaarli-5d8de7587d67b5c3e5d1fed8562d9b87ecde80c1.zip
Feature: bulk creation of bookmarks
This changes creates a new form in addlink page allowing to create multiple bookmarks at once more easily. It focuses on re-using as much existing code and template component as possible. These changes includes: - a new form in addlink (hidden behind a button by default), containing a text area for URL, and tags/private status to apply to created links - this form displays a new template called editlink.batch, itself including editlink template multiple times - User interation in this new templates are handle by a new JS script (shaare-batch.js) making AJAX requests, and therefore does not need page reloading - ManageShaareController has been split into 3 distinct controllers: + ShaareAdd: displays addlink template + ShaareManage: various operation applied on existing shaares (change visibility, pin, deletion, etc.) + ShaarePublish: handles creation/edit forms and saving Shaare's form - Updated translations Fixes #137
Diffstat (limited to 'application')
-rw-r--r--application/front/controller/admin/ManageShaareController.php386
-rw-r--r--application/front/controller/admin/ShaareAddController.php34
-rw-r--r--application/front/controller/admin/ShaareManageController.php202
-rw-r--r--application/front/controller/admin/ShaarePublishController.php222
-rw-r--r--application/render/TemplatePage.php1
5 files changed, 459 insertions, 386 deletions
diff --git a/application/front/controller/admin/ManageShaareController.php b/application/front/controller/admin/ManageShaareController.php
deleted file mode 100644
index e490f85a..00000000
--- a/application/front/controller/admin/ManageShaareController.php
+++ /dev/null
@@ -1,386 +0,0 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
9use Shaarli\Formatter\BookmarkMarkdownFormatter;
10use Shaarli\Render\TemplatePage;
11use Shaarli\Thumbnailer;
12use Slim\Http\Request;
13use Slim\Http\Response;
14
15/**
16 * Class PostBookmarkController
17 *
18 * Slim controller used to handle Shaarli create or edit bookmarks.
19 */
20class ManageShaareController extends ShaarliAdminController
21{
22 /**
23 * GET /admin/add-shaare - Displays the form used to create a new bookmark from an URL
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
32 return $response->write($this->render(TemplatePage::ADDLINK));
33 }
34
35 /**
36 * GET /admin/shaare - Displays the bookmark form for creation.
37 * Note that if the URL is found in existing bookmarks, then it will be in edit mode.
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.)
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);
61 }
62
63 if (empty($url)) {
64 $metadata['title'] = $this->container->conf->get('general.default_note_title', t('Note: '));
65 }
66
67 $link = [
68 'title' => $title ?? $metadata['title'] ?? '',
69 'url' => $url ?? '',
70 'description' => $description ?? $metadata['description'] ?? '',
71 'tags' => $tags ?? $metadata['tags'] ?? '',
72 'private' => $private,
73 ];
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 /**
83 * GET /admin/shaare/{id} - Displays the bookmark form in edition mode.
84 */
85 public function displayEditForm(Request $request, Response $response, array $args): Response
86 {
87 $id = $args['id'] ?? '';
88 try {
89 if (false === ctype_digit($id)) {
90 throw new BookmarkNotFoundException();
91 }
92 $bookmark = $this->container->bookmarkService->get((int) $id); // Read database
93 } catch (BookmarkNotFoundException $e) {
94 $this->saveErrorMessage(sprintf(
95 t('Bookmark with identifier %s could not be found.'),
96 $id
97 ));
98
99 return $this->redirect($response, '/');
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 /**
109 * POST /admin/shaare
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.
116 $id = $request->getParam('lf_id') !== null ? intval(escape($request->getParam('lf_id'))) : null;
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 && true !== $this->container->conf->get('general.enable_async_metadata', true)
133 && $bookmark->shouldUpdateThumbnail()
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);
142 $this->executePageHooks('save_link', $data);
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,
159 ['/admin/add-shaare', '/admin/shaare'], ['addlink', 'post', 'edit_link'],
160 $bookmark->getShortUrl()
161 );
162 }
163
164 /**
165 * GET /admin/shaare/delete - Delete one or multiple bookmarks (depending on `id` query parameter).
166 */
167 public function deleteBookmark(Request $request, Response $response): Response
168 {
169 $this->checkToken($request);
170
171 $ids = escape(trim($request->getParam('id') ?? ''));
172 if (empty($ids) || strpos($ids, ' ') !== false) {
173 // multiple, space-separated ids provided
174 $ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit'));
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');
187 $count = 0;
188 foreach ($ids as $id) {
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
200 $data = $formatter->format($bookmark);
201 $this->executePageHooks('delete_link', $data);
202 $this->container->bookmarkService->remove($bookmark, false);
203 ++ $count;
204 }
205
206 if ($count > 0) {
207 $this->container->bookmarkService->save();
208 }
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.
216 return $this->redirect($response, '/');
217 }
218
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);
273 $this->executePageHooks('save_link', $data);
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
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);
315 $this->executePageHooks('save_link', $data);
316 $bookmark->fromArray($data);
317
318 $this->container->bookmarkService->set($bookmark);
319
320 return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']);
321 }
322
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
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 */
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
361 $data = escape([
362 'link' => $link,
363 'link_is_new' => $isNew,
364 'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '',
365 'source' => $request->getParam('source') ?? '',
366 'tags' => $tags,
367 'default_private_links' => $this->container->conf->get('privacy.default_private_links', false),
368 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true),
369 'retrieve_description' => $this->container->conf->get('general.retrieve_description', false),
370 ]);
371
372 $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK);
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
384 return $response->write($this->render(TemplatePage::EDIT_LINK));
385 }
386}
diff --git a/application/front/controller/admin/ShaareAddController.php b/application/front/controller/admin/ShaareAddController.php
new file mode 100644
index 00000000..8dc386b2
--- /dev/null
+++ b/application/front/controller/admin/ShaareAddController.php
@@ -0,0 +1,34 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use Shaarli\Formatter\BookmarkMarkdownFormatter;
8use Shaarli\Render\TemplatePage;
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12class ShaareAddController extends ShaarliAdminController
13{
14 /**
15 * GET /admin/add-shaare - Displays the form used to create a new bookmark from an URL
16 */
17 public function addShaare(Request $request, Response $response): Response
18 {
19 $tags = $this->container->bookmarkService->bookmarksCountPerTag();
20 if ($this->container->conf->get('formatter') === 'markdown') {
21 $tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1;
22 }
23
24 $this->assignView(
25 'pagetitle',
26 t('Shaare a new link') .' - '. $this->container->conf->get('general.title', 'Shaarli')
27 );
28 $this->assignView('tags', $tags);
29 $this->assignView('default_private_links', $this->container->conf->get('privacy.default_private_links', false));
30 $this->assignView('async_metadata', $this->container->conf->get('general.enable_async_metadata', true));
31
32 return $response->write($this->render(TemplatePage::ADDLINK));
33 }
34}
diff --git a/application/front/controller/admin/ShaareManageController.php b/application/front/controller/admin/ShaareManageController.php
new file mode 100644
index 00000000..7ceb8d8a
--- /dev/null
+++ b/application/front/controller/admin/ShaareManageController.php
@@ -0,0 +1,202 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Class PostBookmarkController
13 *
14 * Slim controller used to handle Shaarli create or edit bookmarks.
15 */
16class ShaareManageController extends ShaarliAdminController
17{
18 /**
19 * GET /admin/shaare/delete - Delete one or multiple bookmarks (depending on `id` query parameter).
20 */
21 public function deleteBookmark(Request $request, Response $response): Response
22 {
23 $this->checkToken($request);
24
25 $ids = escape(trim($request->getParam('id') ?? ''));
26 if (empty($ids) || strpos($ids, ' ') !== false) {
27 // multiple, space-separated ids provided
28 $ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit'));
29 } else {
30 $ids = [$ids];
31 }
32
33 // assert at least one id is given
34 if (0 === count($ids)) {
35 $this->saveErrorMessage(t('Invalid bookmark ID provided.'));
36
37 return $this->redirectFromReferer($request, $response, [], ['delete-shaare']);
38 }
39
40 $formatter = $this->container->formatterFactory->getFormatter('raw');
41 $count = 0;
42 foreach ($ids as $id) {
43 try {
44 $bookmark = $this->container->bookmarkService->get((int) $id);
45 } catch (BookmarkNotFoundException $e) {
46 $this->saveErrorMessage(sprintf(
47 t('Bookmark with identifier %s could not be found.'),
48 $id
49 ));
50
51 continue;
52 }
53
54 $data = $formatter->format($bookmark);
55 $this->executePageHooks('delete_link', $data);
56 $this->container->bookmarkService->remove($bookmark, false);
57 ++ $count;
58 }
59
60 if ($count > 0) {
61 $this->container->bookmarkService->save();
62 }
63
64 // If we are called from the bookmarklet, we must close the popup:
65 if ($request->getParam('source') === 'bookmarklet') {
66 return $response->write('<script>self.close();</script>');
67 }
68
69 // Don't redirect to where we were previously because the datastore has changed.
70 return $this->redirect($response, '/');
71 }
72
73 /**
74 * GET /admin/shaare/visibility
75 *
76 * Change visibility (public/private) of one or multiple bookmarks (depending on `id` query parameter).
77 */
78 public function changeVisibility(Request $request, Response $response): Response
79 {
80 $this->checkToken($request);
81
82 $ids = trim(escape($request->getParam('id') ?? ''));
83 if (empty($ids) || strpos($ids, ' ') !== false) {
84 // multiple, space-separated ids provided
85 $ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit'));
86 } else {
87 // only a single id provided
88 $ids = [$ids];
89 }
90
91 // assert at least one id is given
92 if (0 === count($ids)) {
93 $this->saveErrorMessage(t('Invalid bookmark ID provided.'));
94
95 return $this->redirectFromReferer($request, $response, [], ['change_visibility']);
96 }
97
98 // assert that the visibility is valid
99 $visibility = $request->getParam('newVisibility');
100 if (null === $visibility || false === in_array($visibility, ['public', 'private'], true)) {
101 $this->saveErrorMessage(t('Invalid visibility provided.'));
102
103 return $this->redirectFromReferer($request, $response, [], ['change_visibility']);
104 } else {
105 $isPrivate = $visibility === 'private';
106 }
107
108 $formatter = $this->container->formatterFactory->getFormatter('raw');
109 $count = 0;
110
111 foreach ($ids as $id) {
112 try {
113 $bookmark = $this->container->bookmarkService->get((int) $id);
114 } catch (BookmarkNotFoundException $e) {
115 $this->saveErrorMessage(sprintf(
116 t('Bookmark with identifier %s could not be found.'),
117 $id
118 ));
119
120 continue;
121 }
122
123 $bookmark->setPrivate($isPrivate);
124
125 // To preserve backward compatibility with 3rd parties, plugins still use arrays
126 $data = $formatter->format($bookmark);
127 $this->executePageHooks('save_link', $data);
128 $bookmark->fromArray($data);
129
130 $this->container->bookmarkService->set($bookmark, false);
131 ++$count;
132 }
133
134 if ($count > 0) {
135 $this->container->bookmarkService->save();
136 }
137
138 return $this->redirectFromReferer($request, $response, ['/visibility'], ['change_visibility']);
139 }
140
141 /**
142 * GET /admin/shaare/{id}/pin - Pin or unpin a bookmark.
143 */
144 public function pinBookmark(Request $request, Response $response, array $args): Response
145 {
146 $this->checkToken($request);
147
148 $id = $args['id'] ?? '';
149 try {
150 if (false === ctype_digit($id)) {
151 throw new BookmarkNotFoundException();
152 }
153 $bookmark = $this->container->bookmarkService->get((int) $id); // Read database
154 } catch (BookmarkNotFoundException $e) {
155 $this->saveErrorMessage(sprintf(
156 t('Bookmark with identifier %s could not be found.'),
157 $id
158 ));
159
160 return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']);
161 }
162
163 $formatter = $this->container->formatterFactory->getFormatter('raw');
164
165 $bookmark->setSticky(!$bookmark->isSticky());
166
167 // To preserve backward compatibility with 3rd parties, plugins still use arrays
168 $data = $formatter->format($bookmark);
169 $this->executePageHooks('save_link', $data);
170 $bookmark->fromArray($data);
171
172 $this->container->bookmarkService->set($bookmark);
173
174 return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']);
175 }
176
177 /**
178 * GET /admin/shaare/private/{hash} - Attach a private key to given bookmark, then redirect to the sharing URL.
179 */
180 public function sharePrivate(Request $request, Response $response, array $args): Response
181 {
182 $this->checkToken($request);
183
184 $hash = $args['hash'] ?? '';
185 $bookmark = $this->container->bookmarkService->findByHash($hash);
186
187 if ($bookmark->isPrivate() !== true) {
188 return $this->redirect($response, '/shaare/' . $hash);
189 }
190
191 if (empty($bookmark->getAdditionalContentEntry('private_key'))) {
192 $privateKey = bin2hex(random_bytes(16));
193 $bookmark->addAdditionalContentEntry('private_key', $privateKey);
194 $this->container->bookmarkService->set($bookmark);
195 }
196
197 return $this->redirect(
198 $response,
199 '/shaare/' . $hash . '?key=' . $bookmark->getAdditionalContentEntry('private_key')
200 );
201 }
202}
diff --git a/application/front/controller/admin/ShaarePublishController.php b/application/front/controller/admin/ShaarePublishController.php
new file mode 100644
index 00000000..608f79cf
--- /dev/null
+++ b/application/front/controller/admin/ShaarePublishController.php
@@ -0,0 +1,222 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
9use Shaarli\Formatter\BookmarkMarkdownFormatter;
10use Shaarli\Render\TemplatePage;
11use Shaarli\Thumbnailer;
12use Slim\Http\Request;
13use Slim\Http\Response;
14
15class ShaarePublishController extends ShaarliAdminController
16{
17 /**
18 * GET /admin/shaare - Displays the bookmark form for creation.
19 * Note that if the URL is found in existing bookmarks, then it will be in edit mode.
20 */
21 public function displayCreateForm(Request $request, Response $response): Response
22 {
23 $url = cleanup_url($request->getParam('post'));
24 $link = $this->buildLinkDataFromUrl($request, $url);
25
26 return $this->displayForm($link, $link['linkIsNew'], $request, $response);
27 }
28
29 /**
30 * POST /admin/shaare-batch - Displays multiple creation/edit forms from bulk add in add-link page.
31 */
32 public function displayCreateBatchForms(Request $request, Response $response): Response
33 {
34 $urls = array_map('cleanup_url', explode(PHP_EOL, $request->getParam('urls')));
35
36 $links = [];
37 foreach ($urls as $url) {
38 $link = $this->buildLinkDataFromUrl($request, $url);
39 $data = $this->buildFormData($link, $link['linkIsNew'], $request);
40 $data['token'] = $this->container->sessionManager->generateToken();
41 $data['source'] = 'batch';
42
43 $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK);
44
45 $links[] = $data;
46 }
47
48 $this->assignView('links', $links);
49 $this->assignView('batch_mode', true);
50 $this->assignView('async_metadata', $this->container->conf->get('general.enable_async_metadata', true));
51
52 return $response->write($this->render(TemplatePage::EDIT_LINK_BATCH));
53 }
54
55 /**
56 * GET /admin/shaare/{id} - Displays the bookmark form in edition mode.
57 */
58 public function displayEditForm(Request $request, Response $response, array $args): Response
59 {
60 $id = $args['id'] ?? '';
61 try {
62 if (false === ctype_digit($id)) {
63 throw new BookmarkNotFoundException();
64 }
65 $bookmark = $this->container->bookmarkService->get((int) $id); // Read database
66 } catch (BookmarkNotFoundException $e) {
67 $this->saveErrorMessage(sprintf(
68 t('Bookmark with identifier %s could not be found.'),
69 $id
70 ));
71
72 return $this->redirect($response, '/');
73 }
74
75 $formatter = $this->container->formatterFactory->getFormatter('raw');
76 $link = $formatter->format($bookmark);
77
78 return $this->displayForm($link, false, $request, $response);
79 }
80
81 /**
82 * POST /admin/shaare
83 */
84 public function save(Request $request, Response $response): Response
85 {
86 $this->checkToken($request);
87
88 // lf_id should only be present if the link exists.
89 $id = $request->getParam('lf_id') !== null ? intval(escape($request->getParam('lf_id'))) : null;
90 if (null !== $id && true === $this->container->bookmarkService->exists($id)) {
91 // Edit
92 $bookmark = $this->container->bookmarkService->get($id);
93 } else {
94 // New link
95 $bookmark = new Bookmark();
96 }
97
98 $bookmark->setTitle($request->getParam('lf_title'));
99 $bookmark->setDescription($request->getParam('lf_description'));
100 $bookmark->setUrl($request->getParam('lf_url'), $this->container->conf->get('security.allowed_protocols', []));
101 $bookmark->setPrivate(filter_var($request->getParam('lf_private'), FILTER_VALIDATE_BOOLEAN));
102 $bookmark->setTagsString($request->getParam('lf_tags'));
103
104 if ($this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
105 && true !== $this->container->conf->get('general.enable_async_metadata', true)
106 && $bookmark->shouldUpdateThumbnail()
107 ) {
108 $bookmark->setThumbnail($this->container->thumbnailer->get($bookmark->getUrl()));
109 }
110 $this->container->bookmarkService->addOrSet($bookmark, false);
111
112 // To preserve backward compatibility with 3rd parties, plugins still use arrays
113 $formatter = $this->container->formatterFactory->getFormatter('raw');
114 $data = $formatter->format($bookmark);
115 $this->executePageHooks('save_link', $data);
116
117 $bookmark->fromArray($data);
118 $this->container->bookmarkService->set($bookmark);
119
120 // If we are called from the bookmarklet, we must close the popup:
121 if ($request->getParam('source') === 'bookmarklet') {
122 return $response->write('<script>self.close();</script>');
123 } elseif ($request->getParam('source') === 'batch') {
124 return $response;
125 }
126
127 if (!empty($request->getParam('returnurl'))) {
128 $this->container->environment['HTTP_REFERER'] = escape($request->getParam('returnurl'));
129 }
130
131 return $this->redirectFromReferer(
132 $request,
133 $response,
134 ['/admin/add-shaare', '/admin/shaare'], ['addlink', 'post', 'edit_link'],
135 $bookmark->getShortUrl()
136 );
137 }
138
139 /**
140 * Helper function used to display the shaare form whether it's a new or existing bookmark.
141 *
142 * @param array $link data used in template, either from parameters or from the data store
143 */
144 protected function displayForm(array $link, bool $isNew, Request $request, Response $response): Response
145 {
146 $data = $this->buildFormData($link, $isNew, $request);
147
148 $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK);
149
150 foreach ($data as $key => $value) {
151 $this->assignView($key, $value);
152 }
153
154 $editLabel = false === $isNew ? t('Edit') .' ' : '';
155 $this->assignView(
156 'pagetitle',
157 $editLabel . t('Shaare') .' - '. $this->container->conf->get('general.title', 'Shaarli')
158 );
159
160 return $response->write($this->render(TemplatePage::EDIT_LINK));
161 }
162
163 protected function buildLinkDataFromUrl(Request $request, string $url): array
164 {
165 // Check if URL is not already in database (in this case, we will edit the existing link)
166 $bookmark = $this->container->bookmarkService->findByUrl($url);
167 if (null === $bookmark) {
168 // Get shaare data if it was provided in URL (e.g.: by the bookmarklet).
169 $title = $request->getParam('title');
170 $description = $request->getParam('description');
171 $tags = $request->getParam('tags');
172 $private = filter_var($request->getParam('private'), FILTER_VALIDATE_BOOLEAN);
173
174 // If this is an HTTP(S) link, we try go get the page to extract
175 // the title (otherwise we will to straight to the edit form.)
176 if (true !== $this->container->conf->get('general.enable_async_metadata', true)
177 && empty($title)
178 && strpos(get_url_scheme($url) ?: '', 'http') !== false
179 ) {
180 $metadata = $this->container->metadataRetriever->retrieve($url);
181 }
182
183 if (empty($url)) {
184 $metadata['title'] = $this->container->conf->get('general.default_note_title', t('Note: '));
185 }
186
187 return [
188 'title' => $title ?? $metadata['title'] ?? '',
189 'url' => $url ?? '',
190 'description' => $description ?? $metadata['description'] ?? '',
191 'tags' => $tags ?? $metadata['tags'] ?? '',
192 'private' => $private,
193 'linkIsNew' => true,
194 ];
195 }
196
197 $formatter = $this->container->formatterFactory->getFormatter('raw');
198 $link = $formatter->format($bookmark);
199 $link['linkIsNew'] = false;
200
201 return $link;
202 }
203
204 protected function buildFormData(array $link, bool $isNew, Request $request): array
205 {
206 $tags = $this->container->bookmarkService->bookmarksCountPerTag();
207 if ($this->container->conf->get('formatter') === 'markdown') {
208 $tags[BookmarkMarkdownFormatter::NO_MD_TAG] = 1;
209 }
210
211 return escape([
212 'link' => $link,
213 'link_is_new' => $isNew,
214 'http_referer' => $this->container->environment['HTTP_REFERER'] ?? '',
215 'source' => $request->getParam('source') ?? '',
216 'tags' => $tags,
217 'default_private_links' => $this->container->conf->get('privacy.default_private_links', false),
218 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true),
219 'retrieve_description' => $this->container->conf->get('general.retrieve_description', false),
220 ]);
221 }
222}
diff --git a/application/render/TemplatePage.php b/application/render/TemplatePage.php
index 8af8228a..03b424f3 100644
--- a/application/render/TemplatePage.php
+++ b/application/render/TemplatePage.php
@@ -14,6 +14,7 @@ interface TemplatePage
14 public const DAILY = 'daily'; 14 public const DAILY = 'daily';
15 public const DAILY_RSS = 'dailyrss'; 15 public const DAILY_RSS = 'dailyrss';
16 public const EDIT_LINK = 'editlink'; 16 public const EDIT_LINK = 'editlink';
17 public const EDIT_LINK_BATCH = 'editlink.batch';
17 public const ERROR = 'error'; 18 public const ERROR = 'error';
18 public const EXPORT = 'export'; 19 public const EXPORT = 'export';
19 public const NETSCAPE_EXPORT_BOOKMARKS = 'export.bookmarks'; 20 public const NETSCAPE_EXPORT_BOOKMARKS = 'export.bookmarks';