3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
9 use Slim\Http\Response
;
12 * Class PostBookmarkController
14 * Slim controller used to handle Shaarli create or edit bookmarks.
16 class ShaareManageController
extends ShaarliAdminController
19 * GET /admin/shaare/delete - Delete one or multiple bookmarks (depending on `id` query parameter).
21 public function deleteBookmark(Request
$request, Response
$response): Response
23 $this->checkToken($request);
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'));
33 // assert at least one id is given
34 if (0 === count($ids)) {
35 $this->saveErrorMessage(t('Invalid bookmark ID provided.'));
37 return $this->redirectFromReferer($request, $response, [], ['delete-shaare']);
40 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
42 foreach ($ids as $id) {
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.'),
54 $data = $formatter->format($bookmark);
55 $this->executePageHooks('delete_link', $data);
56 $this->container
->bookmarkService
->remove($bookmark, false);
61 $this->container
->bookmarkService
->save();
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>');
69 // Don't redirect to where we were previously because the datastore has changed.
70 return $this->redirect($response, '/');
74 * GET /admin/shaare/visibility
76 * Change visibility (public/private) of one or multiple bookmarks (depending on `id` query parameter).
78 public function changeVisibility(Request
$request, Response
$response): Response
80 $this->checkToken($request);
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'));
87 // only a single id provided
91 // assert at least one id is given
92 if (0 === count($ids)) {
93 $this->saveErrorMessage(t('Invalid bookmark ID provided.'));
95 return $this->redirectFromReferer($request, $response, [], ['change_visibility']);
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.'));
103 return $this->redirectFromReferer($request, $response, [], ['change_visibility']);
105 $isPrivate = $visibility === 'private';
108 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
111 foreach ($ids as $id) {
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.'),
123 $bookmark->setPrivate($isPrivate);
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);
130 $this->container
->bookmarkService
->set($bookmark, false);
135 $this->container
->bookmarkService
->save();
138 return $this->redirectFromReferer($request, $response, ['/visibility'], ['change_visibility']);
142 * GET /admin/shaare/{id}/pin - Pin or unpin a bookmark.
144 public function pinBookmark(Request
$request, Response
$response, array $args): Response
146 $this->checkToken($request);
148 $id = $args['id'] ?? '';
150 if (false === ctype_digit($id)) {
151 throw new BookmarkNotFoundException();
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.'),
160 return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']);
163 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
165 $bookmark->setSticky(!$bookmark->isSticky());
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);
172 $this->container
->bookmarkService
->set($bookmark);
174 return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']);
178 * GET /admin/shaare/private/{hash} - Attach a private key to given bookmark, then redirect to the sharing URL.
180 public function sharePrivate(Request
$request, Response
$response, array $args): Response
182 $this->checkToken($request);
184 $hash = $args['hash'] ?? '';
185 $bookmark = $this->container
->bookmarkService
->findByHash($hash);
187 if ($bookmark->isPrivate() !== true) {
188 return $this->redirect($response, '/shaare/' . $hash);
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);
197 return $this->redirect(
199 '/shaare/' . $hash . '?key=' . $bookmark->getAdditionalContentEntry('private_key')