3 declare(strict_types
=1);
5 namespace Shaarli\Front\Controller\Admin
;
7 use Shaarli\Bookmark\Bookmark
;
8 use Shaarli\Bookmark\Exception\BookmarkNotFoundException
;
9 use Shaarli\Formatter\BookmarkMarkdownFormatter
;
10 use Shaarli\Render\TemplatePage
;
11 use Shaarli\Thumbnailer
;
12 use Slim\Http\Request
;
13 use Slim\Http\Response
;
16 * Class PostBookmarkController
18 * Slim controller used to handle Shaarli create or edit bookmarks.
20 class ManageShaareController
extends ShaarliAdminController
23 * GET /admin/add-shaare - Displays the form used to create a new bookmark from an URL
25 public function addShaare(Request
$request, Response
$response): Response
29 t('Shaare a new link') .' - '. $this->container
->conf
->get('general.title', 'Shaarli')
32 return $response->write($this->render(TemplatePage
::ADDLINK
));
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.
39 public function displayCreateForm(Request
$request, Response
$response): Response
41 $url = cleanup_url($request->getParam('post'));
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) {
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
);
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 (empty($title) && strpos(get_url_scheme($url) ?: '', 'http') !== false) {
57 $retrieveDescription = $this->container
->conf
->get('general.retrieve_description');
58 // Short timeout to keep the application responsive
59 // The callback will fill $charset and $title with data from the downloaded page.
60 $this->container
->httpAccess
->getHttpResponse(
62 $this->container
->conf
->get('general.download_timeout', 30),
63 $this->container
->conf
->get('general.download_max_size', 4194304),
64 $this->container
->httpAccess
->getCurlDownloadCallback(
72 if (! empty($title) && strtolower($charset) !== 'utf-8') {
73 $title = mb_convert_encoding($title, 'utf-8', $charset);
77 if (empty($url) && empty($title)) {
78 $title = $this->container
->conf
->get('general.default_note_title', t('Note: '));
84 'description' => $description ?? '',
85 'tags' => $tags ?? '',
86 'private' => $private,
89 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
90 $link = $formatter->format($bookmark);
93 return $this->displayForm($link, $linkIsNew, $request, $response);
97 * GET /admin/shaare/{id} - Displays the bookmark form in edition mode.
99 public function displayEditForm(Request
$request, Response
$response, array $args): Response
101 $id = $args['id'] ?? '';
103 if (false === ctype_digit($id)) {
104 throw new BookmarkNotFoundException();
106 $bookmark = $this->container
->bookmarkService
->get((int) $id); // Read database
107 } catch (BookmarkNotFoundException
$e) {
108 $this->saveErrorMessage(sprintf(
109 t('Bookmark with identifier %s could not be found.'),
113 return $this->redirect($response, '/');
116 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
117 $link = $formatter->format($bookmark);
119 return $this->displayForm($link, false, $request, $response);
125 public function save(Request
$request, Response
$response): Response
127 $this->checkToken($request);
129 // lf_id should only be present if the link exists.
130 $id = $request->getParam('lf_id') ? intval(escape($request->getParam('lf_id'))) : null;
131 if (null !== $id && true === $this->container
->bookmarkService
->exists($id)) {
133 $bookmark = $this->container
->bookmarkService
->get($id);
136 $bookmark = new Bookmark();
139 $bookmark->setTitle($request->getParam('lf_title'));
140 $bookmark->setDescription($request->getParam('lf_description'));
141 $bookmark->setUrl($request->getParam('lf_url'), $this->container
->conf
->get('security.allowed_protocols', []));
142 $bookmark->setPrivate(filter_var($request->getParam('lf_private'), FILTER_VALIDATE_BOOLEAN
));
143 $bookmark->setTagsString($request->getParam('lf_tags'));
145 if ($this->container
->conf
->get('thumbnails.mode', Thumbnailer
::MODE_NONE
) !== Thumbnailer
::MODE_NONE
146 && false === $bookmark->isNote()
148 $bookmark->setThumbnail($this->container
->thumbnailer
->get($bookmark->getUrl()));
150 $this->container
->bookmarkService
->addOrSet($bookmark, false);
152 // To preserve backward compatibility with 3rd parties, plugins still use arrays
153 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
154 $data = $formatter->format($bookmark);
155 $this->executePageHooks('save_link', $data);
157 $bookmark->fromArray($data);
158 $this->container
->bookmarkService
->set($bookmark);
160 // If we are called from the bookmarklet, we must close the popup:
161 if ($request->getParam('source') === 'bookmarklet') {
162 return $response->write('<script>self.close();</script>');
165 if (!empty($request->getParam('returnurl'))) {
166 $this->container
->environment
['HTTP_REFERER'] = escape($request->getParam('returnurl'));
169 return $this->redirectFromReferer(
172 ['add-shaare', 'shaare'], ['addlink', 'post', 'edit_link'],
173 $bookmark->getShortUrl()
178 * GET /admin/shaare/delete - Delete one or multiple bookmarks (depending on `id` query parameter).
180 public function deleteBookmark(Request
$request, Response
$response): Response
182 $this->checkToken($request);
184 $ids = escape(trim($request->getParam('id') ?? ''));
185 if (empty($ids) || strpos($ids, ' ') !== false) {
186 // multiple, space-separated ids provided
187 $ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit'));
192 // assert at least one id is given
193 if (0 === count($ids)) {
194 $this->saveErrorMessage(t('Invalid bookmark ID provided.'));
196 return $this->redirectFromReferer($request, $response, [], ['delete-shaare']);
199 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
201 foreach ($ids as $id) {
203 $bookmark = $this->container
->bookmarkService
->get((int) $id);
204 } catch (BookmarkNotFoundException
$e) {
205 $this->saveErrorMessage(sprintf(
206 t('Bookmark with identifier %s could not be found.'),
213 $data = $formatter->format($bookmark);
214 $this->executePageHooks('delete_link', $data);
215 $this->container
->bookmarkService
->remove($bookmark, false);
220 $this->container
->bookmarkService
->save();
223 // If we are called from the bookmarklet, we must close the popup:
224 if ($request->getParam('source') === 'bookmarklet') {
225 return $response->write('<script>self.close();</script>');
228 // Don't redirect to where we were previously because the datastore has changed.
229 return $this->redirect($response, '/');
233 * GET /admin/shaare/visibility
235 * Change visibility (public/private) of one or multiple bookmarks (depending on `id` query parameter).
237 public function changeVisibility(Request
$request, Response
$response): Response
239 $this->checkToken($request);
241 $ids = trim(escape($request->getParam('id') ?? ''));
242 if (empty($ids) || strpos($ids, ' ') !== false) {
243 // multiple, space-separated ids provided
244 $ids = array_values(array_filter(preg_split('/\s+/', $ids), 'ctype_digit'));
246 // only a single id provided
250 // assert at least one id is given
251 if (0 === count($ids)) {
252 $this->saveErrorMessage(t('Invalid bookmark ID provided.'));
254 return $this->redirectFromReferer($request, $response, [], ['change_visibility']);
257 // assert that the visibility is valid
258 $visibility = $request->getParam('newVisibility');
259 if (null === $visibility || false === in_array($visibility, ['public', 'private'], true)) {
260 $this->saveErrorMessage(t('Invalid visibility provided.'));
262 return $this->redirectFromReferer($request, $response, [], ['change_visibility']);
264 $isPrivate = $visibility === 'private';
267 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
270 foreach ($ids as $id) {
272 $bookmark = $this->container
->bookmarkService
->get((int) $id);
273 } catch (BookmarkNotFoundException
$e) {
274 $this->saveErrorMessage(sprintf(
275 t('Bookmark with identifier %s could not be found.'),
282 $bookmark->setPrivate($isPrivate);
284 // To preserve backward compatibility with 3rd parties, plugins still use arrays
285 $data = $formatter->format($bookmark);
286 $this->executePageHooks('save_link', $data);
287 $bookmark->fromArray($data);
289 $this->container
->bookmarkService
->set($bookmark, false);
294 $this->container
->bookmarkService
->save();
297 return $this->redirectFromReferer($request, $response, ['/visibility'], ['change_visibility']);
301 * GET /admin/shaare/{id}/pin - Pin or unpin a bookmark.
303 public function pinBookmark(Request
$request, Response
$response, array $args): Response
305 $this->checkToken($request);
307 $id = $args['id'] ?? '';
309 if (false === ctype_digit($id)) {
310 throw new BookmarkNotFoundException();
312 $bookmark = $this->container
->bookmarkService
->get((int) $id); // Read database
313 } catch (BookmarkNotFoundException
$e) {
314 $this->saveErrorMessage(sprintf(
315 t('Bookmark with identifier %s could not be found.'),
319 return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']);
322 $formatter = $this->container
->formatterFactory
->getFormatter('raw');
324 $bookmark->setSticky(!$bookmark->isSticky());
326 // To preserve backward compatibility with 3rd parties, plugins still use arrays
327 $data = $formatter->format($bookmark);
328 $this->executePageHooks('save_link', $data);
329 $bookmark->fromArray($data);
331 $this->container
->bookmarkService
->set($bookmark);
333 return $this->redirectFromReferer($request, $response, ['/pin'], ['pin']);
337 * Helper function used to display the shaare form whether it's a new or existing bookmark.
339 * @param array $link data used in template, either from parameters or from the data store
341 protected function displayForm(array $link, bool $isNew, Request
$request, Response
$response): Response
343 $tags = $this->container
->bookmarkService
->bookmarksCountPerTag();
344 if ($this->container
->conf
->get('formatter') === 'markdown') {
345 $tags[BookmarkMarkdownFormatter
::NO_MD_TAG
] = 1;
350 'link_is_new' => $isNew,
351 'http_referer' => escape($this->container
->environment
['HTTP_REFERER'] ?? ''),
352 'source' => $request->getParam('source') ?? '',
354 'default_private_links' => $this->container
->conf
->get('privacy.default_private_links', false),
357 $this->executePageHooks('render_editlink', $data, TemplatePage
::EDIT_LINK
);
359 foreach ($data as $key => $value) {
360 $this->assignView($key, $value);
363 $editLabel = false === $isNew ? t('Edit') .' ' : '';
366 $editLabel . t('Shaare') .' - '. $this->container
->conf
->get('general.title', 'Shaarli')
369 return $response->write($this->render(TemplatePage
::EDIT_LINK
));