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 if ($request->getParam('source') === 'batch') {
70 return $response->withStatus(204);
73 // Don't redirect to permalink after deletion.
74 return $this->redirectFromReferer($request, $response, ['shaare/']);
78 * GET /admin/shaare/visibility
80 * Change visibility (public/private) of one or multiple bookmarks (depending on `id` query parameter).
82 public function changeVisibility(Request
$request, Response
$response): Response
84 $this->checkToken($request);
86 $ids = trim(escape($request->getParam('id') ?? ''));
87 if (empty($ids) || strpos($ids, ' ') !== false) {
88 // multiple, space-separated ids provided
89 $ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit'));
91 // only a single id provided
95 // assert at least one id is given
96 if (0 === count($ids)) {
97 $this->saveErrorMessage(t('Invalid bookmark ID provided.'));
99 return $this->redirectFromReferer($request, $response, [], ['change_visibility']);
102 // assert that the visibility is valid
103 $visibility = $request->getParam('newVisibility');
104 if (null === $visibility || false === in_array($visibility, ['public', 'private'], true)) {
105 $this->saveErrorMessage(t('Invalid visibility provided.'));
107 return $this->redirectFromReferer($request, $response, [], ['change_visibility']);
109 $isPrivate = $visibility === 'private';
112 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
115 foreach ($ids as $id) {
117 $bookmark = $this->container
->bookmarkService
->get((int) $id);
118 } catch (BookmarkNotFoundException
$e) {
119 $this->saveErrorMessage(sprintf(
120 t('Bookmark with identifier %s could not be found.'),
127 $bookmark->setPrivate($isPrivate);
129 // To preserve backward compatibility with 3rd parties, plugins still use arrays
130 $data = $formatter->format($bookmark);
131 $this->executePageHooks('save_link', $data);
132 $bookmark->fromArray($data, $this->container
->conf
->get('general.tags_separator', ' '));
134 $this->container
->bookmarkService
->set($bookmark, false);
139 $this->container
->bookmarkService
->save();
142 return $this->redirectFromReferer($request, $response, ['/visibility'], ['change_visibility']);
146 * GET /admin/shaare/{id}/pin - Pin or unpin a bookmark.
148 public function pinBookmark(Request
$request, Response
$response, array $args): Response
150 $this->checkToken($request);
152 $id = $args['id'] ?? '';
154 if (false === ctype_digit($id)) {
155 throw new BookmarkNotFoundException();
157 $bookmark = $this->container
->bookmarkService
->get((int) $id); // Read database
158 } catch (BookmarkNotFoundException
$e) {
159 $this->saveErrorMessage(sprintf(
160 t('Bookmark with identifier %s could not be found.'),
164 return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']);
167 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
169 $bookmark->setSticky(!$bookmark->isSticky());
171 // To preserve backward compatibility with 3rd parties, plugins still use arrays
172 $data = $formatter->format($bookmark);
173 $this->executePageHooks('save_link', $data);
174 $bookmark->fromArray($data, $this->container
->conf
->get('general.tags_separator', ' '));
176 $this->container
->bookmarkService
->set($bookmark);
178 return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']);
182 * GET /admin/shaare/private/{hash} - Attach a private key to given bookmark, then redirect to the sharing URL.
184 public function sharePrivate(Request
$request, Response
$response, array $args): Response
186 $this->checkToken($request);
188 $hash = $args['hash'] ?? '';
189 $bookmark = $this->container
->bookmarkService
->findByHash($hash);
191 if ($bookmark->isPrivate() !== true) {
192 return $this->redirect($response, '/shaare/' . $hash);
195 if (empty($bookmark->getAdditionalContentEntry('private_key'))) {
196 $privateKey = bin2hex(random_bytes(16));
197 $bookmark->addAdditionalContentEntry('private_key', $privateKey);
198 $this->container
->bookmarkService
->set($bookmark);
201 return $this->redirect(
203 '/shaare/' . $hash . '?key=' . $bookmark->getAdditionalContentEntry('private_key')